1 /* obstack.h - object stack macros
2 Copyright (C) 1988-1994,1996-1999,2003,2004 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
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.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
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.
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 All the apparent functions defined here are macros. The idea
27 is that you would use these pre-tested macros to solve a
28 very specific set of problems, and they would run fast.
29 Caution: no side-effects in arguments please!! They may be
30 evaluated MANY times!!
32 These macros operate a stack of objects. Each object starts life
33 small, and may grow to maturity. (Consider building a word syllable
34 by syllable.) An object can move while it is growing. Once it has
35 been "finished" it never changes address again. So the "top of the
36 stack" is typically an immature growing object, while the rest of the
37 stack is of mature, fixed size and fixed address objects.
39 These routines grab large chunks of memory, using a function you
40 supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
41 by calling `obstack_chunk_free'. You must define them and declare
42 them before using any obstack macros.
44 Each independent stack is represented by a `struct obstack'.
45 Each of the obstack macros expects a pointer to such a structure
46 as the first argument.
48 One motivation for this package is the problem of growing char strings
49 in symbol tables. Unless you are "fascist pig with a read-only mind"
50 --Gosper's immortal quote from HAKMEM item 154, out of context--you
51 would not like to put any arbitrary upper limit on the length of your
54 In practice this often means you will build many short symbols and a
55 few long symbols. At the time you are reading a symbol you don't know
56 how long it is. One traditional method is to read a symbol into a
57 buffer, realloc()ating the buffer every time you try to read a symbol
58 that is longer than the buffer. This is beaut, but you still will
59 want to copy the symbol from the buffer to a more permanent
60 symbol-table entry say about half the time.
62 With obstacks, you can work differently. Use one obstack for all symbol
63 names. As you read a symbol, grow the name in the obstack gradually.
64 When the name is complete, finalize it. Then, if the symbol exists already,
65 free the newly read name.
67 The way we do this is to take a large chunk, allocating memory from
68 low addresses. When you want to build a symbol in the chunk you just
69 add chars above the current "high water mark" in the chunk. When you
70 have finished adding chars, because you got to the end of the symbol,
71 you know how long the chars are, and you can create a new object.
72 Mostly the chars will not burst over the highest address of the chunk,
73 because you would typically expect a chunk to be (say) 100 times as
74 long as an average object.
76 In case that isn't clear, when we have enough chars to make up
77 the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
78 so we just point to it where it lies. No moving of chars is
79 needed and this is the second win: potentially long strings need
80 never be explicitly shuffled. Once an object is formed, it does not
81 change its address during its lifetime.
83 When the chars burst over a chunk boundary, we allocate a larger
84 chunk, and then copy the partly formed object from the end of the old
85 chunk to the beginning of the new larger chunk. We then carry on
86 accreting characters to the end of the object as we normally would.
88 A special macro is provided to add a single char at a time to a
89 growing object. This allows the use of register variables, which
90 break the ordinary 'growth' macro.
93 We allocate large chunks.
94 We carve out one object at a time from the current chunk.
95 Once carved, an object never moves.
96 We are free to append data of any size to the currently
98 Exactly one object is growing in an obstack at any one time.
99 You can run one obstack per control block.
100 You may have as many control blocks as you dare.
101 Because of the way we do it, you can `unwind' an obstack
102 back to a previous state. (You may remove objects much
103 as you would with a stack.)
107 /* Don't do the contents of this file more than once. */
116 /* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is
117 defined, as with GNU C, use that; that way we don't pollute the
118 namespace with <stddef.h>'s symbols. Otherwise, include <stddef.h>
119 and use ptrdiff_t. */
121 #ifdef __PTRDIFF_TYPE__
122 # define PTR_INT_TYPE __PTRDIFF_TYPE__
125 # define PTR_INT_TYPE ptrdiff_t
128 /* If B is the base of an object addressed by P, return the result of
129 aligning P to the next multiple of A + 1. B and P must be of type
130 char *. A + 1 must be a power of 2. */
132 #define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
134 /* Similiar to _BPTR_ALIGN (B, P, A), except optimize the common case
135 where pointers can be converted to integers, aligned as integers,
136 and converted back again. If PTR_INT_TYPE is narrower than a
137 pointer (e.g., the AS/400), play it safe and compute the alignment
138 relative to B. Otherwise, use the faster strategy of computing the
139 alignment relative to 0. */
141 #define __PTR_ALIGN(B, P, A) \
142 __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
147 struct _obstack_chunk /* Lives at front of each chunk. */
149 char *limit; /* 1 past end of this chunk */
150 struct _obstack_chunk *prev; /* address of prior chunk or NULL */
151 char contents[4]; /* objects begin here */
154 struct obstack /* control current object in current chunk */
156 long chunk_size; /* preferred size to allocate chunks in */
157 struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
158 char *object_base; /* address of object we are building */
159 char *next_free; /* where to add next char to current object */
160 char *chunk_limit; /* address of char after current chunk */
163 PTR_INT_TYPE tempint;
165 } temp; /* Temporary for some macros. */
166 int alignment_mask; /* Mask of alignment for each object. */
167 /* These prototypes vary based on `use_extra_arg', and we use
168 casts to the prototypeless function type in all assignments,
169 but having prototypes here quiets -Wstrict-prototypes. */
170 struct _obstack_chunk *(*chunkfun) (void *, long);
171 void (*freefun) (void *, struct _obstack_chunk *);
172 void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
173 unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
174 unsigned maybe_empty_object:1;/* There is a possibility that the current
175 chunk contains a zero-length object. This
176 prevents freeing the chunk if we allocate
177 a bigger chunk to replace it. */
178 unsigned alloc_failed:1; /* No longer used, as we now call the failed
179 handler on error, but retained for binary
183 /* Declare the external functions we use; they are in obstack.c. */
185 extern void _obstack_newchunk (struct obstack *, int);
186 extern void _obstack_free (struct obstack *, void *);
187 extern int _obstack_begin (struct obstack *, int, int,
188 void *(*) (long), void (*) (void *));
189 extern int _obstack_begin_1 (struct obstack *, int, int,
190 void *(*) (void *, long),
191 void (*) (void *, void *), void *);
192 extern int _obstack_memory_used (struct obstack *);
194 /* Do the function-declarations after the structs
195 but before defining the macros. */
197 void obstack_init (struct obstack *obstack);
199 void * obstack_alloc (struct obstack *obstack, int size);
201 void * obstack_copy (struct obstack *obstack, const void *address, int size);
202 void * obstack_copy0 (struct obstack *obstack, const void *address, int size);
204 void obstack_free (struct obstack *obstack, void *block);
206 void obstack_blank (struct obstack *obstack, int size);
208 void obstack_grow (struct obstack *obstack, const void *data, int size);
209 void obstack_grow0 (struct obstack *obstack, const void *data, int size);
211 void obstack_1grow (struct obstack *obstack, int data_char);
212 void obstack_ptr_grow (struct obstack *obstack, const void *data);
213 void obstack_int_grow (struct obstack *obstack, int data);
215 void * obstack_finish (struct obstack *obstack);
217 int obstack_object_size (struct obstack *obstack);
219 int obstack_room (struct obstack *obstack);
220 void obstack_make_room (struct obstack *obstack, int size);
221 void obstack_1grow_fast (struct obstack *obstack, int data_char);
222 void obstack_ptr_grow_fast (struct obstack *obstack, const void *data);
223 void obstack_int_grow_fast (struct obstack *obstack, int data);
224 void obstack_blank_fast (struct obstack *obstack, int size);
226 void * obstack_base (struct obstack *obstack);
227 void * obstack_next_free (struct obstack *obstack);
228 int obstack_alignment_mask (struct obstack *obstack);
229 int obstack_chunk_size (struct obstack *obstack);
230 int obstack_memory_used (struct obstack *obstack);
232 /* Error handler called when `obstack_chunk_alloc' failed to allocate
233 more memory. This can be set to a user defined function which
234 should either abort gracefully or use longjump - but shouldn't
235 return. The default action is to print a message and abort. */
236 extern void (*obstack_alloc_failed_handler) (void);
238 /* Exit value used when `print_and_abort' is used. */
239 extern int obstack_exit_failure;
241 /* Pointer to beginning of object being allocated or to be allocated next.
242 Note that this might not be the final address of the object
243 because a new chunk might be needed to hold the final size. */
245 #define obstack_base(h) ((h)->object_base)
247 /* Size for allocating ordinary chunks. */
249 #define obstack_chunk_size(h) ((h)->chunk_size)
251 /* Pointer to next byte not yet allocated in current chunk. */
253 #define obstack_next_free(h) ((h)->next_free)
255 /* Mask specifying low bits that should be clear in address of an object. */
257 #define obstack_alignment_mask(h) ((h)->alignment_mask)
259 /* To prevent prototype warnings provide complete argument list. */
260 #define obstack_init(h) \
261 _obstack_begin ((h), 0, 0, \
262 (void *(*) (long)) obstack_chunk_alloc, \
263 (void (*) (void *)) obstack_chunk_free)
265 #define obstack_begin(h, size) \
266 _obstack_begin ((h), (size), 0, \
267 (void *(*) (long)) obstack_chunk_alloc, \
268 (void (*) (void *)) obstack_chunk_free)
270 #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
271 _obstack_begin ((h), (size), (alignment), \
272 (void *(*) (long)) (chunkfun), \
273 (void (*) (void *)) (freefun))
275 #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
276 _obstack_begin_1 ((h), (size), (alignment), \
277 (void *(*) (void *, long)) (chunkfun), \
278 (void (*) (void *, void *)) (freefun), (arg))
280 #define obstack_chunkfun(h, newchunkfun) \
281 ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
283 #define obstack_freefun(h, newfreefun) \
284 ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
286 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
288 #define obstack_blank_fast(h,n) ((h)->next_free += (n))
290 #define obstack_memory_used(h) _obstack_memory_used (h)
292 #if defined __GNUC__ && defined __STDC__ && __STDC__
293 /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
294 does not implement __extension__. But that compiler doesn't define
296 # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
297 # define __extension__
300 /* For GNU C, if not -traditional,
301 we can define these macros to compute all args only once
302 without using a global variable.
303 Also, we can avoid using the `temp' slot, to make faster code. */
305 # define obstack_object_size(OBSTACK) \
307 ({ struct obstack const *__o = (OBSTACK); \
308 (unsigned) (__o->next_free - __o->object_base); })
310 # define obstack_room(OBSTACK) \
312 ({ struct obstack const *__o = (OBSTACK); \
313 (unsigned) (__o->chunk_limit - __o->next_free); })
315 # define obstack_make_room(OBSTACK,length) \
317 ({ struct obstack *__o = (OBSTACK); \
318 int __len = (length); \
319 if (__o->chunk_limit - __o->next_free < __len) \
320 _obstack_newchunk (__o, __len); \
323 # define obstack_empty_p(OBSTACK) \
325 ({ struct obstack const *__o = (OBSTACK); \
326 (__o->chunk->prev == 0 && __o->next_free - __o->chunk->contents == 0); })
328 # define obstack_grow(OBSTACK,where,length) \
330 ({ struct obstack *__o = (OBSTACK); \
331 int __len = (length); \
332 if (__o->next_free + __len > __o->chunk_limit) \
333 _obstack_newchunk (__o, __len); \
334 memcpy (__o->next_free, where, __len); \
335 __o->next_free += __len; \
338 # define obstack_grow0(OBSTACK,where,length) \
340 ({ struct obstack *__o = (OBSTACK); \
341 int __len = (length); \
342 if (__o->next_free + __len + 1 > __o->chunk_limit) \
343 _obstack_newchunk (__o, __len + 1); \
344 memcpy (__o->next_free, where, __len); \
345 __o->next_free += __len; \
346 *(__o->next_free)++ = 0; \
349 # define obstack_1grow(OBSTACK,datum) \
351 ({ struct obstack *__o = (OBSTACK); \
352 if (__o->next_free + 1 > __o->chunk_limit) \
353 _obstack_newchunk (__o, 1); \
354 obstack_1grow_fast (__o, datum); \
357 /* These assume that the obstack alignment is good enough for pointers
358 or ints, and that the data added so far to the current object
359 shares that much alignment. */
361 # define obstack_ptr_grow(OBSTACK,datum) \
363 ({ struct obstack *__o = (OBSTACK); \
364 if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
365 _obstack_newchunk (__o, sizeof (void *)); \
366 obstack_ptr_grow_fast (__o, datum); }) \
368 # define obstack_int_grow(OBSTACK,datum) \
370 ({ struct obstack *__o = (OBSTACK); \
371 if (__o->next_free + sizeof (int) > __o->chunk_limit) \
372 _obstack_newchunk (__o, sizeof (int)); \
373 obstack_int_grow_fast (__o, datum); })
375 # define obstack_ptr_grow_fast(OBSTACK,aptr) \
377 ({ struct obstack *__o1 = (OBSTACK); \
378 *(const void **) __o1->next_free = (aptr); \
379 __o1->next_free += sizeof (const void *); \
382 # define obstack_int_grow_fast(OBSTACK,aint) \
384 ({ struct obstack *__o1 = (OBSTACK); \
385 *(int *) __o1->next_free = (aint); \
386 __o1->next_free += sizeof (int); \
389 # define obstack_blank(OBSTACK,length) \
391 ({ struct obstack *__o = (OBSTACK); \
392 int __len = (length); \
393 if (__o->chunk_limit - __o->next_free < __len) \
394 _obstack_newchunk (__o, __len); \
395 obstack_blank_fast (__o, __len); \
398 # define obstack_alloc(OBSTACK,length) \
400 ({ struct obstack *__h = (OBSTACK); \
401 obstack_blank (__h, (length)); \
402 obstack_finish (__h); })
404 # define obstack_copy(OBSTACK,where,length) \
406 ({ struct obstack *__h = (OBSTACK); \
407 obstack_grow (__h, (where), (length)); \
408 obstack_finish (__h); })
410 # define obstack_copy0(OBSTACK,where,length) \
412 ({ struct obstack *__h = (OBSTACK); \
413 obstack_grow0 (__h, (where), (length)); \
414 obstack_finish (__h); })
416 /* The local variable is named __o1 to avoid a name conflict
417 when obstack_blank is called. */
418 # define obstack_finish(OBSTACK) \
420 ({ struct obstack *__o1 = (OBSTACK); \
421 void *__value = (void *) __o1->object_base; \
422 if (__o1->next_free == __value) \
423 __o1->maybe_empty_object = 1; \
425 = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
426 __o1->alignment_mask); \
427 if (__o1->next_free - (char *)__o1->chunk \
428 > __o1->chunk_limit - (char *)__o1->chunk) \
429 __o1->next_free = __o1->chunk_limit; \
430 __o1->object_base = __o1->next_free; \
433 # define obstack_free(OBSTACK, OBJ) \
435 ({ struct obstack *__o = (OBSTACK); \
436 void *__obj = (OBJ); \
437 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
438 __o->next_free = __o->object_base = (char *)__obj; \
439 else (obstack_free) (__o, __obj); })
441 #else /* not __GNUC__ or not __STDC__ */
443 # define obstack_object_size(h) \
444 (unsigned) ((h)->next_free - (h)->object_base)
446 # define obstack_room(h) \
447 (unsigned) ((h)->chunk_limit - (h)->next_free)
449 # define obstack_empty_p(h) \
450 ((h)->chunk->prev == 0 && (h)->next_free - (h)->chunk->contents == 0)
452 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
453 so that we can avoid having void expressions
454 in the arms of the conditional expression.
455 Casting the third operand to void was tried before,
456 but some compilers won't accept it. */
458 # define obstack_make_room(h,length) \
459 ( (h)->temp.tempint = (length), \
460 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
461 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
463 # define obstack_grow(h,where,length) \
464 ( (h)->temp.tempint = (length), \
465 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
466 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
467 memcpy ((h)->next_free, where, (h)->temp.tempint), \
468 (h)->next_free += (h)->temp.tempint)
470 # define obstack_grow0(h,where,length) \
471 ( (h)->temp.tempint = (length), \
472 (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \
473 ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \
474 memcpy ((h)->next_free, where, (h)->temp.tempint), \
475 (h)->next_free += (h)->temp.tempint, \
476 *((h)->next_free)++ = 0)
478 # define obstack_1grow(h,datum) \
479 ( (((h)->next_free + 1 > (h)->chunk_limit) \
480 ? (_obstack_newchunk ((h), 1), 0) : 0), \
481 obstack_1grow_fast (h, datum))
483 # define obstack_ptr_grow(h,datum) \
484 ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
485 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
486 obstack_ptr_grow_fast (h, datum))
488 # define obstack_int_grow(h,datum) \
489 ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
490 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
491 obstack_int_grow_fast (h, datum))
493 # define obstack_ptr_grow_fast(h,aptr) \
494 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
496 # define obstack_int_grow_fast(h,aint) \
497 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr))
499 # define obstack_blank(h,length) \
500 ( (h)->temp.tempint = (length), \
501 (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \
502 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
503 obstack_blank_fast (h, (h)->temp.tempint))
505 # define obstack_alloc(h,length) \
506 (obstack_blank ((h), (length)), obstack_finish ((h)))
508 # define obstack_copy(h,where,length) \
509 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
511 # define obstack_copy0(h,where,length) \
512 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
514 # define obstack_finish(h) \
515 ( ((h)->next_free == (h)->object_base \
516 ? (((h)->maybe_empty_object = 1), 0) \
518 (h)->temp.tempptr = (h)->object_base, \
520 = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
521 (h)->alignment_mask), \
522 (((h)->next_free - (char *) (h)->chunk \
523 > (h)->chunk_limit - (char *) (h)->chunk) \
524 ? ((h)->next_free = (h)->chunk_limit) : 0), \
525 (h)->object_base = (h)->next_free, \
528 # define obstack_free(h,obj) \
529 ( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \
530 ((((h)->temp.tempint > 0 \
531 && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \
532 ? (int) ((h)->next_free = (h)->object_base \
533 = (h)->temp.tempint + (char *) (h)->chunk) \
534 : (((obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0)))
536 #endif /* not __GNUC__ or not __STDC__ */
542 #endif /* obstack.h */