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