1 /* obstack.c - subroutines used implicitly by object stack macros
3 Copyright (C) 1988-1994, 1996-2006, 2009-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 # include <shlib-compat.h>
26 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
27 incremented whenever callers compiled using an old obstack.h can no
28 longer properly call the functions in this obstack.c. */
29 #define OBSTACK_INTERFACE_VERSION 1
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. */
40 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
41 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
42 # include <gnu-versions.h>
43 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
54 /* Determine default alignment. */
66 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
67 But in fact it might be less smart and round addresses to as much as
68 DEFAULT_ROUNDING. So we prepare for it to do that. */
71 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
72 DEFAULT_ROUNDING = sizeof (union fooround)
75 /* When we copy a long block of data, this is the unit to do it with.
76 On some machines, copying successive ints does not work;
77 in such a case, redefine COPYING_UNIT to `long' (if that works)
78 or `char' as a last resort. */
80 # define COPYING_UNIT int
84 /* The functions allocating more room by calling `obstack_chunk_alloc'
85 jump to the handler pointed to by `obstack_alloc_failed_handler'.
86 This can be set to a user defined function which should either
87 abort gracefully or use longjump - but shouldn't return. This
88 variable by default points to the internal function
90 static void print_and_abort (void);
91 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
93 /* Exit value used when `print_and_abort' is used. */
96 int obstack_exit_failure = EXIT_FAILURE;
98 # include "exitfail.h"
99 # define obstack_exit_failure exit_failure
103 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
104 /* A looong time ago (before 1994, anyway; we're not sure) this global variable
105 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
106 library still exports it because somebody might use it. */
107 struct obstack *_obstack_compat;
108 compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
112 /* Define a macro that either calls functions with the traditional malloc/free
113 calling interface, or calls functions with the mmalloc/mfree interface
114 (that adds an extra first argument), based on the state of use_extra_arg.
115 For free, do not use ?:, since some compilers, like the MIPS compilers,
116 do not allow (expr) ? void : void. */
118 # define CALL_CHUNKFUN(h, size) \
119 (((h) -> use_extra_arg) \
120 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
121 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
123 # define CALL_FREEFUN(h, old_chunk) \
125 if ((h) -> use_extra_arg) \
126 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
128 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
132 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
133 Objects start on multiples of ALIGNMENT (0 means use default).
134 CHUNKFUN is the function to use to allocate chunks,
135 and FREEFUN the function to free them.
137 Return nonzero if successful, calls obstack_alloc_failed_handler if
141 _obstack_begin (struct obstack *h,
142 int size, int alignment,
143 void *(*chunkfun) (long),
144 void (*freefun) (void *))
146 register struct _obstack_chunk *chunk; /* points to new chunk */
149 alignment = DEFAULT_ALIGNMENT;
151 /* Default size is what GNU malloc can fit in a 4096-byte block. */
153 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
154 Use the values for range checking, because if range checking is off,
155 the extra bytes won't be missed terribly, but if range checking is on
156 and we used a larger request, a whole extra 4096 bytes would be
159 These number are irrelevant to the new GNU malloc. I suspect it is
160 less sensitive to the size of the request. */
161 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
162 + 4 + DEFAULT_ROUNDING - 1)
163 & ~(DEFAULT_ROUNDING - 1));
167 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
168 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
169 h->chunk_size = size;
170 h->alignment_mask = alignment - 1;
171 h->use_extra_arg = 0;
173 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
175 (*obstack_alloc_failed_handler) ();
176 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
178 h->chunk_limit = chunk->limit
179 = (char *) chunk + h->chunk_size;
181 /* The initial chunk now contains no empty object. */
182 h->maybe_empty_object = 0;
188 _obstack_begin_1 (struct obstack *h, int size, int alignment,
189 void *(*chunkfun) (void *, long),
190 void (*freefun) (void *, void *),
193 register struct _obstack_chunk *chunk; /* points to new chunk */
196 alignment = DEFAULT_ALIGNMENT;
198 /* Default size is what GNU malloc can fit in a 4096-byte block. */
200 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
201 Use the values for range checking, because if range checking is off,
202 the extra bytes won't be missed terribly, but if range checking is on
203 and we used a larger request, a whole extra 4096 bytes would be
206 These number are irrelevant to the new GNU malloc. I suspect it is
207 less sensitive to the size of the request. */
208 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
209 + 4 + DEFAULT_ROUNDING - 1)
210 & ~(DEFAULT_ROUNDING - 1));
214 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
215 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
216 h->chunk_size = size;
217 h->alignment_mask = alignment - 1;
219 h->use_extra_arg = 1;
221 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
223 (*obstack_alloc_failed_handler) ();
224 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
226 h->chunk_limit = chunk->limit
227 = (char *) chunk + h->chunk_size;
229 /* The initial chunk now contains no empty object. */
230 h->maybe_empty_object = 0;
235 /* Allocate a new current chunk for the obstack *H
236 on the assumption that LENGTH bytes need to be added
237 to the current object, or a new object of length LENGTH allocated.
238 Copies any partial object from the end of the old chunk
239 to the beginning of the new one. */
242 _obstack_newchunk (struct obstack *h, int length)
244 register struct _obstack_chunk *old_chunk = h->chunk;
245 register struct _obstack_chunk *new_chunk;
246 register long new_size;
247 register long obj_size = h->next_free - h->object_base;
252 /* Compute size for new chunk. */
253 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
254 if (new_size < h->chunk_size)
255 new_size = h->chunk_size;
257 /* Allocate and initialize the new chunk. */
258 new_chunk = CALL_CHUNKFUN (h, new_size);
260 (*obstack_alloc_failed_handler) ();
261 h->chunk = new_chunk;
262 new_chunk->prev = old_chunk;
263 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
265 /* Compute an aligned object_base in the new chunk */
267 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
269 /* Move the existing object to the new chunk.
270 Word at a time is fast and is safe if the object
271 is sufficiently aligned. */
272 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
274 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
276 ((COPYING_UNIT *)object_base)[i]
277 = ((COPYING_UNIT *)h->object_base)[i];
278 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
279 but that can cross a page boundary on a machine
280 which does not do strict alignment for COPYING_UNITS. */
281 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
285 /* Copy remaining bytes one by one. */
286 for (i = already; i < obj_size; i++)
287 object_base[i] = h->object_base[i];
289 /* If the object just copied was the only data in OLD_CHUNK,
290 free that chunk and remove it from the chain.
291 But not if that chunk might contain an empty object. */
292 if (! h->maybe_empty_object
294 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
297 new_chunk->prev = old_chunk->prev;
298 CALL_FREEFUN (h, old_chunk);
301 h->object_base = object_base;
302 h->next_free = h->object_base + obj_size;
303 /* The new chunk certainly contains no empty object yet. */
304 h->maybe_empty_object = 0;
307 libc_hidden_def (_obstack_newchunk)
310 /* Return nonzero if object OBJ has been allocated from obstack H.
311 This is here for debugging.
312 If you use it in a program, you are probably losing. */
314 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
315 obstack.h because it is just for debugging. */
316 int _obstack_allocated_p (struct obstack *h, void *obj);
319 _obstack_allocated_p (struct obstack *h, void *obj)
321 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
322 register struct _obstack_chunk *plp; /* point to previous chunk if any */
325 /* We use >= rather than > since the object cannot be exactly at
326 the beginning of the chunk but might be an empty object exactly
327 at the end of an adjacent chunk. */
328 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
336 /* Free objects in obstack H, including OBJ and everything allocate
337 more recently than OBJ. If OBJ is zero, free everything in H. */
342 __obstack_free (struct obstack *h, void *obj)
344 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
345 register struct _obstack_chunk *plp; /* point to previous chunk if any */
348 /* We use >= because there cannot be an object at the beginning of a chunk.
349 But there can be an empty object at that address
350 at the end of another chunk. */
351 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
354 CALL_FREEFUN (h, lp);
356 /* If we switch chunks, we can't tell whether the new current
357 chunk contains an empty object, so assume that it may. */
358 h->maybe_empty_object = 1;
362 h->object_base = h->next_free = (char *) (obj);
363 h->chunk_limit = lp->limit;
367 /* obj is not in any of the chunks! */
372 /* Older versions of libc used a function _obstack_free intended to be
373 called by non-GCC compilers. */
374 strong_alias (obstack_free, _obstack_free)
378 _obstack_memory_used (struct obstack *h)
380 register struct _obstack_chunk* lp;
381 register int nbytes = 0;
383 for (lp = h->chunk; lp != 0; lp = lp->prev)
385 nbytes += lp->limit - (char *) lp;
390 /* Define the error handler. */
392 # include <libintl.h>
394 # include "gettext.h"
397 # define _(msgid) gettext (msgid)
401 # include <libio/iolibio.h>
404 # ifndef __attribute__
405 /* This feature is available in gcc versions 2.5 and later. */
406 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
407 # define __attribute__(Spec) /* empty */
412 __attribute__ ((noreturn))
413 print_and_abort (void)
415 /* Don't change any of these strings. Yes, it would be possible to add
416 the newline to the string and use fputs or so. But this must not
417 happen because the "memory exhausted" message appears in other places
418 like this and the translation should be reused instead of creating
419 a very similar string which requires a separate translation. */
421 (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
423 fprintf (stderr, "%s\n", _("memory exhausted"));
425 exit (obstack_exit_failure);
428 #endif /* !ELIDE_CODE */