1 /* obstack.c - subroutines used implicitly by object stack macros
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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
48 #if defined _LIBC && defined USE_IN_LIBIO
55 /* Determine default alignment. */
56 struct fooalign {char x; double d;};
57 # define DEFAULT_ALIGNMENT \
58 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
59 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
60 But in fact it might be less smart and round addresses to as much as
61 DEFAULT_ROUNDING. So we prepare for it to do that. */
62 union fooround {long x; double d;};
63 # define DEFAULT_ROUNDING (sizeof (union fooround))
65 /* When we copy a long block of data, this is the unit to do it with.
66 On some machines, copying successive ints does not work;
67 in such a case, redefine COPYING_UNIT to `long' (if that works)
68 or `char' as a last resort. */
70 # define COPYING_UNIT int
74 /* The functions allocating more room by calling `obstack_chunk_alloc'
75 jump to the handler pointed to by `obstack_alloc_failed_handler'.
76 This can be set to a user defined function which should either
77 abort gracefully or use longjump - but shouldn't return. This
78 variable by default points to the internal function
80 static void print_and_abort (void);
81 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
83 /* Exit value used when `print_and_abort' is used. */
88 int obstack_exit_failure = EXIT_FAILURE;
90 /* The non-GNU-C macros copy the obstack into this global variable
91 to avoid multiple evaluation. */
93 struct obstack *_obstack;
95 /* Define a macro that either calls functions with the traditional malloc/free
96 calling interface, or calls functions with the mmalloc/mfree interface
97 (that adds an extra first argument), based on the state of use_extra_arg.
98 For free, do not use ?:, since some compilers, like the MIPS compilers,
99 do not allow (expr) ? void : void. */
101 # define CALL_CHUNKFUN(h, size) \
102 (((h) -> use_extra_arg) \
103 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
104 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
106 # define CALL_FREEFUN(h, old_chunk) \
108 if ((h) -> use_extra_arg) \
109 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
111 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
115 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
116 Objects start on multiples of ALIGNMENT (0 means use default).
117 CHUNKFUN is the function to use to allocate chunks,
118 and FREEFUN the function to free them.
120 Return nonzero if successful, calls obstack_alloc_failed_handler if
124 _obstack_begin (struct obstack *h,
125 int size, int alignment,
126 void *(*chunkfun) (long),
127 void (*freefun) (void *))
129 register struct _obstack_chunk *chunk; /* points to new chunk */
132 alignment = (int) DEFAULT_ALIGNMENT;
134 /* Default size is what GNU malloc can fit in a 4096-byte block. */
136 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
137 Use the values for range checking, because if range checking is off,
138 the extra bytes won't be missed terribly, but if range checking is on
139 and we used a larger request, a whole extra 4096 bytes would be
142 These number are irrelevant to the new GNU malloc. I suspect it is
143 less sensitive to the size of the request. */
144 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
145 + 4 + DEFAULT_ROUNDING - 1)
146 & ~(DEFAULT_ROUNDING - 1));
150 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
151 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
152 h->chunk_size = size;
153 h->alignment_mask = alignment - 1;
154 h->use_extra_arg = 0;
156 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
158 (*obstack_alloc_failed_handler) ();
159 h->next_free = h->object_base = chunk->contents;
160 h->chunk_limit = chunk->limit
161 = (char *) chunk + h->chunk_size;
163 /* The initial chunk now contains no empty object. */
164 h->maybe_empty_object = 0;
170 _obstack_begin_1 (struct obstack *h, int size, int alignment,
171 void *(*chunkfun) (void *, long),
172 void (*freefun) (void *, void *),
175 register struct _obstack_chunk *chunk; /* points to new chunk */
178 alignment = (int) DEFAULT_ALIGNMENT;
180 /* Default size is what GNU malloc can fit in a 4096-byte block. */
182 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
183 Use the values for range checking, because if range checking is off,
184 the extra bytes won't be missed terribly, but if range checking is on
185 and we used a larger request, a whole extra 4096 bytes would be
188 These number are irrelevant to the new GNU malloc. I suspect it is
189 less sensitive to the size of the request. */
190 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
191 + 4 + DEFAULT_ROUNDING - 1)
192 & ~(DEFAULT_ROUNDING - 1));
196 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
197 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
198 h->chunk_size = size;
199 h->alignment_mask = alignment - 1;
201 h->use_extra_arg = 1;
203 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
205 (*obstack_alloc_failed_handler) ();
206 h->next_free = h->object_base = chunk->contents;
207 h->chunk_limit = chunk->limit
208 = (char *) chunk + h->chunk_size;
210 /* The initial chunk now contains no empty object. */
211 h->maybe_empty_object = 0;
216 /* Allocate a new current chunk for the obstack *H
217 on the assumption that LENGTH bytes need to be added
218 to the current object, or a new object of length LENGTH allocated.
219 Copies any partial object from the end of the old chunk
220 to the beginning of the new one. */
223 _obstack_newchunk (struct obstack *h, int length)
225 register struct _obstack_chunk *old_chunk = h->chunk;
226 register struct _obstack_chunk *new_chunk;
227 register long new_size;
228 register long obj_size = h->next_free - h->object_base;
233 /* Compute size for new chunk. */
234 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
235 if (new_size < h->chunk_size)
236 new_size = h->chunk_size;
238 /* Allocate and initialize the new chunk. */
239 new_chunk = CALL_CHUNKFUN (h, new_size);
241 (*obstack_alloc_failed_handler) ();
242 h->chunk = new_chunk;
243 new_chunk->prev = old_chunk;
244 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
246 /* Compute an aligned object_base in the new chunk */
248 __INT_TO_PTR ((__PTR_TO_INT (new_chunk->contents) + h->alignment_mask)
249 & ~ (h->alignment_mask));
251 /* Move the existing object to the new chunk.
252 Word at a time is fast and is safe if the object
253 is sufficiently aligned. */
254 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
256 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
258 ((COPYING_UNIT *)object_base)[i]
259 = ((COPYING_UNIT *)h->object_base)[i];
260 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
261 but that can cross a page boundary on a machine
262 which does not do strict alignment for COPYING_UNITS. */
263 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
267 /* Copy remaining bytes one by one. */
268 for (i = already; i < obj_size; i++)
269 object_base[i] = h->object_base[i];
271 /* If the object just copied was the only data in OLD_CHUNK,
272 free that chunk and remove it from the chain.
273 But not if that chunk might contain an empty object. */
274 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
276 new_chunk->prev = old_chunk->prev;
277 CALL_FREEFUN (h, old_chunk);
280 h->object_base = object_base;
281 h->next_free = h->object_base + obj_size;
282 /* The new chunk certainly contains no empty object yet. */
283 h->maybe_empty_object = 0;
286 /* Return nonzero if object OBJ has been allocated from obstack H.
287 This is here for debugging.
288 If you use it in a program, you are probably losing. */
290 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
291 obstack.h because it is just for debugging. */
292 int _obstack_allocated_p (struct obstack *h, void *obj);
295 _obstack_allocated_p (struct obstack *h, void *obj)
297 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
298 register struct _obstack_chunk *plp; /* point to previous chunk if any */
301 /* We use >= rather than > since the object cannot be exactly at
302 the beginning of the chunk but might be an empty object exactly
303 at the end of an adjacent chunk. */
304 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
312 /* Free objects in obstack H, including OBJ and everything allocate
313 more recently than OBJ. If OBJ is zero, free everything in H. */
317 /* This function has two names with identical definitions.
318 This is the first one, called from non-ANSI code. */
321 _obstack_free (struct obstack *h, void *obj)
323 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
324 register struct _obstack_chunk *plp; /* point to previous chunk if any */
327 /* We use >= because there cannot be an object at the beginning of a chunk.
328 But there can be an empty object at that address
329 at the end of another chunk. */
330 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
333 CALL_FREEFUN (h, lp);
335 /* If we switch chunks, we can't tell whether the new current
336 chunk contains an empty object, so assume that it may. */
337 h->maybe_empty_object = 1;
341 h->object_base = h->next_free = (char *) (obj);
342 h->chunk_limit = lp->limit;
346 /* obj is not in any of the chunks! */
350 /* This function is used from ANSI code. */
353 obstack_free (struct obstack *h, void *obj)
355 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
356 register struct _obstack_chunk *plp; /* point to previous chunk if any */
359 /* We use >= because there cannot be an object at the beginning of a chunk.
360 But there can be an empty object at that address
361 at the end of another chunk. */
362 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
365 CALL_FREEFUN (h, lp);
367 /* If we switch chunks, we can't tell whether the new current
368 chunk contains an empty object, so assume that it may. */
369 h->maybe_empty_object = 1;
373 h->object_base = h->next_free = (char *) (obj);
374 h->chunk_limit = lp->limit;
378 /* obj is not in any of the chunks! */
383 _obstack_memory_used (struct obstack *h)
385 register struct _obstack_chunk* lp;
386 register int nbytes = 0;
388 for (lp = h->chunk; lp != 0; lp = lp->prev)
390 nbytes += lp->limit - (char *) lp;
395 /* Define the error handler. */
397 # include <libintl.h>
399 # include "gettext.h"
401 # define _(msgid) gettext (msgid)
403 # if defined _LIBC && defined USE_IN_LIBIO
404 # include <libio/iolibio.h>
405 # define fputs(s, f) _IO_fputs (s, f)
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. */
424 # if defined _LIBC && defined USE_IN_LIBIO
425 if (_IO_fwide (stderr, 0) > 0)
426 __fwprintf (stderr, L"%s\n", _("memory exhausted"));
429 fprintf (stderr, "%s\n", _("memory exhausted"));
430 exit (obstack_exit_failure);
433 #endif /* !ELIDE_CODE */