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