15796c8dcSSimon Schubert /* obstack.h - object stack macros 25796c8dcSSimon Schubert Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 35796c8dcSSimon Schubert 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008 45796c8dcSSimon Schubert Free Software Foundation, Inc. 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert NOTE: The canonical source of this file is maintained with the GNU C Library. 85796c8dcSSimon Schubert Bugs can be reported to bug-glibc@gnu.org. 95796c8dcSSimon Schubert 105796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify it 115796c8dcSSimon Schubert under the terms of the GNU General Public License as published by the 125796c8dcSSimon Schubert Free Software Foundation; either version 2, or (at your option) any 135796c8dcSSimon Schubert later version. 145796c8dcSSimon Schubert 155796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 165796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 175796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 185796c8dcSSimon Schubert GNU General Public License for more details. 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 215796c8dcSSimon Schubert along with this program; if not, write to the Free Software 225796c8dcSSimon Schubert Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 235796c8dcSSimon Schubert USA. */ 245796c8dcSSimon Schubert 255796c8dcSSimon Schubert /* Summary: 265796c8dcSSimon Schubert 275796c8dcSSimon Schubert All the apparent functions defined here are macros. The idea 285796c8dcSSimon Schubert is that you would use these pre-tested macros to solve a 295796c8dcSSimon Schubert very specific set of problems, and they would run fast. 305796c8dcSSimon Schubert Caution: no side-effects in arguments please!! They may be 315796c8dcSSimon Schubert evaluated MANY times!! 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert These macros operate a stack of objects. Each object starts life 345796c8dcSSimon Schubert small, and may grow to maturity. (Consider building a word syllable 355796c8dcSSimon Schubert by syllable.) An object can move while it is growing. Once it has 365796c8dcSSimon Schubert been "finished" it never changes address again. So the "top of the 375796c8dcSSimon Schubert stack" is typically an immature growing object, while the rest of the 385796c8dcSSimon Schubert stack is of mature, fixed size and fixed address objects. 395796c8dcSSimon Schubert 405796c8dcSSimon Schubert These routines grab large chunks of memory, using a function you 415796c8dcSSimon Schubert supply, called `obstack_chunk_alloc'. On occasion, they free chunks, 425796c8dcSSimon Schubert by calling `obstack_chunk_free'. You must define them and declare 435796c8dcSSimon Schubert them before using any obstack macros. 445796c8dcSSimon Schubert 455796c8dcSSimon Schubert Each independent stack is represented by a `struct obstack'. 465796c8dcSSimon Schubert Each of the obstack macros expects a pointer to such a structure 475796c8dcSSimon Schubert as the first argument. 485796c8dcSSimon Schubert 495796c8dcSSimon Schubert One motivation for this package is the problem of growing char strings 505796c8dcSSimon Schubert in symbol tables. Unless you are "fascist pig with a read-only mind" 515796c8dcSSimon Schubert --Gosper's immortal quote from HAKMEM item 154, out of context--you 525796c8dcSSimon Schubert would not like to put any arbitrary upper limit on the length of your 535796c8dcSSimon Schubert symbols. 545796c8dcSSimon Schubert 555796c8dcSSimon Schubert In practice this often means you will build many short symbols and a 565796c8dcSSimon Schubert few long symbols. At the time you are reading a symbol you don't know 575796c8dcSSimon Schubert how long it is. One traditional method is to read a symbol into a 585796c8dcSSimon Schubert buffer, realloc()ating the buffer every time you try to read a symbol 595796c8dcSSimon Schubert that is longer than the buffer. This is beaut, but you still will 605796c8dcSSimon Schubert want to copy the symbol from the buffer to a more permanent 615796c8dcSSimon Schubert symbol-table entry say about half the time. 625796c8dcSSimon Schubert 635796c8dcSSimon Schubert With obstacks, you can work differently. Use one obstack for all symbol 645796c8dcSSimon Schubert names. As you read a symbol, grow the name in the obstack gradually. 655796c8dcSSimon Schubert When the name is complete, finalize it. Then, if the symbol exists already, 665796c8dcSSimon Schubert free the newly read name. 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert The way we do this is to take a large chunk, allocating memory from 695796c8dcSSimon Schubert low addresses. When you want to build a symbol in the chunk you just 705796c8dcSSimon Schubert add chars above the current "high water mark" in the chunk. When you 715796c8dcSSimon Schubert have finished adding chars, because you got to the end of the symbol, 725796c8dcSSimon Schubert you know how long the chars are, and you can create a new object. 735796c8dcSSimon Schubert Mostly the chars will not burst over the highest address of the chunk, 745796c8dcSSimon Schubert because you would typically expect a chunk to be (say) 100 times as 755796c8dcSSimon Schubert long as an average object. 765796c8dcSSimon Schubert 775796c8dcSSimon Schubert In case that isn't clear, when we have enough chars to make up 785796c8dcSSimon Schubert the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed) 795796c8dcSSimon Schubert so we just point to it where it lies. No moving of chars is 805796c8dcSSimon Schubert needed and this is the second win: potentially long strings need 815796c8dcSSimon Schubert never be explicitly shuffled. Once an object is formed, it does not 825796c8dcSSimon Schubert change its address during its lifetime. 835796c8dcSSimon Schubert 845796c8dcSSimon Schubert When the chars burst over a chunk boundary, we allocate a larger 855796c8dcSSimon Schubert chunk, and then copy the partly formed object from the end of the old 865796c8dcSSimon Schubert chunk to the beginning of the new larger chunk. We then carry on 875796c8dcSSimon Schubert accreting characters to the end of the object as we normally would. 885796c8dcSSimon Schubert 895796c8dcSSimon Schubert A special macro is provided to add a single char at a time to a 905796c8dcSSimon Schubert growing object. This allows the use of register variables, which 915796c8dcSSimon Schubert break the ordinary 'growth' macro. 925796c8dcSSimon Schubert 935796c8dcSSimon Schubert Summary: 945796c8dcSSimon Schubert We allocate large chunks. 955796c8dcSSimon Schubert We carve out one object at a time from the current chunk. 965796c8dcSSimon Schubert Once carved, an object never moves. 975796c8dcSSimon Schubert We are free to append data of any size to the currently 985796c8dcSSimon Schubert growing object. 995796c8dcSSimon Schubert Exactly one object is growing in an obstack at any one time. 1005796c8dcSSimon Schubert You can run one obstack per control block. 1015796c8dcSSimon Schubert You may have as many control blocks as you dare. 1025796c8dcSSimon Schubert Because of the way we do it, you can `unwind' an obstack 1035796c8dcSSimon Schubert back to a previous state. (You may remove objects much 1045796c8dcSSimon Schubert as you would with a stack.) 1055796c8dcSSimon Schubert */ 1065796c8dcSSimon Schubert 1075796c8dcSSimon Schubert 1085796c8dcSSimon Schubert /* Don't do the contents of this file more than once. */ 1095796c8dcSSimon Schubert 1105796c8dcSSimon Schubert #ifndef _OBSTACK_H 1115796c8dcSSimon Schubert #define _OBSTACK_H 1 1125796c8dcSSimon Schubert 1135796c8dcSSimon Schubert #ifdef __cplusplus 1145796c8dcSSimon Schubert extern "C" { 1155796c8dcSSimon Schubert #endif 1165796c8dcSSimon Schubert 1175796c8dcSSimon Schubert /* We use subtraction of (char *) 0 instead of casting to int 1185796c8dcSSimon Schubert because on word-addressable machines a simple cast to int 1195796c8dcSSimon Schubert may ignore the byte-within-word field of the pointer. */ 1205796c8dcSSimon Schubert 1215796c8dcSSimon Schubert #ifndef __PTR_TO_INT 1225796c8dcSSimon Schubert # define __PTR_TO_INT(P) ((P) - (char *) 0) 1235796c8dcSSimon Schubert #endif 1245796c8dcSSimon Schubert 1255796c8dcSSimon Schubert #ifndef __INT_TO_PTR 1265796c8dcSSimon Schubert # define __INT_TO_PTR(P) ((P) + (char *) 0) 1275796c8dcSSimon Schubert #endif 1285796c8dcSSimon Schubert 1295796c8dcSSimon Schubert /* We need the type of the resulting object. If __PTRDIFF_TYPE__ is 1305796c8dcSSimon Schubert defined, as with GNU C, use that; that way we don't pollute the 1315796c8dcSSimon Schubert namespace with <stddef.h>'s symbols. Otherwise, if <stddef.h> is 1325796c8dcSSimon Schubert available, include it and use ptrdiff_t. In traditional C, long is 1335796c8dcSSimon Schubert the best that we can do. */ 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert #ifdef __PTRDIFF_TYPE__ 1365796c8dcSSimon Schubert # define PTR_INT_TYPE __PTRDIFF_TYPE__ 1375796c8dcSSimon Schubert #else 1385796c8dcSSimon Schubert # ifdef HAVE_STDDEF_H 1395796c8dcSSimon Schubert # include <stddef.h> 1405796c8dcSSimon Schubert # define PTR_INT_TYPE ptrdiff_t 1415796c8dcSSimon Schubert # else 1425796c8dcSSimon Schubert # define PTR_INT_TYPE long 1435796c8dcSSimon Schubert # endif 1445796c8dcSSimon Schubert #endif 1455796c8dcSSimon Schubert 1465796c8dcSSimon Schubert #if defined _LIBC || defined HAVE_STRING_H 1475796c8dcSSimon Schubert # include <string.h> 1485796c8dcSSimon Schubert # define _obstack_memcpy(To, From, N) memcpy ((To), (From), (N)) 1495796c8dcSSimon Schubert #else 1505796c8dcSSimon Schubert # ifdef memcpy 1515796c8dcSSimon Schubert # define _obstack_memcpy(To, From, N) memcpy ((To), (char *)(From), (N)) 1525796c8dcSSimon Schubert # else 1535796c8dcSSimon Schubert # define _obstack_memcpy(To, From, N) bcopy ((char *)(From), (To), (N)) 1545796c8dcSSimon Schubert # endif 1555796c8dcSSimon Schubert #endif 1565796c8dcSSimon Schubert 1575796c8dcSSimon Schubert struct _obstack_chunk /* Lives at front of each chunk. */ 1585796c8dcSSimon Schubert { 1595796c8dcSSimon Schubert char *limit; /* 1 past end of this chunk */ 1605796c8dcSSimon Schubert struct _obstack_chunk *prev; /* address of prior chunk or NULL */ 1615796c8dcSSimon Schubert char contents[4]; /* objects begin here */ 1625796c8dcSSimon Schubert }; 1635796c8dcSSimon Schubert 1645796c8dcSSimon Schubert struct obstack /* control current object in current chunk */ 1655796c8dcSSimon Schubert { 1665796c8dcSSimon Schubert long chunk_size; /* preferred size to allocate chunks in */ 1675796c8dcSSimon Schubert struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */ 1685796c8dcSSimon Schubert char *object_base; /* address of object we are building */ 1695796c8dcSSimon Schubert char *next_free; /* where to add next char to current object */ 1705796c8dcSSimon Schubert char *chunk_limit; /* address of char after current chunk */ 1715796c8dcSSimon Schubert PTR_INT_TYPE temp; /* Temporary for some macros. */ 1725796c8dcSSimon Schubert int alignment_mask; /* Mask of alignment for each object. */ 1735796c8dcSSimon Schubert /* These prototypes vary based on `use_extra_arg', and we use 1745796c8dcSSimon Schubert casts to the prototypeless function type in all assignments, 1755796c8dcSSimon Schubert but having prototypes here quiets -Wstrict-prototypes. */ 1765796c8dcSSimon Schubert struct _obstack_chunk *(*chunkfun) (void *, long); 1775796c8dcSSimon Schubert void (*freefun) (void *, struct _obstack_chunk *); 1785796c8dcSSimon Schubert void *extra_arg; /* first arg for chunk alloc/dealloc funcs */ 1795796c8dcSSimon Schubert unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */ 1805796c8dcSSimon Schubert unsigned maybe_empty_object:1;/* There is a possibility that the current 1815796c8dcSSimon Schubert chunk contains a zero-length object. This 1825796c8dcSSimon Schubert prevents freeing the chunk if we allocate 1835796c8dcSSimon Schubert a bigger chunk to replace it. */ 1845796c8dcSSimon Schubert unsigned alloc_failed:1; /* No longer used, as we now call the failed 1855796c8dcSSimon Schubert handler on error, but retained for binary 1865796c8dcSSimon Schubert compatibility. */ 1875796c8dcSSimon Schubert }; 1885796c8dcSSimon Schubert 1895796c8dcSSimon Schubert /* Declare the external functions we use; they are in obstack.c. */ 1905796c8dcSSimon Schubert 1915796c8dcSSimon Schubert extern void _obstack_newchunk (struct obstack *, int); 1925796c8dcSSimon Schubert extern void _obstack_free (struct obstack *, void *); 1935796c8dcSSimon Schubert extern int _obstack_begin (struct obstack *, int, int, 1945796c8dcSSimon Schubert void *(*) (long), void (*) (void *)); 1955796c8dcSSimon Schubert extern int _obstack_begin_1 (struct obstack *, int, int, 1965796c8dcSSimon Schubert void *(*) (void *, long), 1975796c8dcSSimon Schubert void (*) (void *, void *), void *); 1985796c8dcSSimon Schubert extern int _obstack_memory_used (struct obstack *); 1995796c8dcSSimon Schubert 2005796c8dcSSimon Schubert /* Do the function-declarations after the structs 2015796c8dcSSimon Schubert but before defining the macros. */ 2025796c8dcSSimon Schubert 2035796c8dcSSimon Schubert void obstack_init (struct obstack *obstack); 2045796c8dcSSimon Schubert 2055796c8dcSSimon Schubert void * obstack_alloc (struct obstack *obstack, int size); 2065796c8dcSSimon Schubert 2075796c8dcSSimon Schubert void * obstack_copy (struct obstack *obstack, void *address, int size); 2085796c8dcSSimon Schubert void * obstack_copy0 (struct obstack *obstack, void *address, int size); 2095796c8dcSSimon Schubert 2105796c8dcSSimon Schubert void obstack_free (struct obstack *obstack, void *block); 2115796c8dcSSimon Schubert 2125796c8dcSSimon Schubert void obstack_blank (struct obstack *obstack, int size); 2135796c8dcSSimon Schubert 2145796c8dcSSimon Schubert void obstack_grow (struct obstack *obstack, void *data, int size); 2155796c8dcSSimon Schubert void obstack_grow0 (struct obstack *obstack, void *data, int size); 2165796c8dcSSimon Schubert 2175796c8dcSSimon Schubert void obstack_1grow (struct obstack *obstack, int data_char); 2185796c8dcSSimon Schubert void obstack_ptr_grow (struct obstack *obstack, void *data); 2195796c8dcSSimon Schubert void obstack_int_grow (struct obstack *obstack, int data); 2205796c8dcSSimon Schubert 2215796c8dcSSimon Schubert void * obstack_finish (struct obstack *obstack); 2225796c8dcSSimon Schubert 2235796c8dcSSimon Schubert int obstack_object_size (struct obstack *obstack); 2245796c8dcSSimon Schubert 2255796c8dcSSimon Schubert int obstack_room (struct obstack *obstack); 2265796c8dcSSimon Schubert void obstack_make_room (struct obstack *obstack, int size); 2275796c8dcSSimon Schubert void obstack_1grow_fast (struct obstack *obstack, int data_char); 2285796c8dcSSimon Schubert void obstack_ptr_grow_fast (struct obstack *obstack, void *data); 2295796c8dcSSimon Schubert void obstack_int_grow_fast (struct obstack *obstack, int data); 2305796c8dcSSimon Schubert void obstack_blank_fast (struct obstack *obstack, int size); 2315796c8dcSSimon Schubert 2325796c8dcSSimon Schubert void * obstack_base (struct obstack *obstack); 2335796c8dcSSimon Schubert void * obstack_next_free (struct obstack *obstack); 2345796c8dcSSimon Schubert int obstack_alignment_mask (struct obstack *obstack); 2355796c8dcSSimon Schubert int obstack_chunk_size (struct obstack *obstack); 2365796c8dcSSimon Schubert int obstack_memory_used (struct obstack *obstack); 2375796c8dcSSimon Schubert 2385796c8dcSSimon Schubert /* Error handler called when `obstack_chunk_alloc' failed to allocate 2395796c8dcSSimon Schubert more memory. This can be set to a user defined function. The 2405796c8dcSSimon Schubert default action is to print a message and abort. */ 2415796c8dcSSimon Schubert extern void (*obstack_alloc_failed_handler) (void); 2425796c8dcSSimon Schubert 2435796c8dcSSimon Schubert /* Exit value used when `print_and_abort' is used. */ 2445796c8dcSSimon Schubert extern int obstack_exit_failure; 2455796c8dcSSimon Schubert 2465796c8dcSSimon Schubert /* Pointer to beginning of object being allocated or to be allocated next. 2475796c8dcSSimon Schubert Note that this might not be the final address of the object 2485796c8dcSSimon Schubert because a new chunk might be needed to hold the final size. */ 2495796c8dcSSimon Schubert 2505796c8dcSSimon Schubert #define obstack_base(h) ((h)->object_base) 2515796c8dcSSimon Schubert 2525796c8dcSSimon Schubert /* Size for allocating ordinary chunks. */ 2535796c8dcSSimon Schubert 2545796c8dcSSimon Schubert #define obstack_chunk_size(h) ((h)->chunk_size) 2555796c8dcSSimon Schubert 2565796c8dcSSimon Schubert /* Pointer to next byte not yet allocated in current chunk. */ 2575796c8dcSSimon Schubert 2585796c8dcSSimon Schubert #define obstack_next_free(h) ((h)->next_free) 2595796c8dcSSimon Schubert 2605796c8dcSSimon Schubert /* Mask specifying low bits that should be clear in address of an object. */ 2615796c8dcSSimon Schubert 2625796c8dcSSimon Schubert #define obstack_alignment_mask(h) ((h)->alignment_mask) 2635796c8dcSSimon Schubert 2645796c8dcSSimon Schubert /* To prevent prototype warnings provide complete argument list in 2655796c8dcSSimon Schubert standard C version. */ 2665796c8dcSSimon Schubert # define obstack_init(h) \ 2675796c8dcSSimon Schubert _obstack_begin ((h), 0, 0, \ 2685796c8dcSSimon Schubert (void *(*) (long)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free) 2695796c8dcSSimon Schubert 2705796c8dcSSimon Schubert # define obstack_begin(h, size) \ 2715796c8dcSSimon Schubert _obstack_begin ((h), (size), 0, \ 2725796c8dcSSimon Schubert (void *(*) (long)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free) 2735796c8dcSSimon Schubert 2745796c8dcSSimon Schubert # define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \ 2755796c8dcSSimon Schubert _obstack_begin ((h), (size), (alignment), \ 2765796c8dcSSimon Schubert (void *(*) (long)) (chunkfun), (void (*) (void *)) (freefun)) 2775796c8dcSSimon Schubert 2785796c8dcSSimon Schubert # define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \ 2795796c8dcSSimon Schubert _obstack_begin_1 ((h), (size), (alignment), \ 2805796c8dcSSimon Schubert (void *(*) (void *, long)) (chunkfun), \ 2815796c8dcSSimon Schubert (void (*) (void *, void *)) (freefun), (arg)) 2825796c8dcSSimon Schubert 2835796c8dcSSimon Schubert # define obstack_chunkfun(h, newchunkfun) \ 2845796c8dcSSimon Schubert ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun)) 2855796c8dcSSimon Schubert 2865796c8dcSSimon Schubert # define obstack_freefun(h, newfreefun) \ 2875796c8dcSSimon Schubert ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun)) 2885796c8dcSSimon Schubert 2895796c8dcSSimon Schubert #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar)) 2905796c8dcSSimon Schubert 2915796c8dcSSimon Schubert #define obstack_blank_fast(h,n) ((h)->next_free += (n)) 2925796c8dcSSimon Schubert 2935796c8dcSSimon Schubert #define obstack_memory_used(h) _obstack_memory_used (h) 2945796c8dcSSimon Schubert 2955796c8dcSSimon Schubert #if defined __GNUC__ && defined __STDC__ && __STDC__ 2965796c8dcSSimon Schubert /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and 2975796c8dcSSimon Schubert does not implement __extension__. But that compiler doesn't define 2985796c8dcSSimon Schubert __GNUC_MINOR__. */ 2995796c8dcSSimon Schubert # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__) 3005796c8dcSSimon Schubert # define __extension__ 3015796c8dcSSimon Schubert # endif 3025796c8dcSSimon Schubert 3035796c8dcSSimon Schubert /* For GNU C, if not -traditional, 3045796c8dcSSimon Schubert we can define these macros to compute all args only once 3055796c8dcSSimon Schubert without using a global variable. 3065796c8dcSSimon Schubert Also, we can avoid using the `temp' slot, to make faster code. */ 3075796c8dcSSimon Schubert 3085796c8dcSSimon Schubert # define obstack_object_size(OBSTACK) \ 3095796c8dcSSimon Schubert __extension__ \ 3105796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3115796c8dcSSimon Schubert (unsigned) (__o->next_free - __o->object_base); }) 3125796c8dcSSimon Schubert 3135796c8dcSSimon Schubert # define obstack_room(OBSTACK) \ 3145796c8dcSSimon Schubert __extension__ \ 3155796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3165796c8dcSSimon Schubert (unsigned) (__o->chunk_limit - __o->next_free); }) 3175796c8dcSSimon Schubert 3185796c8dcSSimon Schubert # define obstack_make_room(OBSTACK,length) \ 3195796c8dcSSimon Schubert __extension__ \ 3205796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3215796c8dcSSimon Schubert int __len = (length); \ 3225796c8dcSSimon Schubert if (__o->chunk_limit - __o->next_free < __len) \ 3235796c8dcSSimon Schubert _obstack_newchunk (__o, __len); \ 3245796c8dcSSimon Schubert (void) 0; }) 3255796c8dcSSimon Schubert 3265796c8dcSSimon Schubert # define obstack_empty_p(OBSTACK) \ 3275796c8dcSSimon Schubert __extension__ \ 3285796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3295796c8dcSSimon Schubert (__o->chunk->prev == 0 && __o->next_free - __o->chunk->contents == 0); }) 3305796c8dcSSimon Schubert 3315796c8dcSSimon Schubert # define obstack_grow(OBSTACK,where,length) \ 3325796c8dcSSimon Schubert __extension__ \ 3335796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3345796c8dcSSimon Schubert int __len = (length); \ 3355796c8dcSSimon Schubert if (__o->next_free + __len > __o->chunk_limit) \ 3365796c8dcSSimon Schubert _obstack_newchunk (__o, __len); \ 3375796c8dcSSimon Schubert _obstack_memcpy (__o->next_free, (where), __len); \ 3385796c8dcSSimon Schubert __o->next_free += __len; \ 3395796c8dcSSimon Schubert (void) 0; }) 3405796c8dcSSimon Schubert 3415796c8dcSSimon Schubert # define obstack_grow0(OBSTACK,where,length) \ 3425796c8dcSSimon Schubert __extension__ \ 3435796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3445796c8dcSSimon Schubert int __len = (length); \ 3455796c8dcSSimon Schubert if (__o->next_free + __len + 1 > __o->chunk_limit) \ 3465796c8dcSSimon Schubert _obstack_newchunk (__o, __len + 1); \ 3475796c8dcSSimon Schubert _obstack_memcpy (__o->next_free, (where), __len); \ 3485796c8dcSSimon Schubert __o->next_free += __len; \ 3495796c8dcSSimon Schubert *(__o->next_free)++ = 0; \ 3505796c8dcSSimon Schubert (void) 0; }) 3515796c8dcSSimon Schubert 3525796c8dcSSimon Schubert # define obstack_1grow(OBSTACK,datum) \ 3535796c8dcSSimon Schubert __extension__ \ 3545796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3555796c8dcSSimon Schubert if (__o->next_free + 1 > __o->chunk_limit) \ 3565796c8dcSSimon Schubert _obstack_newchunk (__o, 1); \ 3575796c8dcSSimon Schubert obstack_1grow_fast (__o, datum); \ 3585796c8dcSSimon Schubert (void) 0; }) 3595796c8dcSSimon Schubert 3605796c8dcSSimon Schubert /* These assume that the obstack alignment is good enough for pointers or ints, 3615796c8dcSSimon Schubert and that the data added so far to the current object 3625796c8dcSSimon Schubert shares that much alignment. */ 3635796c8dcSSimon Schubert 3645796c8dcSSimon Schubert # define obstack_ptr_grow(OBSTACK,datum) \ 3655796c8dcSSimon Schubert __extension__ \ 3665796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3675796c8dcSSimon Schubert if (__o->next_free + sizeof (void *) > __o->chunk_limit) \ 3685796c8dcSSimon Schubert _obstack_newchunk (__o, sizeof (void *)); \ 3695796c8dcSSimon Schubert obstack_ptr_grow_fast (__o, datum); }) 3705796c8dcSSimon Schubert 3715796c8dcSSimon Schubert # define obstack_int_grow(OBSTACK,datum) \ 3725796c8dcSSimon Schubert __extension__ \ 3735796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3745796c8dcSSimon Schubert if (__o->next_free + sizeof (int) > __o->chunk_limit) \ 3755796c8dcSSimon Schubert _obstack_newchunk (__o, sizeof (int)); \ 3765796c8dcSSimon Schubert obstack_int_grow_fast (__o, datum); }) 3775796c8dcSSimon Schubert 3785796c8dcSSimon Schubert # define obstack_ptr_grow_fast(OBSTACK,aptr) \ 3795796c8dcSSimon Schubert __extension__ \ 3805796c8dcSSimon Schubert ({ struct obstack *__o1 = (OBSTACK); \ 3815796c8dcSSimon Schubert *(const void **) __o1->next_free = (aptr); \ 3825796c8dcSSimon Schubert __o1->next_free += sizeof (const void *); \ 3835796c8dcSSimon Schubert (void) 0; }) 3845796c8dcSSimon Schubert 3855796c8dcSSimon Schubert # define obstack_int_grow_fast(OBSTACK,aint) \ 3865796c8dcSSimon Schubert __extension__ \ 3875796c8dcSSimon Schubert ({ struct obstack *__o1 = (OBSTACK); \ 3885796c8dcSSimon Schubert *(int *) __o1->next_free = (aint); \ 3895796c8dcSSimon Schubert __o1->next_free += sizeof (int); \ 3905796c8dcSSimon Schubert (void) 0; }) 3915796c8dcSSimon Schubert 3925796c8dcSSimon Schubert # define obstack_blank(OBSTACK,length) \ 3935796c8dcSSimon Schubert __extension__ \ 3945796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 3955796c8dcSSimon Schubert int __len = (length); \ 3965796c8dcSSimon Schubert if (__o->chunk_limit - __o->next_free < __len) \ 3975796c8dcSSimon Schubert _obstack_newchunk (__o, __len); \ 3985796c8dcSSimon Schubert obstack_blank_fast (__o, __len); \ 3995796c8dcSSimon Schubert (void) 0; }) 4005796c8dcSSimon Schubert 4015796c8dcSSimon Schubert # define obstack_alloc(OBSTACK,length) \ 4025796c8dcSSimon Schubert __extension__ \ 4035796c8dcSSimon Schubert ({ struct obstack *__h = (OBSTACK); \ 4045796c8dcSSimon Schubert obstack_blank (__h, (length)); \ 4055796c8dcSSimon Schubert obstack_finish (__h); }) 4065796c8dcSSimon Schubert 4075796c8dcSSimon Schubert # define obstack_copy(OBSTACK,where,length) \ 4085796c8dcSSimon Schubert __extension__ \ 4095796c8dcSSimon Schubert ({ struct obstack *__h = (OBSTACK); \ 4105796c8dcSSimon Schubert obstack_grow (__h, (where), (length)); \ 4115796c8dcSSimon Schubert obstack_finish (__h); }) 4125796c8dcSSimon Schubert 4135796c8dcSSimon Schubert # define obstack_copy0(OBSTACK,where,length) \ 4145796c8dcSSimon Schubert __extension__ \ 4155796c8dcSSimon Schubert ({ struct obstack *__h = (OBSTACK); \ 4165796c8dcSSimon Schubert obstack_grow0 (__h, (where), (length)); \ 4175796c8dcSSimon Schubert obstack_finish (__h); }) 4185796c8dcSSimon Schubert 4195796c8dcSSimon Schubert /* The local variable is named __o1 to avoid a name conflict 4205796c8dcSSimon Schubert when obstack_blank is called. */ 4215796c8dcSSimon Schubert # define obstack_finish(OBSTACK) \ 4225796c8dcSSimon Schubert __extension__ \ 4235796c8dcSSimon Schubert ({ struct obstack *__o1 = (OBSTACK); \ 4245796c8dcSSimon Schubert void *value; \ 4255796c8dcSSimon Schubert value = (void *) __o1->object_base; \ 4265796c8dcSSimon Schubert if (__o1->next_free == value) \ 4275796c8dcSSimon Schubert __o1->maybe_empty_object = 1; \ 4285796c8dcSSimon Schubert __o1->next_free \ 4295796c8dcSSimon Schubert = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)\ 4305796c8dcSSimon Schubert & ~ (__o1->alignment_mask)); \ 4315796c8dcSSimon Schubert if (__o1->next_free - (char *)__o1->chunk \ 4325796c8dcSSimon Schubert > __o1->chunk_limit - (char *)__o1->chunk) \ 4335796c8dcSSimon Schubert __o1->next_free = __o1->chunk_limit; \ 4345796c8dcSSimon Schubert __o1->object_base = __o1->next_free; \ 4355796c8dcSSimon Schubert value; }) 4365796c8dcSSimon Schubert 4375796c8dcSSimon Schubert # define obstack_free(OBSTACK, OBJ) \ 4385796c8dcSSimon Schubert __extension__ \ 4395796c8dcSSimon Schubert ({ struct obstack *__o = (OBSTACK); \ 4405796c8dcSSimon Schubert void *__obj = (void *) (OBJ); \ 4415796c8dcSSimon Schubert if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \ 4425796c8dcSSimon Schubert __o->next_free = __o->object_base = (char *) __obj; \ 4435796c8dcSSimon Schubert else (obstack_free) (__o, __obj); }) 4445796c8dcSSimon Schubert 4455796c8dcSSimon Schubert #else /* not __GNUC__ or not __STDC__ */ 4465796c8dcSSimon Schubert 4475796c8dcSSimon Schubert # define obstack_object_size(h) \ 4485796c8dcSSimon Schubert (unsigned) ((h)->next_free - (h)->object_base) 4495796c8dcSSimon Schubert 4505796c8dcSSimon Schubert # define obstack_room(h) \ 4515796c8dcSSimon Schubert (unsigned) ((h)->chunk_limit - (h)->next_free) 4525796c8dcSSimon Schubert 4535796c8dcSSimon Schubert # define obstack_empty_p(h) \ 4545796c8dcSSimon Schubert ((h)->chunk->prev == 0 && (h)->next_free - (h)->chunk->contents == 0) 4555796c8dcSSimon Schubert 4565796c8dcSSimon Schubert /* Note that the call to _obstack_newchunk is enclosed in (..., 0) 4575796c8dcSSimon Schubert so that we can avoid having void expressions 4585796c8dcSSimon Schubert in the arms of the conditional expression. 4595796c8dcSSimon Schubert Casting the third operand to void was tried before, 4605796c8dcSSimon Schubert but some compilers won't accept it. */ 4615796c8dcSSimon Schubert 4625796c8dcSSimon Schubert # define obstack_make_room(h,length) \ 4635796c8dcSSimon Schubert ( (h)->temp = (length), \ 4645796c8dcSSimon Schubert (((h)->next_free + (h)->temp > (h)->chunk_limit) \ 4655796c8dcSSimon Schubert ? (_obstack_newchunk ((h), (h)->temp), 0) : 0)) 4665796c8dcSSimon Schubert 4675796c8dcSSimon Schubert # define obstack_grow(h,where,length) \ 4685796c8dcSSimon Schubert ( (h)->temp = (length), \ 4695796c8dcSSimon Schubert (((h)->next_free + (h)->temp > (h)->chunk_limit) \ 4705796c8dcSSimon Schubert ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \ 4715796c8dcSSimon Schubert _obstack_memcpy ((h)->next_free, (where), (h)->temp), \ 4725796c8dcSSimon Schubert (h)->next_free += (h)->temp) 4735796c8dcSSimon Schubert 4745796c8dcSSimon Schubert # define obstack_grow0(h,where,length) \ 4755796c8dcSSimon Schubert ( (h)->temp = (length), \ 4765796c8dcSSimon Schubert (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \ 4775796c8dcSSimon Schubert ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0), \ 4785796c8dcSSimon Schubert _obstack_memcpy ((h)->next_free, (where), (h)->temp), \ 4795796c8dcSSimon Schubert (h)->next_free += (h)->temp, \ 4805796c8dcSSimon Schubert *((h)->next_free)++ = 0) 4815796c8dcSSimon Schubert 4825796c8dcSSimon Schubert # define obstack_1grow(h,datum) \ 4835796c8dcSSimon Schubert ( (((h)->next_free + 1 > (h)->chunk_limit) \ 4845796c8dcSSimon Schubert ? (_obstack_newchunk ((h), 1), 0) : 0), \ 4855796c8dcSSimon Schubert obstack_1grow_fast (h, datum)) 4865796c8dcSSimon Schubert 4875796c8dcSSimon Schubert # define obstack_ptr_grow(h,datum) \ 4885796c8dcSSimon Schubert ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \ 4895796c8dcSSimon Schubert ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \ 4905796c8dcSSimon Schubert obstack_ptr_grow_fast (h, datum)) 4915796c8dcSSimon Schubert 4925796c8dcSSimon Schubert # define obstack_int_grow(h,datum) \ 4935796c8dcSSimon Schubert ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \ 4945796c8dcSSimon Schubert ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \ 4955796c8dcSSimon Schubert obstack_int_grow_fast (h, datum)) 4965796c8dcSSimon Schubert 4975796c8dcSSimon Schubert # define obstack_ptr_grow_fast(h,aptr) \ 4985796c8dcSSimon Schubert (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr)) 4995796c8dcSSimon Schubert 5005796c8dcSSimon Schubert # define obstack_int_grow_fast(h,aint) \ 5015796c8dcSSimon Schubert (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr)) 5025796c8dcSSimon Schubert 5035796c8dcSSimon Schubert # define obstack_blank(h,length) \ 5045796c8dcSSimon Schubert ( (h)->temp = (length), \ 5055796c8dcSSimon Schubert (((h)->chunk_limit - (h)->next_free < (h)->temp) \ 5065796c8dcSSimon Schubert ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \ 5075796c8dcSSimon Schubert obstack_blank_fast (h, (h)->temp)) 5085796c8dcSSimon Schubert 5095796c8dcSSimon Schubert # define obstack_alloc(h,length) \ 5105796c8dcSSimon Schubert (obstack_blank ((h), (length)), obstack_finish ((h))) 5115796c8dcSSimon Schubert 5125796c8dcSSimon Schubert # define obstack_copy(h,where,length) \ 5135796c8dcSSimon Schubert (obstack_grow ((h), (where), (length)), obstack_finish ((h))) 5145796c8dcSSimon Schubert 5155796c8dcSSimon Schubert # define obstack_copy0(h,where,length) \ 5165796c8dcSSimon Schubert (obstack_grow0 ((h), (where), (length)), obstack_finish ((h))) 5175796c8dcSSimon Schubert 5185796c8dcSSimon Schubert # define obstack_finish(h) \ 5195796c8dcSSimon Schubert ( ((h)->next_free == (h)->object_base \ 5205796c8dcSSimon Schubert ? (((h)->maybe_empty_object = 1), 0) \ 5215796c8dcSSimon Schubert : 0), \ 5225796c8dcSSimon Schubert (h)->temp = __PTR_TO_INT ((h)->object_base), \ 5235796c8dcSSimon Schubert (h)->next_free \ 5245796c8dcSSimon Schubert = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \ 5255796c8dcSSimon Schubert & ~ ((h)->alignment_mask)), \ 5265796c8dcSSimon Schubert (((h)->next_free - (char *) (h)->chunk \ 5275796c8dcSSimon Schubert > (h)->chunk_limit - (char *) (h)->chunk) \ 5285796c8dcSSimon Schubert ? ((h)->next_free = (h)->chunk_limit) : 0), \ 5295796c8dcSSimon Schubert (h)->object_base = (h)->next_free, \ 5305796c8dcSSimon Schubert (void *) __INT_TO_PTR ((h)->temp)) 5315796c8dcSSimon Schubert 5325796c8dcSSimon Schubert # define obstack_free(h,obj) \ 5335796c8dcSSimon Schubert ( (h)->temp = (char *) (obj) - (char *) (h)->chunk, \ 5345796c8dcSSimon Schubert (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\ 535*a45ae5f8SJohn Marino ? (((h)->next_free = (h)->object_base \ 536*a45ae5f8SJohn Marino = (h)->temp + (char *) (h)->chunk), 0) \ 537*a45ae5f8SJohn Marino : ((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0))) 5385796c8dcSSimon Schubert 5395796c8dcSSimon Schubert #endif /* not __GNUC__ or not __STDC__ */ 5405796c8dcSSimon Schubert 5415796c8dcSSimon Schubert #ifdef __cplusplus 5425796c8dcSSimon Schubert } /* C++ */ 5435796c8dcSSimon Schubert #endif 5445796c8dcSSimon Schubert 5455796c8dcSSimon Schubert #endif /* obstack.h */ 546