home *** CD-ROM | disk | FTP | other *** search
- /* BUMP_ALLOC macro - increase table allocation by one element.
- Copyright (C) 1990 Free Software Foundation, Inc.
- Francois Pinard <pinard@iro.umontreal.ca>, 1990.
-
- $Id$
- */
-
-
- /* Only if necessary, bump the allocation of the array pointed to by TABLE,
- which already has COUNT elements in it, to contain at least one more
- element. However, space is not allocated one element at a time, but
- rather (2 ^ EXPONENT) elements at a time. Each element of the array is
- of type TYPE. The parameter SIZE provides the size of each array
- element; since it cannot always be deduced directly from `sizeof TYPE'.
-
- Routines `xmalloc' and `xrealloc' are called to do the actual memory
- management. This implies that the program will abort with an `Out of
- memory!' error if any problem arise.
-
- To work correctly, at least EXPONENT and SIZE should always be the same
- for all uses of this macro for any given TABLE. A secure way to achieve
- this is to never use this macro directly, but use it to define other
- macros, which would then be TABLE-specific.
-
- The first time through, COUNT is usually zero. Note that COUNT is not
- updated by this macro, but it should be udpate elsewhere, later. This is
- convenient, because it allows TABLE[COUNT] to refer to the new element at
- the end. Once its construction is completed, COUNT++ will record it in
- the table. Calling this macro several times in a row without updating
- COUNT is a bad thing to do. */
-
-
- /* The `else' at the end of this macro is to avoid undesirable effects if it
- was called inside an `if else' sequence. This also avoids using curly
- braces, which might also have undesirable effects. */
-
-
- #define BUMP_ALLOC_VARSIZE(table, count, exponent, type, size) \
- if (((count) & ((1 << (exponent)) - 1)) == 0) \
- if ((count) == 0) \
- (table) = (type *) xmalloc ((1 << exponent) * size); \
- else \
- (table) \
- = (type *) \
- xrealloc ((table), \
- ((count) + (1 << (exponent))) * size); \
- else
-
-
- /* This simpler macro is used for the usual case where `sizeof TYPE' yields
- the correct value for the size of each element entry. This is the same
- as the previous definition, but the SIZE argument is omitted.
-
- Of course, the TYPE parameter should then have the same value for all
- macro calls related to a specific TABLE. */
-
- #define BUMP_ALLOC(table, count, exponent, type) \
- BUMP_ALLOC_VARSIZE ((table), (count), (exponent), type, sizeof (type))
-