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