1169695Skan/* obstack.h - object stack macros
2169695Skan   Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
3169695Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005
4169695Skan   Free Software Foundation, Inc.
5169695Skan
6169695Skan
7169695Skan   NOTE: The canonical source of this file is maintained with the GNU C Library.
8169695Skan   Bugs can be reported to bug-glibc@gnu.org.
9169695Skan
10169695Skan   This program is free software; you can redistribute it and/or modify it
11169695Skan   under the terms of the GNU General Public License as published by the
12169695Skan   Free Software Foundation; either version 2, or (at your option) any
13169695Skan   later version.
14169695Skan
15169695Skan   This program is distributed in the hope that it will be useful,
16169695Skan   but WITHOUT ANY WARRANTY; without even the implied warranty of
17169695Skan   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18169695Skan   GNU General Public License for more details.
19169695Skan
20169695Skan   You should have received a copy of the GNU General Public License
21169695Skan   along with this program; if not, write to the Free Software
22169695Skan   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
23169695Skan   USA.  */
24169695Skan
25169695Skan/* Summary:
26169695Skan
27169695SkanAll the apparent functions defined here are macros. The idea
28169695Skanis that you would use these pre-tested macros to solve a
29169695Skanvery specific set of problems, and they would run fast.
30169695SkanCaution: no side-effects in arguments please!! They may be
31169695Skanevaluated MANY times!!
32169695Skan
33169695SkanThese macros operate a stack of objects.  Each object starts life
34169695Skansmall, and may grow to maturity.  (Consider building a word syllable
35169695Skanby syllable.)  An object can move while it is growing.  Once it has
36169695Skanbeen "finished" it never changes address again.  So the "top of the
37169695Skanstack" is typically an immature growing object, while the rest of the
38169695Skanstack is of mature, fixed size and fixed address objects.
39169695Skan
40169695SkanThese routines grab large chunks of memory, using a function you
41169695Skansupply, called `obstack_chunk_alloc'.  On occasion, they free chunks,
42169695Skanby calling `obstack_chunk_free'.  You must define them and declare
43169695Skanthem before using any obstack macros.
44169695Skan
45169695SkanEach independent stack is represented by a `struct obstack'.
46169695SkanEach of the obstack macros expects a pointer to such a structure
47169695Skanas the first argument.
48169695Skan
49169695SkanOne motivation for this package is the problem of growing char strings
50169695Skanin symbol tables.  Unless you are "fascist pig with a read-only mind"
51169695Skan--Gosper's immortal quote from HAKMEM item 154, out of context--you
52169695Skanwould not like to put any arbitrary upper limit on the length of your
53169695Skansymbols.
54169695Skan
55169695SkanIn practice this often means you will build many short symbols and a
56169695Skanfew long symbols.  At the time you are reading a symbol you don't know
57169695Skanhow long it is.  One traditional method is to read a symbol into a
58169695Skanbuffer, realloc()ating the buffer every time you try to read a symbol
59169695Skanthat is longer than the buffer.  This is beaut, but you still will
60169695Skanwant to copy the symbol from the buffer to a more permanent
61169695Skansymbol-table entry say about half the time.
62169695Skan
63169695SkanWith obstacks, you can work differently.  Use one obstack for all symbol
64169695Skannames.  As you read a symbol, grow the name in the obstack gradually.
65169695SkanWhen the name is complete, finalize it.  Then, if the symbol exists already,
66169695Skanfree the newly read name.
67169695Skan
68169695SkanThe way we do this is to take a large chunk, allocating memory from
69169695Skanlow addresses.  When you want to build a symbol in the chunk you just
70169695Skanadd chars above the current "high water mark" in the chunk.  When you
71169695Skanhave finished adding chars, because you got to the end of the symbol,
72169695Skanyou know how long the chars are, and you can create a new object.
73169695SkanMostly the chars will not burst over the highest address of the chunk,
74169695Skanbecause you would typically expect a chunk to be (say) 100 times as
75169695Skanlong as an average object.
76169695Skan
77169695SkanIn case that isn't clear, when we have enough chars to make up
78169695Skanthe object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
79169695Skanso we just point to it where it lies.  No moving of chars is
80169695Skanneeded and this is the second win: potentially long strings need
81169695Skannever be explicitly shuffled. Once an object is formed, it does not
82169695Skanchange its address during its lifetime.
83169695Skan
84169695SkanWhen the chars burst over a chunk boundary, we allocate a larger
85169695Skanchunk, and then copy the partly formed object from the end of the old
86169695Skanchunk to the beginning of the new larger chunk.  We then carry on
87169695Skanaccreting characters to the end of the object as we normally would.
88169695Skan
89169695SkanA special macro is provided to add a single char at a time to a
90169695Skangrowing object.  This allows the use of register variables, which
91169695Skanbreak the ordinary 'growth' macro.
92169695Skan
93169695SkanSummary:
94169695Skan	We allocate large chunks.
95169695Skan	We carve out one object at a time from the current chunk.
96169695Skan	Once carved, an object never moves.
97169695Skan	We are free to append data of any size to the currently
98169695Skan	  growing object.
99169695Skan	Exactly one object is growing in an obstack at any one time.
100169695Skan	You can run one obstack per control block.
101169695Skan	You may have as many control blocks as you dare.
102169695Skan	Because of the way we do it, you can `unwind' an obstack
103169695Skan	  back to a previous state. (You may remove objects much
104169695Skan	  as you would with a stack.)
105169695Skan*/
106169695Skan
107169695Skan
108169695Skan/* Don't do the contents of this file more than once.  */
109169695Skan
110169695Skan#ifndef _OBSTACK_H
111169695Skan#define _OBSTACK_H 1
112169695Skan
113169695Skan#ifdef __cplusplus
114169695Skanextern "C" {
115169695Skan#endif
116169695Skan
117169695Skan/* We use subtraction of (char *) 0 instead of casting to int
118169695Skan   because on word-addressable machines a simple cast to int
119169695Skan   may ignore the byte-within-word field of the pointer.  */
120169695Skan
121169695Skan#ifndef __PTR_TO_INT
122169695Skan# define __PTR_TO_INT(P) ((P) - (char *) 0)
123169695Skan#endif
124169695Skan
125169695Skan#ifndef __INT_TO_PTR
126169695Skan# define __INT_TO_PTR(P) ((P) + (char *) 0)
127169695Skan#endif
128169695Skan
129169695Skan/* We need the type of the resulting object.  If __PTRDIFF_TYPE__ is
130169695Skan   defined, as with GNU C, use that; that way we don't pollute the
131169695Skan   namespace with <stddef.h>'s symbols.  Otherwise, if <stddef.h> is
132169695Skan   available, include it and use ptrdiff_t.  In traditional C, long is
133169695Skan   the best that we can do.  */
134169695Skan
135169695Skan#ifdef __PTRDIFF_TYPE__
136169695Skan# define PTR_INT_TYPE __PTRDIFF_TYPE__
137169695Skan#else
138169695Skan# ifdef HAVE_STDDEF_H
139169695Skan#  include <stddef.h>
140169695Skan#  define PTR_INT_TYPE ptrdiff_t
141169695Skan# else
142169695Skan#  define PTR_INT_TYPE long
143169695Skan# endif
144169695Skan#endif
145169695Skan
146169695Skan#if defined _LIBC || defined HAVE_STRING_H
147169695Skan# include <string.h>
148169695Skan# define _obstack_memcpy(To, From, N) memcpy ((To), (From), (N))
149169695Skan#else
150169695Skan# ifdef memcpy
151169695Skan#  define _obstack_memcpy(To, From, N) memcpy ((To), (char *)(From), (N))
152169695Skan# else
153169695Skan#  define _obstack_memcpy(To, From, N) bcopy ((char *)(From), (To), (N))
154169695Skan# endif
155169695Skan#endif
156169695Skan
157169695Skanstruct _obstack_chunk		/* Lives at front of each chunk. */
158169695Skan{
159169695Skan  char  *limit;			/* 1 past end of this chunk */
160169695Skan  struct _obstack_chunk *prev;	/* address of prior chunk or NULL */
161169695Skan  char	contents[4];		/* objects begin here */
162169695Skan};
163169695Skan
164169695Skanstruct obstack		/* control current object in current chunk */
165169695Skan{
166169695Skan  long	chunk_size;		/* preferred size to allocate chunks in */
167169695Skan  struct _obstack_chunk *chunk;	/* address of current struct obstack_chunk */
168169695Skan  char	*object_base;		/* address of object we are building */
169169695Skan  char	*next_free;		/* where to add next char to current object */
170169695Skan  char	*chunk_limit;		/* address of char after current chunk */
171169695Skan  PTR_INT_TYPE temp;		/* Temporary for some macros.  */
172169695Skan  int   alignment_mask;		/* Mask of alignment for each object. */
173169695Skan  /* These prototypes vary based on `use_extra_arg', and we use
174169695Skan     casts to the prototypeless function type in all assignments,
175169695Skan     but having prototypes here quiets -Wstrict-prototypes.  */
176169695Skan  struct _obstack_chunk *(*chunkfun) (void *, long);
177169695Skan  void (*freefun) (void *, struct _obstack_chunk *);
178169695Skan  void *extra_arg;		/* first arg for chunk alloc/dealloc funcs */
179169695Skan  unsigned use_extra_arg:1;	/* chunk alloc/dealloc funcs take extra arg */
180169695Skan  unsigned maybe_empty_object:1;/* There is a possibility that the current
181169695Skan				   chunk contains a zero-length object.  This
182169695Skan				   prevents freeing the chunk if we allocate
183169695Skan				   a bigger chunk to replace it. */
184169695Skan  unsigned alloc_failed:1;	/* No longer used, as we now call the failed
185169695Skan				   handler on error, but retained for binary
186169695Skan				   compatibility.  */
187169695Skan};
188169695Skan
189169695Skan/* Declare the external functions we use; they are in obstack.c.  */
190169695Skan
191169695Skanextern void _obstack_newchunk (struct obstack *, int);
192169695Skanextern void _obstack_free (struct obstack *, void *);
193169695Skanextern int _obstack_begin (struct obstack *, int, int,
194169695Skan			    void *(*) (long), void (*) (void *));
195169695Skanextern int _obstack_begin_1 (struct obstack *, int, int,
196169695Skan			     void *(*) (void *, long),
197169695Skan			     void (*) (void *, void *), void *);
198169695Skanextern int _obstack_memory_used (struct obstack *);
199169695Skan
200169695Skan/* Do the function-declarations after the structs
201169695Skan   but before defining the macros.  */
202169695Skan
203169695Skanvoid obstack_init (struct obstack *obstack);
204169695Skan
205169695Skanvoid * obstack_alloc (struct obstack *obstack, int size);
206169695Skan
207169695Skanvoid * obstack_copy (struct obstack *obstack, void *address, int size);
208169695Skanvoid * obstack_copy0 (struct obstack *obstack, void *address, int size);
209169695Skan
210169695Skanvoid obstack_free (struct obstack *obstack, void *block);
211169695Skan
212169695Skanvoid obstack_blank (struct obstack *obstack, int size);
213169695Skan
214169695Skanvoid obstack_grow (struct obstack *obstack, void *data, int size);
215169695Skanvoid obstack_grow0 (struct obstack *obstack, void *data, int size);
216169695Skan
217169695Skanvoid obstack_1grow (struct obstack *obstack, int data_char);
218169695Skanvoid obstack_ptr_grow (struct obstack *obstack, void *data);
219169695Skanvoid obstack_int_grow (struct obstack *obstack, int data);
220169695Skan
221169695Skanvoid * obstack_finish (struct obstack *obstack);
222169695Skan
223169695Skanint obstack_object_size (struct obstack *obstack);
224169695Skan
225169695Skanint obstack_room (struct obstack *obstack);
226169695Skanvoid obstack_make_room (struct obstack *obstack, int size);
227169695Skanvoid obstack_1grow_fast (struct obstack *obstack, int data_char);
228169695Skanvoid obstack_ptr_grow_fast (struct obstack *obstack, void *data);
229169695Skanvoid obstack_int_grow_fast (struct obstack *obstack, int data);
230169695Skanvoid obstack_blank_fast (struct obstack *obstack, int size);
231169695Skan
232169695Skanvoid * obstack_base (struct obstack *obstack);
233169695Skanvoid * obstack_next_free (struct obstack *obstack);
234169695Skanint obstack_alignment_mask (struct obstack *obstack);
235169695Skanint obstack_chunk_size (struct obstack *obstack);
236169695Skanint obstack_memory_used (struct obstack *obstack);
237169695Skan
238169695Skan/* Error handler called when `obstack_chunk_alloc' failed to allocate
239169695Skan   more memory.  This can be set to a user defined function.  The
240169695Skan   default action is to print a message and abort.  */
241169695Skanextern void (*obstack_alloc_failed_handler) (void);
242169695Skan
243169695Skan/* Exit value used when `print_and_abort' is used.  */
244169695Skanextern int obstack_exit_failure;
245169695Skan
246169695Skan/* Pointer to beginning of object being allocated or to be allocated next.
247169695Skan   Note that this might not be the final address of the object
248169695Skan   because a new chunk might be needed to hold the final size.  */
249169695Skan
250169695Skan#define obstack_base(h) ((h)->object_base)
251169695Skan
252169695Skan/* Size for allocating ordinary chunks.  */
253169695Skan
254169695Skan#define obstack_chunk_size(h) ((h)->chunk_size)
255169695Skan
256169695Skan/* Pointer to next byte not yet allocated in current chunk.  */
257169695Skan
258169695Skan#define obstack_next_free(h)	((h)->next_free)
259169695Skan
260169695Skan/* Mask specifying low bits that should be clear in address of an object.  */
261169695Skan
262169695Skan#define obstack_alignment_mask(h) ((h)->alignment_mask)
263169695Skan
264169695Skan/* To prevent prototype warnings provide complete argument list in
265169695Skan   standard C version.  */
266169695Skan# define obstack_init(h) \
267169695Skan  _obstack_begin ((h), 0, 0, \
268169695Skan		  (void *(*) (long)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free)
269169695Skan
270169695Skan# define obstack_begin(h, size) \
271169695Skan  _obstack_begin ((h), (size), 0, \
272169695Skan		  (void *(*) (long)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free)
273169695Skan
274169695Skan# define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
275169695Skan  _obstack_begin ((h), (size), (alignment), \
276169695Skan		    (void *(*) (long)) (chunkfun), (void (*) (void *)) (freefun))
277169695Skan
278169695Skan# define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
279169695Skan  _obstack_begin_1 ((h), (size), (alignment), \
280169695Skan		    (void *(*) (void *, long)) (chunkfun), \
281169695Skan		    (void (*) (void *, void *)) (freefun), (arg))
282169695Skan
283169695Skan# define obstack_chunkfun(h, newchunkfun) \
284169695Skan  ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
285169695Skan
286169695Skan# define obstack_freefun(h, newfreefun) \
287169695Skan  ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
288169695Skan
289169695Skan#define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
290169695Skan
291169695Skan#define obstack_blank_fast(h,n) ((h)->next_free += (n))
292169695Skan
293169695Skan#define obstack_memory_used(h) _obstack_memory_used (h)
294169695Skan
295169695Skan#if defined __GNUC__ && defined __STDC__ && __STDC__
296169695Skan/* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
297169695Skan   does not implement __extension__.  But that compiler doesn't define
298169695Skan   __GNUC_MINOR__.  */
299169695Skan# if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
300169695Skan#  define __extension__
301169695Skan# endif
302169695Skan
303169695Skan/* For GNU C, if not -traditional,
304169695Skan   we can define these macros to compute all args only once
305169695Skan   without using a global variable.
306169695Skan   Also, we can avoid using the `temp' slot, to make faster code.  */
307169695Skan
308169695Skan# define obstack_object_size(OBSTACK)					\
309169695Skan  __extension__								\
310169695Skan  ({ struct obstack *__o = (OBSTACK);					\
311169695Skan     (unsigned) (__o->next_free - __o->object_base); })
312169695Skan
313169695Skan# define obstack_room(OBSTACK)						\
314169695Skan  __extension__								\
315169695Skan  ({ struct obstack *__o = (OBSTACK);					\
316169695Skan     (unsigned) (__o->chunk_limit - __o->next_free); })
317169695Skan
318169695Skan# define obstack_make_room(OBSTACK,length)				\
319169695Skan__extension__								\
320169695Skan({ struct obstack *__o = (OBSTACK);					\
321169695Skan   int __len = (length);						\
322169695Skan   if (__o->chunk_limit - __o->next_free < __len)			\
323169695Skan     _obstack_newchunk (__o, __len);					\
324169695Skan   (void) 0; })
325169695Skan
326169695Skan# define obstack_empty_p(OBSTACK)					\
327169695Skan  __extension__								\
328169695Skan  ({ struct obstack *__o = (OBSTACK);					\
329169695Skan     (__o->chunk->prev == 0 && __o->next_free - __o->chunk->contents == 0); })
330169695Skan
331169695Skan# define obstack_grow(OBSTACK,where,length)				\
332169695Skan__extension__								\
333169695Skan({ struct obstack *__o = (OBSTACK);					\
334169695Skan   int __len = (length);						\
335169695Skan   if (__o->next_free + __len > __o->chunk_limit)			\
336169695Skan     _obstack_newchunk (__o, __len);					\
337169695Skan   _obstack_memcpy (__o->next_free, (where), __len);			\
338169695Skan   __o->next_free += __len;						\
339169695Skan   (void) 0; })
340169695Skan
341169695Skan# define obstack_grow0(OBSTACK,where,length)				\
342169695Skan__extension__								\
343169695Skan({ struct obstack *__o = (OBSTACK);					\
344169695Skan   int __len = (length);						\
345169695Skan   if (__o->next_free + __len + 1 > __o->chunk_limit)			\
346169695Skan     _obstack_newchunk (__o, __len + 1);				\
347169695Skan   _obstack_memcpy (__o->next_free, (where), __len);			\
348169695Skan   __o->next_free += __len;						\
349169695Skan   *(__o->next_free)++ = 0;						\
350169695Skan   (void) 0; })
351169695Skan
352169695Skan# define obstack_1grow(OBSTACK,datum)					\
353169695Skan__extension__								\
354169695Skan({ struct obstack *__o = (OBSTACK);					\
355169695Skan   if (__o->next_free + 1 > __o->chunk_limit)				\
356169695Skan     _obstack_newchunk (__o, 1);					\
357169695Skan   obstack_1grow_fast (__o, datum);					\
358169695Skan   (void) 0; })
359169695Skan
360169695Skan/* These assume that the obstack alignment is good enough for pointers or ints,
361169695Skan   and that the data added so far to the current object
362169695Skan   shares that much alignment.  */
363169695Skan
364169695Skan# define obstack_ptr_grow(OBSTACK,datum)				\
365169695Skan__extension__								\
366169695Skan({ struct obstack *__o = (OBSTACK);					\
367169695Skan   if (__o->next_free + sizeof (void *) > __o->chunk_limit)		\
368169695Skan     _obstack_newchunk (__o, sizeof (void *));				\
369169695Skan   obstack_ptr_grow_fast (__o, datum); })
370169695Skan
371169695Skan# define obstack_int_grow(OBSTACK,datum)				\
372169695Skan__extension__								\
373169695Skan({ struct obstack *__o = (OBSTACK);					\
374169695Skan   if (__o->next_free + sizeof (int) > __o->chunk_limit)		\
375169695Skan     _obstack_newchunk (__o, sizeof (int));				\
376169695Skan   obstack_int_grow_fast (__o, datum); })
377169695Skan
378169695Skan# define obstack_ptr_grow_fast(OBSTACK,aptr)				\
379169695Skan__extension__								\
380169695Skan({ struct obstack *__o1 = (OBSTACK);					\
381169695Skan   *(const void **) __o1->next_free = (aptr);				\
382169695Skan   __o1->next_free += sizeof (const void *);				\
383169695Skan   (void) 0; })
384169695Skan
385169695Skan# define obstack_int_grow_fast(OBSTACK,aint)				\
386169695Skan__extension__								\
387169695Skan({ struct obstack *__o1 = (OBSTACK);					\
388169695Skan   *(int *) __o1->next_free = (aint);					\
389169695Skan   __o1->next_free += sizeof (int);					\
390169695Skan   (void) 0; })
391169695Skan
392169695Skan# define obstack_blank(OBSTACK,length)					\
393169695Skan__extension__								\
394169695Skan({ struct obstack *__o = (OBSTACK);					\
395169695Skan   int __len = (length);						\
396169695Skan   if (__o->chunk_limit - __o->next_free < __len)			\
397169695Skan     _obstack_newchunk (__o, __len);					\
398169695Skan   obstack_blank_fast (__o, __len);					\
399169695Skan   (void) 0; })
400169695Skan
401169695Skan# define obstack_alloc(OBSTACK,length)					\
402169695Skan__extension__								\
403169695Skan({ struct obstack *__h = (OBSTACK);					\
404169695Skan   obstack_blank (__h, (length));					\
405169695Skan   obstack_finish (__h); })
406169695Skan
407169695Skan# define obstack_copy(OBSTACK,where,length)				\
408169695Skan__extension__								\
409169695Skan({ struct obstack *__h = (OBSTACK);					\
410169695Skan   obstack_grow (__h, (where), (length));				\
411169695Skan   obstack_finish (__h); })
412169695Skan
413169695Skan# define obstack_copy0(OBSTACK,where,length)				\
414169695Skan__extension__								\
415169695Skan({ struct obstack *__h = (OBSTACK);					\
416169695Skan   obstack_grow0 (__h, (where), (length));				\
417169695Skan   obstack_finish (__h); })
418169695Skan
419169695Skan/* The local variable is named __o1 to avoid a name conflict
420169695Skan   when obstack_blank is called.  */
421169695Skan# define obstack_finish(OBSTACK)  					\
422169695Skan__extension__								\
423169695Skan({ struct obstack *__o1 = (OBSTACK);					\
424169695Skan   void *value;								\
425169695Skan   value = (void *) __o1->object_base;					\
426169695Skan   if (__o1->next_free == value)					\
427169695Skan     __o1->maybe_empty_object = 1;					\
428169695Skan   __o1->next_free							\
429169695Skan     = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)\
430169695Skan		     & ~ (__o1->alignment_mask));			\
431169695Skan   if (__o1->next_free - (char *)__o1->chunk				\
432169695Skan       > __o1->chunk_limit - (char *)__o1->chunk)			\
433169695Skan     __o1->next_free = __o1->chunk_limit;				\
434169695Skan   __o1->object_base = __o1->next_free;					\
435169695Skan   value; })
436169695Skan
437169695Skan# define obstack_free(OBSTACK, OBJ)					\
438169695Skan__extension__								\
439169695Skan({ struct obstack *__o = (OBSTACK);					\
440169695Skan   void *__obj = (void *) (OBJ);					\
441169695Skan   if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit)  \
442169695Skan     __o->next_free = __o->object_base = (char *) __obj;		\
443169695Skan   else (obstack_free) (__o, __obj); })
444169695Skan
445169695Skan#else /* not __GNUC__ or not __STDC__ */
446169695Skan
447169695Skan# define obstack_object_size(h) \
448169695Skan (unsigned) ((h)->next_free - (h)->object_base)
449169695Skan
450169695Skan# define obstack_room(h)		\
451169695Skan (unsigned) ((h)->chunk_limit - (h)->next_free)
452169695Skan
453169695Skan# define obstack_empty_p(h) \
454169695Skan ((h)->chunk->prev == 0 && (h)->next_free - (h)->chunk->contents == 0)
455169695Skan
456169695Skan/* Note that the call to _obstack_newchunk is enclosed in (..., 0)
457169695Skan   so that we can avoid having void expressions
458169695Skan   in the arms of the conditional expression.
459169695Skan   Casting the third operand to void was tried before,
460169695Skan   but some compilers won't accept it.  */
461169695Skan
462169695Skan# define obstack_make_room(h,length)					\
463169695Skan( (h)->temp = (length),							\
464169695Skan  (((h)->next_free + (h)->temp > (h)->chunk_limit)			\
465169695Skan   ? (_obstack_newchunk ((h), (h)->temp), 0) : 0))
466169695Skan
467169695Skan# define obstack_grow(h,where,length)					\
468169695Skan( (h)->temp = (length),							\
469169695Skan  (((h)->next_free + (h)->temp > (h)->chunk_limit)			\
470169695Skan   ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),			\
471169695Skan  _obstack_memcpy ((h)->next_free, (where), (h)->temp),			\
472169695Skan  (h)->next_free += (h)->temp)
473169695Skan
474169695Skan# define obstack_grow0(h,where,length)					\
475169695Skan( (h)->temp = (length),							\
476169695Skan  (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit)			\
477169695Skan   ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0),			\
478169695Skan  _obstack_memcpy ((h)->next_free, (where), (h)->temp),			\
479169695Skan  (h)->next_free += (h)->temp,						\
480169695Skan  *((h)->next_free)++ = 0)
481169695Skan
482169695Skan# define obstack_1grow(h,datum)						\
483169695Skan( (((h)->next_free + 1 > (h)->chunk_limit)				\
484169695Skan   ? (_obstack_newchunk ((h), 1), 0) : 0),				\
485169695Skan  obstack_1grow_fast (h, datum))
486169695Skan
487169695Skan# define obstack_ptr_grow(h,datum)					\
488169695Skan( (((h)->next_free + sizeof (char *) > (h)->chunk_limit)		\
489169695Skan   ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0),		\
490169695Skan  obstack_ptr_grow_fast (h, datum))
491169695Skan
492169695Skan# define obstack_int_grow(h,datum)					\
493169695Skan( (((h)->next_free + sizeof (int) > (h)->chunk_limit)			\
494169695Skan   ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0),			\
495169695Skan  obstack_int_grow_fast (h, datum))
496169695Skan
497169695Skan# define obstack_ptr_grow_fast(h,aptr)					\
498169695Skan  (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
499169695Skan
500169695Skan# define obstack_int_grow_fast(h,aint)					\
501169695Skan  (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr))
502169695Skan
503169695Skan# define obstack_blank(h,length)					\
504169695Skan( (h)->temp = (length),							\
505169695Skan  (((h)->chunk_limit - (h)->next_free < (h)->temp)			\
506169695Skan   ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),			\
507169695Skan  obstack_blank_fast (h, (h)->temp))
508169695Skan
509169695Skan# define obstack_alloc(h,length)					\
510169695Skan (obstack_blank ((h), (length)), obstack_finish ((h)))
511169695Skan
512169695Skan# define obstack_copy(h,where,length)					\
513169695Skan (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
514169695Skan
515169695Skan# define obstack_copy0(h,where,length)					\
516169695Skan (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
517169695Skan
518169695Skan# define obstack_finish(h)  						\
519169695Skan( ((h)->next_free == (h)->object_base					\
520169695Skan   ? (((h)->maybe_empty_object = 1), 0)					\
521169695Skan   : 0),								\
522169695Skan  (h)->temp = __PTR_TO_INT ((h)->object_base),				\
523169695Skan  (h)->next_free							\
524169695Skan    = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask)	\
525169695Skan		    & ~ ((h)->alignment_mask)),				\
526169695Skan  (((h)->next_free - (char *) (h)->chunk				\
527169695Skan    > (h)->chunk_limit - (char *) (h)->chunk)				\
528169695Skan   ? ((h)->next_free = (h)->chunk_limit) : 0),				\
529169695Skan  (h)->object_base = (h)->next_free,					\
530169695Skan  __INT_TO_PTR ((h)->temp))
531169695Skan
532169695Skan# define obstack_free(h,obj)						\
533169695Skan( (h)->temp = (char *) (obj) - (char *) (h)->chunk,			\
534169695Skan  (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
535169695Skan   ? (int) ((h)->next_free = (h)->object_base				\
536169695Skan	    = (h)->temp + (char *) (h)->chunk)				\
537169695Skan   : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0)))
538169695Skan
539169695Skan#endif /* not __GNUC__ or not __STDC__ */
540169695Skan
541169695Skan#ifdef __cplusplus
542169695Skan}	/* C++ */
543169695Skan#endif
544169695Skan
545169695Skan#endif /* obstack.h */
546