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