1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
24 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
25 incremented whenever callers compiled using an old obstack.h can no
26 longer properly call the functions in this obstack.c. */
27 #define OBSTACK_INTERFACE_VERSION 1
29 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
33 /* Determine default alignment. */
45 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
46 But in fact it might be less smart and round addresses to as much as
47 DEFAULT_ROUNDING. So we prepare for it to do that. */
50 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
51 DEFAULT_ROUNDING = sizeof (union fooround)
54 /* When we copy a long block of data, this is the unit to do it with.
55 On some machines, copying successive ints does not work;
56 in such a case, redefine COPYING_UNIT to `long' (if that works)
57 or `char' as a last resort. */
59 # define COPYING_UNIT int
63 /* The functions allocating more room by calling `obstack_chunk_alloc'
64 jump to the handler pointed to by `obstack_alloc_failed_handler'.
65 This can be set to a user defined function which should either
66 abort gracefully or use longjump - but shouldn't return. This
67 variable by default points to the internal function
69 static void print_and_abort (void);
70 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
72 /* Exit value used when `print_and_abort' is used. */
74 int obstack_exit_failure = EXIT_FAILURE;
76 /* Define a macro that either calls functions with the traditional malloc/free
77 calling interface, or calls functions with the mmalloc/mfree interface
78 (that adds an extra first argument), based on the state of use_extra_arg.
79 For free, do not use ?:, since some compilers, like the MIPS compilers,
80 do not allow (expr) ? void : void. */
82 # define CALL_CHUNKFUN(h, size) \
83 (((h) -> use_extra_arg) \
84 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
85 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
87 # define CALL_FREEFUN(h, old_chunk) \
89 if ((h) -> use_extra_arg) \
90 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
92 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
96 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
97 Objects start on multiples of ALIGNMENT (0 means use default).
98 CHUNKFUN is the function to use to allocate chunks,
99 and FREEFUN the function to free them.
101 Return nonzero if successful, calls obstack_alloc_failed_handler if
105 _obstack_begin (struct obstack *h,
106 int size, int alignment,
107 void *(*chunkfun) (long),
108 void (*freefun) (void *))
110 register struct _obstack_chunk *chunk; /* points to new chunk */
113 alignment = DEFAULT_ALIGNMENT;
115 /* Default size is what GNU malloc can fit in a 4096-byte block. */
117 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
118 Use the values for range checking, because if range checking is off,
119 the extra bytes won't be missed terribly, but if range checking is on
120 and we used a larger request, a whole extra 4096 bytes would be
123 These number are irrelevant to the new GNU malloc. I suspect it is
124 less sensitive to the size of the request. */
125 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
126 + 4 + DEFAULT_ROUNDING - 1)
127 & ~(DEFAULT_ROUNDING - 1));
131 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
132 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
133 h->chunk_size = size;
134 h->alignment_mask = alignment - 1;
135 h->use_extra_arg = 0;
137 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
139 (*obstack_alloc_failed_handler) ();
140 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
142 h->chunk_limit = chunk->limit
143 = (char *) chunk + h->chunk_size;
145 /* The initial chunk now contains no empty object. */
146 h->maybe_empty_object = 0;
152 _obstack_begin_1 (struct obstack *h, int size, int alignment,
153 void *(*chunkfun) (void *, long),
154 void (*freefun) (void *, void *),
157 register struct _obstack_chunk *chunk; /* points to new chunk */
160 alignment = DEFAULT_ALIGNMENT;
162 /* Default size is what GNU malloc can fit in a 4096-byte block. */
164 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
165 Use the values for range checking, because if range checking is off,
166 the extra bytes won't be missed terribly, but if range checking is on
167 and we used a larger request, a whole extra 4096 bytes would be
170 These number are irrelevant to the new GNU malloc. I suspect it is
171 less sensitive to the size of the request. */
172 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
173 + 4 + DEFAULT_ROUNDING - 1)
174 & ~(DEFAULT_ROUNDING - 1));
178 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
179 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
180 h->chunk_size = size;
181 h->alignment_mask = alignment - 1;
183 h->use_extra_arg = 1;
185 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
187 (*obstack_alloc_failed_handler) ();
188 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
190 h->chunk_limit = chunk->limit
191 = (char *) chunk + h->chunk_size;
193 /* The initial chunk now contains no empty object. */
194 h->maybe_empty_object = 0;
199 /* Allocate a new current chunk for the obstack *H
200 on the assumption that LENGTH bytes need to be added
201 to the current object, or a new object of length LENGTH allocated.
202 Copies any partial object from the end of the old chunk
203 to the beginning of the new one. */
206 _obstack_newchunk (struct obstack *h, int length)
208 register struct _obstack_chunk *old_chunk = h->chunk;
209 register struct _obstack_chunk *new_chunk;
210 register long new_size;
211 register long obj_size = h->next_free - h->object_base;
216 /* Compute size for new chunk. */
217 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
218 if (new_size < h->chunk_size)
219 new_size = h->chunk_size;
221 /* Allocate and initialize the new chunk. */
222 new_chunk = CALL_CHUNKFUN (h, new_size);
224 (*obstack_alloc_failed_handler) ();
225 h->chunk = new_chunk;
226 new_chunk->prev = old_chunk;
227 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
229 /* Compute an aligned object_base in the new chunk */
231 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
233 /* Move the existing object to the new chunk.
234 Word at a time is fast and is safe if the object
235 is sufficiently aligned. */
236 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
238 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
240 ((COPYING_UNIT *)object_base)[i]
241 = ((COPYING_UNIT *)h->object_base)[i];
242 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
243 but that can cross a page boundary on a machine
244 which does not do strict alignment for COPYING_UNITS. */
245 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
249 /* Copy remaining bytes one by one. */
250 for (i = already; i < obj_size; i++)
251 object_base[i] = h->object_base[i];
253 /* If the object just copied was the only data in OLD_CHUNK,
254 free that chunk and remove it from the chain.
255 But not if that chunk might contain an empty object. */
256 if (! h->maybe_empty_object
258 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
261 new_chunk->prev = old_chunk->prev;
262 CALL_FREEFUN (h, old_chunk);
265 h->object_base = object_base;
266 h->next_free = h->object_base + obj_size;
267 /* The new chunk certainly contains no empty object yet. */
268 h->maybe_empty_object = 0;
271 /* Return nonzero if object OBJ has been allocated from obstack H.
272 This is here for debugging.
273 If you use it in a program, you are probably losing. */
275 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
276 obstack.h because it is just for debugging. */
277 int _obstack_allocated_p (struct obstack *h, void *obj);
280 _obstack_allocated_p (struct obstack *h, void *obj)
282 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
283 register struct _obstack_chunk *plp; /* point to previous chunk if any */
286 /* We use >= rather than > since the object cannot be exactly at
287 the beginning of the chunk but might be an empty object exactly
288 at the end of an adjacent chunk. */
289 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
297 /* Free objects in obstack H, including OBJ and everything allocate
298 more recently than OBJ. If OBJ is zero, free everything in H. */
303 obstack_free (struct obstack *h, void *obj)
305 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
306 register struct _obstack_chunk *plp; /* point to previous chunk if any */
309 /* We use >= because there cannot be an object at the beginning of a chunk.
310 But there can be an empty object at that address
311 at the end of another chunk. */
312 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
315 CALL_FREEFUN (h, lp);
317 /* If we switch chunks, we can't tell whether the new current
318 chunk contains an empty object, so assume that it may. */
319 h->maybe_empty_object = 1;
323 h->object_base = h->next_free = (char *) (obj);
324 h->chunk_limit = lp->limit;
328 /* obj is not in any of the chunks! */
333 _obstack_memory_used (struct obstack *h)
335 register struct _obstack_chunk* lp;
336 register int nbytes = 0;
338 for (lp = h->chunk; lp != 0; lp = lp->prev)
340 nbytes += lp->limit - (char *) lp;
346 __attribute__ ((noreturn))
347 print_and_abort (void)
349 /* Don't change any of these strings. Yes, it would be possible to add
350 the newline to the string and use fputs or so. But this must not
351 happen because the "memory exhausted" message appears in other places
352 like this and the translation should be reused instead of creating
353 a very similar string which requires a separate translation. */
354 fprintf (stderr, "%s\n", "memory exhausted");
355 exit (obstack_exit_failure);