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