1 /* obstack.h - object stack macros
2 Copyright (C) 1988 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 All the apparent functions defined here are macros. The idea
21 is that you would use these pre-tested macros to solve a
22 very specific set of problems, and they would run fast.
23 Caution: no side-effects in arguments please!! They may be
24 evaluated MANY times!!
26 These macros operate a stack of objects. Each object starts life
27 small, and may grow to maturity. (Consider building a word syllable
28 by syllable.) An object can move while it is growing. Once it has
29 been "finished" it never changes address again. So the "top of the
30 stack" is typically an immature growing object, while the rest of the
31 stack is of mature, fixed size and fixed address objects.
33 These routines grab large chunks of memory, using a function you
34 supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
35 by calling `obstack_chunk_free'. You must define them and declare
36 them before using any obstack macros.
38 Each independent stack is represented by a `struct obstack'.
39 Each of the obstack macros expects a pointer to such a structure
40 as the first argument.
42 One motivation for this package is the problem of growing char strings
43 in symbol tables. Unless you are "fascist pig with a read-only mind"
44 [Gosper's immortal quote from HAKMEM item 154, out of context] you
45 would not like to put any arbitrary upper limit on the length of your
48 In practice this often means you will build many short symbols and a
49 few long symbols. At the time you are reading a symbol you don't know
50 how long it is. One traditional method is to read a symbol into a
51 buffer, realloc()ating the buffer every time you try to read a symbol
52 that is longer than the buffer. This is beaut, but you still will
53 want to copy the symbol from the buffer to a more permanent
54 symbol-table entry say about half the time.
56 With obstacks, you can work differently. Use one obstack for all symbol
57 names. As you read a symbol, grow the name in the obstack gradually.
58 When the name is complete, finalize it. Then, if the symbol exists already,
59 free the newly read name.
61 The way we do this is to take a large chunk, allocating memory from
62 low addresses. When you want to build a symbol in the chunk you just
63 add chars above the current "high water mark" in the chunk. When you
64 have finished adding chars, because you got to the end of the symbol,
65 you know how long the chars are, and you can create a new object.
66 Mostly the chars will not burst over the highest address of the chunk,
67 because you would typically expect a chunk to be (say) 100 times as
68 long as an average object.
70 In case that isn't clear, when we have enough chars to make up
71 the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
72 so we just point to it where it lies. No moving of chars is
73 needed and this is the second win: potentially long strings need
74 never be explicitly shuffled. Once an object is formed, it does not
75 change its address during its lifetime.
77 When the chars burst over a chunk boundary, we allocate a larger
78 chunk, and then copy the partly formed object from the end of the old
79 chunk to the beginning of the new larger chunk. We then carry on
80 accreting characters to the end of the object as we normally would.
82 A special macro is provided to add a single char at a time to a
83 growing object. This allows the use of register variables, which
84 break the ordinary 'growth' macro.
87 We allocate large chunks.
88 We carve out one object at a time from the current chunk.
89 Once carved, an object never moves.
90 We are free to append data of any size to the currently
92 Exactly one object is growing in an obstack at any one time.
93 You can run one obstack per control block.
94 You may have as many control blocks as you dare.
95 Because of the way we do it, you can `unwind' a obstack
96 back to a previous state. (You may remove objects much
97 as you would with a stack.)
101 /* Don't do the contents of this file more than once. */
106 /* We use subtraction of (char *)0 instead of casting to int
107 because on word-addressable machines a simple cast to int
108 may ignore the byte-within-word field of the pointer. */
111 #define __PTR_TO_INT(P) ((P) - (char *)0)
115 #define __INT_TO_PTR(P) ((P) + (char *)0)
118 struct _obstack_chunk /* Lives at front of each chunk. */
120 char *limit; /* 1 past end of this chunk */
121 struct _obstack_chunk *prev; /* address of prior chunk or NULL */
122 char contents[4]; /* objects begin here */
125 struct obstack /* control current object in current chunk */
127 long chunk_size; /* preferred size to allocate chunks in */
128 struct _obstack_chunk* chunk; /* address of current struct obstack_chunk */
129 char *object_base; /* address of object we are building */
130 char *next_free; /* where to add next char to current object */
131 char *chunk_limit; /* address of char after current chunk */
132 int temp; /* Temporary for some macros. */
133 int alignment_mask; /* Mask of alignment for each object. */
134 struct _obstack_chunk *(*chunkfun) (); /* User's fcn to allocate a chunk. */
135 void (*freefun) (); /* User's function to free a chunk. */
136 void *area_id; /* Select which region to alloc/free in */
137 int flags; /* Miscellaneous special purpose flags */
140 /* Declare bits for flags word. */
142 /* Means there is a possibility the current chunk contains a zero-length
143 object. This prevents freeing the chunk if we allocate a bigger chunk
146 #define OBSTACK_MAYBE_EMPTY_OBJECT (1 << 0)
148 /* Means that the allocation and deallocation functions take two arguments,
149 ala the mmalloc package. The first argument is a generic pointer that
150 is saved in the area_id member of the obstack struct. */
152 #define OBSTACK_MMALLOC_LIKE (1 << 1)
154 /* Declare the external functions we use; they are in obstack.c. */
157 extern void _obstack_newchunk (struct obstack *, int);
158 extern void _obstack_free (struct obstack *, void *);
159 extern void _obstack_begin (struct obstack *, int, int,
160 void *(*) (int), void (*) (int), void *, int);
162 extern void _obstack_newchunk ();
163 extern void _obstack_free ();
164 extern void _obstack_begin ();
169 /* Do the function-declarations after the structs
170 but before defining the macros. */
172 void obstack_init (struct obstack *obstack);
174 void * obstack_alloc (struct obstack *obstack, int size);
176 void * obstack_copy (struct obstack *obstack, void *address, int size);
177 void * obstack_copy0 (struct obstack *obstack, void *address, int size);
179 void obstack_free (struct obstack *obstack, void *block);
181 void obstack_blank (struct obstack *obstack, int size);
183 void obstack_grow (struct obstack *obstack, void *data, int size);
184 void obstack_grow0 (struct obstack *obstack, void *data, int size);
186 void obstack_1grow (struct obstack *obstack, int data_char);
187 void obstack_ptr_grow (struct obstack *obstack, void *data);
188 void obstack_int_grow (struct obstack *obstack, int data);
190 void * obstack_finish (struct obstack *obstack);
192 int obstack_object_size (struct obstack *obstack);
194 int obstack_room (struct obstack *obstack);
195 void obstack_1grow_fast (struct obstack *obstack, int data_char);
196 void obstack_ptr_grow_fast (struct obstack *obstack, void *data);
197 void obstack_int_grow_fast (struct obstack *obstack, int data);
198 void obstack_blank_fast (struct obstack *obstack, int size);
200 void * obstack_base (struct obstack *obstack);
201 void * obstack_next_free (struct obstack *obstack);
202 int obstack_alignment_mask (struct obstack *obstack);
203 int obstack_chunk_size (struct obstack *obstack);
205 #endif /* __STDC__ */
207 /* Non-ANSI C cannot really support alternative functions for these macros,
208 so we do not declare them. */
210 /* Pointer to beginning of object being allocated or to be allocated next.
211 Note that this might not be the final address of the object
212 because a new chunk might be needed to hold the final size. */
214 #define obstack_base(h) ((h)->object_base)
216 /* Size for allocating ordinary chunks. */
218 #define obstack_chunk_size(h) ((h)->chunk_size)
220 /* Pointer to next byte not yet allocated in current chunk. */
222 #define obstack_next_free(h) ((h)->next_free)
224 /* Mask specifying low bits that should be clear in address of an object. */
226 #define obstack_alignment_mask(h) ((h)->alignment_mask)
228 #define obstack_init(h) \
229 _obstack_begin ((h), 0, 0, \
230 (void *(*) ()) obstack_chunk_alloc, (void (*) ())obstack_chunk_free, (void *) 0, 0)
232 #define obstack_begin(h, size) \
233 _obstack_begin ((h), (size), 0, \
234 (void *(*) ()) obstack_chunk_alloc, (void (*) ())obstack_chunk_free, (void *) 0, 0)
236 #define obstack_full_begin(h,size,alignment,chunkfun,freefun,area_id,flags) \
237 _obstack_begin ((h), (size), (alignment), \
238 (void *(*) ()) (chunkfun), (void (*) ()) (freefun), \
241 #define obstack_chunkfun(h, newchunkfun) \
242 ((h) -> chunkfun = (struct _obstack_chunk *(*)()) (newchunkfun))
244 #define obstack_freefun(h, newfreefun) \
245 ((h) -> freefun = (void (*)()) (newfreefun))
247 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
249 #define obstack_blank_fast(h,n) ((h)->next_free += (n))
251 #if defined (__GNUC__) && defined (__STDC__)
253 #define __extension__
256 /* For GNU C, if not -traditional,
257 we can define these macros to compute all args only once
258 without using a global variable.
259 Also, we can avoid using the `temp' slot, to make faster code. */
261 #define obstack_object_size(OBSTACK) \
263 ({ struct obstack *__o = (OBSTACK); \
264 (unsigned) (__o->next_free - __o->object_base); })
266 #define obstack_room(OBSTACK) \
268 ({ struct obstack *__o = (OBSTACK); \
269 (unsigned) (__o->chunk_limit - __o->next_free); })
271 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
272 so that we can avoid having void expressions
273 in the arms of the conditional expression.
274 Casting the third operand to void was tried before,
275 but some compilers won't accept it. */
276 #define obstack_grow(OBSTACK,where,length) \
278 ({ struct obstack *__o = (OBSTACK); \
279 int __len = (length); \
280 ((__o->next_free + __len > __o->chunk_limit) \
281 ? (_obstack_newchunk (__o, __len), 0) : 0); \
282 bcopy (where, __o->next_free, __len); \
283 __o->next_free += __len; \
286 #define obstack_grow0(OBSTACK,where,length) \
288 ({ struct obstack *__o = (OBSTACK); \
289 int __len = (length); \
290 ((__o->next_free + __len + 1 > __o->chunk_limit) \
291 ? (_obstack_newchunk (__o, __len + 1), 0) : 0), \
292 bcopy (where, __o->next_free, __len), \
293 __o->next_free += __len, \
294 *(__o->next_free)++ = 0; \
297 #define obstack_1grow(OBSTACK,datum) \
299 ({ struct obstack *__o = (OBSTACK); \
300 ((__o->next_free + 1 > __o->chunk_limit) \
301 ? (_obstack_newchunk (__o, 1), 0) : 0), \
302 *(__o->next_free)++ = (datum); \
305 /* These assume that the obstack alignment is good enough for pointers or ints,
306 and that the data added so far to the current object
307 shares that much alignment. */
309 #define obstack_ptr_grow(OBSTACK,datum) \
311 ({ struct obstack *__o = (OBSTACK); \
312 ((__o->next_free + sizeof (void *) > __o->chunk_limit) \
313 ? (_obstack_newchunk (__o, sizeof (void *)), 0) : 0), \
314 *(*(void ***)&__o->next_free)++ = ((void *)datum); \
317 #define obstack_int_grow(OBSTACK,datum) \
319 ({ struct obstack *__o = (OBSTACK); \
320 ((__o->next_free + sizeof (int) > __o->chunk_limit) \
321 ? (_obstack_newchunk (__o, sizeof (int)), 0) : 0), \
322 *(*(int **)&__o->next_free)++ = ((int)datum); \
325 #define obstack_ptr_grow_fast(h,aptr) (*(*(void ***)&(h)->next_free)++ = (void *)aptr)
326 #define obstack_int_grow_fast(h,aint) (*(*(int **)&(h)->next_free)++ = (int)aint)
328 #define obstack_blank(OBSTACK,length) \
330 ({ struct obstack *__o = (OBSTACK); \
331 int __len = (length); \
332 ((__o->chunk_limit - __o->next_free < __len) \
333 ? (_obstack_newchunk (__o, __len), 0) : 0); \
334 __o->next_free += __len; \
337 #define obstack_alloc(OBSTACK,length) \
339 ({ struct obstack *__h = (OBSTACK); \
340 obstack_blank (__h, (length)); \
341 obstack_finish (__h); })
343 #define obstack_copy(OBSTACK,where,length) \
345 ({ struct obstack *__h = (OBSTACK); \
346 obstack_grow (__h, (where), (length)); \
347 obstack_finish (__h); })
349 #define obstack_copy0(OBSTACK,where,length) \
351 ({ struct obstack *__h = (OBSTACK); \
352 obstack_grow0 (__h, (where), (length)); \
353 obstack_finish (__h); })
355 /* The local variable is named __o1 to avoid a name conflict
356 when obstack_blank is called. */
357 #define obstack_finish(OBSTACK) \
359 ({ struct obstack *__o1 = (OBSTACK); \
360 void *value = (void *) __o1->object_base; \
361 if (__o1->next_free == value) \
362 __o1->flags |= OBSTACK_MAYBE_EMPTY_OBJECT; \
364 = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)\
365 & ~ (__o1->alignment_mask)); \
366 ((__o1->next_free - (char *)__o1->chunk \
367 > __o1->chunk_limit - (char *)__o1->chunk) \
368 ? (__o1->next_free = __o1->chunk_limit) : 0); \
369 __o1->object_base = __o1->next_free; \
372 #define obstack_free(OBSTACK, OBJ) \
374 ({ struct obstack *__o = (OBSTACK); \
375 void *__obj = (OBJ); \
376 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
377 __o->next_free = __o->object_base = __obj; \
378 else (obstack_free) (__o, __obj); })
380 #else /* not __GNUC__ or not __STDC__ */
382 #define obstack_object_size(h) \
383 (unsigned) ((h)->next_free - (h)->object_base)
385 #define obstack_room(h) \
386 (unsigned) ((h)->chunk_limit - (h)->next_free)
388 #define obstack_grow(h,where,length) \
389 ( (h)->temp = (length), \
390 (((h)->next_free + (h)->temp > (h)->chunk_limit) \
391 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
392 bcopy (where, (h)->next_free, (h)->temp), \
393 (h)->next_free += (h)->temp)
395 #define obstack_grow0(h,where,length) \
396 ( (h)->temp = (length), \
397 (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \
398 ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0), \
399 bcopy (where, (h)->next_free, (h)->temp), \
400 (h)->next_free += (h)->temp, \
401 *((h)->next_free)++ = 0)
403 #define obstack_1grow(h,datum) \
404 ( (((h)->next_free + 1 > (h)->chunk_limit) \
405 ? (_obstack_newchunk ((h), 1), 0) : 0), \
406 *((h)->next_free)++ = (datum))
408 #define obstack_ptr_grow(h,datum) \
409 ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
410 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
411 *(*(char ***)&(h)->next_free)++ = ((char *)datum))
413 #define obstack_int_grow(h,datum) \
414 ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
415 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
416 *(*(int **)&(h)->next_free)++ = ((int)datum))
418 #define obstack_ptr_grow_fast(h,aptr) (*(*(char ***)&(h)->next_free)++ = (char *)aptr)
419 #define obstack_int_grow_fast(h,aint) (*(*(int **)&(h)->next_free)++ = (int)aint)
420 #define obstack_blank(h,length) \
421 ( (h)->temp = (length), \
422 (((h)->chunk_limit - (h)->next_free < (h)->temp) \
423 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
424 (h)->next_free += (h)->temp)
426 #define obstack_alloc(h,length) \
427 (obstack_blank ((h), (length)), obstack_finish ((h)))
429 #define obstack_copy(h,where,length) \
430 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
432 #define obstack_copy0(h,where,length) \
433 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
435 #define obstack_finish(h) \
436 ( ((h)->next_free == (h)->object_base \
437 ? (((h)->flags |= OBSTACK_MAYBE_EMPTY_OBJECT), 0) \
439 (h)->temp = __PTR_TO_INT ((h)->object_base), \
441 = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \
442 & ~ ((h)->alignment_mask)), \
443 (((h)->next_free - (char *)(h)->chunk \
444 > (h)->chunk_limit - (char *)(h)->chunk) \
445 ? ((h)->next_free = (h)->chunk_limit) : 0), \
446 (h)->object_base = (h)->next_free, \
447 __INT_TO_PTR ((h)->temp))
450 #define obstack_free(h,obj) \
451 ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
452 (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
453 ? (int) ((h)->next_free = (h)->object_base \
454 = (h)->temp + (char *) (h)->chunk) \
455 : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0)))
457 #define obstack_free(h,obj) \
458 ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
459 (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
460 ? (int) ((h)->next_free = (h)->object_base \
461 = (h)->temp + (char *) (h)->chunk) \
462 : (_obstack_free ((h), (h)->temp + (char *) (h)->chunk), 0)))
465 #endif /* not __GNUC__ or not __STDC__ */
467 #endif /* not __OBSTACKS__ */