(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / malloc / obstack.h
blobd18ef40b6e82c5fc2967081893b97b4af720c1e9
1 /* obstack.h - object stack macros
2 Copyright (C) 1988-1994,1996-1999,2003,2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. Its master source is NOT part of
4 the C library, however. The master source lives in /gd/gnu/lib.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 /* Summary:
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
49 symbols.
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.
89 Summary:
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
94 growing object.
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. */
106 #ifndef _OBSTACK_H
107 #define _OBSTACK_H 1
109 #ifdef __cplusplus
110 extern "C" {
111 #endif
113 /* We use subtraction of (char *) 0 instead of casting to int
114 because on word-addressable machines a simple cast to int
115 may ignore the byte-within-word field of the pointer. */
117 #ifndef __PTR_TO_INT
118 # define __PTR_TO_INT(P) ((P) - (char *) 0)
119 #endif
121 #ifndef __INT_TO_PTR
122 # define __INT_TO_PTR(P) ((P) + (char *) 0)
123 #endif
125 /* We need the type of the resulting object. 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. */
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
137 #include <string.h>
139 struct _obstack_chunk /* Lives at front of each chunk. */
141 char *limit; /* 1 past end of this chunk */
142 struct _obstack_chunk *prev; /* address of prior chunk or NULL */
143 char contents[4]; /* objects begin here */
146 struct obstack /* control current object in current chunk */
148 long chunk_size; /* preferred size to allocate chunks in */
149 struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
150 char *object_base; /* address of object we are building */
151 char *next_free; /* where to add next char to current object */
152 char *chunk_limit; /* address of char after current chunk */
153 PTR_INT_TYPE temp; /* Temporary for some macros. */
154 int alignment_mask; /* Mask of alignment for each object. */
155 /* These prototypes vary based on `use_extra_arg', and we use
156 casts to the prototypeless function type in all assignments,
157 but having prototypes here quiets -Wstrict-prototypes. */
158 struct _obstack_chunk *(*chunkfun) (void *, long);
159 void (*freefun) (void *, struct _obstack_chunk *);
160 void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
161 unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
162 unsigned maybe_empty_object:1;/* There is a possibility that the current
163 chunk contains a zero-length object. This
164 prevents freeing the chunk if we allocate
165 a bigger chunk to replace it. */
166 unsigned alloc_failed:1; /* No longer used, as we now call the failed
167 handler on error, but retained for binary
168 compatibility. */
171 /* Declare the external functions we use; they are in obstack.c. */
173 extern void _obstack_newchunk (struct obstack *, int);
174 extern int _obstack_begin (struct obstack *, int, int,
175 void *(*) (long), void (*) (void *));
176 extern int _obstack_begin_1 (struct obstack *, int, int,
177 void *(*) (void *, long),
178 void (*) (void *, void *), void *);
179 extern int _obstack_memory_used (struct obstack *);
181 void obstack_free (struct obstack *obstack, void *block);
184 /* Error handler called when `obstack_chunk_alloc' failed to allocate
185 more memory. This can be set to a user defined function which
186 should either abort gracefully or use longjump - but shouldn't
187 return. The default action is to print a message and abort. */
188 extern void (*obstack_alloc_failed_handler) (void);
190 /* Exit value used when `print_and_abort' is used. */
191 extern int obstack_exit_failure;
193 /* Pointer to beginning of object being allocated or to be allocated next.
194 Note that this might not be the final address of the object
195 because a new chunk might be needed to hold the final size. */
197 #define obstack_base(h) ((void *) (h)->object_base)
199 /* Size for allocating ordinary chunks. */
201 #define obstack_chunk_size(h) ((h)->chunk_size)
203 /* Pointer to next byte not yet allocated in current chunk. */
205 #define obstack_next_free(h) ((h)->next_free)
207 /* Mask specifying low bits that should be clear in address of an object. */
209 #define obstack_alignment_mask(h) ((h)->alignment_mask)
211 /* To prevent prototype warnings provide complete argument list. */
212 #define obstack_init(h) \
213 _obstack_begin ((h), 0, 0, \
214 (void *(*) (long)) obstack_chunk_alloc, \
215 (void (*) (void *)) obstack_chunk_free)
217 #define obstack_begin(h, size) \
218 _obstack_begin ((h), (size), 0, \
219 (void *(*) (long)) obstack_chunk_alloc, \
220 (void (*) (void *)) obstack_chunk_free)
222 #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
223 _obstack_begin ((h), (size), (alignment), \
224 (void *(*) (long)) (chunkfun), \
225 (void (*) (void *)) (freefun))
227 #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
228 _obstack_begin_1 ((h), (size), (alignment), \
229 (void *(*) (void *, long)) (chunkfun), \
230 (void (*) (void *, void *)) (freefun), (arg))
232 #define obstack_chunkfun(h, newchunkfun) \
233 ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
235 #define obstack_freefun(h, newfreefun) \
236 ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
238 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
240 #define obstack_blank_fast(h,n) ((h)->next_free += (n))
242 #define obstack_memory_used(h) _obstack_memory_used (h)
244 #if defined __GNUC__ && defined __STDC__ && __STDC__
245 /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
246 does not implement __extension__. But that compiler doesn't define
247 __GNUC_MINOR__. */
248 # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
249 # define __extension__
250 # endif
252 /* For GNU C, if not -traditional,
253 we can define these macros to compute all args only once
254 without using a global variable.
255 Also, we can avoid using the `temp' slot, to make faster code. */
257 # define obstack_object_size(OBSTACK) \
258 __extension__ \
259 ({ struct obstack const *__o = (OBSTACK); \
260 (unsigned) (__o->next_free - __o->object_base); })
262 # define obstack_room(OBSTACK) \
263 __extension__ \
264 ({ struct obstack const *__o = (OBSTACK); \
265 (unsigned) (__o->chunk_limit - __o->next_free); })
267 # define obstack_make_room(OBSTACK,length) \
268 __extension__ \
269 ({ struct obstack *__o = (OBSTACK); \
270 int __len = (length); \
271 if (__o->chunk_limit - __o->next_free < __len) \
272 _obstack_newchunk (__o, __len); \
273 (void) 0; })
275 # define obstack_empty_p(OBSTACK) \
276 __extension__ \
277 ({ struct obstack const *__o = (OBSTACK); \
278 (__o->chunk->prev == 0 && __o->next_free - __o->chunk->contents == 0); })
280 # define obstack_grow(OBSTACK,where,length) \
281 __extension__ \
282 ({ struct obstack *__o = (OBSTACK); \
283 int __len = (length); \
284 if (__o->next_free + __len > __o->chunk_limit) \
285 _obstack_newchunk (__o, __len); \
286 memcpy (__o->next_free, where, __len); \
287 __o->next_free += __len; \
288 (void) 0; })
290 # define obstack_grow0(OBSTACK,where,length) \
291 __extension__ \
292 ({ struct obstack *__o = (OBSTACK); \
293 int __len = (length); \
294 if (__o->next_free + __len + 1 > __o->chunk_limit) \
295 _obstack_newchunk (__o, __len + 1); \
296 memcpy (__o->next_free, where, __len); \
297 __o->next_free += __len; \
298 *(__o->next_free)++ = 0; \
299 (void) 0; })
301 # define obstack_1grow(OBSTACK,datum) \
302 __extension__ \
303 ({ struct obstack *__o = (OBSTACK); \
304 if (__o->next_free + 1 > __o->chunk_limit) \
305 _obstack_newchunk (__o, 1); \
306 obstack_1grow_fast (__o, datum); \
307 (void) 0; })
309 /* These assume that the obstack alignment is good enough for pointers
310 or ints, and that the data added so far to the current object
311 shares that much alignment. */
313 # define obstack_ptr_grow(OBSTACK,datum) \
314 __extension__ \
315 ({ struct obstack *__o = (OBSTACK); \
316 if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
317 _obstack_newchunk (__o, sizeof (void *)); \
318 obstack_ptr_grow_fast (__o, datum); }) \
320 # define obstack_int_grow(OBSTACK,datum) \
321 __extension__ \
322 ({ struct obstack *__o = (OBSTACK); \
323 if (__o->next_free + sizeof (int) > __o->chunk_limit) \
324 _obstack_newchunk (__o, sizeof (int)); \
325 obstack_int_grow_fast (__o, datum); })
327 # define obstack_ptr_grow_fast(OBSTACK,aptr) \
328 __extension__ \
329 ({ struct obstack *__o1 = (OBSTACK); \
330 *(const void **) __o1->next_free = (aptr); \
331 __o1->next_free += sizeof (const void *); \
332 (void) 0; })
334 # define obstack_int_grow_fast(OBSTACK,aint) \
335 __extension__ \
336 ({ struct obstack *__o1 = (OBSTACK); \
337 *(int *) __o1->next_free = (aint); \
338 __o1->next_free += sizeof (int); \
339 (void) 0; })
341 # define obstack_blank(OBSTACK,length) \
342 __extension__ \
343 ({ struct obstack *__o = (OBSTACK); \
344 int __len = (length); \
345 if (__o->chunk_limit - __o->next_free < __len) \
346 _obstack_newchunk (__o, __len); \
347 obstack_blank_fast (__o, __len); \
348 (void) 0; })
350 # define obstack_alloc(OBSTACK,length) \
351 __extension__ \
352 ({ struct obstack *__h = (OBSTACK); \
353 obstack_blank (__h, (length)); \
354 obstack_finish (__h); })
356 # define obstack_copy(OBSTACK,where,length) \
357 __extension__ \
358 ({ struct obstack *__h = (OBSTACK); \
359 obstack_grow (__h, (where), (length)); \
360 obstack_finish (__h); })
362 # define obstack_copy0(OBSTACK,where,length) \
363 __extension__ \
364 ({ struct obstack *__h = (OBSTACK); \
365 obstack_grow0 (__h, (where), (length)); \
366 obstack_finish (__h); })
368 /* The local variable is named __o1 to avoid a name conflict
369 when obstack_blank is called. */
370 # define obstack_finish(OBSTACK) \
371 __extension__ \
372 ({ struct obstack *__o1 = (OBSTACK); \
373 void *__value = (void *) __o1->object_base; \
374 if (__o1->next_free == __value) \
375 __o1->maybe_empty_object = 1; \
376 __o1->next_free \
377 = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)\
378 & ~ (__o1->alignment_mask)); \
379 if (__o1->next_free - (char *)__o1->chunk \
380 > __o1->chunk_limit - (char *)__o1->chunk) \
381 __o1->next_free = __o1->chunk_limit; \
382 __o1->object_base = __o1->next_free; \
383 __value; })
385 # define obstack_free(OBSTACK, OBJ) \
386 __extension__ \
387 ({ struct obstack *__o = (OBSTACK); \
388 void *__obj = (OBJ); \
389 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
390 __o->next_free = __o->object_base = (char *)__obj; \
391 else (obstack_free) (__o, __obj); })
393 #else /* not __GNUC__ or not __STDC__ */
395 # define obstack_object_size(h) \
396 (unsigned) ((h)->next_free - (h)->object_base)
398 # define obstack_room(h) \
399 (unsigned) ((h)->chunk_limit - (h)->next_free)
401 # define obstack_empty_p(h) \
402 ((h)->chunk->prev == 0 && (h)->next_free - (h)->chunk->contents == 0)
404 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
405 so that we can avoid having void expressions
406 in the arms of the conditional expression.
407 Casting the third operand to void was tried before,
408 but some compilers won't accept it. */
410 # define obstack_make_room(h,length) \
411 ( (h)->temp = (length), \
412 (((h)->next_free + (h)->temp > (h)->chunk_limit) \
413 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0))
415 # define obstack_grow(h,where,length) \
416 ( (h)->temp = (length), \
417 (((h)->next_free + (h)->temp > (h)->chunk_limit) \
418 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
419 memcpy ((h)->next_free, where, (h)->temp), \
420 (h)->next_free += (h)->temp)
422 # define obstack_grow0(h,where,length) \
423 ( (h)->temp = (length), \
424 (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \
425 ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0), \
426 memcpy ((h)->next_free, where, (h)->temp), \
427 (h)->next_free += (h)->temp, \
428 *((h)->next_free)++ = 0)
430 # define obstack_1grow(h,datum) \
431 ( (((h)->next_free + 1 > (h)->chunk_limit) \
432 ? (_obstack_newchunk ((h), 1), 0) : 0), \
433 obstack_1grow_fast (h, datum))
435 # define obstack_ptr_grow(h,datum) \
436 ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
437 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
438 obstack_ptr_grow_fast (h, datum))
440 # define obstack_int_grow(h,datum) \
441 ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
442 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
443 obstack_int_grow_fast (h, datum))
445 # define obstack_ptr_grow_fast(h,aptr) \
446 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
448 # define obstack_int_grow_fast(h,aint) \
449 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr))
451 # define obstack_blank(h,length) \
452 ( (h)->temp = (length), \
453 (((h)->chunk_limit - (h)->next_free < (h)->temp) \
454 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
455 obstack_blank_fast (h, (h)->temp))
457 # define obstack_alloc(h,length) \
458 (obstack_blank ((h), (length)), obstack_finish ((h)))
460 # define obstack_copy(h,where,length) \
461 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
463 # define obstack_copy0(h,where,length) \
464 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
466 # define obstack_finish(h) \
467 ( ((h)->next_free == (h)->object_base \
468 ? (((h)->maybe_empty_object = 1), 0) \
469 : 0), \
470 (h)->temp = __PTR_TO_INT ((h)->object_base), \
471 (h)->next_free \
472 = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \
473 & ~ ((h)->alignment_mask)), \
474 (((h)->next_free - (char *) (h)->chunk \
475 > (h)->chunk_limit - (char *) (h)->chunk) \
476 ? ((h)->next_free = (h)->chunk_limit) : 0), \
477 (h)->object_base = (h)->next_free, \
478 (void *) __INT_TO_PTR ((h)->temp))
480 # define obstack_free(h,obj) \
481 ( (h)->temp = (char *) (obj) - (char *) (h)->chunk, \
482 (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
483 ? (int) ((h)->next_free = (h)->object_base \
484 = (h)->temp + (char *) (h)->chunk) \
485 : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0)))
487 #endif /* not __GNUC__ or not __STDC__ */
489 #ifdef __cplusplus
490 } /* C++ */
491 #endif
493 #endif /* obstack.h */