Make it possible to rename obstack_free.
[gnulib.git] / lib / obstack.h
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 \f
112 /* We need the type of a pointer subtraction.  If __PTRDIFF_TYPE__ is
113    defined, as with GNU C, use that; that way we don't pollute the
114    namespace with <stddef.h>'s symbols.  Otherwise, include <stddef.h>
115    and use ptrdiff_t.  */
116
117 #ifdef __PTRDIFF_TYPE__
118 # define PTR_INT_TYPE __PTRDIFF_TYPE__
119 #else
120 # include <stddef.h>
121 # define PTR_INT_TYPE ptrdiff_t
122 #endif
123
124 /* If B is the base of an object addressed by P, return the result of
125    aligning P to the next multiple of A + 1.  B and P must be of type
126    char *.  A + 1 must be a power of 2.  */
127
128 #define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
129
130 /* Similiar to _BPTR_ALIGN (B, P, A), except optimize the common case
131    where pointers can be converted to integers, aligned as integers,
132    and converted back again.  If PTR_INT_TYPE is narrower than a
133    pointer (e.g., the AS/400), play it safe and compute the alignment
134    relative to B.  Otherwise, use the faster strategy of computing the
135    alignment relative to 0.  */
136
137 #define __PTR_ALIGN(B, P, A)                                                \
138   __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
139                 P, A)
140
141 #include <string.h>
142
143 struct _obstack_chunk           /* Lives at front of each chunk. */
144 {
145   char  *limit;                 /* 1 past end of this chunk */
146   struct _obstack_chunk *prev;  /* address of prior chunk or NULL */
147   char  contents[4];            /* objects begin here */
148 };
149
150 struct obstack          /* control current object in current chunk */
151 {
152   long  chunk_size;             /* preferred size to allocate chunks in */
153   struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
154   char  *object_base;           /* address of object we are building */
155   char  *next_free;             /* where to add next char to current object */
156   char  *chunk_limit;           /* address of char after current chunk */
157   union
158   {
159     PTR_INT_TYPE tempint;
160     void *tempptr;
161   } temp;                       /* Temporary for some macros.  */
162   int   alignment_mask;         /* Mask of alignment for each object. */
163   /* These prototypes vary based on `use_extra_arg', and we use
164      casts to the prototypeless function type in all assignments,
165      but having prototypes here quiets -Wstrict-prototypes.  */
166   struct _obstack_chunk *(*chunkfun) (void *, long);
167   void (*freefun) (void *, struct _obstack_chunk *);
168   void *extra_arg;              /* first arg for chunk alloc/dealloc funcs */
169   unsigned use_extra_arg:1;     /* chunk alloc/dealloc funcs take extra arg */
170   unsigned maybe_empty_object:1;/* There is a possibility that the current
171                                    chunk contains a zero-length object.  This
172                                    prevents freeing the chunk if we allocate
173                                    a bigger chunk to replace it. */
174   unsigned alloc_failed:1;      /* No longer used, as we now call the failed
175                                    handler on error, but retained for binary
176                                    compatibility.  */
177 };
178
179 /* Declare the external functions we use; they are in obstack.c.  */
180
181 extern void _obstack_newchunk (struct obstack *, int);
182 extern int _obstack_begin (struct obstack *, int, int,
183                             void *(*) (long), void (*) (void *));
184 extern int _obstack_begin_1 (struct obstack *, int, int,
185                              void *(*) (void *, long),
186                              void (*) (void *, void *), void *);
187 extern int _obstack_memory_used (struct obstack *);
188
189 /* The default name of the function for freeing a chunk is 'obstack_free',
190    but gnulib users can override this by defining '__obstack_free'.  */
191 #ifndef __obstack_free
192 # define __obstack_free obstack_free
193 #endif
194 extern void __obstack_free (struct obstack *obstack, void *block);
195
196 \f
197 /* Error handler called when `obstack_chunk_alloc' failed to allocate
198    more memory.  This can be set to a user defined function which
199    should either abort gracefully or use longjump - but shouldn't
200    return.  The default action is to print a message and abort.  */
201 extern void (*obstack_alloc_failed_handler) (void);
202
203 /* Exit value used when `print_and_abort' is used.  */
204 extern int obstack_exit_failure;
205 \f
206 /* Pointer to beginning of object being allocated or to be allocated next.
207    Note that this might not be the final address of the object
208    because a new chunk might be needed to hold the final size.  */
209
210 #define obstack_base(h) ((void *) (h)->object_base)
211
212 /* Size for allocating ordinary chunks.  */
213
214 #define obstack_chunk_size(h) ((h)->chunk_size)
215
216 /* Pointer to next byte not yet allocated in current chunk.  */
217
218 #define obstack_next_free(h)    ((h)->next_free)
219
220 /* Mask specifying low bits that should be clear in address of an object.  */
221
222 #define obstack_alignment_mask(h) ((h)->alignment_mask)
223
224 /* To prevent prototype warnings provide complete argument list.  */
225 #define obstack_init(h)                                         \
226   _obstack_begin ((h), 0, 0,                                    \
227                   (void *(*) (long)) obstack_chunk_alloc,       \
228                   (void (*) (void *)) obstack_chunk_free)
229
230 #define obstack_begin(h, size)                                  \
231   _obstack_begin ((h), (size), 0,                               \
232                   (void *(*) (long)) obstack_chunk_alloc,       \
233                   (void (*) (void *)) obstack_chunk_free)
234
235 #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun)  \
236   _obstack_begin ((h), (size), (alignment),                                \
237                   (void *(*) (long)) (chunkfun),                           \
238                   (void (*) (void *)) (freefun))
239
240 #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
241   _obstack_begin_1 ((h), (size), (alignment),                           \
242                     (void *(*) (void *, long)) (chunkfun),              \
243                     (void (*) (void *, void *)) (freefun), (arg))
244
245 #define obstack_chunkfun(h, newchunkfun) \
246   ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
247
248 #define obstack_freefun(h, newfreefun) \
249   ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
250
251 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
252
253 #define obstack_blank_fast(h,n) ((h)->next_free += (n))
254
255 #define obstack_memory_used(h) _obstack_memory_used (h)
256 \f
257 #if defined __GNUC__ && defined __STDC__ && __STDC__
258 /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
259    does not implement __extension__.  But that compiler doesn't define
260    __GNUC_MINOR__.  */
261 # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
262 #  define __extension__
263 # endif
264
265 /* For GNU C, if not -traditional,
266    we can define these macros to compute all args only once
267    without using a global variable.
268    Also, we can avoid using the `temp' slot, to make faster code.  */
269
270 # define obstack_object_size(OBSTACK)                                   \
271   __extension__                                                         \
272   ({ struct obstack const *__o = (OBSTACK);                             \
273      (unsigned) (__o->next_free - __o->object_base); })
274
275 # define obstack_room(OBSTACK)                                          \
276   __extension__                                                         \
277   ({ struct obstack const *__o = (OBSTACK);                             \
278      (unsigned) (__o->chunk_limit - __o->next_free); })
279
280 # define obstack_make_room(OBSTACK,length)                              \
281 __extension__                                                           \
282 ({ struct obstack *__o = (OBSTACK);                                     \
283    int __len = (length);                                                \
284    if (__o->chunk_limit - __o->next_free < __len)                       \
285      _obstack_newchunk (__o, __len);                                    \
286    (void) 0; })
287
288 # define obstack_empty_p(OBSTACK)                                       \
289   __extension__                                                         \
290   ({ struct obstack const *__o = (OBSTACK);                             \
291      (__o->chunk->prev == 0                                             \
292       && __o->next_free == __PTR_ALIGN ((char *) __o->chunk,            \
293                                         __o->chunk->contents,           \
294                                         __o->alignment_mask)); })
295
296 # define obstack_grow(OBSTACK,where,length)                             \
297 __extension__                                                           \
298 ({ struct obstack *__o = (OBSTACK);                                     \
299    int __len = (length);                                                \
300    if (__o->next_free + __len > __o->chunk_limit)                       \
301      _obstack_newchunk (__o, __len);                                    \
302    memcpy (__o->next_free, where, __len);                               \
303    __o->next_free += __len;                                             \
304    (void) 0; })
305
306 # define obstack_grow0(OBSTACK,where,length)                            \
307 __extension__                                                           \
308 ({ struct obstack *__o = (OBSTACK);                                     \
309    int __len = (length);                                                \
310    if (__o->next_free + __len + 1 > __o->chunk_limit)                   \
311      _obstack_newchunk (__o, __len + 1);                                \
312    memcpy (__o->next_free, where, __len);                               \
313    __o->next_free += __len;                                             \
314    *(__o->next_free)++ = 0;                                             \
315    (void) 0; })
316
317 # define obstack_1grow(OBSTACK,datum)                                   \
318 __extension__                                                           \
319 ({ struct obstack *__o = (OBSTACK);                                     \
320    if (__o->next_free + 1 > __o->chunk_limit)                           \
321      _obstack_newchunk (__o, 1);                                        \
322    obstack_1grow_fast (__o, datum);                                     \
323    (void) 0; })
324
325 /* These assume that the obstack alignment is good enough for pointers
326    or ints, and that the data added so far to the current object
327    shares that much alignment.  */
328
329 # define obstack_ptr_grow(OBSTACK,datum)                                \
330 __extension__                                                           \
331 ({ struct obstack *__o = (OBSTACK);                                     \
332    if (__o->next_free + sizeof (void *) > __o->chunk_limit)             \
333      _obstack_newchunk (__o, sizeof (void *));                          \
334    obstack_ptr_grow_fast (__o, datum); })                               \
335
336 # define obstack_int_grow(OBSTACK,datum)                                \
337 __extension__                                                           \
338 ({ struct obstack *__o = (OBSTACK);                                     \
339    if (__o->next_free + sizeof (int) > __o->chunk_limit)                \
340      _obstack_newchunk (__o, sizeof (int));                             \
341    obstack_int_grow_fast (__o, datum); })
342
343 # define obstack_ptr_grow_fast(OBSTACK,aptr)                            \
344 __extension__                                                           \
345 ({ struct obstack *__o1 = (OBSTACK);                                    \
346    *(const void **) __o1->next_free = (aptr);                           \
347    __o1->next_free += sizeof (const void *);                            \
348    (void) 0; })
349
350 # define obstack_int_grow_fast(OBSTACK,aint)                            \
351 __extension__                                                           \
352 ({ struct obstack *__o1 = (OBSTACK);                                    \
353    *(int *) __o1->next_free = (aint);                                   \
354    __o1->next_free += sizeof (int);                                     \
355    (void) 0; })
356
357 # define obstack_blank(OBSTACK,length)                                  \
358 __extension__                                                           \
359 ({ struct obstack *__o = (OBSTACK);                                     \
360    int __len = (length);                                                \
361    if (__o->chunk_limit - __o->next_free < __len)                       \
362      _obstack_newchunk (__o, __len);                                    \
363    obstack_blank_fast (__o, __len);                                     \
364    (void) 0; })
365
366 # define obstack_alloc(OBSTACK,length)                                  \
367 __extension__                                                           \
368 ({ struct obstack *__h = (OBSTACK);                                     \
369    obstack_blank (__h, (length));                                       \
370    obstack_finish (__h); })
371
372 # define obstack_copy(OBSTACK,where,length)                             \
373 __extension__                                                           \
374 ({ struct obstack *__h = (OBSTACK);                                     \
375    obstack_grow (__h, (where), (length));                               \
376    obstack_finish (__h); })
377
378 # define obstack_copy0(OBSTACK,where,length)                            \
379 __extension__                                                           \
380 ({ struct obstack *__h = (OBSTACK);                                     \
381    obstack_grow0 (__h, (where), (length));                              \
382    obstack_finish (__h); })
383
384 /* The local variable is named __o1 to avoid a name conflict
385    when obstack_blank is called.  */
386 # define obstack_finish(OBSTACK)                                        \
387 __extension__                                                           \
388 ({ struct obstack *__o1 = (OBSTACK);                                    \
389    void *__value = (void *) __o1->object_base;                          \
390    if (__o1->next_free == __value)                                      \
391      __o1->maybe_empty_object = 1;                                      \
392    __o1->next_free                                                      \
393      = __PTR_ALIGN (__o1->object_base, __o1->next_free,                 \
394                     __o1->alignment_mask);                              \
395    if (__o1->next_free - (char *)__o1->chunk                            \
396        > __o1->chunk_limit - (char *)__o1->chunk)                       \
397      __o1->next_free = __o1->chunk_limit;                               \
398    __o1->object_base = __o1->next_free;                                 \
399    __value; })
400
401 # define obstack_free(OBSTACK, OBJ)                                     \
402 __extension__                                                           \
403 ({ struct obstack *__o = (OBSTACK);                                     \
404    void *__obj = (OBJ);                                                 \
405    if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit)  \
406      __o->next_free = __o->object_base = (char *)__obj;                 \
407    else (__obstack_free) (__o, __obj); })
408 \f
409 #else /* not __GNUC__ or not __STDC__ */
410
411 # define obstack_object_size(h) \
412  (unsigned) ((h)->next_free - (h)->object_base)
413
414 # define obstack_room(h)                \
415  (unsigned) ((h)->chunk_limit - (h)->next_free)
416
417 # define obstack_empty_p(h) \
418  ((h)->chunk->prev == 0                                                 \
419   && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk,                \
420                                     (h)->chunk->contents,               \
421                                     (h)->alignment_mask))
422
423 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
424    so that we can avoid having void expressions
425    in the arms of the conditional expression.
426    Casting the third operand to void was tried before,
427    but some compilers won't accept it.  */
428
429 # define obstack_make_room(h,length)                                    \
430 ( (h)->temp.tempint = (length),                                         \
431   (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit)              \
432    ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
433
434 # define obstack_grow(h,where,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   memcpy ((h)->next_free, where, (h)->temp.tempint),                    \
439   (h)->next_free += (h)->temp.tempint)
440
441 # define obstack_grow0(h,where,length)                                  \
442 ( (h)->temp.tempint = (length),                                         \
443   (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit)          \
444    ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0),          \
445   memcpy ((h)->next_free, where, (h)->temp.tempint),                    \
446   (h)->next_free += (h)->temp.tempint,                                  \
447   *((h)->next_free)++ = 0)
448
449 # define obstack_1grow(h,datum)                                         \
450 ( (((h)->next_free + 1 > (h)->chunk_limit)                              \
451    ? (_obstack_newchunk ((h), 1), 0) : 0),                              \
452   obstack_1grow_fast (h, datum))
453
454 # define obstack_ptr_grow(h,datum)                                      \
455 ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit)                \
456    ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0),                \
457   obstack_ptr_grow_fast (h, datum))
458
459 # define obstack_int_grow(h,datum)                                      \
460 ( (((h)->next_free + sizeof (int) > (h)->chunk_limit)                   \
461    ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0),                   \
462   obstack_int_grow_fast (h, datum))
463
464 # define obstack_ptr_grow_fast(h,aptr)                                  \
465   (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
466
467 # define obstack_int_grow_fast(h,aint)                                  \
468   (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
469
470 # define obstack_blank(h,length)                                        \
471 ( (h)->temp.tempint = (length),                                         \
472   (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint)              \
473    ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0),              \
474   obstack_blank_fast (h, (h)->temp.tempint))
475
476 # define obstack_alloc(h,length)                                        \
477  (obstack_blank ((h), (length)), obstack_finish ((h)))
478
479 # define obstack_copy(h,where,length)                                   \
480  (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
481
482 # define obstack_copy0(h,where,length)                                  \
483  (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
484
485 # define obstack_finish(h)                                              \
486 ( ((h)->next_free == (h)->object_base                                   \
487    ? (((h)->maybe_empty_object = 1), 0)                                 \
488    : 0),                                                                \
489   (h)->temp.tempptr = (h)->object_base,                                 \
490   (h)->next_free                                                        \
491     = __PTR_ALIGN ((h)->object_base, (h)->next_free,                    \
492                    (h)->alignment_mask),                                \
493   (((h)->next_free - (char *) (h)->chunk                                \
494     > (h)->chunk_limit - (char *) (h)->chunk)                           \
495    ? ((h)->next_free = (h)->chunk_limit) : 0),                          \
496   (h)->object_base = (h)->next_free,                                    \
497   (h)->temp.tempptr)
498
499 # define obstack_free(h,obj)                                            \
500 ( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk,             \
501   ((((h)->temp.tempint > 0                                              \
502     && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk))     \
503    ? (int) ((h)->next_free = (h)->object_base                           \
504             = (h)->temp.tempint + (char *) (h)->chunk)                  \
505    : (((__obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0)))
506
507 #endif /* not __GNUC__ or not __STDC__ */
508
509 #ifdef __cplusplus
510 }       /* C++ */
511 #endif
512
513 #endif /* obstack.h */