*** empty log message ***
[coreutils.git] / lib / bumpalloc.h
blob1f55b9ad9fccbd3619b96c5d389f0e1179fec59b
1 /* BUMP_ALLOC macro - increase table allocation by one element.
2 Copyright (C) 1990, 1991, 1993, 1998, 2000 Free Software Foundation, Inc.
3 François Pinard <pinard@iro.umontreal.ca>, 1990.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /*-------------------------------------------------------------------------.
20 | Bump the allocation of the array pointed to by TABLE whenever required. |
21 | The table already has already COUNT elements in it, this macro ensure it |
22 | has enough space to accommodate at least one more element. Space is |
23 | allocated (2 ^ EXPONENT) elements at a time. Each element of the array |
24 | is of type TYPE. |
25 `-------------------------------------------------------------------------*/
27 /* Routines `xmalloc' and `xrealloc' are called to do the actual memory
28 management. This implies that the program will abort with a "memory
29 exhausted" error if any problem arise.
31 To work correctly, at least EXPONENT and TYPE should always be the
32 same for all uses of this macro for any given TABLE. A secure way to
33 achieve this is to never use this macro directly, but use it to define
34 other macros, which would then be TABLE-specific.
36 The first time through, COUNT is usually zero. Note that COUNT is not
37 updated by this macro, but it should be update elsewhere, later. This
38 is convenient, because it allows TABLE[COUNT] to refer to the new
39 element at the end. Once its construction is completed, COUNT++ will
40 record it in the table. Calling this macro several times in a row
41 without updating COUNT is a bad thing to do. */
43 #define BUMP_ALLOC(Table, Count, Exponent, Type) \
44 BUMP_ALLOC_WITH_SIZE ((Table), (Count), (Exponent), Type, sizeof (Type))
46 /* In cases `sizeof TYPE' would not always yield the correct value for
47 the size of each element entry, this macro accepts a supplementary
48 SIZE argument. The EXPONENT, TYPE and SIZE parameters should still
49 have the same value for all macro calls related to a specific TABLE. */
51 #define BUMP_ALLOC_WITH_SIZE(Table, Count, Exponent, Type, Size) \
52 do \
53 { \
54 if (((Count) & (~(~0 << (Exponent)))) == 0) \
55 { \
56 if ((Count) == 0) \
57 (Table) = (Type *) xmalloc ((1 << (Exponent)) * (Size)); \
58 else \
59 (Table) = (Type *) \
60 xrealloc ((Table), ((Count) + (1 << (Exponent))) * (Size)); \
61 } \
62 } \
63 while (0)