home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / GPTX01AS.ZIP / BUMPALLO.H < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  2.5 KB  |  59 lines

  1. /* BUMP_ALLOC macro - increase table allocation by one element.
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.    Francois Pinard <pinard@iro.umontreal.ca>, 1990.
  4.  
  5.    $Id$
  6. */
  7.  
  8.  
  9. /* Only if necessary, bump the allocation of the array pointed to by TABLE,
  10.    which already has COUNT elements in it, to contain at least one more
  11.    element.  However, space is not allocated one element at a time, but
  12.    rather (2 ^ EXPONENT) elements at a time.  Each element of the array is
  13.    of type TYPE.  The parameter SIZE provides the size of each array
  14.    element; since it cannot always be deduced directly from `sizeof TYPE'.
  15.  
  16.    Routines `xmalloc' and `xrealloc' are called to do the actual memory
  17.    management.  This implies that the program will abort with an `Out of
  18.    memory!' error if any problem arise.
  19.  
  20.    To work correctly, at least EXPONENT and SIZE should always be the same
  21.    for all uses of this macro for any given TABLE.  A secure way to achieve
  22.    this is to never use this macro directly, but use it to define other
  23.    macros, which would then be TABLE-specific.
  24.  
  25.    The first time through, COUNT is usually zero.  Note that COUNT is not
  26.    updated by this macro, but it should be udpate elsewhere, later.  This is
  27.    convenient, because it allows TABLE[COUNT] to refer to the new element at
  28.    the end.  Once its construction is completed, COUNT++ will record it in
  29.    the table.  Calling this macro several times in a row without updating
  30.    COUNT is a bad thing to do.  */
  31.  
  32.  
  33. /* The `else' at the end of this macro is to avoid undesirable effects if it
  34.    was called inside an `if else' sequence.  This also avoids using curly
  35.    braces, which might also have undesirable effects.  */
  36.  
  37.  
  38. #define BUMP_ALLOC_VARSIZE(table, count, exponent, type, size) \
  39.   if (((count) & ((1 << (exponent)) - 1)) == 0)                \
  40.     if ((count) == 0)                            \
  41.       (table) = (type *) xmalloc ((1 << exponent) * size);        \
  42.     else                                \
  43.       (table)                                \
  44.     = (type *)                            \
  45.       xrealloc ((table),                        \
  46.          ((count) + (1 << (exponent))) * size);            \
  47.   else
  48.  
  49.  
  50. /* This simpler macro is used for the usual case where `sizeof TYPE' yields
  51.    the correct value for the size of each element entry.  This is the same
  52.    as the previous definition, but the SIZE argument is omitted.
  53.  
  54.    Of course, the TYPE parameter should then have the same value for all
  55.    macro calls related to a specific TABLE.  */
  56.  
  57. #define BUMP_ALLOC(table, count, exponent, type) \
  58.   BUMP_ALLOC_VARSIZE ((table), (count), (exponent), type, sizeof (type))
  59.