diff options
author | Jim Meyering <jim@meyering.net> | 1998-08-14 14:09:05 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1998-08-14 14:09:05 +0000 |
commit | 7b0caffd312443449cb1f398ded7104794e1dc69 (patch) | |
tree | ceb02d7a74d884d8a081af4eb48f37b2e9479be1 /lib/bumpalloc.h | |
parent | ed2a7b4e5325503fdb3b4766b25ae9a42618a9e3 (diff) | |
download | coreutils-7b0caffd312443449cb1f398ded7104794e1dc69.tar.xz |
.
Diffstat (limited to 'lib/bumpalloc.h')
-rw-r--r-- | lib/bumpalloc.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/bumpalloc.h b/lib/bumpalloc.h new file mode 100644 index 000000000..ce37d1c68 --- /dev/null +++ b/lib/bumpalloc.h @@ -0,0 +1,61 @@ +/* BUMP_ALLOC macro - increase table allocation by one element. + Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc. + François Pinard <pinard@iro.umontreal.ca>, 1990. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +/*-------------------------------------------------------------------------. +| Bump the allocation of the array pointed to by TABLE whenever required. | +| The table already has already COUNT elements in it, this macro ensure it | +| has enough space to accommodate at least one more element. Space is | +| allocated (2 ^ EXPONENT) elements at a time. Each element of the array | +| is of type TYPE. | +`-------------------------------------------------------------------------*/ + +/* Routines `xmalloc' and `xrealloc' are called to do the actual memory + management. This implies that the program will abort with an `Memory + exhausted!' error if any problem arise. + + To work correctly, at least EXPONENT and TYPE 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 update 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. */ + +#define BUMP_ALLOC(Table, Count, Exponent, Type) \ + BUMP_ALLOC_WITH_SIZE ((Table), (Count), (Exponent), Type, sizeof (Type)) + +/* In cases `sizeof TYPE' would not always yield the correct value for + the size of each element entry, this macro accepts a supplementary + SIZE argument. The EXPONENT, TYPE and SIZE parameters should still + have the same value for all macro calls related to a specific TABLE. */ + +#define BUMP_ALLOC_WITH_SIZE(Table, Count, Exponent, Type, Size) \ + do \ + { \ + if (((Count) & (~(~0 << (Exponent)))) == 0) \ + if ((Count) == 0) \ + (Table) = (Type *) xmalloc ((1 << (Exponent)) * (Size)); \ + else \ + (Table) = (Type *) \ + xrealloc ((Table), ((Count) + (1 << (Exponent))) * (Size)); \ + } \ + while (0) |