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