133965Sjdp/* obstack.h - object stack macros
278828Sobrien   Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
3218822Sdim   1999, 2000, 2001, 2002, 2003, 2004, 2005
477298Sobrien   Free Software Foundation, Inc.
533965Sjdp
633965Sjdp
760484Sobrien   NOTE: The canonical source of this file is maintained with the GNU C Library.
860484Sobrien   Bugs can be reported to bug-glibc@gnu.org.
933965Sjdp
1060484Sobrien   This program is free software; you can redistribute it and/or modify it
1160484Sobrien   under the terms of the GNU General Public License as published by the
1260484Sobrien   Free Software Foundation; either version 2, or (at your option) any
1360484Sobrien   later version.
1433965Sjdp
1560484Sobrien   This program is distributed in the hope that it will be useful,
1660484Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1760484Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1860484Sobrien   GNU General Public License for more details.
1960484Sobrien
2060484Sobrien   You should have received a copy of the GNU General Public License
2160484Sobrien   along with this program; if not, write to the Free Software
22218822Sdim   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
2360484Sobrien   USA.  */
2460484Sobrien
2533965Sjdp/* Summary:
2633965Sjdp
2733965SjdpAll the apparent functions defined here are macros. The idea
2833965Sjdpis that you would use these pre-tested macros to solve a
2933965Sjdpvery specific set of problems, and they would run fast.
3033965SjdpCaution: no side-effects in arguments please!! They may be
3133965Sjdpevaluated MANY times!!
3233965Sjdp
3333965SjdpThese macros operate a stack of objects.  Each object starts life
3433965Sjdpsmall, and may grow to maturity.  (Consider building a word syllable
3533965Sjdpby syllable.)  An object can move while it is growing.  Once it has
3633965Sjdpbeen "finished" it never changes address again.  So the "top of the
3733965Sjdpstack" is typically an immature growing object, while the rest of the
3833965Sjdpstack is of mature, fixed size and fixed address objects.
3933965Sjdp
4033965SjdpThese routines grab large chunks of memory, using a function you
4133965Sjdpsupply, called `obstack_chunk_alloc'.  On occasion, they free chunks,
4233965Sjdpby calling `obstack_chunk_free'.  You must define them and declare
4333965Sjdpthem before using any obstack macros.
4433965Sjdp
4533965SjdpEach independent stack is represented by a `struct obstack'.
4633965SjdpEach of the obstack macros expects a pointer to such a structure
4733965Sjdpas the first argument.
4833965Sjdp
4933965SjdpOne motivation for this package is the problem of growing char strings
5033965Sjdpin symbol tables.  Unless you are "fascist pig with a read-only mind"
5133965Sjdp--Gosper's immortal quote from HAKMEM item 154, out of context--you
5233965Sjdpwould not like to put any arbitrary upper limit on the length of your
5333965Sjdpsymbols.
5433965Sjdp
5533965SjdpIn practice this often means you will build many short symbols and a
5633965Sjdpfew long symbols.  At the time you are reading a symbol you don't know
5733965Sjdphow long it is.  One traditional method is to read a symbol into a
5833965Sjdpbuffer, realloc()ating the buffer every time you try to read a symbol
5933965Sjdpthat is longer than the buffer.  This is beaut, but you still will
6033965Sjdpwant to copy the symbol from the buffer to a more permanent
6133965Sjdpsymbol-table entry say about half the time.
6233965Sjdp
6333965SjdpWith obstacks, you can work differently.  Use one obstack for all symbol
6433965Sjdpnames.  As you read a symbol, grow the name in the obstack gradually.
6533965SjdpWhen the name is complete, finalize it.  Then, if the symbol exists already,
6633965Sjdpfree the newly read name.
6733965Sjdp
6833965SjdpThe way we do this is to take a large chunk, allocating memory from
6933965Sjdplow addresses.  When you want to build a symbol in the chunk you just
7033965Sjdpadd chars above the current "high water mark" in the chunk.  When you
7133965Sjdphave finished adding chars, because you got to the end of the symbol,
7233965Sjdpyou know how long the chars are, and you can create a new object.
7333965SjdpMostly the chars will not burst over the highest address of the chunk,
7433965Sjdpbecause you would typically expect a chunk to be (say) 100 times as
7533965Sjdplong as an average object.
7633965Sjdp
7733965SjdpIn case that isn't clear, when we have enough chars to make up
7833965Sjdpthe object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
7933965Sjdpso we just point to it where it lies.  No moving of chars is
8033965Sjdpneeded and this is the second win: potentially long strings need
8133965Sjdpnever be explicitly shuffled. Once an object is formed, it does not
8233965Sjdpchange its address during its lifetime.
8333965Sjdp
8433965SjdpWhen the chars burst over a chunk boundary, we allocate a larger
8533965Sjdpchunk, and then copy the partly formed object from the end of the old
8633965Sjdpchunk to the beginning of the new larger chunk.  We then carry on
8733965Sjdpaccreting characters to the end of the object as we normally would.
8833965Sjdp
8933965SjdpA special macro is provided to add a single char at a time to a
9033965Sjdpgrowing object.  This allows the use of register variables, which
9133965Sjdpbreak the ordinary 'growth' macro.
9233965Sjdp
9333965SjdpSummary:
9433965Sjdp	We allocate large chunks.
9533965Sjdp	We carve out one object at a time from the current chunk.
9633965Sjdp	Once carved, an object never moves.
9733965Sjdp	We are free to append data of any size to the currently
9833965Sjdp	  growing object.
9933965Sjdp	Exactly one object is growing in an obstack at any one time.
10033965Sjdp	You can run one obstack per control block.
10133965Sjdp	You may have as many control blocks as you dare.
10233965Sjdp	Because of the way we do it, you can `unwind' an obstack
10333965Sjdp	  back to a previous state. (You may remove objects much
10433965Sjdp	  as you would with a stack.)
10533965Sjdp*/
10633965Sjdp
10733965Sjdp
10833965Sjdp/* Don't do the contents of this file more than once.  */
10933965Sjdp
11060484Sobrien#ifndef _OBSTACK_H
11160484Sobrien#define _OBSTACK_H 1
11260484Sobrien
11360484Sobrien#ifdef __cplusplus
11460484Sobrienextern "C" {
11560484Sobrien#endif
11633965Sjdp
11733965Sjdp/* We use subtraction of (char *) 0 instead of casting to int
11833965Sjdp   because on word-addressable machines a simple cast to int
11933965Sjdp   may ignore the byte-within-word field of the pointer.  */
12033965Sjdp
12133965Sjdp#ifndef __PTR_TO_INT
12260484Sobrien# define __PTR_TO_INT(P) ((P) - (char *) 0)
12333965Sjdp#endif
12433965Sjdp
12533965Sjdp#ifndef __INT_TO_PTR
12660484Sobrien# define __INT_TO_PTR(P) ((P) + (char *) 0)
12733965Sjdp#endif
12833965Sjdp
12960484Sobrien/* We need the type of the resulting object.  If __PTRDIFF_TYPE__ is
13060484Sobrien   defined, as with GNU C, use that; that way we don't pollute the
13160484Sobrien   namespace with <stddef.h>'s symbols.  Otherwise, if <stddef.h> is
13260484Sobrien   available, include it and use ptrdiff_t.  In traditional C, long is
13360484Sobrien   the best that we can do.  */
13433965Sjdp
13560484Sobrien#ifdef __PTRDIFF_TYPE__
13660484Sobrien# define PTR_INT_TYPE __PTRDIFF_TYPE__
13760484Sobrien#else
13860484Sobrien# ifdef HAVE_STDDEF_H
13960484Sobrien#  include <stddef.h>
14060484Sobrien#  define PTR_INT_TYPE ptrdiff_t
14160484Sobrien# else
14260484Sobrien#  define PTR_INT_TYPE long
14360484Sobrien# endif
14433965Sjdp#endif
14533965Sjdp
14660484Sobrien#if defined _LIBC || defined HAVE_STRING_H
14760484Sobrien# include <string.h>
148218822Sdim# define _obstack_memcpy(To, From, N) memcpy ((To), (From), (N))
14933965Sjdp#else
15060484Sobrien# ifdef memcpy
15160484Sobrien#  define _obstack_memcpy(To, From, N) memcpy ((To), (char *)(From), (N))
15260484Sobrien# else
15360484Sobrien#  define _obstack_memcpy(To, From, N) bcopy ((char *)(From), (To), (N))
15460484Sobrien# endif
15533965Sjdp#endif
15633965Sjdp
15733965Sjdpstruct _obstack_chunk		/* Lives at front of each chunk. */
15833965Sjdp{
15933965Sjdp  char  *limit;			/* 1 past end of this chunk */
16033965Sjdp  struct _obstack_chunk *prev;	/* address of prior chunk or NULL */
16133965Sjdp  char	contents[4];		/* objects begin here */
16233965Sjdp};
16333965Sjdp
16433965Sjdpstruct obstack		/* control current object in current chunk */
16533965Sjdp{
16633965Sjdp  long	chunk_size;		/* preferred size to allocate chunks in */
16733965Sjdp  struct _obstack_chunk *chunk;	/* address of current struct obstack_chunk */
16833965Sjdp  char	*object_base;		/* address of object we are building */
16933965Sjdp  char	*next_free;		/* where to add next char to current object */
17033965Sjdp  char	*chunk_limit;		/* address of char after current chunk */
17133965Sjdp  PTR_INT_TYPE temp;		/* Temporary for some macros.  */
17233965Sjdp  int   alignment_mask;		/* Mask of alignment for each object. */
17333965Sjdp  /* These prototypes vary based on `use_extra_arg', and we use
17433965Sjdp     casts to the prototypeless function type in all assignments,
17533965Sjdp     but having prototypes here quiets -Wstrict-prototypes.  */
17633965Sjdp  struct _obstack_chunk *(*chunkfun) (void *, long);
17733965Sjdp  void (*freefun) (void *, struct _obstack_chunk *);
17833965Sjdp  void *extra_arg;		/* first arg for chunk alloc/dealloc funcs */
17933965Sjdp  unsigned use_extra_arg:1;	/* chunk alloc/dealloc funcs take extra arg */
18033965Sjdp  unsigned maybe_empty_object:1;/* There is a possibility that the current
18133965Sjdp				   chunk contains a zero-length object.  This
18233965Sjdp				   prevents freeing the chunk if we allocate
18333965Sjdp				   a bigger chunk to replace it. */
18460484Sobrien  unsigned alloc_failed:1;	/* No longer used, as we now call the failed
18560484Sobrien				   handler on error, but retained for binary
18660484Sobrien				   compatibility.  */
18733965Sjdp};
18833965Sjdp
18933965Sjdp/* Declare the external functions we use; they are in obstack.c.  */
19033965Sjdp
19133965Sjdpextern void _obstack_newchunk (struct obstack *, int);
19233965Sjdpextern void _obstack_free (struct obstack *, void *);
19333965Sjdpextern int _obstack_begin (struct obstack *, int, int,
19433965Sjdp			    void *(*) (long), void (*) (void *));
19533965Sjdpextern int _obstack_begin_1 (struct obstack *, int, int,
19633965Sjdp			     void *(*) (void *, long),
19733965Sjdp			     void (*) (void *, void *), void *);
19833965Sjdpextern int _obstack_memory_used (struct obstack *);
19933965Sjdp
20033965Sjdp/* Do the function-declarations after the structs
20133965Sjdp   but before defining the macros.  */
20233965Sjdp
20333965Sjdpvoid obstack_init (struct obstack *obstack);
20433965Sjdp
20533965Sjdpvoid * obstack_alloc (struct obstack *obstack, int size);
20633965Sjdp
20733965Sjdpvoid * obstack_copy (struct obstack *obstack, void *address, int size);
20833965Sjdpvoid * obstack_copy0 (struct obstack *obstack, void *address, int size);
20933965Sjdp
21033965Sjdpvoid obstack_free (struct obstack *obstack, void *block);
21133965Sjdp
21233965Sjdpvoid obstack_blank (struct obstack *obstack, int size);
21333965Sjdp
21433965Sjdpvoid obstack_grow (struct obstack *obstack, void *data, int size);
21533965Sjdpvoid obstack_grow0 (struct obstack *obstack, void *data, int size);
21633965Sjdp
21733965Sjdpvoid obstack_1grow (struct obstack *obstack, int data_char);
21833965Sjdpvoid obstack_ptr_grow (struct obstack *obstack, void *data);
21933965Sjdpvoid obstack_int_grow (struct obstack *obstack, int data);
22033965Sjdp
22133965Sjdpvoid * obstack_finish (struct obstack *obstack);
22233965Sjdp
22333965Sjdpint obstack_object_size (struct obstack *obstack);
22433965Sjdp
22533965Sjdpint obstack_room (struct obstack *obstack);
22660484Sobrienvoid obstack_make_room (struct obstack *obstack, int size);
22733965Sjdpvoid obstack_1grow_fast (struct obstack *obstack, int data_char);
22833965Sjdpvoid obstack_ptr_grow_fast (struct obstack *obstack, void *data);
22933965Sjdpvoid obstack_int_grow_fast (struct obstack *obstack, int data);
23033965Sjdpvoid obstack_blank_fast (struct obstack *obstack, int size);
23133965Sjdp
23233965Sjdpvoid * obstack_base (struct obstack *obstack);
23333965Sjdpvoid * obstack_next_free (struct obstack *obstack);
23433965Sjdpint obstack_alignment_mask (struct obstack *obstack);
23533965Sjdpint obstack_chunk_size (struct obstack *obstack);
23633965Sjdpint obstack_memory_used (struct obstack *obstack);
23733965Sjdp
23860484Sobrien/* Error handler called when `obstack_chunk_alloc' failed to allocate
23960484Sobrien   more memory.  This can be set to a user defined function.  The
24060484Sobrien   default action is to print a message and abort.  */
24160484Sobrienextern void (*obstack_alloc_failed_handler) (void);
24260484Sobrien
24360484Sobrien/* Exit value used when `print_and_abort' is used.  */
24460484Sobrienextern int obstack_exit_failure;
24533965Sjdp
24633965Sjdp/* Pointer to beginning of object being allocated or to be allocated next.
24733965Sjdp   Note that this might not be the final address of the object
24833965Sjdp   because a new chunk might be needed to hold the final size.  */
24933965Sjdp
25060484Sobrien#define obstack_base(h) ((h)->object_base)
25133965Sjdp
25233965Sjdp/* Size for allocating ordinary chunks.  */
25333965Sjdp
25433965Sjdp#define obstack_chunk_size(h) ((h)->chunk_size)
25533965Sjdp
25633965Sjdp/* Pointer to next byte not yet allocated in current chunk.  */
25733965Sjdp
25860484Sobrien#define obstack_next_free(h)	((h)->next_free)
25933965Sjdp
26033965Sjdp/* Mask specifying low bits that should be clear in address of an object.  */
26133965Sjdp
26233965Sjdp#define obstack_alignment_mask(h) ((h)->alignment_mask)
26333965Sjdp
26433965Sjdp/* To prevent prototype warnings provide complete argument list in
26533965Sjdp   standard C version.  */
26660484Sobrien# define obstack_init(h) \
26733965Sjdp  _obstack_begin ((h), 0, 0, \
26833965Sjdp		  (void *(*) (long)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free)
26933965Sjdp
27060484Sobrien# define obstack_begin(h, size) \
27133965Sjdp  _obstack_begin ((h), (size), 0, \
27233965Sjdp		  (void *(*) (long)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free)
27333965Sjdp
27460484Sobrien# define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
27533965Sjdp  _obstack_begin ((h), (size), (alignment), \
27633965Sjdp		    (void *(*) (long)) (chunkfun), (void (*) (void *)) (freefun))
27733965Sjdp
27860484Sobrien# define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
27933965Sjdp  _obstack_begin_1 ((h), (size), (alignment), \
28038889Sjdp		    (void *(*) (void *, long)) (chunkfun), \
28138889Sjdp		    (void (*) (void *, void *)) (freefun), (arg))
28233965Sjdp
28360484Sobrien# define obstack_chunkfun(h, newchunkfun) \
28438889Sjdp  ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
28533965Sjdp
28660484Sobrien# define obstack_freefun(h, newfreefun) \
28738889Sjdp  ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
28833965Sjdp
289130561Sobrien#define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
29033965Sjdp
29133965Sjdp#define obstack_blank_fast(h,n) ((h)->next_free += (n))
29233965Sjdp
29333965Sjdp#define obstack_memory_used(h) _obstack_memory_used (h)
29433965Sjdp
29560484Sobrien#if defined __GNUC__ && defined __STDC__ && __STDC__
29633965Sjdp/* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
29733965Sjdp   does not implement __extension__.  But that compiler doesn't define
29833965Sjdp   __GNUC_MINOR__.  */
29960484Sobrien# if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
30060484Sobrien#  define __extension__
30160484Sobrien# endif
30233965Sjdp
30333965Sjdp/* For GNU C, if not -traditional,
30433965Sjdp   we can define these macros to compute all args only once
30533965Sjdp   without using a global variable.
30633965Sjdp   Also, we can avoid using the `temp' slot, to make faster code.  */
30733965Sjdp
30860484Sobrien# define obstack_object_size(OBSTACK)					\
30933965Sjdp  __extension__								\
31033965Sjdp  ({ struct obstack *__o = (OBSTACK);					\
31133965Sjdp     (unsigned) (__o->next_free - __o->object_base); })
31233965Sjdp
31360484Sobrien# define obstack_room(OBSTACK)						\
31433965Sjdp  __extension__								\
31533965Sjdp  ({ struct obstack *__o = (OBSTACK);					\
31633965Sjdp     (unsigned) (__o->chunk_limit - __o->next_free); })
31733965Sjdp
31860484Sobrien# define obstack_make_room(OBSTACK,length)				\
31933965Sjdp__extension__								\
32033965Sjdp({ struct obstack *__o = (OBSTACK);					\
32133965Sjdp   int __len = (length);						\
32260484Sobrien   if (__o->chunk_limit - __o->next_free < __len)			\
32360484Sobrien     _obstack_newchunk (__o, __len);					\
32460484Sobrien   (void) 0; })
32560484Sobrien
32660484Sobrien# define obstack_empty_p(OBSTACK)					\
32760484Sobrien  __extension__								\
32860484Sobrien  ({ struct obstack *__o = (OBSTACK);					\
32960484Sobrien     (__o->chunk->prev == 0 && __o->next_free - __o->chunk->contents == 0); })
33060484Sobrien
33160484Sobrien# define obstack_grow(OBSTACK,where,length)				\
33260484Sobrien__extension__								\
33360484Sobrien({ struct obstack *__o = (OBSTACK);					\
33460484Sobrien   int __len = (length);						\
33533965Sjdp   if (__o->next_free + __len > __o->chunk_limit)			\
33633965Sjdp     _obstack_newchunk (__o, __len);					\
33760484Sobrien   _obstack_memcpy (__o->next_free, (where), __len);			\
33860484Sobrien   __o->next_free += __len;						\
33933965Sjdp   (void) 0; })
34033965Sjdp
34160484Sobrien# define obstack_grow0(OBSTACK,where,length)				\
34233965Sjdp__extension__								\
34333965Sjdp({ struct obstack *__o = (OBSTACK);					\
34433965Sjdp   int __len = (length);						\
34533965Sjdp   if (__o->next_free + __len + 1 > __o->chunk_limit)			\
34633965Sjdp     _obstack_newchunk (__o, __len + 1);				\
34760484Sobrien   _obstack_memcpy (__o->next_free, (where), __len);			\
34860484Sobrien   __o->next_free += __len;						\
34960484Sobrien   *(__o->next_free)++ = 0;						\
35033965Sjdp   (void) 0; })
35133965Sjdp
35260484Sobrien# define obstack_1grow(OBSTACK,datum)					\
35333965Sjdp__extension__								\
35433965Sjdp({ struct obstack *__o = (OBSTACK);					\
35533965Sjdp   if (__o->next_free + 1 > __o->chunk_limit)				\
35633965Sjdp     _obstack_newchunk (__o, 1);					\
357130561Sobrien   obstack_1grow_fast (__o, datum);					\
35833965Sjdp   (void) 0; })
35933965Sjdp
36033965Sjdp/* These assume that the obstack alignment is good enough for pointers or ints,
36133965Sjdp   and that the data added so far to the current object
36233965Sjdp   shares that much alignment.  */
36333965Sjdp
36460484Sobrien# define obstack_ptr_grow(OBSTACK,datum)				\
36533965Sjdp__extension__								\
36633965Sjdp({ struct obstack *__o = (OBSTACK);					\
36733965Sjdp   if (__o->next_free + sizeof (void *) > __o->chunk_limit)		\
36833965Sjdp     _obstack_newchunk (__o, sizeof (void *));				\
369130561Sobrien   obstack_ptr_grow_fast (__o, datum); })
37033965Sjdp
37160484Sobrien# define obstack_int_grow(OBSTACK,datum)				\
37233965Sjdp__extension__								\
37333965Sjdp({ struct obstack *__o = (OBSTACK);					\
37433965Sjdp   if (__o->next_free + sizeof (int) > __o->chunk_limit)		\
37533965Sjdp     _obstack_newchunk (__o, sizeof (int));				\
376130561Sobrien   obstack_int_grow_fast (__o, datum); })
377130561Sobrien
378130561Sobrien# define obstack_ptr_grow_fast(OBSTACK,aptr)				\
379130561Sobrien__extension__								\
380130561Sobrien({ struct obstack *__o1 = (OBSTACK);					\
381130561Sobrien   *(const void **) __o1->next_free = (aptr);				\
382130561Sobrien   __o1->next_free += sizeof (const void *);				\
38333965Sjdp   (void) 0; })
38433965Sjdp
385130561Sobrien# define obstack_int_grow_fast(OBSTACK,aint)				\
386130561Sobrien__extension__								\
387130561Sobrien({ struct obstack *__o1 = (OBSTACK);					\
388130561Sobrien   *(int *) __o1->next_free = (aint);					\
389130561Sobrien   __o1->next_free += sizeof (int);					\
390130561Sobrien   (void) 0; })
39133965Sjdp
39260484Sobrien# define obstack_blank(OBSTACK,length)					\
39333965Sjdp__extension__								\
39433965Sjdp({ struct obstack *__o = (OBSTACK);					\
39533965Sjdp   int __len = (length);						\
39633965Sjdp   if (__o->chunk_limit - __o->next_free < __len)			\
39733965Sjdp     _obstack_newchunk (__o, __len);					\
398130561Sobrien   obstack_blank_fast (__o, __len);					\
39933965Sjdp   (void) 0; })
40033965Sjdp
40160484Sobrien# define obstack_alloc(OBSTACK,length)					\
40233965Sjdp__extension__								\
40333965Sjdp({ struct obstack *__h = (OBSTACK);					\
40433965Sjdp   obstack_blank (__h, (length));					\
40533965Sjdp   obstack_finish (__h); })
40633965Sjdp
40760484Sobrien# define obstack_copy(OBSTACK,where,length)				\
40833965Sjdp__extension__								\
40933965Sjdp({ struct obstack *__h = (OBSTACK);					\
41033965Sjdp   obstack_grow (__h, (where), (length));				\
41133965Sjdp   obstack_finish (__h); })
41233965Sjdp
41360484Sobrien# define obstack_copy0(OBSTACK,where,length)				\
41433965Sjdp__extension__								\
41533965Sjdp({ struct obstack *__h = (OBSTACK);					\
41633965Sjdp   obstack_grow0 (__h, (where), (length));				\
41733965Sjdp   obstack_finish (__h); })
41833965Sjdp
41933965Sjdp/* The local variable is named __o1 to avoid a name conflict
42033965Sjdp   when obstack_blank is called.  */
42160484Sobrien# define obstack_finish(OBSTACK)  					\
42233965Sjdp__extension__								\
42333965Sjdp({ struct obstack *__o1 = (OBSTACK);					\
42433965Sjdp   void *value;								\
42560484Sobrien   value = (void *) __o1->object_base;					\
42660484Sobrien   if (__o1->next_free == value)					\
42760484Sobrien     __o1->maybe_empty_object = 1;					\
42860484Sobrien   __o1->next_free							\
42960484Sobrien     = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)\
43060484Sobrien		     & ~ (__o1->alignment_mask));			\
43160484Sobrien   if (__o1->next_free - (char *)__o1->chunk				\
43260484Sobrien       > __o1->chunk_limit - (char *)__o1->chunk)			\
43360484Sobrien     __o1->next_free = __o1->chunk_limit;				\
43460484Sobrien   __o1->object_base = __o1->next_free;					\
43533965Sjdp   value; })
43633965Sjdp
43760484Sobrien# define obstack_free(OBSTACK, OBJ)					\
43833965Sjdp__extension__								\
43933965Sjdp({ struct obstack *__o = (OBSTACK);					\
440218822Sdim   void *__obj = (void *) (OBJ);					\
44133965Sjdp   if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit)  \
442218822Sdim     __o->next_free = __o->object_base = (char *) __obj;		\
44333965Sjdp   else (obstack_free) (__o, __obj); })
44433965Sjdp
44533965Sjdp#else /* not __GNUC__ or not __STDC__ */
44633965Sjdp
44760484Sobrien# define obstack_object_size(h) \
44860484Sobrien (unsigned) ((h)->next_free - (h)->object_base)
44933965Sjdp
45060484Sobrien# define obstack_room(h)		\
45133965Sjdp (unsigned) ((h)->chunk_limit - (h)->next_free)
45233965Sjdp
45360484Sobrien# define obstack_empty_p(h) \
45460484Sobrien ((h)->chunk->prev == 0 && (h)->next_free - (h)->chunk->contents == 0)
45560484Sobrien
45633965Sjdp/* Note that the call to _obstack_newchunk is enclosed in (..., 0)
45733965Sjdp   so that we can avoid having void expressions
45833965Sjdp   in the arms of the conditional expression.
45933965Sjdp   Casting the third operand to void was tried before,
46033965Sjdp   but some compilers won't accept it.  */
46133965Sjdp
46260484Sobrien# define obstack_make_room(h,length)					\
46333965Sjdp( (h)->temp = (length),							\
46433965Sjdp  (((h)->next_free + (h)->temp > (h)->chunk_limit)			\
46560484Sobrien   ? (_obstack_newchunk ((h), (h)->temp), 0) : 0))
46660484Sobrien
46760484Sobrien# define obstack_grow(h,where,length)					\
46860484Sobrien( (h)->temp = (length),							\
46960484Sobrien  (((h)->next_free + (h)->temp > (h)->chunk_limit)			\
47033965Sjdp   ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),			\
47160484Sobrien  _obstack_memcpy ((h)->next_free, (where), (h)->temp),			\
47260484Sobrien  (h)->next_free += (h)->temp)
47333965Sjdp
47460484Sobrien# define obstack_grow0(h,where,length)					\
47533965Sjdp( (h)->temp = (length),							\
47633965Sjdp  (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit)			\
47733965Sjdp   ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0),			\
47860484Sobrien  _obstack_memcpy ((h)->next_free, (where), (h)->temp),			\
47933965Sjdp  (h)->next_free += (h)->temp,						\
48060484Sobrien  *((h)->next_free)++ = 0)
48133965Sjdp
48260484Sobrien# define obstack_1grow(h,datum)						\
48333965Sjdp( (((h)->next_free + 1 > (h)->chunk_limit)				\
48433965Sjdp   ? (_obstack_newchunk ((h), 1), 0) : 0),				\
485130561Sobrien  obstack_1grow_fast (h, datum))
48633965Sjdp
48760484Sobrien# define obstack_ptr_grow(h,datum)					\
48833965Sjdp( (((h)->next_free + sizeof (char *) > (h)->chunk_limit)		\
48933965Sjdp   ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0),		\
490130561Sobrien  obstack_ptr_grow_fast (h, datum))
49133965Sjdp
49260484Sobrien# define obstack_int_grow(h,datum)					\
49333965Sjdp( (((h)->next_free + sizeof (int) > (h)->chunk_limit)			\
49433965Sjdp   ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0),			\
495130561Sobrien  obstack_int_grow_fast (h, datum))
49633965Sjdp
497130561Sobrien# define obstack_ptr_grow_fast(h,aptr)					\
498130561Sobrien  (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
49933965Sjdp
500130561Sobrien# define obstack_int_grow_fast(h,aint)					\
501130561Sobrien  (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr))
502130561Sobrien
50360484Sobrien# define obstack_blank(h,length)					\
50433965Sjdp( (h)->temp = (length),							\
50533965Sjdp  (((h)->chunk_limit - (h)->next_free < (h)->temp)			\
50633965Sjdp   ? (_obstack_newchunk ((h), (h)->temp), 0) : 0),			\
507130561Sobrien  obstack_blank_fast (h, (h)->temp))
50833965Sjdp
50960484Sobrien# define obstack_alloc(h,length)					\
51033965Sjdp (obstack_blank ((h), (length)), obstack_finish ((h)))
51133965Sjdp
51260484Sobrien# define obstack_copy(h,where,length)					\
51333965Sjdp (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
51433965Sjdp
51560484Sobrien# define obstack_copy0(h,where,length)					\
51633965Sjdp (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
51733965Sjdp
51860484Sobrien# define obstack_finish(h)  						\
51960484Sobrien( ((h)->next_free == (h)->object_base					\
52033965Sjdp   ? (((h)->maybe_empty_object = 1), 0)					\
52133965Sjdp   : 0),								\
52233965Sjdp  (h)->temp = __PTR_TO_INT ((h)->object_base),				\
52333965Sjdp  (h)->next_free							\
52433965Sjdp    = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask)	\
52533965Sjdp		    & ~ ((h)->alignment_mask)),				\
52660484Sobrien  (((h)->next_free - (char *) (h)->chunk				\
52733965Sjdp    > (h)->chunk_limit - (char *) (h)->chunk)				\
52833965Sjdp   ? ((h)->next_free = (h)->chunk_limit) : 0),				\
52933965Sjdp  (h)->object_base = (h)->next_free,					\
53060484Sobrien  __INT_TO_PTR ((h)->temp))
53133965Sjdp
532218822Sdim# define obstack_free(h,obj)						\
53333965Sjdp( (h)->temp = (char *) (obj) - (char *) (h)->chunk,			\
53433965Sjdp  (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
53533965Sjdp   ? (int) ((h)->next_free = (h)->object_base				\
53633965Sjdp	    = (h)->temp + (char *) (h)->chunk)				\
53733965Sjdp   : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0)))
53833965Sjdp
53933965Sjdp#endif /* not __GNUC__ or not __STDC__ */
54033965Sjdp
54160484Sobrien#ifdef __cplusplus
54260484Sobrien}	/* C++ */
54360484Sobrien#endif
54460484Sobrien
54560484Sobrien#endif /* obstack.h */
546