exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / obstack.in.h
blob28fb4d8916dcb8421b2a3684859e372775340a55
1 /* obstack.h - object stack macros
2 Copyright (C) 1988-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Summary:
20 All the apparent functions defined here are macros. The idea
21 is that you would use these pre-tested macros to solve a
22 very specific set of problems, and they would run fast.
23 Caution: no side-effects in arguments please!! They may be
24 evaluated MANY times!!
26 These macros operate a stack of objects. Each object starts life
27 small, and may grow to maturity. (Consider building a word syllable
28 by syllable.) An object can move while it is growing. Once it has
29 been "finished" it never changes address again. So the "top of the
30 stack" is typically an immature growing object, while the rest of the
31 stack is of mature, fixed size and fixed address objects.
33 These routines grab large chunks of memory, using a function you
34 supply, called 'obstack_chunk_alloc'. On occasion, they free chunks,
35 by calling 'obstack_chunk_free'. You must define them and declare
36 them before using any obstack macros.
38 Each independent stack is represented by a 'struct obstack'.
39 Each of the obstack macros expects a pointer to such a structure
40 as the first argument.
42 One motivation for this package is the problem of growing char strings
43 in symbol tables. Unless you are "fascist pig with a read-only mind"
44 --Gosper's immortal quote from HAKMEM item 154, out of context--you
45 would not like to put any arbitrary upper limit on the length of your
46 symbols.
48 In practice this often means you will build many short symbols and a
49 few long symbols. At the time you are reading a symbol you don't know
50 how long it is. One traditional method is to read a symbol into a
51 buffer, realloc()ating the buffer every time you try to read a symbol
52 that is longer than the buffer. This is beaut, but you still will
53 want to copy the symbol from the buffer to a more permanent
54 symbol-table entry say about half the time.
56 With obstacks, you can work differently. Use one obstack for all symbol
57 names. As you read a symbol, grow the name in the obstack gradually.
58 When the name is complete, finalize it. Then, if the symbol exists already,
59 free the newly read name.
61 The way we do this is to take a large chunk, allocating memory from
62 low addresses. When you want to build a symbol in the chunk you just
63 add chars above the current "high water mark" in the chunk. When you
64 have finished adding chars, because you got to the end of the symbol,
65 you know how long the chars are, and you can create a new object.
66 Mostly the chars will not burst over the highest address of the chunk,
67 because you would typically expect a chunk to be (say) 100 times as
68 long as an average object.
70 In case that isn't clear, when we have enough chars to make up
71 the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
72 so we just point to it where it lies. No moving of chars is
73 needed and this is the second win: potentially long strings need
74 never be explicitly shuffled. Once an object is formed, it does not
75 change its address during its lifetime.
77 When the chars burst over a chunk boundary, we allocate a larger
78 chunk, and then copy the partly formed object from the end of the old
79 chunk to the beginning of the new larger chunk. We then carry on
80 accreting characters to the end of the object as we normally would.
82 A special macro is provided to add a single char at a time to a
83 growing object. This allows the use of register variables, which
84 break the ordinary 'growth' macro.
86 Summary:
87 We allocate large chunks.
88 We carve out one object at a time from the current chunk.
89 Once carved, an object never moves.
90 We are free to append data of any size to the currently
91 growing object.
92 Exactly one object is growing in an obstack at any one time.
93 You can run one obstack per control block.
94 You may have as many control blocks as you dare.
95 Because of the way we do it, you can "unwind" an obstack
96 back to a previous state. (You may remove objects much
97 as you would with a stack.)
100 /* Documentation (part of the GNU libc manual):
101 <https://www.gnu.org/software/libc/manual/html_node/Obstacks.html> */
104 /* Don't do the contents of this file more than once. */
105 #ifndef _OBSTACK_H
106 #define _OBSTACK_H 1
108 /* This file uses _Noreturn, _GL_ATTRIBUTE_PURE. */
109 #if !_GL_CONFIG_H_INCLUDED
110 #error "Please include config.h first."
111 #endif
113 #include <stddef.h> /* For size_t and ptrdiff_t. */
114 #include <stdint.h> /* For uintptr_t. */
115 #include <string.h> /* For memcpy. */
117 #if __STDC_VERSION__ < 199901L || defined __HP_cc
118 # define __FLEXIBLE_ARRAY_MEMBER 1
119 #else
120 # define __FLEXIBLE_ARRAY_MEMBER
121 #endif
123 /* These macros highlight the places where this implementation
124 is different from the one in GNU libc. */
125 #ifdef _LIBC
126 # define _OBSTACK_SIZE_T unsigned int
127 # define _CHUNK_SIZE_T unsigned long
128 # define _OBSTACK_CAST(type, expr) ((type) (expr))
129 #else
130 /* In Gnulib, we use sane types, especially for 64-bit hosts. */
131 # define _OBSTACK_SIZE_T size_t
132 # define _CHUNK_SIZE_T size_t
133 # define _OBSTACK_CAST(type, expr) (expr)
134 #endif
136 /* __PTR_ALIGN(B, P, A) returns the result of aligning P to the next multiple
137 of A + 1. B must be the base of an object addressed by P. B and P must be
138 of type char *. A + 1 must be a power of 2.
139 If ptrdiff_t is narrower than a pointer (e.g., the AS/400), play it
140 safe and compute the alignment relative to B. Otherwise, use the
141 faster strategy of computing the alignment through uintptr_t. */
142 #if @SMALL_PTRDIFF_T@
143 # define __PTR_ALIGN(B, P, A) \
144 ((B) + (((P) - (B) + (A)) & ~(A)))
145 #else
146 # define __PTR_ALIGN(B, P, A) \
147 ((P) + ((- (uintptr_t) (P)) & (A)))
148 #endif
150 #ifndef __attribute_pure__
151 # define __attribute_pure__ _GL_ATTRIBUTE_PURE
152 #endif
154 /* Not the same as _Noreturn, since it also works with function pointers. */
155 #ifndef __attribute_noreturn__
156 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__) || defined __clang__ || 0x5110 <= __SUNPRO_C
157 # define __attribute_noreturn__ __attribute__ ((__noreturn__))
158 # else
159 # define __attribute_noreturn__
160 # endif
161 #endif
163 #ifdef __cplusplus
164 extern "C" {
165 #endif
167 struct _obstack_chunk /* Lives at front of each chunk. */
169 char *limit; /* 1 past end of this chunk */
170 struct _obstack_chunk *prev; /* address of prior chunk or NULL */
171 char contents[__FLEXIBLE_ARRAY_MEMBER]; /* objects begin here */
174 struct obstack /* control current object in current chunk */
176 _CHUNK_SIZE_T chunk_size; /* preferred size to allocate chunks in */
177 struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
178 char *object_base; /* address of object we are building */
179 char *next_free; /* where to add next char to current object */
180 char *chunk_limit; /* address of char after current chunk */
181 union
183 _OBSTACK_SIZE_T i;
184 void *p;
185 } temp; /* Temporary for some macros. */
186 _OBSTACK_SIZE_T alignment_mask; /* Mask of alignment for each object. */
188 /* These prototypes vary based on 'use_extra_arg'. */
189 union
191 void *(*plain) (size_t);
192 void *(*extra) (void *, size_t);
193 } chunkfun;
194 union
196 void (*plain) (void *);
197 void (*extra) (void *, void *);
198 } freefun;
200 void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
201 unsigned use_extra_arg : 1; /* chunk alloc/dealloc funcs take extra arg */
202 unsigned maybe_empty_object : 1; /* There is a possibility that the current
203 chunk contains a zero-length object. This
204 prevents freeing the chunk if we allocate
205 a bigger chunk to replace it. */
206 unsigned alloc_failed : 1; /* No longer used, as we now call the failed
207 handler on error, but retained for binary
208 compatibility. */
211 /* Declare the external functions we use; they are in obstack.c. */
213 #if @REPLACE_OBSTACK@
214 # define _obstack_newchunk rpl_obstack_newchunk
215 # define _obstack_free rpl_obstack_free
216 # define _obstack_begin rpl_obstack_begin
217 # define _obstack_begin_1 rpl_obstack_begin_1
218 # define _obstack_memory_used rpl_obstack_memory_used
219 # define _obstack_allocated_p rpl_obstack_allocated_p
220 #endif
221 extern void _obstack_newchunk (struct obstack *, _OBSTACK_SIZE_T);
222 extern void _obstack_free (struct obstack *, void *);
223 extern int _obstack_begin (struct obstack *,
224 _OBSTACK_SIZE_T, _OBSTACK_SIZE_T,
225 void *(*) (size_t), void (*) (void *));
226 extern int _obstack_begin_1 (struct obstack *,
227 _OBSTACK_SIZE_T, _OBSTACK_SIZE_T,
228 void *(*) (void *, size_t),
229 void (*) (void *, void *), void *);
230 extern _OBSTACK_SIZE_T _obstack_memory_used (struct obstack *)
231 __attribute_pure__;
234 /* Error handler called when 'obstack_chunk_alloc' failed to allocate
235 more memory. This can be set to a user defined function which
236 should either abort gracefully or use longjump - but shouldn't
237 return. The default action is to print a message and abort. */
238 extern __attribute_noreturn__ void (*obstack_alloc_failed_handler) (void);
240 /* Exit value used when 'print_and_abort' is used. */
241 extern int obstack_exit_failure;
243 /* Pointer to beginning of object being allocated or to be allocated next.
244 Note that this might not be the final address of the object
245 because a new chunk might be needed to hold the final size. */
247 #define obstack_base(h) ((void *) (h)->object_base)
249 /* Size for allocating ordinary chunks. */
251 #define obstack_chunk_size(h) ((h)->chunk_size)
253 /* Pointer to next byte not yet allocated in current chunk. */
255 #define obstack_next_free(h) ((void *) (h)->next_free)
257 /* Mask specifying low bits that should be clear in address of an object. */
259 #define obstack_alignment_mask(h) ((h)->alignment_mask)
261 /* To prevent prototype warnings provide complete argument list. */
262 #define obstack_init(h) \
263 _obstack_begin ((h), 0, 0, \
264 _OBSTACK_CAST (void *(*) (size_t), obstack_chunk_alloc), \
265 _OBSTACK_CAST (void (*) (void *), obstack_chunk_free))
267 #define obstack_begin(h, size) \
268 _obstack_begin ((h), (size), 0, \
269 _OBSTACK_CAST (void *(*) (size_t), obstack_chunk_alloc), \
270 _OBSTACK_CAST (void (*) (void *), obstack_chunk_free))
272 #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
273 _obstack_begin ((h), (size), (alignment), \
274 _OBSTACK_CAST (void *(*) (size_t), chunkfun), \
275 _OBSTACK_CAST (void (*) (void *), freefun))
277 #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
278 _obstack_begin_1 ((h), (size), (alignment), \
279 _OBSTACK_CAST (void *(*) (void *, size_t), chunkfun), \
280 _OBSTACK_CAST (void (*) (void *, void *), freefun), arg)
282 #define obstack_chunkfun(h, newchunkfun) \
283 ((void) ((h)->chunkfun.extra = (void *(*) (void *, size_t)) (newchunkfun)))
285 #define obstack_freefun(h, newfreefun) \
286 ((void) ((h)->freefun.extra = (void *(*) (void *, void *)) (newfreefun)))
288 #define obstack_1grow_fast(h, achar) ((void) (*((h)->next_free)++ = (achar)))
290 #define obstack_blank_fast(h, n) ((void) ((h)->next_free += (n)))
292 #define obstack_memory_used(h) _obstack_memory_used (h)
294 #if defined __GNUC__ || defined __clang__
295 # if !(defined __GNUC_MINOR__ && __GNUC__ * 1000 + __GNUC_MINOR__ >= 2008 \
296 || defined __clang__)
297 # define __extension__
298 # endif
300 /* For GNU C, if not -traditional,
301 we can define these macros to compute all args only once
302 without using a global variable.
303 Also, we can avoid using the 'temp' slot, to make faster code. */
305 # define obstack_object_size(OBSTACK) \
306 __extension__ \
307 ({ struct obstack const *__o = (OBSTACK); \
308 (_OBSTACK_SIZE_T) (__o->next_free - __o->object_base); })
310 /* The local variable is named __o1 to avoid a shadowed variable
311 warning when invoked from other obstack macros. */
312 # define obstack_room(OBSTACK) \
313 __extension__ \
314 ({ struct obstack const *__o1 = (OBSTACK); \
315 (_OBSTACK_SIZE_T) (__o1->chunk_limit - __o1->next_free); })
317 # define obstack_make_room(OBSTACK, length) \
318 __extension__ \
319 ({ struct obstack *__o = (OBSTACK); \
320 _OBSTACK_SIZE_T __len = (length); \
321 if (obstack_room (__o) < __len) \
322 _obstack_newchunk (__o, __len); \
323 (void) 0; })
325 # define obstack_empty_p(OBSTACK) \
326 __extension__ \
327 ({ struct obstack const *__o = (OBSTACK); \
328 (__o->chunk->prev == 0 \
329 && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
330 __o->chunk->contents, \
331 __o->alignment_mask)); })
333 # define obstack_grow(OBSTACK, where, length) \
334 __extension__ \
335 ({ struct obstack *__o = (OBSTACK); \
336 _OBSTACK_SIZE_T __len = (length); \
337 if (obstack_room (__o) < __len) \
338 _obstack_newchunk (__o, __len); \
339 memcpy (__o->next_free, where, __len); \
340 __o->next_free += __len; \
341 (void) 0; })
343 # define obstack_grow0(OBSTACK, where, length) \
344 __extension__ \
345 ({ struct obstack *__o = (OBSTACK); \
346 _OBSTACK_SIZE_T __len = (length); \
347 if (obstack_room (__o) < __len + 1) \
348 _obstack_newchunk (__o, __len + 1); \
349 memcpy (__o->next_free, where, __len); \
350 __o->next_free += __len; \
351 *(__o->next_free)++ = 0; \
352 (void) 0; })
354 # define obstack_1grow(OBSTACK, datum) \
355 __extension__ \
356 ({ struct obstack *__o = (OBSTACK); \
357 if (obstack_room (__o) < 1) \
358 _obstack_newchunk (__o, 1); \
359 obstack_1grow_fast (__o, datum); })
361 /* These assume that the obstack alignment is good enough for pointers
362 or ints, and that the data added so far to the current object
363 shares that much alignment. */
365 # define obstack_ptr_grow(OBSTACK, datum) \
366 __extension__ \
367 ({ struct obstack *__o = (OBSTACK); \
368 if (obstack_room (__o) < sizeof (void *)) \
369 _obstack_newchunk (__o, sizeof (void *)); \
370 obstack_ptr_grow_fast (__o, datum); })
372 # define obstack_int_grow(OBSTACK, datum) \
373 __extension__ \
374 ({ struct obstack *__o = (OBSTACK); \
375 if (obstack_room (__o) < sizeof (int)) \
376 _obstack_newchunk (__o, sizeof (int)); \
377 obstack_int_grow_fast (__o, datum); })
379 # define obstack_ptr_grow_fast(OBSTACK, aptr) \
380 __extension__ \
381 ({ struct obstack *__o1 = (OBSTACK); \
382 void *__p1 = __o1->next_free; \
383 *(const void **) __p1 = (aptr); \
384 __o1->next_free += sizeof (const void *); \
385 (void) 0; })
387 # define obstack_int_grow_fast(OBSTACK, aint) \
388 __extension__ \
389 ({ struct obstack *__o1 = (OBSTACK); \
390 void *__p1 = __o1->next_free; \
391 *(int *) __p1 = (aint); \
392 __o1->next_free += sizeof (int); \
393 (void) 0; })
395 # define obstack_blank(OBSTACK, length) \
396 __extension__ \
397 ({ struct obstack *__o = (OBSTACK); \
398 _OBSTACK_SIZE_T __len = (length); \
399 if (obstack_room (__o) < __len) \
400 _obstack_newchunk (__o, __len); \
401 obstack_blank_fast (__o, __len); })
403 # define obstack_alloc(OBSTACK, length) \
404 __extension__ \
405 ({ struct obstack *__h = (OBSTACK); \
406 obstack_blank (__h, (length)); \
407 obstack_finish (__h); })
409 # define obstack_copy(OBSTACK, where, length) \
410 __extension__ \
411 ({ struct obstack *__h = (OBSTACK); \
412 obstack_grow (__h, (where), (length)); \
413 obstack_finish (__h); })
415 # define obstack_copy0(OBSTACK, where, length) \
416 __extension__ \
417 ({ struct obstack *__h = (OBSTACK); \
418 obstack_grow0 (__h, (where), (length)); \
419 obstack_finish (__h); })
421 /* The local variable is named __o1 to avoid a shadowed variable
422 warning when invoked from other obstack macros, typically obstack_free. */
423 # define obstack_finish(OBSTACK) \
424 __extension__ \
425 ({ struct obstack *__o1 = (OBSTACK); \
426 void *__value = (void *) __o1->object_base; \
427 if (__o1->next_free == __value) \
428 __o1->maybe_empty_object = 1; \
429 __o1->next_free \
430 = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
431 __o1->alignment_mask); \
432 if ((size_t) (__o1->next_free - (char *) __o1->chunk) \
433 > (size_t) (__o1->chunk_limit - (char *) __o1->chunk)) \
434 __o1->next_free = __o1->chunk_limit; \
435 __o1->object_base = __o1->next_free; \
436 __value; })
438 # define obstack_free(OBSTACK, OBJ) \
439 __extension__ \
440 ({ struct obstack *__o = (OBSTACK); \
441 void *__obj = (void *) (OBJ); \
442 if (__obj > (void *) __o->chunk && __obj < (void *) __o->chunk_limit) \
443 __o->next_free = __o->object_base = (char *) __obj; \
444 else \
445 _obstack_free (__o, __obj); })
447 #else /* not __GNUC__ */
449 # define obstack_object_size(h) \
450 ((_OBSTACK_SIZE_T) ((h)->next_free - (h)->object_base))
452 # define obstack_room(h) \
453 ((_OBSTACK_SIZE_T) ((h)->chunk_limit - (h)->next_free))
455 # define obstack_empty_p(h) \
456 ((h)->chunk->prev == 0 \
457 && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
458 (h)->chunk->contents, \
459 (h)->alignment_mask))
461 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
462 so that we can avoid having void expressions
463 in the arms of the conditional expression.
464 Casting the third operand to void was tried before,
465 but some compilers won't accept it. */
467 # define obstack_make_room(h, length) \
468 ((h)->temp.i = (length), \
469 ((obstack_room (h) < (h)->temp.i) \
470 ? (_obstack_newchunk (h, (h)->temp.i), 0) : 0), \
471 (void) 0)
473 # define obstack_grow(h, where, length) \
474 ((h)->temp.i = (length), \
475 ((obstack_room (h) < (h)->temp.i) \
476 ? (_obstack_newchunk ((h), (h)->temp.i), 0) : 0), \
477 memcpy ((h)->next_free, where, (h)->temp.i), \
478 (h)->next_free += (h)->temp.i, \
479 (void) 0)
481 # define obstack_grow0(h, where, length) \
482 ((h)->temp.i = (length), \
483 ((obstack_room (h) < (h)->temp.i + 1) \
484 ? (_obstack_newchunk ((h), (h)->temp.i + 1), 0) : 0), \
485 memcpy ((h)->next_free, where, (h)->temp.i), \
486 (h)->next_free += (h)->temp.i, \
487 *((h)->next_free)++ = 0, \
488 (void) 0)
490 # define obstack_1grow(h, datum) \
491 (((obstack_room (h) < 1) \
492 ? (_obstack_newchunk ((h), 1), 0) : 0), \
493 obstack_1grow_fast (h, datum))
495 # define obstack_ptr_grow(h, datum) \
496 (((obstack_room (h) < sizeof (char *)) \
497 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
498 obstack_ptr_grow_fast (h, datum))
500 # define obstack_int_grow(h, datum) \
501 (((obstack_room (h) < sizeof (int)) \
502 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
503 obstack_int_grow_fast (h, datum))
505 # define obstack_ptr_grow_fast(h, aptr) \
506 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr), \
507 (void) 0)
509 # define obstack_int_grow_fast(h, aint) \
510 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint), \
511 (void) 0)
513 # define obstack_blank(h, length) \
514 ((h)->temp.i = (length), \
515 ((obstack_room (h) < (h)->temp.i) \
516 ? (_obstack_newchunk ((h), (h)->temp.i), 0) : 0), \
517 obstack_blank_fast (h, (h)->temp.i))
519 # define obstack_alloc(h, length) \
520 (obstack_blank ((h), (length)), obstack_finish ((h)))
522 # define obstack_copy(h, where, length) \
523 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
525 # define obstack_copy0(h, where, length) \
526 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
528 # define obstack_finish(h) \
529 (((h)->next_free == (h)->object_base \
530 ? (((h)->maybe_empty_object = 1), 0) \
531 : 0), \
532 (h)->temp.p = (h)->object_base, \
533 (h)->next_free \
534 = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
535 (h)->alignment_mask), \
536 (((size_t) ((h)->next_free - (char *) (h)->chunk) \
537 > (size_t) ((h)->chunk_limit - (char *) (h)->chunk)) \
538 ? ((h)->next_free = (h)->chunk_limit) : 0), \
539 (h)->object_base = (h)->next_free, \
540 (h)->temp.p)
542 # define obstack_free(h, obj) \
543 ((h)->temp.p = (void *) (obj), \
544 (((h)->temp.p > (void *) (h)->chunk \
545 && (h)->temp.p < (void *) (h)->chunk_limit) \
546 ? (void) ((h)->next_free = (h)->object_base = (char *) (h)->temp.p) \
547 : _obstack_free ((h), (h)->temp.p)))
549 #endif /* not __GNUC__ */
551 #ifdef __cplusplus
552 } /* C++ */
553 #endif
555 #endif /* _OBSTACK_H */