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