1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* obstack.c - subroutines used implicitly by object stack macros
5 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
6 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2010 Free Software
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 # include <shlib-compat.h>
30 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
31 incremented whenever callers compiled using an old obstack.h can no
32 longer properly call the functions in this obstack.c. */
33 #define OBSTACK_INTERFACE_VERSION 1
35 /* Comment out all this code if we are using the GNU C Library, and are not
36 actually compiling the library itself, and the installed library
37 supports the same library interface we do. This code is part of the GNU
38 C Library, but also included in many other GNU distributions. Compiling
39 and linking in this code is a waste when using the GNU C library
40 (especially if it is a shared library). Rather than having every GNU
41 program understand `configure --with-gnu-libc' and omit the object
42 files, it is simpler to just do this in the source for each such file. */
44 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
45 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
46 # include <gnu-versions.h>
47 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
58 /* Determine default alignment. */
70 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
71 But in fact it might be less smart and round addresses to as much as
72 DEFAULT_ROUNDING. So we prepare for it to do that. */
75 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
76 DEFAULT_ROUNDING = sizeof (union fooround)
79 /* When we copy a long block of data, this is the unit to do it with.
80 On some machines, copying successive ints does not work;
81 in such a case, redefine COPYING_UNIT to `long' (if that works)
82 or `char' as a last resort. */
84 # define COPYING_UNIT int
88 /* The functions allocating more room by calling `obstack_chunk_alloc'
89 jump to the handler pointed to by `obstack_alloc_failed_handler'.
90 This can be set to a user defined function which should either
91 abort gracefully or use longjump - but shouldn't return. This
92 variable by default points to the internal function
94 static void print_and_abort (void);
95 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
97 /* Exit value used when `print_and_abort' is used. */
100 int obstack_exit_failure = EXIT_FAILURE;
102 # include "exitfail.h"
103 # define obstack_exit_failure exit_failure
107 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
108 /* A looong time ago (before 1994, anyway; we're not sure) this global variable
109 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
110 library still exports it because somebody might use it. */
111 struct obstack *_obstack_compat;
112 compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
116 /* Define a macro that either calls functions with the traditional malloc/free
117 calling interface, or calls functions with the mmalloc/mfree interface
118 (that adds an extra first argument), based on the state of use_extra_arg.
119 For free, do not use ?:, since some compilers, like the MIPS compilers,
120 do not allow (expr) ? void : void. */
122 # define CALL_CHUNKFUN(h, size) \
123 (((h) -> use_extra_arg) \
124 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
125 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
127 # define CALL_FREEFUN(h, old_chunk) \
129 if ((h) -> use_extra_arg) \
130 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
132 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
136 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
137 Objects start on multiples of ALIGNMENT (0 means use default).
138 CHUNKFUN is the function to use to allocate chunks,
139 and FREEFUN the function to free them.
141 Return nonzero if successful, calls obstack_alloc_failed_handler if
145 _obstack_begin (struct obstack *h,
146 int size, int alignment,
147 void *(*chunkfun) (long),
148 void (*freefun) (void *))
150 register struct _obstack_chunk *chunk; /* points to new chunk */
153 alignment = DEFAULT_ALIGNMENT;
155 /* Default size is what GNU malloc can fit in a 4096-byte block. */
157 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
158 Use the values for range checking, because if range checking is off,
159 the extra bytes won't be missed terribly, but if range checking is on
160 and we used a larger request, a whole extra 4096 bytes would be
163 These number are irrelevant to the new GNU malloc. I suspect it is
164 less sensitive to the size of the request. */
165 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
166 + 4 + DEFAULT_ROUNDING - 1)
167 & ~(DEFAULT_ROUNDING - 1));
171 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
172 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
173 h->chunk_size = size;
174 h->alignment_mask = alignment - 1;
175 h->use_extra_arg = 0;
177 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
179 (*obstack_alloc_failed_handler) ();
180 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
182 h->chunk_limit = chunk->limit
183 = (char *) chunk + h->chunk_size;
185 /* The initial chunk now contains no empty object. */
186 h->maybe_empty_object = 0;
192 _obstack_begin_1 (struct obstack *h, int size, int alignment,
193 void *(*chunkfun) (void *, long),
194 void (*freefun) (void *, void *),
197 register struct _obstack_chunk *chunk; /* points to new chunk */
200 alignment = DEFAULT_ALIGNMENT;
202 /* Default size is what GNU malloc can fit in a 4096-byte block. */
204 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
205 Use the values for range checking, because if range checking is off,
206 the extra bytes won't be missed terribly, but if range checking is on
207 and we used a larger request, a whole extra 4096 bytes would be
210 These number are irrelevant to the new GNU malloc. I suspect it is
211 less sensitive to the size of the request. */
212 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
213 + 4 + DEFAULT_ROUNDING - 1)
214 & ~(DEFAULT_ROUNDING - 1));
218 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
219 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
220 h->chunk_size = size;
221 h->alignment_mask = alignment - 1;
223 h->use_extra_arg = 1;
225 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
227 (*obstack_alloc_failed_handler) ();
228 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
230 h->chunk_limit = chunk->limit
231 = (char *) chunk + h->chunk_size;
233 /* The initial chunk now contains no empty object. */
234 h->maybe_empty_object = 0;
239 /* Allocate a new current chunk for the obstack *H
240 on the assumption that LENGTH bytes need to be added
241 to the current object, or a new object of length LENGTH allocated.
242 Copies any partial object from the end of the old chunk
243 to the beginning of the new one. */
246 _obstack_newchunk (struct obstack *h, int length)
248 register struct _obstack_chunk *old_chunk = h->chunk;
249 register struct _obstack_chunk *new_chunk;
250 register long new_size;
251 register long obj_size = h->next_free - h->object_base;
256 /* Compute size for new chunk. */
257 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
258 if (new_size < h->chunk_size)
259 new_size = h->chunk_size;
261 /* Allocate and initialize the new chunk. */
262 new_chunk = CALL_CHUNKFUN (h, new_size);
264 (*obstack_alloc_failed_handler) ();
265 h->chunk = new_chunk;
266 new_chunk->prev = old_chunk;
267 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
269 /* Compute an aligned object_base in the new chunk */
271 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
273 /* Move the existing object to the new chunk.
274 Word at a time is fast and is safe if the object
275 is sufficiently aligned. */
276 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
278 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
280 ((COPYING_UNIT *)object_base)[i]
281 = ((COPYING_UNIT *)h->object_base)[i];
282 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
283 but that can cross a page boundary on a machine
284 which does not do strict alignment for COPYING_UNITS. */
285 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
289 /* Copy remaining bytes one by one. */
290 for (i = already; i < obj_size; i++)
291 object_base[i] = h->object_base[i];
293 /* If the object just copied was the only data in OLD_CHUNK,
294 free that chunk and remove it from the chain.
295 But not if that chunk might contain an empty object. */
296 if (! h->maybe_empty_object
298 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
301 new_chunk->prev = old_chunk->prev;
302 CALL_FREEFUN (h, old_chunk);
305 h->object_base = object_base;
306 h->next_free = h->object_base + obj_size;
307 /* The new chunk certainly contains no empty object yet. */
308 h->maybe_empty_object = 0;
311 libc_hidden_def (_obstack_newchunk)
314 /* Return nonzero if object OBJ has been allocated from obstack H.
315 This is here for debugging.
316 If you use it in a program, you are probably losing. */
318 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
319 obstack.h because it is just for debugging. */
320 int _obstack_allocated_p (struct obstack *h, void *obj);
323 _obstack_allocated_p (struct obstack *h, void *obj)
325 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
326 register struct _obstack_chunk *plp; /* point to previous chunk if any */
329 /* We use >= rather than > since the object cannot be exactly at
330 the beginning of the chunk but might be an empty object exactly
331 at the end of an adjacent chunk. */
332 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
340 /* Free objects in obstack H, including OBJ and everything allocate
341 more recently than OBJ. If OBJ is zero, free everything in H. */
346 __obstack_free (struct obstack *h, void *obj)
348 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
349 register struct _obstack_chunk *plp; /* point to previous chunk if any */
352 /* We use >= because there cannot be an object at the beginning of a chunk.
353 But there can be an empty object at that address
354 at the end of another chunk. */
355 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
358 CALL_FREEFUN (h, lp);
360 /* If we switch chunks, we can't tell whether the new current
361 chunk contains an empty object, so assume that it may. */
362 h->maybe_empty_object = 1;
366 h->object_base = h->next_free = (char *) (obj);
367 h->chunk_limit = lp->limit;
371 /* obj is not in any of the chunks! */
376 /* Older versions of libc used a function _obstack_free intended to be
377 called by non-GCC compilers. */
378 strong_alias (obstack_free, _obstack_free)
382 _obstack_memory_used (struct obstack *h)
384 register struct _obstack_chunk* lp;
385 register int nbytes = 0;
387 for (lp = h->chunk; lp != 0; lp = lp->prev)
389 nbytes += lp->limit - (char *) lp;
394 /* Define the error handler. */
396 # include <libintl.h>
398 # include "gettext.h"
401 # define _(msgid) gettext (msgid)
405 # include <libio/iolibio.h>
408 # ifndef __attribute__
409 /* This feature is available in gcc versions 2.5 and later. */
410 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
411 # define __attribute__(Spec) /* empty */
416 __attribute__ ((noreturn))
417 print_and_abort (void)
419 /* Don't change any of these strings. Yes, it would be possible to add
420 the newline to the string and use fputs or so. But this must not
421 happen because the "memory exhausted" message appears in other places
422 like this and the translation should be reused instead of creating
423 a very similar string which requires a separate translation. */
425 (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
427 fprintf (stderr, "%s\n", _("memory exhausted"));
429 exit (obstack_exit_failure);
432 #endif /* !ELIDE_CODE */