1 /* obstack.h - object stack macros
2 Copyright (C) 1988-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
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!!
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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
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.)
102 /* Don't do the contents of this file more than once. */
107 #ifndef _OBSTACK_INTERFACE_VERSION
108 # define _OBSTACK_INTERFACE_VERSION 2
111 #include <stddef.h> /* For size_t and ptrdiff_t. */
112 #include <string.h> /* For __GNU_LIBRARY__, and memcpy. */
114 #if _OBSTACK_INTERFACE_VERSION == 1
115 /* For binary compatibility with obstack version 1, which used "int"
116 and "long" for these two types. */
117 # define _OBSTACK_SIZE_T unsigned int
118 # define _CHUNK_SIZE_T unsigned long
119 # define _OBSTACK_CAST(type, expr) ((type) (expr))
121 /* Version 2 with sane types, especially for 64-bit hosts. */
122 # define _OBSTACK_SIZE_T size_t
123 # define _CHUNK_SIZE_T size_t
124 # define _OBSTACK_CAST(type, expr) (expr)
127 /* If B is the base of an object addressed by P, return the result of
128 aligning P to the next multiple of A + 1. B and P must be of type
129 char *. A + 1 must be a power of 2. */
131 #define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
133 /* Similar to __BPTR_ALIGN (B, P, A), except optimize the common case
134 where pointers can be converted to integers, aligned as integers,
135 and converted back again. If ptrdiff_t is narrower than a
136 pointer (e.g., the AS/400), play it safe and compute the alignment
137 relative to B. Otherwise, use the faster strategy of computing the
138 alignment relative to 0. */
140 #define __PTR_ALIGN(B, P, A) \
141 __BPTR_ALIGN (sizeof (ptrdiff_t) < sizeof (void *) ? (B) : (char *) 0, \
144 #ifndef __attribute_pure__
145 # define __attribute_pure__ _GL_ATTRIBUTE_PURE
148 /* Not the same as _Noreturn, since it also works with function pointers. */
149 #ifndef __attribute_noreturn__
150 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__) || 0x5110 <= __SUNPRO_C
151 # define __attribute_noreturn__ __attribute__ ((__noreturn__))
153 # define __attribute_noreturn__
161 struct _obstack_chunk /* Lives at front of each chunk. */
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 */
168 struct obstack /* control current object in current chunk */
170 _CHUNK_SIZE_T 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 */
179 } temp; /* Temporary for some macros. */
180 _OBSTACK_SIZE_T alignment_mask; /* Mask of alignment for each object. */
182 /* These prototypes vary based on 'use_extra_arg'. */
185 void *(*plain) (size_t);
186 void *(*extra) (void *, size_t);
190 void (*plain) (void *);
191 void (*extra) (void *, void *);
194 void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
195 unsigned use_extra_arg : 1; /* chunk alloc/dealloc funcs take extra arg */
196 unsigned maybe_empty_object : 1; /* There is a possibility that the current
197 chunk contains a zero-length object. This
198 prevents freeing the chunk if we allocate
199 a bigger chunk to replace it. */
200 unsigned alloc_failed : 1; /* No longer used, as we now call the failed
201 handler on error, but retained for binary
205 /* Declare the external functions we use; they are in obstack.c. */
207 extern void _obstack_newchunk (struct obstack *, _OBSTACK_SIZE_T);
208 extern void _obstack_free (struct obstack *, void *);
209 extern int _obstack_begin (struct obstack *,
210 _OBSTACK_SIZE_T, _OBSTACK_SIZE_T,
211 void *(*) (size_t), void (*) (void *));
212 extern int _obstack_begin_1 (struct obstack *,
213 _OBSTACK_SIZE_T, _OBSTACK_SIZE_T,
214 void *(*) (void *, size_t),
215 void (*) (void *, void *), void *);
216 extern _OBSTACK_SIZE_T _obstack_memory_used (struct obstack *)
220 /* Error handler called when 'obstack_chunk_alloc' failed to allocate
221 more memory. This can be set to a user defined function which
222 should either abort gracefully or use longjump - but shouldn't
223 return. The default action is to print a message and abort. */
224 extern __attribute_noreturn__ void (*obstack_alloc_failed_handler) (void);
226 /* Exit value used when 'print_and_abort' is used. */
227 extern int obstack_exit_failure;
229 /* Pointer to beginning of object being allocated or to be allocated next.
230 Note that this might not be the final address of the object
231 because a new chunk might be needed to hold the final size. */
233 #define obstack_base(h) ((void *) (h)->object_base)
235 /* Size for allocating ordinary chunks. */
237 #define obstack_chunk_size(h) ((h)->chunk_size)
239 /* Pointer to next byte not yet allocated in current chunk. */
241 #define obstack_next_free(h) ((void *) (h)->next_free)
243 /* Mask specifying low bits that should be clear in address of an object. */
245 #define obstack_alignment_mask(h) ((h)->alignment_mask)
247 /* To prevent prototype warnings provide complete argument list. */
248 #define obstack_init(h) \
249 _obstack_begin ((h), 0, 0, \
250 _OBSTACK_CAST (void *(*) (size_t), obstack_chunk_alloc), \
251 _OBSTACK_CAST (void (*) (void *), obstack_chunk_free))
253 #define obstack_begin(h, size) \
254 _obstack_begin ((h), (size), 0, \
255 _OBSTACK_CAST (void *(*) (size_t), obstack_chunk_alloc), \
256 _OBSTACK_CAST (void (*) (void *), obstack_chunk_free))
258 #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
259 _obstack_begin ((h), (size), (alignment), \
260 _OBSTACK_CAST (void *(*) (size_t), chunkfun), \
261 _OBSTACK_CAST (void (*) (void *), freefun))
263 #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
264 _obstack_begin_1 ((h), (size), (alignment), \
265 _OBSTACK_CAST (void *(*) (void *, size_t), chunkfun), \
266 _OBSTACK_CAST (void (*) (void *, void *), freefun), arg)
268 #define obstack_chunkfun(h, newchunkfun) \
269 ((void) ((h)->chunkfun.extra = (void *(*) (void *, size_t)) (newchunkfun)))
271 #define obstack_freefun(h, newfreefun) \
272 ((void) ((h)->freefun.extra = (void *(*) (void *, void *)) (newfreefun)))
274 #define obstack_1grow_fast(h, achar) ((void) (*((h)->next_free)++ = (achar)))
276 #define obstack_blank_fast(h, n) ((void) ((h)->next_free += (n)))
278 #define obstack_memory_used(h) _obstack_memory_used (h)
281 # if !defined __GNUC_MINOR__ || __GNUC__ * 1000 + __GNUC_MINOR__ < 2008
282 # define __extension__
285 /* For GNU C, if not -traditional,
286 we can define these macros to compute all args only once
287 without using a global variable.
288 Also, we can avoid using the 'temp' slot, to make faster code. */
290 # define obstack_object_size(OBSTACK) \
292 ({ struct obstack const *__o = (OBSTACK); \
293 (_OBSTACK_SIZE_T) (__o->next_free - __o->object_base); })
295 /* The local variable is named __o1 to avoid a shadowed variable
296 warning when invoked from other obstack macros. */
297 # define obstack_room(OBSTACK) \
299 ({ struct obstack const *__o1 = (OBSTACK); \
300 (_OBSTACK_SIZE_T) (__o1->chunk_limit - __o1->next_free); })
302 # define obstack_make_room(OBSTACK, length) \
304 ({ struct obstack *__o = (OBSTACK); \
305 _OBSTACK_SIZE_T __len = (length); \
306 if (obstack_room (__o) < __len) \
307 _obstack_newchunk (__o, __len); \
310 # define obstack_empty_p(OBSTACK) \
312 ({ struct obstack const *__o = (OBSTACK); \
313 (__o->chunk->prev == 0 \
314 && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
315 __o->chunk->contents, \
316 __o->alignment_mask)); })
318 # define obstack_grow(OBSTACK, where, length) \
320 ({ struct obstack *__o = (OBSTACK); \
321 _OBSTACK_SIZE_T __len = (length); \
322 if (obstack_room (__o) < __len) \
323 _obstack_newchunk (__o, __len); \
324 memcpy (__o->next_free, where, __len); \
325 __o->next_free += __len; \
328 # define obstack_grow0(OBSTACK, where, length) \
330 ({ struct obstack *__o = (OBSTACK); \
331 _OBSTACK_SIZE_T __len = (length); \
332 if (obstack_room (__o) < __len + 1) \
333 _obstack_newchunk (__o, __len + 1); \
334 memcpy (__o->next_free, where, __len); \
335 __o->next_free += __len; \
336 *(__o->next_free)++ = 0; \
339 # define obstack_1grow(OBSTACK, datum) \
341 ({ struct obstack *__o = (OBSTACK); \
342 if (obstack_room (__o) < 1) \
343 _obstack_newchunk (__o, 1); \
344 obstack_1grow_fast (__o, datum); })
346 /* These assume that the obstack alignment is good enough for pointers
347 or ints, and that the data added so far to the current object
348 shares that much alignment. */
350 # define obstack_ptr_grow(OBSTACK, datum) \
352 ({ struct obstack *__o = (OBSTACK); \
353 if (obstack_room (__o) < sizeof (void *)) \
354 _obstack_newchunk (__o, sizeof (void *)); \
355 obstack_ptr_grow_fast (__o, datum); })
357 # define obstack_int_grow(OBSTACK, datum) \
359 ({ struct obstack *__o = (OBSTACK); \
360 if (obstack_room (__o) < sizeof (int)) \
361 _obstack_newchunk (__o, sizeof (int)); \
362 obstack_int_grow_fast (__o, datum); })
364 # define obstack_ptr_grow_fast(OBSTACK, aptr) \
366 ({ struct obstack *__o1 = (OBSTACK); \
367 void *__p1 = __o1->next_free; \
368 *(const void **) __p1 = (aptr); \
369 __o1->next_free += sizeof (const void *); \
372 # define obstack_int_grow_fast(OBSTACK, aint) \
374 ({ struct obstack *__o1 = (OBSTACK); \
375 void *__p1 = __o1->next_free; \
376 *(int *) __p1 = (aint); \
377 __o1->next_free += sizeof (int); \
380 # define obstack_blank(OBSTACK, length) \
382 ({ struct obstack *__o = (OBSTACK); \
383 _OBSTACK_SIZE_T __len = (length); \
384 if (obstack_room (__o) < __len) \
385 _obstack_newchunk (__o, __len); \
386 obstack_blank_fast (__o, __len); })
388 # define obstack_alloc(OBSTACK, length) \
390 ({ struct obstack *__h = (OBSTACK); \
391 obstack_blank (__h, (length)); \
392 obstack_finish (__h); })
394 # define obstack_copy(OBSTACK, where, length) \
396 ({ struct obstack *__h = (OBSTACK); \
397 obstack_grow (__h, (where), (length)); \
398 obstack_finish (__h); })
400 # define obstack_copy0(OBSTACK, where, length) \
402 ({ struct obstack *__h = (OBSTACK); \
403 obstack_grow0 (__h, (where), (length)); \
404 obstack_finish (__h); })
406 /* The local variable is named __o1 to avoid a shadowed variable
407 warning when invoked from other obstack macros, typically obstack_free. */
408 # define obstack_finish(OBSTACK) \
410 ({ struct obstack *__o1 = (OBSTACK); \
411 void *__value = (void *) __o1->object_base; \
412 if (__o1->next_free == __value) \
413 __o1->maybe_empty_object = 1; \
415 = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
416 __o1->alignment_mask); \
417 if ((size_t) (__o1->next_free - (char *) __o1->chunk) \
418 > (size_t) (__o1->chunk_limit - (char *) __o1->chunk)) \
419 __o1->next_free = __o1->chunk_limit; \
420 __o1->object_base = __o1->next_free; \
423 # define obstack_free(OBSTACK, OBJ) \
425 ({ struct obstack *__o = (OBSTACK); \
426 void *__obj = (void *) (OBJ); \
427 if (__obj > (void *) __o->chunk && __obj < (void *) __o->chunk_limit) \
428 __o->next_free = __o->object_base = (char *) __obj; \
430 _obstack_free (__o, __obj); })
432 #else /* not __GNUC__ */
434 # define obstack_object_size(h) \
435 ((_OBSTACK_SIZE_T) ((h)->next_free - (h)->object_base))
437 # define obstack_room(h) \
438 ((_OBSTACK_SIZE_T) ((h)->chunk_limit - (h)->next_free))
440 # define obstack_empty_p(h) \
441 ((h)->chunk->prev == 0 \
442 && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
443 (h)->chunk->contents, \
444 (h)->alignment_mask))
446 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
447 so that we can avoid having void expressions
448 in the arms of the conditional expression.
449 Casting the third operand to void was tried before,
450 but some compilers won't accept it. */
452 # define obstack_make_room(h, length) \
453 ((h)->temp.i = (length), \
454 ((obstack_room (h) < (h)->temp.i) \
455 ? (_obstack_newchunk (h, (h)->temp.i), 0) : 0), \
458 # define obstack_grow(h, where, length) \
459 ((h)->temp.i = (length), \
460 ((obstack_room (h) < (h)->temp.i) \
461 ? (_obstack_newchunk ((h), (h)->temp.i), 0) : 0), \
462 memcpy ((h)->next_free, where, (h)->temp.i), \
463 (h)->next_free += (h)->temp.i, \
466 # define obstack_grow0(h, where, length) \
467 ((h)->temp.i = (length), \
468 ((obstack_room (h) < (h)->temp.i + 1) \
469 ? (_obstack_newchunk ((h), (h)->temp.i + 1), 0) : 0), \
470 memcpy ((h)->next_free, where, (h)->temp.i), \
471 (h)->next_free += (h)->temp.i, \
472 *((h)->next_free)++ = 0, \
475 # define obstack_1grow(h, datum) \
476 (((obstack_room (h) < 1) \
477 ? (_obstack_newchunk ((h), 1), 0) : 0), \
478 obstack_1grow_fast (h, datum))
480 # define obstack_ptr_grow(h, datum) \
481 (((obstack_room (h) < sizeof (char *)) \
482 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
483 obstack_ptr_grow_fast (h, datum))
485 # define obstack_int_grow(h, datum) \
486 (((obstack_room (h) < sizeof (int)) \
487 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
488 obstack_int_grow_fast (h, datum))
490 # define obstack_ptr_grow_fast(h, aptr) \
491 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr), \
494 # define obstack_int_grow_fast(h, aint) \
495 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint), \
498 # define obstack_blank(h, length) \
499 ((h)->temp.i = (length), \
500 ((obstack_room (h) < (h)->temp.i) \
501 ? (_obstack_newchunk ((h), (h)->temp.i), 0) : 0), \
502 obstack_blank_fast (h, (h)->temp.i))
504 # define obstack_alloc(h, length) \
505 (obstack_blank ((h), (length)), obstack_finish ((h)))
507 # define obstack_copy(h, where, length) \
508 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
510 # define obstack_copy0(h, where, length) \
511 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
513 # define obstack_finish(h) \
514 (((h)->next_free == (h)->object_base \
515 ? (((h)->maybe_empty_object = 1), 0) \
517 (h)->temp.p = (h)->object_base, \
519 = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
520 (h)->alignment_mask), \
521 (((size_t) ((h)->next_free - (char *) (h)->chunk) \
522 > (size_t) ((h)->chunk_limit - (char *) (h)->chunk)) \
523 ? ((h)->next_free = (h)->chunk_limit) : 0), \
524 (h)->object_base = (h)->next_free, \
527 # define obstack_free(h, obj) \
528 ((h)->temp.p = (void *) (obj), \
529 (((h)->temp.p > (void *) (h)->chunk \
530 && (h)->temp.p < (void *) (h)->chunk_limit) \
531 ? (void) ((h)->next_free = (h)->object_base = (char *) (h)->temp.p) \
532 : _obstack_free ((h), (h)->temp.p)))
534 #endif /* not __GNUC__ */
540 #endif /* _OBSTACK_H */