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. */
18 * $FreeBSD: src/gnu/usr.bin/as/obstack.h,v 1.6 1999/08/27 23:34:20 peter Exp $
19 * $DragonFly: src/gnu/usr.bin/as/Attic/obstack.h,v 1.2 2003/06/17 04:25:44 dillon Exp $
25 All the apparent functions defined here are macros. The idea
26 is that you would use these pre-tested macros to solve a
27 very specific set of problems, and they would run fast.
28 Caution: no side-effects in arguments please!! They may be
29 evaluated MANY times!!
31 These macros operate a stack of objects. Each object starts life
32 small, and may grow to maturity. (Consider building a word syllable
33 by syllable.) An object can move while it is growing. Once it has
34 been "finished" it never changes address again. So the "top of the
35 stack" is typically an immature growing object, while the rest of the
36 stack is of mature, fixed size and fixed address objects.
38 These routines grab large chunks of memory, using a function you
39 supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
40 by calling `obstack_chunk_free'. You must define them and declare
41 them before using any obstack macros.
43 Each independent stack is represented by a `struct obstack'.
44 Each of the obstack macros expects a pointer to such a structure
45 as the first argument.
47 One motivation for this package is the problem of growing char strings
48 in symbol tables. Unless you are "fascist pig with a read-only mind"
49 [Gosper's immortal quote from HAKMEM item 154, out of context] you
50 would not like to put any arbitrary upper limit on the length of your
53 In practice this often means you will build many short symbols and a
54 few long symbols. At the time you are reading a symbol you don't know
55 how long it is. One traditional method is to read a symbol into a
56 buffer, realloc()ating the buffer every time you try to read a symbol
57 that is longer than the buffer. This is beaut, but you still will
58 want to copy the symbol from the buffer to a more permanent
59 symbol-table entry say about half the time.
61 With obstacks, you can work differently. Use one obstack for all symbol
62 names. As you read a symbol, grow the name in the obstack gradually.
63 When the name is complete, finalize it. Then, if the symbol exists already,
64 free the newly read name.
66 The way we do this is to take a large chunk, allocating memory from
67 low addresses. When you want to build a symbol in the chunk you just
68 add chars above the current "high water mark" in the chunk. When you
69 have finished adding chars, because you got to the end of the symbol,
70 you know how long the chars are, and you can create a new object.
71 Mostly the chars will not burst over the highest address of the chunk,
72 because you would typically expect a chunk to be (say) 100 times as
73 long as an average object.
75 In case that isn't clear, when we have enough chars to make up
76 the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
77 so we just point to it where it lies. No moving of chars is
78 needed and this is the second win: potentially long strings need
79 never be explicitly shuffled. Once an object is formed, it does not
80 change its address during its lifetime.
82 When the chars burst over a chunk boundary, we allocate a larger
83 chunk, and then copy the partly formed object from the end of the old
84 chunk to the beginning of the new larger chunk. We then carry on
85 accreting characters to the end of the object as we normally would.
87 A special macro is provided to add a single char at a time to a
88 growing object. This allows the use of register variables, which
89 break the ordinary 'growth' macro.
92 We allocate large chunks.
93 We carve out one object at a time from the current chunk.
94 Once carved, an object never moves.
95 We are free to append data of any size to the currently
97 Exactly one object is growing in an obstack at any one time.
98 You can run one obstack per control block.
99 You may have as many control blocks as you dare.
100 Because of the way we do it, you can `unwind' a obstack
101 back to a previous state. (You may remove objects much
102 as you would with a stack.)
106 /* Don't do the contents of this file more than once. */
111 /* We use subtraction of (char *)0 instead of casting to int
112 because on word-addressable machines a simple cast to int
113 may ignore the byte-within-word field of the pointer. */
116 #define __PTR_TO_INT(P) ((P) - (char *)0)
120 #define __INT_TO_PTR(P) ((P) + (char *)0)
123 struct _obstack_chunk /* Lives at front of each chunk. */
125 char *limit; /* 1 past end of this chunk */
126 struct _obstack_chunk *prev; /* address of prior chunk or NULL */
127 char contents[4]; /* objects begin here */
130 struct obstack /* control current object in current chunk */
132 long chunk_size; /* preferred size to allocate chunks in */
133 struct _obstack_chunk* chunk; /* address of current struct obstack_chunk */
134 char *object_base; /* address of object we are building */
135 char *next_free; /* where to add next char to current object */
136 char *chunk_limit; /* address of char after current chunk */
137 int temp; /* Temporary for some macros. */
138 int alignment_mask; /* Mask of alignment for each object. */
139 struct _obstack_chunk *(*chunkfun) (); /* User's fcn to allocate a chunk. */
140 void (*freefun) (); /* User's function to free a chunk. */
141 /* Nonzero means there is a possibility the current chunk contains
142 a zero-length object. This prevents freeing the chunk
143 if we allocate a bigger chunk to replace it. */
144 char maybe_empty_object;
147 /* Declare the external functions we use; they are in obstack.c. */
150 extern void _obstack_newchunk (struct obstack *, int);
151 extern void _obstack_free (struct obstack *, void *);
152 extern void _obstack_begin (struct obstack *, int, int,
153 void *(*) (), void (*) ());
155 extern void _obstack_newchunk ();
156 extern void _obstack_free ();
157 extern void _obstack_begin ();
162 /* Do the function-declarations after the structs
163 but before defining the macros. */
165 void obstack_init (struct obstack *obstack);
167 void * obstack_alloc (struct obstack *obstack, int size);
169 void * obstack_copy (struct obstack *obstack, void *address, int size);
170 void * obstack_copy0 (struct obstack *obstack, void *address, int size);
172 void obstack_free (struct obstack *obstack, void *block);
174 void obstack_blank (struct obstack *obstack, int size);
176 void obstack_grow (struct obstack *obstack, void *data, int size);
177 void obstack_grow0 (struct obstack *obstack, void *data, int size);
179 void obstack_1grow (struct obstack *obstack, int data_char);
180 void obstack_ptr_grow (struct obstack *obstack, void *data);
181 void obstack_int_grow (struct obstack *obstack, int data);
183 void * obstack_finish (struct obstack *obstack);
185 int obstack_object_size (struct obstack *obstack);
187 int obstack_room (struct obstack *obstack);
188 void obstack_1grow_fast (struct obstack *obstack, int data_char);
189 void obstack_ptr_grow_fast (struct obstack *obstack, void *data);
190 void obstack_int_grow_fast (struct obstack *obstack, int data);
191 void obstack_blank_fast (struct obstack *obstack, int size);
193 void * obstack_base (struct obstack *obstack);
194 void * obstack_next_free (struct obstack *obstack);
195 int obstack_alignment_mask (struct obstack *obstack);
196 int obstack_chunk_size (struct obstack *obstack);
198 #endif /* __STDC__ */
200 /* Non-ANSI C cannot really support alternative functions for these macros,
201 so we do not declare them. */
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) ((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 #define obstack_init(h) \
222 _obstack_begin ((h), 0, 0, \
223 (void *(*) ()) obstack_chunk_alloc, (void (*) ())obstack_chunk_free)
225 #define obstack_begin(h, size) \
226 _obstack_begin ((h), (size), 0, \
227 (void *(*) ()) obstack_chunk_alloc, (void (*) ())obstack_chunk_free)
229 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
231 #define obstack_blank_fast(h,n) ((h)->next_free += (n))
233 #if defined (__GNUC__) && defined (__STDC__)
235 #define __extension__
238 /* For GNU C, if not -traditional,
239 we can define these macros to compute all args only once
240 without using a global variable.
241 Also, we can avoid using the `temp' slot, to make faster code. */
243 #define obstack_object_size(OBSTACK) \
245 ({ struct obstack *__o = (OBSTACK); \
246 (unsigned) (__o->next_free - __o->object_base); })
248 #define obstack_room(OBSTACK) \
250 ({ struct obstack *__o = (OBSTACK); \
251 (unsigned) (__o->chunk_limit - __o->next_free); })
253 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
254 so that we can avoid having void expressions
255 in the arms of the conditional expression.
256 Casting the third operand to void was tried before,
257 but some compilers won't accept it. */
258 #define obstack_grow(OBSTACK,where,length) \
260 ({ struct obstack *__o = (OBSTACK); \
261 int __len = (length); \
262 ((__o->next_free + __len > __o->chunk_limit) \
263 ? (_obstack_newchunk (__o, __len), 0) : 0); \
264 memcpy (__o->next_free, where, __len); \
265 __o->next_free += __len; \
268 #define obstack_grow0(OBSTACK,where,length) \
270 ({ struct obstack *__o = (OBSTACK); \
271 int __len = (length); \
272 ((__o->next_free + __len + 1 > __o->chunk_limit) \
273 ? (_obstack_newchunk (__o, __len + 1), 0) : 0), \
274 memcpy (__o->next_free, where, __len), \
275 __o->next_free += __len, \
276 *(__o->next_free)++ = 0; \
279 #define obstack_1grow(OBSTACK,datum) \
281 ({ struct obstack *__o = (OBSTACK); \
282 ((__o->next_free + 1 > __o->chunk_limit) \
283 ? (_obstack_newchunk (__o, 1), 0) : 0), \
284 *(__o->next_free)++ = (datum); \
287 /* These assume that the obstack alignment is good enough for pointers or ints,
288 and that the data added so far to the current object
289 shares that much alignment. */
291 #define obstack_ptr_grow(OBSTACK,datum) \
293 ({ struct obstack *__o = (OBSTACK); \
294 ((__o->next_free + sizeof (void *) > __o->chunk_limit) \
295 ? (_obstack_newchunk (__o, sizeof (void *)), 0) : 0), \
296 *(*(void ***)&__o->next_free)++ = ((void *)datum); \
299 #define obstack_int_grow(OBSTACK,datum) \
301 ({ struct obstack *__o = (OBSTACK); \
302 ((__o->next_free + sizeof (int) > __o->chunk_limit) \
303 ? (_obstack_newchunk (__o, sizeof (int)), 0) : 0), \
304 *(*(int **)&__o->next_free)++ = ((int)datum); \
307 #define obstack_ptr_grow_fast(h,aptr) (*(*(void ***)&(h)->next_free)++ = (void *)aptr)
308 #define obstack_int_grow_fast(h,aint) (*(*(int **)&(h)->next_free)++ = (int)aint)
310 #define obstack_blank(OBSTACK,length) \
312 ({ struct obstack *__o = (OBSTACK); \
313 int __len = (length); \
314 ((__o->chunk_limit - __o->next_free < __len) \
315 ? (_obstack_newchunk (__o, __len), 0) : 0); \
316 __o->next_free += __len; \
319 #define obstack_alloc(OBSTACK,length) \
321 ({ struct obstack *__h = (OBSTACK); \
322 obstack_blank (__h, (length)); \
323 obstack_finish (__h); })
325 #define obstack_copy(OBSTACK,where,length) \
327 ({ struct obstack *__h = (OBSTACK); \
328 obstack_grow (__h, (where), (length)); \
329 obstack_finish (__h); })
331 #define obstack_copy0(OBSTACK,where,length) \
333 ({ struct obstack *__h = (OBSTACK); \
334 obstack_grow0 (__h, (where), (length)); \
335 obstack_finish (__h); })
337 /* The local variable is named __o1 to avoid a name conflict
338 when obstack_blank is called. */
339 #define obstack_finish(OBSTACK) \
341 ({ struct obstack *__o1 = (OBSTACK); \
342 void *value = (void *) __o1->object_base; \
343 if (__o1->next_free == value) \
344 __o1->maybe_empty_object = 1; \
346 = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)\
347 & ~ (__o1->alignment_mask)); \
348 ((__o1->next_free - (char *)__o1->chunk \
349 > __o1->chunk_limit - (char *)__o1->chunk) \
350 ? (__o1->next_free = __o1->chunk_limit) : 0); \
351 __o1->object_base = __o1->next_free; \
354 #define obstack_free(OBSTACK, OBJ) \
356 ({ struct obstack *__o = (OBSTACK); \
357 void *__obj = (OBJ); \
358 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
359 __o->next_free = __o->object_base = __obj; \
360 else (obstack_free) (__o, __obj); })
362 #else /* not __GNUC__ or not __STDC__ */
364 #define obstack_object_size(h) \
365 (unsigned) ((h)->next_free - (h)->object_base)
367 #define obstack_room(h) \
368 (unsigned) ((h)->chunk_limit - (h)->next_free)
370 #define obstack_grow(h,where,length) \
371 ( (h)->temp = (length), \
372 (((h)->next_free + (h)->temp > (h)->chunk_limit) \
373 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
374 memcpy ((h)->next_free, where, (h)->temp), \
375 (h)->next_free += (h)->temp)
377 #define obstack_grow0(h,where,length) \
378 ( (h)->temp = (length), \
379 (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \
380 ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0), \
381 memcpy ((h)->next_free, where, (h)->temp), \
382 (h)->next_free += (h)->temp, \
383 *((h)->next_free)++ = 0)
385 #define obstack_1grow(h,datum) \
386 ( (((h)->next_free + 1 > (h)->chunk_limit) \
387 ? (_obstack_newchunk ((h), 1), 0) : 0), \
388 *((h)->next_free)++ = (datum))
390 #define obstack_ptr_grow(h,datum) \
391 ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
392 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
393 *(*(char ***)&(h)->next_free)++ = ((char *)datum))
395 #define obstack_int_grow(h,datum) \
396 ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
397 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
398 *(*(int **)&(h)->next_free)++ = ((int)datum))
400 #define obstack_ptr_grow_fast(h,aptr) (*(*(char ***)&(h)->next_free)++ = (char *)aptr)
401 #define obstack_int_grow_fast(h,aint) (*(*(int **)&(h)->next_free)++ = (int)aint)
402 #define obstack_blank(h,length) \
403 ( (h)->temp = (length), \
404 (((h)->chunk_limit - (h)->next_free < (h)->temp) \
405 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
406 (h)->next_free += (h)->temp)
408 #define obstack_alloc(h,length) \
409 (obstack_blank ((h), (length)), obstack_finish ((h)))
411 #define obstack_copy(h,where,length) \
412 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
414 #define obstack_copy0(h,where,length) \
415 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
417 #define obstack_finish(h) \
418 ( ((h)->next_free == (h)->object_base \
419 ? (((h)->maybe_empty_object = 1), 0) \
421 (h)->temp = __PTR_TO_INT ((h)->object_base), \
423 = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \
424 & ~ ((h)->alignment_mask)), \
425 (((h)->next_free - (char *)(h)->chunk \
426 > (h)->chunk_limit - (char *)(h)->chunk) \
427 ? ((h)->next_free = (h)->chunk_limit) : 0), \
428 (h)->object_base = (h)->next_free, \
429 __INT_TO_PTR ((h)->temp))
432 #define obstack_free(h,obj) \
433 ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
434 (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
435 ? (int) ((h)->next_free = (h)->object_base \
436 = (h)->temp + (char *) (h)->chunk) \
437 : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0)))
439 #define obstack_free(h,obj) \
440 ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
441 (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
442 ? (int) ((h)->next_free = (h)->object_base \
443 = (h)->temp + (char *) (h)->chunk) \
444 : (_obstack_free ((h), (h)->temp + (char *) (h)->chunk), 0)))
447 #endif /* not __GNUC__ or not __STDC__ */
449 #endif /* not __OBSTACKS__ */