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