xref: /dflybsd-src/contrib/binutils-2.27/include/obstack.h (revision e656dc90e3d65d744d534af2f5ea88cf8101ebcf)
1*a9fa9459Szrj /* obstack.h - object stack macros
2*a9fa9459Szrj    Copyright (C) 1988-2016 Free Software Foundation, Inc.
3*a9fa9459Szrj    This file is part of the GNU C Library.
4*a9fa9459Szrj 
5*a9fa9459Szrj    The GNU C Library is free software; you can redistribute it and/or
6*a9fa9459Szrj    modify it under the terms of the GNU Lesser General Public
7*a9fa9459Szrj    License as published by the Free Software Foundation; either
8*a9fa9459Szrj    version 2.1 of the License, or (at your option) any later version.
9*a9fa9459Szrj 
10*a9fa9459Szrj    The GNU C Library is distributed in the hope that it will be useful,
11*a9fa9459Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*a9fa9459Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*a9fa9459Szrj    Lesser General Public License for more details.
14*a9fa9459Szrj 
15*a9fa9459Szrj    You should have received a copy of the GNU Lesser General Public
16*a9fa9459Szrj    License along with the GNU C Library; if not, see
17*a9fa9459Szrj    <http://www.gnu.org/licenses/>.  */
18*a9fa9459Szrj 
19*a9fa9459Szrj /* Summary:
20*a9fa9459Szrj 
21*a9fa9459Szrj    All the apparent functions defined here are macros. The idea
22*a9fa9459Szrj    is that you would use these pre-tested macros to solve a
23*a9fa9459Szrj    very specific set of problems, and they would run fast.
24*a9fa9459Szrj    Caution: no side-effects in arguments please!! They may be
25*a9fa9459Szrj    evaluated MANY times!!
26*a9fa9459Szrj 
27*a9fa9459Szrj    These macros operate a stack of objects.  Each object starts life
28*a9fa9459Szrj    small, and may grow to maturity.  (Consider building a word syllable
29*a9fa9459Szrj    by syllable.)  An object can move while it is growing.  Once it has
30*a9fa9459Szrj    been "finished" it never changes address again.  So the "top of the
31*a9fa9459Szrj    stack" is typically an immature growing object, while the rest of the
32*a9fa9459Szrj    stack is of mature, fixed size and fixed address objects.
33*a9fa9459Szrj 
34*a9fa9459Szrj    These routines grab large chunks of memory, using a function you
35*a9fa9459Szrj    supply, called 'obstack_chunk_alloc'.  On occasion, they free chunks,
36*a9fa9459Szrj    by calling 'obstack_chunk_free'.  You must define them and declare
37*a9fa9459Szrj    them before using any obstack macros.
38*a9fa9459Szrj 
39*a9fa9459Szrj    Each independent stack is represented by a 'struct obstack'.
40*a9fa9459Szrj    Each of the obstack macros expects a pointer to such a structure
41*a9fa9459Szrj    as the first argument.
42*a9fa9459Szrj 
43*a9fa9459Szrj    One motivation for this package is the problem of growing char strings
44*a9fa9459Szrj    in symbol tables.  Unless you are "fascist pig with a read-only mind"
45*a9fa9459Szrj    --Gosper's immortal quote from HAKMEM item 154, out of context--you
46*a9fa9459Szrj    would not like to put any arbitrary upper limit on the length of your
47*a9fa9459Szrj    symbols.
48*a9fa9459Szrj 
49*a9fa9459Szrj    In practice this often means you will build many short symbols and a
50*a9fa9459Szrj    few long symbols.  At the time you are reading a symbol you don't know
51*a9fa9459Szrj    how long it is.  One traditional method is to read a symbol into a
52*a9fa9459Szrj    buffer, realloc()ating the buffer every time you try to read a symbol
53*a9fa9459Szrj    that is longer than the buffer.  This is beaut, but you still will
54*a9fa9459Szrj    want to copy the symbol from the buffer to a more permanent
55*a9fa9459Szrj    symbol-table entry say about half the time.
56*a9fa9459Szrj 
57*a9fa9459Szrj    With obstacks, you can work differently.  Use one obstack for all symbol
58*a9fa9459Szrj    names.  As you read a symbol, grow the name in the obstack gradually.
59*a9fa9459Szrj    When the name is complete, finalize it.  Then, if the symbol exists already,
60*a9fa9459Szrj    free the newly read name.
61*a9fa9459Szrj 
62*a9fa9459Szrj    The way we do this is to take a large chunk, allocating memory from
63*a9fa9459Szrj    low addresses.  When you want to build a symbol in the chunk you just
64*a9fa9459Szrj    add chars above the current "high water mark" in the chunk.  When you
65*a9fa9459Szrj    have finished adding chars, because you got to the end of the symbol,
66*a9fa9459Szrj    you know how long the chars are, and you can create a new object.
67*a9fa9459Szrj    Mostly the chars will not burst over the highest address of the chunk,
68*a9fa9459Szrj    because you would typically expect a chunk to be (say) 100 times as
69*a9fa9459Szrj    long as an average object.
70*a9fa9459Szrj 
71*a9fa9459Szrj    In case that isn't clear, when we have enough chars to make up
72*a9fa9459Szrj    the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
73*a9fa9459Szrj    so we just point to it where it lies.  No moving of chars is
74*a9fa9459Szrj    needed and this is the second win: potentially long strings need
75*a9fa9459Szrj    never be explicitly shuffled. Once an object is formed, it does not
76*a9fa9459Szrj    change its address during its lifetime.
77*a9fa9459Szrj 
78*a9fa9459Szrj    When the chars burst over a chunk boundary, we allocate a larger
79*a9fa9459Szrj    chunk, and then copy the partly formed object from the end of the old
80*a9fa9459Szrj    chunk to the beginning of the new larger chunk.  We then carry on
81*a9fa9459Szrj    accreting characters to the end of the object as we normally would.
82*a9fa9459Szrj 
83*a9fa9459Szrj    A special macro is provided to add a single char at a time to a
84*a9fa9459Szrj    growing object.  This allows the use of register variables, which
85*a9fa9459Szrj    break the ordinary 'growth' macro.
86*a9fa9459Szrj 
87*a9fa9459Szrj    Summary:
88*a9fa9459Szrj         We allocate large chunks.
89*a9fa9459Szrj         We carve out one object at a time from the current chunk.
90*a9fa9459Szrj         Once carved, an object never moves.
91*a9fa9459Szrj         We are free to append data of any size to the currently
92*a9fa9459Szrj           growing object.
93*a9fa9459Szrj         Exactly one object is growing in an obstack at any one time.
94*a9fa9459Szrj         You can run one obstack per control block.
95*a9fa9459Szrj         You may have as many control blocks as you dare.
96*a9fa9459Szrj         Because of the way we do it, you can "unwind" an obstack
97*a9fa9459Szrj           back to a previous state. (You may remove objects much
98*a9fa9459Szrj           as you would with a stack.)
99*a9fa9459Szrj  */
100*a9fa9459Szrj 
101*a9fa9459Szrj 
102*a9fa9459Szrj /* Don't do the contents of this file more than once.  */
103*a9fa9459Szrj 
104*a9fa9459Szrj #ifndef _OBSTACK_H
105*a9fa9459Szrj #define _OBSTACK_H 1
106*a9fa9459Szrj 
107*a9fa9459Szrj #ifndef _OBSTACK_INTERFACE_VERSION
108*a9fa9459Szrj # define _OBSTACK_INTERFACE_VERSION 2
109*a9fa9459Szrj #endif
110*a9fa9459Szrj 
111*a9fa9459Szrj #include <stddef.h>             /* For size_t and ptrdiff_t.  */
112*a9fa9459Szrj #include <string.h>             /* For __GNU_LIBRARY__, and memcpy.  */
113*a9fa9459Szrj 
114*a9fa9459Szrj #if _OBSTACK_INTERFACE_VERSION == 1
115*a9fa9459Szrj /* For binary compatibility with obstack version 1, which used "int"
116*a9fa9459Szrj    and "long" for these two types.  */
117*a9fa9459Szrj # define _OBSTACK_SIZE_T unsigned int
118*a9fa9459Szrj # define _CHUNK_SIZE_T unsigned long
119*a9fa9459Szrj # define _OBSTACK_CAST(type, expr) ((type) (expr))
120*a9fa9459Szrj #else
121*a9fa9459Szrj /* Version 2 with sane types, especially for 64-bit hosts.  */
122*a9fa9459Szrj # define _OBSTACK_SIZE_T size_t
123*a9fa9459Szrj # define _CHUNK_SIZE_T size_t
124*a9fa9459Szrj # define _OBSTACK_CAST(type, expr) (expr)
125*a9fa9459Szrj #endif
126*a9fa9459Szrj 
127*a9fa9459Szrj /* If B is the base of an object addressed by P, return the result of
128*a9fa9459Szrj    aligning P to the next multiple of A + 1.  B and P must be of type
129*a9fa9459Szrj    char *.  A + 1 must be a power of 2.  */
130*a9fa9459Szrj 
131*a9fa9459Szrj #define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
132*a9fa9459Szrj 
133*a9fa9459Szrj /* Similar to __BPTR_ALIGN (B, P, A), except optimize the common case
134*a9fa9459Szrj    where pointers can be converted to integers, aligned as integers,
135*a9fa9459Szrj    and converted back again.  If ptrdiff_t is narrower than a
136*a9fa9459Szrj    pointer (e.g., the AS/400), play it safe and compute the alignment
137*a9fa9459Szrj    relative to B.  Otherwise, use the faster strategy of computing the
138*a9fa9459Szrj    alignment relative to 0.  */
139*a9fa9459Szrj 
140*a9fa9459Szrj #define __PTR_ALIGN(B, P, A)						      \
141*a9fa9459Szrj   __BPTR_ALIGN (sizeof (ptrdiff_t) < sizeof (void *) ? (B) : (char *) 0,      \
142*a9fa9459Szrj                 P, A)
143*a9fa9459Szrj 
144*a9fa9459Szrj #ifndef __attribute_pure__
145*a9fa9459Szrj # if defined __GNUC_MINOR__ && __GNUC__ * 1000 + __GNUC_MINOR__ >= 2096
146*a9fa9459Szrj #  define __attribute_pure__ __attribute__ ((__pure__))
147*a9fa9459Szrj # else
148*a9fa9459Szrj #  define __attribute_pure__
149*a9fa9459Szrj # endif
150*a9fa9459Szrj #endif
151*a9fa9459Szrj 
152*a9fa9459Szrj #ifdef __cplusplus
153*a9fa9459Szrj extern "C" {
154*a9fa9459Szrj #endif
155*a9fa9459Szrj 
156*a9fa9459Szrj struct _obstack_chunk           /* Lives at front of each chunk. */
157*a9fa9459Szrj {
158*a9fa9459Szrj   char *limit;                  /* 1 past end of this chunk */
159*a9fa9459Szrj   struct _obstack_chunk *prev;  /* address of prior chunk or NULL */
160*a9fa9459Szrj   char contents[4];             /* objects begin here */
161*a9fa9459Szrj };
162*a9fa9459Szrj 
163*a9fa9459Szrj struct obstack          /* control current object in current chunk */
164*a9fa9459Szrj {
165*a9fa9459Szrj   _CHUNK_SIZE_T chunk_size;     /* preferred size to allocate chunks in */
166*a9fa9459Szrj   struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
167*a9fa9459Szrj   char *object_base;            /* address of object we are building */
168*a9fa9459Szrj   char *next_free;              /* where to add next char to current object */
169*a9fa9459Szrj   char *chunk_limit;            /* address of char after current chunk */
170*a9fa9459Szrj   union
171*a9fa9459Szrj   {
172*a9fa9459Szrj     _OBSTACK_SIZE_T i;
173*a9fa9459Szrj     void *p;
174*a9fa9459Szrj   } temp;                       /* Temporary for some macros.  */
175*a9fa9459Szrj   _OBSTACK_SIZE_T alignment_mask;  /* Mask of alignment for each object. */
176*a9fa9459Szrj 
177*a9fa9459Szrj   /* These prototypes vary based on 'use_extra_arg'.  */
178*a9fa9459Szrj   union
179*a9fa9459Szrj   {
180*a9fa9459Szrj     void *(*plain) (size_t);
181*a9fa9459Szrj     void *(*extra) (void *, size_t);
182*a9fa9459Szrj   } chunkfun;
183*a9fa9459Szrj   union
184*a9fa9459Szrj   {
185*a9fa9459Szrj     void (*plain) (void *);
186*a9fa9459Szrj     void (*extra) (void *, void *);
187*a9fa9459Szrj   } freefun;
188*a9fa9459Szrj 
189*a9fa9459Szrj   void *extra_arg;              /* first arg for chunk alloc/dealloc funcs */
190*a9fa9459Szrj   unsigned use_extra_arg : 1;     /* chunk alloc/dealloc funcs take extra arg */
191*a9fa9459Szrj   unsigned maybe_empty_object : 1; /* There is a possibility that the current
192*a9fa9459Szrj                                       chunk contains a zero-length object.  This
193*a9fa9459Szrj                                       prevents freeing the chunk if we allocate
194*a9fa9459Szrj                                       a bigger chunk to replace it. */
195*a9fa9459Szrj   unsigned alloc_failed : 1;      /* No longer used, as we now call the failed
196*a9fa9459Szrj                                      handler on error, but retained for binary
197*a9fa9459Szrj                                      compatibility.  */
198*a9fa9459Szrj };
199*a9fa9459Szrj 
200*a9fa9459Szrj /* Declare the external functions we use; they are in obstack.c.  */
201*a9fa9459Szrj 
202*a9fa9459Szrj extern void _obstack_newchunk (struct obstack *, _OBSTACK_SIZE_T);
203*a9fa9459Szrj extern void _obstack_free (struct obstack *, void *);
204*a9fa9459Szrj extern int _obstack_begin (struct obstack *,
205*a9fa9459Szrj                            _OBSTACK_SIZE_T, _OBSTACK_SIZE_T,
206*a9fa9459Szrj                            void *(*) (size_t), void (*) (void *));
207*a9fa9459Szrj extern int _obstack_begin_1 (struct obstack *,
208*a9fa9459Szrj                              _OBSTACK_SIZE_T, _OBSTACK_SIZE_T,
209*a9fa9459Szrj                              void *(*) (void *, size_t),
210*a9fa9459Szrj                              void (*) (void *, void *), void *);
211*a9fa9459Szrj extern _OBSTACK_SIZE_T _obstack_memory_used (struct obstack *)
212*a9fa9459Szrj   __attribute_pure__;
213*a9fa9459Szrj 
214*a9fa9459Szrj 
215*a9fa9459Szrj /* Error handler called when 'obstack_chunk_alloc' failed to allocate
216*a9fa9459Szrj    more memory.  This can be set to a user defined function which
217*a9fa9459Szrj    should either abort gracefully or use longjump - but shouldn't
218*a9fa9459Szrj    return.  The default action is to print a message and abort.  */
219*a9fa9459Szrj extern void (*obstack_alloc_failed_handler) (void);
220*a9fa9459Szrj 
221*a9fa9459Szrj /* Exit value used when 'print_and_abort' is used.  */
222*a9fa9459Szrj extern int obstack_exit_failure;
223*a9fa9459Szrj 
224*a9fa9459Szrj /* Pointer to beginning of object being allocated or to be allocated next.
225*a9fa9459Szrj    Note that this might not be the final address of the object
226*a9fa9459Szrj    because a new chunk might be needed to hold the final size.  */
227*a9fa9459Szrj 
228*a9fa9459Szrj #define obstack_base(h) ((void *) (h)->object_base)
229*a9fa9459Szrj 
230*a9fa9459Szrj /* Size for allocating ordinary chunks.  */
231*a9fa9459Szrj 
232*a9fa9459Szrj #define obstack_chunk_size(h) ((h)->chunk_size)
233*a9fa9459Szrj 
234*a9fa9459Szrj /* Pointer to next byte not yet allocated in current chunk.  */
235*a9fa9459Szrj 
236*a9fa9459Szrj #define obstack_next_free(h) ((void *) (h)->next_free)
237*a9fa9459Szrj 
238*a9fa9459Szrj /* Mask specifying low bits that should be clear in address of an object.  */
239*a9fa9459Szrj 
240*a9fa9459Szrj #define obstack_alignment_mask(h) ((h)->alignment_mask)
241*a9fa9459Szrj 
242*a9fa9459Szrj /* To prevent prototype warnings provide complete argument list.  */
243*a9fa9459Szrj #define obstack_init(h)							      \
244*a9fa9459Szrj   _obstack_begin ((h), 0, 0,						      \
245*a9fa9459Szrj                   _OBSTACK_CAST (void *(*) (size_t), obstack_chunk_alloc),    \
246*a9fa9459Szrj                   _OBSTACK_CAST (void (*) (void *), obstack_chunk_free))
247*a9fa9459Szrj 
248*a9fa9459Szrj #define obstack_begin(h, size)						      \
249*a9fa9459Szrj   _obstack_begin ((h), (size), 0,					      \
250*a9fa9459Szrj                   _OBSTACK_CAST (void *(*) (size_t), obstack_chunk_alloc), \
251*a9fa9459Szrj                   _OBSTACK_CAST (void (*) (void *), obstack_chunk_free))
252*a9fa9459Szrj 
253*a9fa9459Szrj #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun)     \
254*a9fa9459Szrj   _obstack_begin ((h), (size), (alignment),				      \
255*a9fa9459Szrj                   _OBSTACK_CAST (void *(*) (size_t), chunkfun),		      \
256*a9fa9459Szrj                   _OBSTACK_CAST (void (*) (void *), freefun))
257*a9fa9459Szrj 
258*a9fa9459Szrj #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
259*a9fa9459Szrj   _obstack_begin_1 ((h), (size), (alignment),				      \
260*a9fa9459Szrj                     _OBSTACK_CAST (void *(*) (void *, size_t), chunkfun),     \
261*a9fa9459Szrj                     _OBSTACK_CAST (void (*) (void *, void *), freefun), arg)
262*a9fa9459Szrj 
263*a9fa9459Szrj #define obstack_chunkfun(h, newchunkfun)				      \
264*a9fa9459Szrj   ((void) ((h)->chunkfun.extra = (void *(*) (void *, size_t)) (newchunkfun)))
265*a9fa9459Szrj 
266*a9fa9459Szrj #define obstack_freefun(h, newfreefun)					      \
267*a9fa9459Szrj   ((void) ((h)->freefun.extra = (void *(*) (void *, void *)) (newfreefun)))
268*a9fa9459Szrj 
269*a9fa9459Szrj #define obstack_1grow_fast(h, achar) ((void) (*((h)->next_free)++ = (achar)))
270*a9fa9459Szrj 
271*a9fa9459Szrj #define obstack_blank_fast(h, n) ((void) ((h)->next_free += (n)))
272*a9fa9459Szrj 
273*a9fa9459Szrj #define obstack_memory_used(h) _obstack_memory_used (h)
274*a9fa9459Szrj 
275*a9fa9459Szrj #if defined __GNUC__
276*a9fa9459Szrj # if !defined __GNUC_MINOR__ || __GNUC__ * 1000 + __GNUC_MINOR__ < 2008
277*a9fa9459Szrj #  define __extension__
278*a9fa9459Szrj # endif
279*a9fa9459Szrj 
280*a9fa9459Szrj /* For GNU C, if not -traditional,
281*a9fa9459Szrj    we can define these macros to compute all args only once
282*a9fa9459Szrj    without using a global variable.
283*a9fa9459Szrj    Also, we can avoid using the 'temp' slot, to make faster code.  */
284*a9fa9459Szrj 
285*a9fa9459Szrj # define obstack_object_size(OBSTACK)					      \
286*a9fa9459Szrj   __extension__								      \
287*a9fa9459Szrj     ({ struct obstack const *__o = (OBSTACK);				      \
288*a9fa9459Szrj        (_OBSTACK_SIZE_T) (__o->next_free - __o->object_base); })
289*a9fa9459Szrj 
290*a9fa9459Szrj /* The local variable is named __o1 to avoid a shadowed variable
291*a9fa9459Szrj    warning when invoked from other obstack macros.  */
292*a9fa9459Szrj # define obstack_room(OBSTACK)						      \
293*a9fa9459Szrj   __extension__								      \
294*a9fa9459Szrj     ({ struct obstack const *__o1 = (OBSTACK);				      \
295*a9fa9459Szrj        (_OBSTACK_SIZE_T) (__o1->chunk_limit - __o1->next_free); })
296*a9fa9459Szrj 
297*a9fa9459Szrj # define obstack_make_room(OBSTACK, length)				      \
298*a9fa9459Szrj   __extension__								      \
299*a9fa9459Szrj     ({ struct obstack *__o = (OBSTACK);					      \
300*a9fa9459Szrj        _OBSTACK_SIZE_T __len = (length);				      \
301*a9fa9459Szrj        if (obstack_room (__o) < __len)					      \
302*a9fa9459Szrj          _obstack_newchunk (__o, __len);				      \
303*a9fa9459Szrj        (void) 0; })
304*a9fa9459Szrj 
305*a9fa9459Szrj # define obstack_empty_p(OBSTACK)					      \
306*a9fa9459Szrj   __extension__								      \
307*a9fa9459Szrj     ({ struct obstack const *__o = (OBSTACK);				      \
308*a9fa9459Szrj        (__o->chunk->prev == 0						      \
309*a9fa9459Szrj         && __o->next_free == __PTR_ALIGN ((char *) __o->chunk,		      \
310*a9fa9459Szrj                                           __o->chunk->contents,		      \
311*a9fa9459Szrj                                           __o->alignment_mask)); })
312*a9fa9459Szrj 
313*a9fa9459Szrj # define obstack_grow(OBSTACK, where, length)				      \
314*a9fa9459Szrj   __extension__								      \
315*a9fa9459Szrj     ({ struct obstack *__o = (OBSTACK);					      \
316*a9fa9459Szrj        _OBSTACK_SIZE_T __len = (length);				      \
317*a9fa9459Szrj        if (obstack_room (__o) < __len)					      \
318*a9fa9459Szrj          _obstack_newchunk (__o, __len);				      \
319*a9fa9459Szrj        memcpy (__o->next_free, where, __len);				      \
320*a9fa9459Szrj        __o->next_free += __len;						      \
321*a9fa9459Szrj        (void) 0; })
322*a9fa9459Szrj 
323*a9fa9459Szrj # define obstack_grow0(OBSTACK, where, length)				      \
324*a9fa9459Szrj   __extension__								      \
325*a9fa9459Szrj     ({ struct obstack *__o = (OBSTACK);					      \
326*a9fa9459Szrj        _OBSTACK_SIZE_T __len = (length);				      \
327*a9fa9459Szrj        if (obstack_room (__o) < __len + 1)				      \
328*a9fa9459Szrj          _obstack_newchunk (__o, __len + 1);				      \
329*a9fa9459Szrj        memcpy (__o->next_free, where, __len);				      \
330*a9fa9459Szrj        __o->next_free += __len;						      \
331*a9fa9459Szrj        *(__o->next_free)++ = 0;						      \
332*a9fa9459Szrj        (void) 0; })
333*a9fa9459Szrj 
334*a9fa9459Szrj # define obstack_1grow(OBSTACK, datum)					      \
335*a9fa9459Szrj   __extension__								      \
336*a9fa9459Szrj     ({ struct obstack *__o = (OBSTACK);					      \
337*a9fa9459Szrj        if (obstack_room (__o) < 1)					      \
338*a9fa9459Szrj          _obstack_newchunk (__o, 1);					      \
339*a9fa9459Szrj        obstack_1grow_fast (__o, datum); })
340*a9fa9459Szrj 
341*a9fa9459Szrj /* These assume that the obstack alignment is good enough for pointers
342*a9fa9459Szrj    or ints, and that the data added so far to the current object
343*a9fa9459Szrj    shares that much alignment.  */
344*a9fa9459Szrj 
345*a9fa9459Szrj # define obstack_ptr_grow(OBSTACK, datum)				      \
346*a9fa9459Szrj   __extension__								      \
347*a9fa9459Szrj     ({ struct obstack *__o = (OBSTACK);					      \
348*a9fa9459Szrj        if (obstack_room (__o) < sizeof (void *))			      \
349*a9fa9459Szrj          _obstack_newchunk (__o, sizeof (void *));			      \
350*a9fa9459Szrj        obstack_ptr_grow_fast (__o, datum); })
351*a9fa9459Szrj 
352*a9fa9459Szrj # define obstack_int_grow(OBSTACK, datum)				      \
353*a9fa9459Szrj   __extension__								      \
354*a9fa9459Szrj     ({ struct obstack *__o = (OBSTACK);					      \
355*a9fa9459Szrj        if (obstack_room (__o) < sizeof (int))				      \
356*a9fa9459Szrj          _obstack_newchunk (__o, sizeof (int));				      \
357*a9fa9459Szrj        obstack_int_grow_fast (__o, datum); })
358*a9fa9459Szrj 
359*a9fa9459Szrj # define obstack_ptr_grow_fast(OBSTACK, aptr)				      \
360*a9fa9459Szrj   __extension__								      \
361*a9fa9459Szrj     ({ struct obstack *__o1 = (OBSTACK);				      \
362*a9fa9459Szrj        void *__p1 = __o1->next_free;					      \
363*a9fa9459Szrj        *(const void **) __p1 = (aptr);					      \
364*a9fa9459Szrj        __o1->next_free += sizeof (const void *);			      \
365*a9fa9459Szrj        (void) 0; })
366*a9fa9459Szrj 
367*a9fa9459Szrj # define obstack_int_grow_fast(OBSTACK, aint)				      \
368*a9fa9459Szrj   __extension__								      \
369*a9fa9459Szrj     ({ struct obstack *__o1 = (OBSTACK);				      \
370*a9fa9459Szrj        void *__p1 = __o1->next_free;					      \
371*a9fa9459Szrj        *(int *) __p1 = (aint);						      \
372*a9fa9459Szrj        __o1->next_free += sizeof (int);					      \
373*a9fa9459Szrj        (void) 0; })
374*a9fa9459Szrj 
375*a9fa9459Szrj # define obstack_blank(OBSTACK, length)					      \
376*a9fa9459Szrj   __extension__								      \
377*a9fa9459Szrj     ({ struct obstack *__o = (OBSTACK);					      \
378*a9fa9459Szrj        _OBSTACK_SIZE_T __len = (length);				      \
379*a9fa9459Szrj        if (obstack_room (__o) < __len)					      \
380*a9fa9459Szrj          _obstack_newchunk (__o, __len);				      \
381*a9fa9459Szrj        obstack_blank_fast (__o, __len); })
382*a9fa9459Szrj 
383*a9fa9459Szrj # define obstack_alloc(OBSTACK, length)					      \
384*a9fa9459Szrj   __extension__								      \
385*a9fa9459Szrj     ({ struct obstack *__h = (OBSTACK);					      \
386*a9fa9459Szrj        obstack_blank (__h, (length));					      \
387*a9fa9459Szrj        obstack_finish (__h); })
388*a9fa9459Szrj 
389*a9fa9459Szrj # define obstack_copy(OBSTACK, where, length)				      \
390*a9fa9459Szrj   __extension__								      \
391*a9fa9459Szrj     ({ struct obstack *__h = (OBSTACK);					      \
392*a9fa9459Szrj        obstack_grow (__h, (where), (length));				      \
393*a9fa9459Szrj        obstack_finish (__h); })
394*a9fa9459Szrj 
395*a9fa9459Szrj # define obstack_copy0(OBSTACK, where, length)				      \
396*a9fa9459Szrj   __extension__								      \
397*a9fa9459Szrj     ({ struct obstack *__h = (OBSTACK);					      \
398*a9fa9459Szrj        obstack_grow0 (__h, (where), (length));				      \
399*a9fa9459Szrj        obstack_finish (__h); })
400*a9fa9459Szrj 
401*a9fa9459Szrj /* The local variable is named __o1 to avoid a shadowed variable
402*a9fa9459Szrj    warning when invoked from other obstack macros, typically obstack_free.  */
403*a9fa9459Szrj # define obstack_finish(OBSTACK)					      \
404*a9fa9459Szrj   __extension__								      \
405*a9fa9459Szrj     ({ struct obstack *__o1 = (OBSTACK);				      \
406*a9fa9459Szrj        void *__value = (void *) __o1->object_base;			      \
407*a9fa9459Szrj        if (__o1->next_free == __value)					      \
408*a9fa9459Szrj          __o1->maybe_empty_object = 1;					      \
409*a9fa9459Szrj        __o1->next_free							      \
410*a9fa9459Szrj          = __PTR_ALIGN (__o1->object_base, __o1->next_free,		      \
411*a9fa9459Szrj                         __o1->alignment_mask);				      \
412*a9fa9459Szrj        if ((size_t) (__o1->next_free - (char *) __o1->chunk)		      \
413*a9fa9459Szrj            > (size_t) (__o1->chunk_limit - (char *) __o1->chunk))	      \
414*a9fa9459Szrj          __o1->next_free = __o1->chunk_limit;				      \
415*a9fa9459Szrj        __o1->object_base = __o1->next_free;				      \
416*a9fa9459Szrj        __value; })
417*a9fa9459Szrj 
418*a9fa9459Szrj # define obstack_free(OBSTACK, OBJ)					      \
419*a9fa9459Szrj   __extension__								      \
420*a9fa9459Szrj     ({ struct obstack *__o = (OBSTACK);					      \
421*a9fa9459Szrj        void *__obj = (void *) (OBJ);					      \
422*a9fa9459Szrj        if (__obj > (void *) __o->chunk && __obj < (void *) __o->chunk_limit)  \
423*a9fa9459Szrj          __o->next_free = __o->object_base = (char *) __obj;		      \
424*a9fa9459Szrj        else								      \
425*a9fa9459Szrj          _obstack_free (__o, __obj); })
426*a9fa9459Szrj 
427*a9fa9459Szrj #else /* not __GNUC__ */
428*a9fa9459Szrj 
429*a9fa9459Szrj # define obstack_object_size(h)						      \
430*a9fa9459Szrj   ((_OBSTACK_SIZE_T) ((h)->next_free - (h)->object_base))
431*a9fa9459Szrj 
432*a9fa9459Szrj # define obstack_room(h)						      \
433*a9fa9459Szrj   ((_OBSTACK_SIZE_T) ((h)->chunk_limit - (h)->next_free))
434*a9fa9459Szrj 
435*a9fa9459Szrj # define obstack_empty_p(h)						      \
436*a9fa9459Szrj   ((h)->chunk->prev == 0						      \
437*a9fa9459Szrj    && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk,		      \
438*a9fa9459Szrj                                      (h)->chunk->contents,		      \
439*a9fa9459Szrj                                      (h)->alignment_mask))
440*a9fa9459Szrj 
441*a9fa9459Szrj /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
442*a9fa9459Szrj    so that we can avoid having void expressions
443*a9fa9459Szrj    in the arms of the conditional expression.
444*a9fa9459Szrj    Casting the third operand to void was tried before,
445*a9fa9459Szrj    but some compilers won't accept it.  */
446*a9fa9459Szrj 
447*a9fa9459Szrj # define obstack_make_room(h, length)					      \
448*a9fa9459Szrj   ((h)->temp.i = (length),						      \
449*a9fa9459Szrj    ((obstack_room (h) < (h)->temp.i)					      \
450*a9fa9459Szrj     ? (_obstack_newchunk (h, (h)->temp.i), 0) : 0),			      \
451*a9fa9459Szrj    (void) 0)
452*a9fa9459Szrj 
453*a9fa9459Szrj # define obstack_grow(h, where, length)					      \
454*a9fa9459Szrj   ((h)->temp.i = (length),						      \
455*a9fa9459Szrj    ((obstack_room (h) < (h)->temp.i)					      \
456*a9fa9459Szrj    ? (_obstack_newchunk ((h), (h)->temp.i), 0) : 0),			      \
457*a9fa9459Szrj    memcpy ((h)->next_free, where, (h)->temp.i),				      \
458*a9fa9459Szrj    (h)->next_free += (h)->temp.i,					      \
459*a9fa9459Szrj    (void) 0)
460*a9fa9459Szrj 
461*a9fa9459Szrj # define obstack_grow0(h, where, length)				      \
462*a9fa9459Szrj   ((h)->temp.i = (length),						      \
463*a9fa9459Szrj    ((obstack_room (h) < (h)->temp.i + 1)				      \
464*a9fa9459Szrj    ? (_obstack_newchunk ((h), (h)->temp.i + 1), 0) : 0),		      \
465*a9fa9459Szrj    memcpy ((h)->next_free, where, (h)->temp.i),				      \
466*a9fa9459Szrj    (h)->next_free += (h)->temp.i,					      \
467*a9fa9459Szrj    *((h)->next_free)++ = 0,						      \
468*a9fa9459Szrj    (void) 0)
469*a9fa9459Szrj 
470*a9fa9459Szrj # define obstack_1grow(h, datum)					      \
471*a9fa9459Szrj   (((obstack_room (h) < 1)						      \
472*a9fa9459Szrj     ? (_obstack_newchunk ((h), 1), 0) : 0),				      \
473*a9fa9459Szrj    obstack_1grow_fast (h, datum))
474*a9fa9459Szrj 
475*a9fa9459Szrj # define obstack_ptr_grow(h, datum)					      \
476*a9fa9459Szrj   (((obstack_room (h) < sizeof (char *))				      \
477*a9fa9459Szrj     ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0),		      \
478*a9fa9459Szrj    obstack_ptr_grow_fast (h, datum))
479*a9fa9459Szrj 
480*a9fa9459Szrj # define obstack_int_grow(h, datum)					      \
481*a9fa9459Szrj   (((obstack_room (h) < sizeof (int))					      \
482*a9fa9459Szrj     ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0),			      \
483*a9fa9459Szrj    obstack_int_grow_fast (h, datum))
484*a9fa9459Szrj 
485*a9fa9459Szrj # define obstack_ptr_grow_fast(h, aptr)					      \
486*a9fa9459Szrj   (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr),	      \
487*a9fa9459Szrj    (void) 0)
488*a9fa9459Szrj 
489*a9fa9459Szrj # define obstack_int_grow_fast(h, aint)					      \
490*a9fa9459Szrj   (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint),		      \
491*a9fa9459Szrj    (void) 0)
492*a9fa9459Szrj 
493*a9fa9459Szrj # define obstack_blank(h, length)					      \
494*a9fa9459Szrj   ((h)->temp.i = (length),						      \
495*a9fa9459Szrj    ((obstack_room (h) < (h)->temp.i)					      \
496*a9fa9459Szrj    ? (_obstack_newchunk ((h), (h)->temp.i), 0) : 0),			      \
497*a9fa9459Szrj    obstack_blank_fast (h, (h)->temp.i))
498*a9fa9459Szrj 
499*a9fa9459Szrj # define obstack_alloc(h, length)					      \
500*a9fa9459Szrj   (obstack_blank ((h), (length)), obstack_finish ((h)))
501*a9fa9459Szrj 
502*a9fa9459Szrj # define obstack_copy(h, where, length)					      \
503*a9fa9459Szrj   (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
504*a9fa9459Szrj 
505*a9fa9459Szrj # define obstack_copy0(h, where, length)				      \
506*a9fa9459Szrj   (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
507*a9fa9459Szrj 
508*a9fa9459Szrj # define obstack_finish(h)						      \
509*a9fa9459Szrj   (((h)->next_free == (h)->object_base					      \
510*a9fa9459Szrj     ? (((h)->maybe_empty_object = 1), 0)				      \
511*a9fa9459Szrj     : 0),								      \
512*a9fa9459Szrj    (h)->temp.p = (h)->object_base,					      \
513*a9fa9459Szrj    (h)->next_free							      \
514*a9fa9459Szrj      = __PTR_ALIGN ((h)->object_base, (h)->next_free,			      \
515*a9fa9459Szrj                     (h)->alignment_mask),				      \
516*a9fa9459Szrj    (((size_t) ((h)->next_free - (char *) (h)->chunk)			      \
517*a9fa9459Szrj      > (size_t) ((h)->chunk_limit - (char *) (h)->chunk))		      \
518*a9fa9459Szrj    ? ((h)->next_free = (h)->chunk_limit) : 0),				      \
519*a9fa9459Szrj    (h)->object_base = (h)->next_free,					      \
520*a9fa9459Szrj    (h)->temp.p)
521*a9fa9459Szrj 
522*a9fa9459Szrj # define obstack_free(h, obj)						      \
523*a9fa9459Szrj   ((h)->temp.p = (void *) (obj),					      \
524*a9fa9459Szrj    (((h)->temp.p > (void *) (h)->chunk					      \
525*a9fa9459Szrj      && (h)->temp.p < (void *) (h)->chunk_limit)			      \
526*a9fa9459Szrj     ? (void) ((h)->next_free = (h)->object_base = (char *) (h)->temp.p)       \
527*a9fa9459Szrj     : _obstack_free ((h), (h)->temp.p)))
528*a9fa9459Szrj 
529*a9fa9459Szrj #endif /* not __GNUC__ */
530*a9fa9459Szrj 
531*a9fa9459Szrj #ifdef __cplusplus
532*a9fa9459Szrj }       /* C++ */
533*a9fa9459Szrj #endif
534*a9fa9459Szrj 
535*a9fa9459Szrj #endif /* _OBSTACK_H */
536