1 /* obstack.h - object stack macros
2 Copyright (C) 1988-1994,1996-1999,2003,2004,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
23 All the apparent functions defined here are macros. The idea
24 is that you would use these pre-tested macros to solve a
25 very specific set of problems, and they would run fast.
26 Caution: no side-effects in arguments please!! They may be
27 evaluated MANY times!!
29 These macros operate a stack of objects. Each object starts life
30 small, and may grow to maturity. (Consider building a word syllable
31 by syllable.) An object can move while it is growing. Once it has
32 been "finished" it never changes address again. So the "top of the
33 stack" is typically an immature growing object, while the rest of the
34 stack is of mature, fixed size and fixed address objects.
36 These routines grab large chunks of memory, using a function you
37 supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
38 by calling `obstack_chunk_free'. You must define them and declare
39 them before using any obstack macros.
41 Each independent stack is represented by a `struct obstack'.
42 Each of the obstack macros expects a pointer to such a structure
43 as the first argument.
45 One motivation for this package is the problem of growing char strings
46 in symbol tables. Unless you are "fascist pig with a read-only mind"
47 --Gosper's immortal quote from HAKMEM item 154, out of context--you
48 would not like to put any arbitrary upper limit on the length of your
51 In practice this often means you will build many short symbols and a
52 few long symbols. At the time you are reading a symbol you don't know
53 how long it is. One traditional method is to read a symbol into a
54 buffer, realloc()ating the buffer every time you try to read a symbol
55 that is longer than the buffer. This is beaut, but you still will
56 want to copy the symbol from the buffer to a more permanent
57 symbol-table entry say about half the time.
59 With obstacks, you can work differently. Use one obstack for all symbol
60 names. As you read a symbol, grow the name in the obstack gradually.
61 When the name is complete, finalize it. Then, if the symbol exists already,
62 free the newly read name.
64 The way we do this is to take a large chunk, allocating memory from
65 low addresses. When you want to build a symbol in the chunk you just
66 add chars above the current "high water mark" in the chunk. When you
67 have finished adding chars, because you got to the end of the symbol,
68 you know how long the chars are, and you can create a new object.
69 Mostly the chars will not burst over the highest address of the chunk,
70 because you would typically expect a chunk to be (say) 100 times as
71 long as an average object.
73 In case that isn't clear, when we have enough chars to make up
74 the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
75 so we just point to it where it lies. No moving of chars is
76 needed and this is the second win: potentially long strings need
77 never be explicitly shuffled. Once an object is formed, it does not
78 change its address during its lifetime.
80 When the chars burst over a chunk boundary, we allocate a larger
81 chunk, and then copy the partly formed object from the end of the old
82 chunk to the beginning of the new larger chunk. We then carry on
83 accreting characters to the end of the object as we normally would.
85 A special macro is provided to add a single char at a time to a
86 growing object. This allows the use of register variables, which
87 break the ordinary 'growth' macro.
90 We allocate large chunks.
91 We carve out one object at a time from the current chunk.
92 Once carved, an object never moves.
93 We are free to append data of any size to the currently
95 Exactly one object is growing in an obstack at any one time.
96 You can run one obstack per control block.
97 You may have as many control blocks as you dare.
98 Because of the way we do it, you can `unwind' an obstack
99 back to a previous state. (You may remove objects much
100 as you would with a stack.)
104 /* Don't do the contents of this file more than once. */
113 /* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is
114 defined, as with GNU C, use that; that way we don't pollute the
115 namespace with <stddef.h>'s symbols. Otherwise, include <stddef.h>
116 and use ptrdiff_t. */
118 #ifdef __PTRDIFF_TYPE__
119 # define PTR_INT_TYPE __PTRDIFF_TYPE__
122 # define PTR_INT_TYPE ptrdiff_t
125 /* If B is the base of an object addressed by P, return the result of
126 aligning P to the next multiple of A + 1. B and P must be of type
127 char *. A + 1 must be a power of 2. */
129 #define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
131 /* Similiar to _BPTR_ALIGN (B, P, A), except optimize the common case
132 where pointers can be converted to integers, aligned as integers,
133 and converted back again. If PTR_INT_TYPE is narrower than a
134 pointer (e.g., the AS/400), play it safe and compute the alignment
135 relative to B. Otherwise, use the faster strategy of computing the
136 alignment relative to 0. */
138 #define __PTR_ALIGN(B, P, A) \
139 __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
145 struct _obstack_chunk /* Lives at front of each chunk. */
147 char *limit; /* 1 past end of this chunk */
148 struct _obstack_chunk *prev; /* address of prior chunk or NULL */
149 char contents[4]; /* objects begin here */
152 struct obstack /* control current object in current chunk */
154 long chunk_size; /* preferred size to allocate chunks in */
155 struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
156 char *object_base; /* address of object we are building */
157 char *next_free; /* where to add next char to current object */
158 char *chunk_limit; /* address of char after current chunk */
161 PTR_INT_TYPE tempint;
163 } temp; /* Temporary for some macros. */
164 int alignment_mask; /* Mask of alignment for each object. */
165 /* These prototypes vary based on `use_extra_arg', and we use
166 casts to the prototypeless function type in all assignments,
167 but having prototypes here quiets -Wstrict-prototypes. */
168 struct _obstack_chunk *(*chunkfun) (void *, long);
169 void (*freefun) (void *, struct _obstack_chunk *);
170 void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
171 unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
172 unsigned maybe_empty_object:1;/* There is a possibility that the current
173 chunk contains a zero-length object. This
174 prevents freeing the chunk if we allocate
175 a bigger chunk to replace it. */
176 unsigned alloc_failed:1; /* No longer used, as we now call the failed
177 handler on error, but retained for binary
181 /* Declare the external functions we use; they are in obstack.c. */
183 extern void _obstack_newchunk (struct obstack *, int);
184 extern int _obstack_begin (struct obstack *, int, int,
185 void *(*) (long), void (*) (void *));
186 extern int _obstack_begin_1 (struct obstack *, int, int,
187 void *(*) (void *, long),
188 void (*) (void *, void *), void *);
189 extern int _obstack_memory_used (struct obstack *);
191 void obstack_free (struct obstack *obstack, void *block);
194 /* Error handler called when `obstack_chunk_alloc' failed to allocate
195 more memory. This can be set to a user defined function which
196 should either abort gracefully or use longjump - but shouldn't
197 return. The default action is to print a message and abort. */
198 extern void (*obstack_alloc_failed_handler) (void);
200 /* Exit value used when `print_and_abort' is used. */
201 extern int obstack_exit_failure;
203 /* Pointer to beginning of object being allocated or to be allocated next.
204 Note that this might not be the final address of the object
205 because a new chunk might be needed to hold the final size. */
207 #define obstack_base(h) ((void *) (h)->object_base)
209 /* Size for allocating ordinary chunks. */
211 #define obstack_chunk_size(h) ((h)->chunk_size)
213 /* Pointer to next byte not yet allocated in current chunk. */
215 #define obstack_next_free(h) ((h)->next_free)
217 /* Mask specifying low bits that should be clear in address of an object. */
219 #define obstack_alignment_mask(h) ((h)->alignment_mask)
221 /* To prevent prototype warnings provide complete argument list. */
222 #define obstack_init(h) \
223 _obstack_begin ((h), 0, 0, \
224 (void *(*) (long)) obstack_chunk_alloc, \
225 (void (*) (void *)) obstack_chunk_free)
227 #define obstack_begin(h, size) \
228 _obstack_begin ((h), (size), 0, \
229 (void *(*) (long)) obstack_chunk_alloc, \
230 (void (*) (void *)) obstack_chunk_free)
232 #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
233 _obstack_begin ((h), (size), (alignment), \
234 (void *(*) (long)) (chunkfun), \
235 (void (*) (void *)) (freefun))
237 #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
238 _obstack_begin_1 ((h), (size), (alignment), \
239 (void *(*) (void *, long)) (chunkfun), \
240 (void (*) (void *, void *)) (freefun), (arg))
242 #define obstack_chunkfun(h, newchunkfun) \
243 ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
245 #define obstack_freefun(h, newfreefun) \
246 ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
248 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
250 #define obstack_blank_fast(h,n) ((h)->next_free += (n))
252 #define obstack_memory_used(h) _obstack_memory_used (h)
254 #if defined __GNUC__ && defined __STDC__ && __STDC__
255 /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
256 does not implement __extension__. But that compiler doesn't define
258 # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
259 # define __extension__
262 /* For GNU C, if not -traditional,
263 we can define these macros to compute all args only once
264 without using a global variable.
265 Also, we can avoid using the `temp' slot, to make faster code. */
267 # define obstack_object_size(OBSTACK) \
269 ({ struct obstack const *__o = (OBSTACK); \
270 (unsigned) (__o->next_free - __o->object_base); })
272 # define obstack_room(OBSTACK) \
274 ({ struct obstack const *__o = (OBSTACK); \
275 (unsigned) (__o->chunk_limit - __o->next_free); })
277 # define obstack_make_room(OBSTACK,length) \
279 ({ struct obstack *__o = (OBSTACK); \
280 int __len = (length); \
281 if (__o->chunk_limit - __o->next_free < __len) \
282 _obstack_newchunk (__o, __len); \
285 # define obstack_empty_p(OBSTACK) \
287 ({ struct obstack const *__o = (OBSTACK); \
288 (__o->chunk->prev == 0 \
289 && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
290 __o->chunk->contents, \
291 __o->alignment_mask)); })
293 # define obstack_grow(OBSTACK,where,length) \
295 ({ struct obstack *__o = (OBSTACK); \
296 int __len = (length); \
297 if (__o->next_free + __len > __o->chunk_limit) \
298 _obstack_newchunk (__o, __len); \
299 memcpy (__o->next_free, where, __len); \
300 __o->next_free += __len; \
303 # define obstack_grow0(OBSTACK,where,length) \
305 ({ struct obstack *__o = (OBSTACK); \
306 int __len = (length); \
307 if (__o->next_free + __len + 1 > __o->chunk_limit) \
308 _obstack_newchunk (__o, __len + 1); \
309 memcpy (__o->next_free, where, __len); \
310 __o->next_free += __len; \
311 *(__o->next_free)++ = 0; \
314 # define obstack_1grow(OBSTACK,datum) \
316 ({ struct obstack *__o = (OBSTACK); \
317 if (__o->next_free + 1 > __o->chunk_limit) \
318 _obstack_newchunk (__o, 1); \
319 obstack_1grow_fast (__o, datum); \
322 /* These assume that the obstack alignment is good enough for pointers
323 or ints, and that the data added so far to the current object
324 shares that much alignment. */
326 # define obstack_ptr_grow(OBSTACK,datum) \
328 ({ struct obstack *__o = (OBSTACK); \
329 if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
330 _obstack_newchunk (__o, sizeof (void *)); \
331 obstack_ptr_grow_fast (__o, datum); }) \
333 # define obstack_int_grow(OBSTACK,datum) \
335 ({ struct obstack *__o = (OBSTACK); \
336 if (__o->next_free + sizeof (int) > __o->chunk_limit) \
337 _obstack_newchunk (__o, sizeof (int)); \
338 obstack_int_grow_fast (__o, datum); })
340 # define obstack_ptr_grow_fast(OBSTACK,aptr) \
342 ({ struct obstack *__o1 = (OBSTACK); \
343 *(const void **) __o1->next_free = (aptr); \
344 __o1->next_free += sizeof (const void *); \
347 # define obstack_int_grow_fast(OBSTACK,aint) \
349 ({ struct obstack *__o1 = (OBSTACK); \
350 *(int *) __o1->next_free = (aint); \
351 __o1->next_free += sizeof (int); \
354 # define obstack_blank(OBSTACK,length) \
356 ({ struct obstack *__o = (OBSTACK); \
357 int __len = (length); \
358 if (__o->chunk_limit - __o->next_free < __len) \
359 _obstack_newchunk (__o, __len); \
360 obstack_blank_fast (__o, __len); \
363 # define obstack_alloc(OBSTACK,length) \
365 ({ struct obstack *__h = (OBSTACK); \
366 obstack_blank (__h, (length)); \
367 obstack_finish (__h); })
369 # define obstack_copy(OBSTACK,where,length) \
371 ({ struct obstack *__h = (OBSTACK); \
372 obstack_grow (__h, (where), (length)); \
373 obstack_finish (__h); })
375 # define obstack_copy0(OBSTACK,where,length) \
377 ({ struct obstack *__h = (OBSTACK); \
378 obstack_grow0 (__h, (where), (length)); \
379 obstack_finish (__h); })
381 /* The local variable is named __o1 to avoid a name conflict
382 when obstack_blank is called. */
383 # define obstack_finish(OBSTACK) \
385 ({ struct obstack *__o1 = (OBSTACK); \
386 void *__value = (void *) __o1->object_base; \
387 if (__o1->next_free == __value) \
388 __o1->maybe_empty_object = 1; \
390 = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
391 __o1->alignment_mask); \
392 if (__o1->next_free - (char *)__o1->chunk \
393 > __o1->chunk_limit - (char *)__o1->chunk) \
394 __o1->next_free = __o1->chunk_limit; \
395 __o1->object_base = __o1->next_free; \
398 # define obstack_free(OBSTACK, OBJ) \
400 ({ struct obstack *__o = (OBSTACK); \
401 void *__obj = (OBJ); \
402 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
403 __o->next_free = __o->object_base = (char *)__obj; \
404 else (obstack_free) (__o, __obj); })
406 #else /* not __GNUC__ or not __STDC__ */
408 # define obstack_object_size(h) \
409 (unsigned) ((h)->next_free - (h)->object_base)
411 # define obstack_room(h) \
412 (unsigned) ((h)->chunk_limit - (h)->next_free)
414 # define obstack_empty_p(h) \
415 ((h)->chunk->prev == 0 \
416 && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
417 (h)->chunk->contents, \
418 (h)->alignment_mask))
420 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
421 so that we can avoid having void expressions
422 in the arms of the conditional expression.
423 Casting the third operand to void was tried before,
424 but some compilers won't accept it. */
426 # define obstack_make_room(h,length) \
427 ( (h)->temp.tempint = (length), \
428 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
429 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
431 # define obstack_grow(h,where,length) \
432 ( (h)->temp.tempint = (length), \
433 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
434 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
435 memcpy ((h)->next_free, where, (h)->temp.tempint), \
436 (h)->next_free += (h)->temp.tempint)
438 # define obstack_grow0(h,where,length) \
439 ( (h)->temp.tempint = (length), \
440 (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \
441 ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \
442 memcpy ((h)->next_free, where, (h)->temp.tempint), \
443 (h)->next_free += (h)->temp.tempint, \
444 *((h)->next_free)++ = 0)
446 # define obstack_1grow(h,datum) \
447 ( (((h)->next_free + 1 > (h)->chunk_limit) \
448 ? (_obstack_newchunk ((h), 1), 0) : 0), \
449 obstack_1grow_fast (h, datum))
451 # define obstack_ptr_grow(h,datum) \
452 ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
453 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
454 obstack_ptr_grow_fast (h, datum))
456 # define obstack_int_grow(h,datum) \
457 ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
458 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
459 obstack_int_grow_fast (h, datum))
461 # define obstack_ptr_grow_fast(h,aptr) \
462 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
464 # define obstack_int_grow_fast(h,aint) \
465 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
467 # define obstack_blank(h,length) \
468 ( (h)->temp.tempint = (length), \
469 (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \
470 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
471 obstack_blank_fast (h, (h)->temp.tempint))
473 # define obstack_alloc(h,length) \
474 (obstack_blank ((h), (length)), obstack_finish ((h)))
476 # define obstack_copy(h,where,length) \
477 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
479 # define obstack_copy0(h,where,length) \
480 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
482 # define obstack_finish(h) \
483 ( ((h)->next_free == (h)->object_base \
484 ? (((h)->maybe_empty_object = 1), 0) \
486 (h)->temp.tempptr = (h)->object_base, \
488 = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
489 (h)->alignment_mask), \
490 (((h)->next_free - (char *) (h)->chunk \
491 > (h)->chunk_limit - (char *) (h)->chunk) \
492 ? ((h)->next_free = (h)->chunk_limit) : 0), \
493 (h)->object_base = (h)->next_free, \
496 # define obstack_free(h,obj) \
497 ( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \
498 ((((h)->temp.tempint > 0 \
499 && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \
500 ? (int) ((h)->next_free = (h)->object_base \
501 = (h)->temp.tempint + (char *) (h)->chunk) \
502 : (((obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0)))
504 #endif /* not __GNUC__ or not __STDC__ */
506 int obstack_vprintf(struct obstack *obst, const char *fmt, va_list ap);
507 int obstack_printf(struct obstack *obst, const char *fmt, ...);
513 #endif /* obstack.h */