timevar: import from Bison.
[gnulib.git] / lib / obstack.c
blobc85483f72bd8b2e90aa055a28acb158f9fbfaf28
1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 #ifdef _LIBC
21 # include <obstack.h>
22 #else
23 # include <config.h>
24 # include "obstack.h"
25 #endif
27 /* NOTE BEFORE MODIFYING THIS FILE: _OBSTACK_INTERFACE_VERSION in
28 obstack.h must be incremented whenever callers compiled using an old
29 obstack.h can no longer properly call the functions in this file. */
31 /* Comment out all this code if we are using the GNU C Library, and are not
32 actually compiling the library itself, and the installed library
33 supports the same library interface we do. This code is part of the GNU
34 C Library, but also included in many other GNU distributions. Compiling
35 and linking in this code is a waste when using the GNU C library
36 (especially if it is a shared library). Rather than having every GNU
37 program understand 'configure --with-gnu-libc' and omit the object
38 files, it is simpler to just do this in the source for each such file. */
39 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
40 # include <gnu-versions.h>
41 # if (_GNU_OBSTACK_INTERFACE_VERSION == _OBSTACK_INTERFACE_VERSION \
42 || (_GNU_OBSTACK_INTERFACE_VERSION == 1 \
43 && _OBSTACK_INTERFACE_VERSION == 2 \
44 && defined SIZEOF_INT && defined SIZEOF_SIZE_T \
45 && SIZEOF_INT == SIZEOF_SIZE_T))
46 # define _OBSTACK_ELIDE_CODE
47 # endif
48 #endif
50 #ifndef _OBSTACK_ELIDE_CODE
51 /* If GCC, or if an oddball (testing?) host that #defines __alignof__,
52 use the already-supplied __alignof__. Otherwise, this must be Gnulib
53 (as glibc assumes GCC); defer to Gnulib's alignof_type. */
54 # if !defined __GNUC__ && !defined __alignof__
55 # include <alignof.h>
56 # define __alignof__(type) alignof_type (type)
57 # endif
58 # include <stdlib.h>
59 # include <stdint.h>
61 # ifndef MAX
62 # define MAX(a,b) ((a) > (b) ? (a) : (b))
63 # endif
65 /* Determine default alignment. */
67 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
68 But in fact it might be less smart and round addresses to as much as
69 DEFAULT_ROUNDING. So we prepare for it to do that.
71 DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h. */
72 #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \
73 MAX (__alignof__ (uintmax_t), \
74 __alignof__ (void *)))
75 #define DEFAULT_ROUNDING MAX (sizeof (long double), \
76 MAX (sizeof (uintmax_t), \
77 sizeof (void *)))
79 /* Call functions with either the traditional malloc/free calling
80 interface, or the mmalloc/mfree interface (that adds an extra first
81 argument), based on the value of use_extra_arg. */
83 static void *
84 call_chunkfun (struct obstack *h, size_t size)
86 if (h->use_extra_arg)
87 return h->chunkfun.extra (h->extra_arg, size);
88 else
89 return h->chunkfun.plain (size);
92 static void
93 call_freefun (struct obstack *h, void *old_chunk)
95 if (h->use_extra_arg)
96 h->freefun.extra (h->extra_arg, old_chunk);
97 else
98 h->freefun.plain (old_chunk);
102 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
103 Objects start on multiples of ALIGNMENT (0 means use default).
105 Return nonzero if successful, calls obstack_alloc_failed_handler if
106 allocation fails. */
108 static int
109 _obstack_begin_worker (struct obstack *h,
110 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment)
112 struct _obstack_chunk *chunk; /* points to new chunk */
114 if (alignment == 0)
115 alignment = DEFAULT_ALIGNMENT;
116 if (size == 0)
117 /* Default size is what GNU malloc can fit in a 4096-byte block. */
119 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
120 Use the values for range checking, because if range checking is off,
121 the extra bytes won't be missed terribly, but if range checking is on
122 and we used a larger request, a whole extra 4096 bytes would be
123 allocated.
125 These number are irrelevant to the new GNU malloc. I suspect it is
126 less sensitive to the size of the request. */
127 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
128 + 4 + DEFAULT_ROUNDING - 1)
129 & ~(DEFAULT_ROUNDING - 1));
130 size = 4096 - extra;
133 h->chunk_size = size;
134 h->alignment_mask = alignment - 1;
136 chunk = h->chunk = call_chunkfun (h, h->chunk_size);
137 if (!chunk)
138 (*obstack_alloc_failed_handler) ();
139 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
140 alignment - 1);
141 h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size;
142 chunk->prev = 0;
143 /* The initial chunk now contains no empty object. */
144 h->maybe_empty_object = 0;
145 h->alloc_failed = 0;
146 return 1;
150 _obstack_begin (struct obstack *h,
151 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
152 void *(*chunkfun) (size_t),
153 void (*freefun) (void *))
155 h->chunkfun.plain = chunkfun;
156 h->freefun.plain = freefun;
157 h->use_extra_arg = 0;
158 return _obstack_begin_worker (h, size, alignment);
162 _obstack_begin_1 (struct obstack *h,
163 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
164 void *(*chunkfun) (void *, size_t),
165 void (*freefun) (void *, void *),
166 void *arg)
168 h->chunkfun.extra = chunkfun;
169 h->freefun.extra = freefun;
170 h->extra_arg = arg;
171 h->use_extra_arg = 1;
172 return _obstack_begin_worker (h, size, alignment);
175 /* Allocate a new current chunk for the obstack *H
176 on the assumption that LENGTH bytes need to be added
177 to the current object, or a new object of length LENGTH allocated.
178 Copies any partial object from the end of the old chunk
179 to the beginning of the new one. */
181 void
182 _obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length)
184 struct _obstack_chunk *old_chunk = h->chunk;
185 struct _obstack_chunk *new_chunk = 0;
186 size_t obj_size = h->next_free - h->object_base;
187 char *object_base;
189 /* Compute size for new chunk. */
190 size_t sum1 = obj_size + length;
191 size_t sum2 = sum1 + h->alignment_mask;
192 size_t new_size = sum2 + (obj_size >> 3) + 100;
193 if (new_size < sum2)
194 new_size = sum2;
195 if (new_size < h->chunk_size)
196 new_size = h->chunk_size;
198 /* Allocate and initialize the new chunk. */
199 if (obj_size <= sum1 && sum1 <= sum2)
200 new_chunk = call_chunkfun (h, new_size);
201 if (!new_chunk)
202 (*obstack_alloc_failed_handler)();
203 h->chunk = new_chunk;
204 new_chunk->prev = old_chunk;
205 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
207 /* Compute an aligned object_base in the new chunk */
208 object_base =
209 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
211 /* Move the existing object to the new chunk. */
212 memcpy (object_base, h->object_base, obj_size);
214 /* If the object just copied was the only data in OLD_CHUNK,
215 free that chunk and remove it from the chain.
216 But not if that chunk might contain an empty object. */
217 if (!h->maybe_empty_object
218 && (h->object_base
219 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
220 h->alignment_mask)))
222 new_chunk->prev = old_chunk->prev;
223 call_freefun (h, old_chunk);
226 h->object_base = object_base;
227 h->next_free = h->object_base + obj_size;
228 /* The new chunk certainly contains no empty object yet. */
229 h->maybe_empty_object = 0;
232 /* Return nonzero if object OBJ has been allocated from obstack H.
233 This is here for debugging.
234 If you use it in a program, you are probably losing. */
236 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
237 obstack.h because it is just for debugging. */
238 int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__;
241 _obstack_allocated_p (struct obstack *h, void *obj)
243 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
244 struct _obstack_chunk *plp; /* point to previous chunk if any */
246 lp = (h)->chunk;
247 /* We use >= rather than > since the object cannot be exactly at
248 the beginning of the chunk but might be an empty object exactly
249 at the end of an adjacent chunk. */
250 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
252 plp = lp->prev;
253 lp = plp;
255 return lp != 0;
258 /* Free objects in obstack H, including OBJ and everything allocate
259 more recently than OBJ. If OBJ is zero, free everything in H. */
261 void
262 _obstack_free (struct obstack *h, void *obj)
264 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
265 struct _obstack_chunk *plp; /* point to previous chunk if any */
267 lp = h->chunk;
268 /* We use >= because there cannot be an object at the beginning of a chunk.
269 But there can be an empty object at that address
270 at the end of another chunk. */
271 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
273 plp = lp->prev;
274 call_freefun (h, lp);
275 lp = plp;
276 /* If we switch chunks, we can't tell whether the new current
277 chunk contains an empty object, so assume that it may. */
278 h->maybe_empty_object = 1;
280 if (lp)
282 h->object_base = h->next_free = (char *) (obj);
283 h->chunk_limit = lp->limit;
284 h->chunk = lp;
286 else if (obj != 0)
287 /* obj is not in any of the chunks! */
288 abort ();
291 _OBSTACK_SIZE_T
292 _obstack_memory_used (struct obstack *h)
294 struct _obstack_chunk *lp;
295 _OBSTACK_SIZE_T nbytes = 0;
297 for (lp = h->chunk; lp != 0; lp = lp->prev)
299 nbytes += lp->limit - (char *) lp;
301 return nbytes;
304 # ifndef _OBSTACK_NO_ERROR_HANDLER
305 /* Define the error handler. */
306 # include <stdio.h>
308 /* Exit value used when 'print_and_abort' is used. */
309 # ifdef _LIBC
310 int obstack_exit_failure = EXIT_FAILURE;
311 # else
312 # include "exitfail.h"
313 # define obstack_exit_failure exit_failure
314 # endif
316 # ifdef _LIBC
317 # include <libintl.h>
318 # else
319 # include "gettext.h"
320 # endif
321 # ifndef _
322 # define _(msgid) gettext (msgid)
323 # endif
325 # ifdef _LIBC
326 # include <libio/iolibio.h>
327 # endif
329 static _Noreturn void
330 print_and_abort (void)
332 /* Don't change any of these strings. Yes, it would be possible to add
333 the newline to the string and use fputs or so. But this must not
334 happen because the "memory exhausted" message appears in other places
335 like this and the translation should be reused instead of creating
336 a very similar string which requires a separate translation. */
337 # ifdef _LIBC
338 (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
339 # else
340 fprintf (stderr, "%s\n", _("memory exhausted"));
341 # endif
342 exit (obstack_exit_failure);
345 /* The functions allocating more room by calling 'obstack_chunk_alloc'
346 jump to the handler pointed to by 'obstack_alloc_failed_handler'.
347 This can be set to a user defined function which should either
348 abort gracefully or use longjump - but shouldn't return. This
349 variable by default points to the internal function
350 'print_and_abort'. */
351 __attribute_noreturn__ void (*obstack_alloc_failed_handler) (void)
352 = print_and_abort;
353 # endif /* !_OBSTACK_NO_ERROR_HANDLER */
354 #endif /* !_OBSTACK_ELIDE_CODE */