Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / ptx / bumpalloc.h
1 /* BUMP_ALLOC macro - increase table allocation by one element.
2    Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc.
3    Francois Pinard <pinard@iro.umontreal.ca>, 1990.
4
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.
9
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.
14
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
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
18
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 `-------------------------------------------------------------------------*/
26
27 /* Routines `xmalloc' and `xrealloc' are called to do the actual memory
28    management.  This implies that the program will abort with an `Virtual
29    Memory exhausted!' error if any problem arise.
30
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.
35
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.  */
42
43 #define BUMP_ALLOC(Table, Count, Exponent, Type) \
44   BUMP_ALLOC_WITH_SIZE ((Table), (Count), (Exponent), Type, sizeof (Type))
45
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.  */
50
51 #define BUMP_ALLOC_WITH_SIZE(Table, Count, Exponent, Type, Size) \
52   if (((Count) & (~(~0 << (Exponent)))) == 0)                           \
53     if ((Count) == 0)                                                   \
54       (Table) = (Type *) xmalloc ((1 << (Exponent)) * (Size));          \
55     else                                                                \
56       (Table) = (Type *)                                                \
57         xrealloc ((Table), ((Count) + (1 << (Exponent))) * (Size));     \
58   else