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.
5
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.
10
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.
15
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. */
20
21
22 #ifdef _LIBC
23 # include <obstack.h>
24 # include <shlib-compat.h>
25 #else
26 # ifdef HAVE_CONFIG_H
27 # ifdef HAVE_CLEWN
28 # include <config.h>
29 # else
30 # include <auto/config.h>
31 # endif
32 # endif
33 # include <stdint.h>
34 # include "obstack.h"
35 #endif
36
37 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
38 incremented whenever callers compiled using an old obstack.h can no
39 longer properly call the functions in this obstack.c. */
40 #define OBSTACK_INTERFACE_VERSION 1
41
42 /* Comment out all this code if we are using the GNU C Library, and are not
43 actually compiling the library itself, and the installed library
44 supports the same library interface we do. This code is part of the GNU
45 C Library, but also included in many other GNU distributions. Compiling
46 and linking in this code is a waste when using the GNU C library
47 (especially if it is a shared library). Rather than having every GNU
48 program understand `configure --with-gnu-libc' and omit the object
49 files, it is simpler to just do this in the source for each such file. */
50
51 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
52 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
53 # include <gnu-versions.h>
54 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
55 # define ELIDE_CODE
56 # endif
57 #endif
58
59 #include <stddef.h>
60
61 #ifndef ELIDE_CODE
62
63
64 # if HAVE_INTTYPES_H
65 # include <inttypes.h>
66 # endif
67 # if HAVE_STDINT_H || defined _LIBC
68 # include <stdint.h>
69 # endif
70
71 /* Determine default alignment. */
72 union fooround
73 {
74 uintmax_t i;
75 long double d;
76 void *p;
77 };
78 struct fooalign
79 {
80 char c;
81 union fooround u;
82 };
83 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
84 But in fact it might be less smart and round addresses to as much as
85 DEFAULT_ROUNDING. So we prepare for it to do that. */
86 enum
87 {
88 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
89 DEFAULT_ROUNDING = sizeof (union fooround)
90 };
91
92 /* When we copy a long block of data, this is the unit to do it with.
93 On some machines, copying successive ints does not work;
94 in such a case, redefine COPYING_UNIT to `long' (if that works)
95 or `char' as a last resort. */
96 # ifndef COPYING_UNIT
97 # define COPYING_UNIT int
98 # endif
99
100
101 /* The functions allocating more room by calling `obstack_chunk_alloc'
102 jump to the handler pointed to by `obstack_alloc_failed_handler'.
103 This can be set to a user defined function which should either
104 abort gracefully or use longjump - but shouldn't return. This
105 variable by default points to the internal function
106 `print_and_abort'. */
107 static void print_and_abort (void);
108 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
109
110 /* Exit value used when `print_and_abort' is used. */
111 # include <stdlib.h>
112 # ifdef _LIBC
113 int obstack_exit_failure = EXIT_FAILURE;
114 # else
115 # define obstack_exit_failure EXIT_FAILURE
116 # endif
117
118 # ifdef _LIBC
119 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
120 /* A looong time ago (before 1994, anyway; we're not sure) this global variable
121 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
122 library still exports it because somebody might use it. */
123 struct obstack *_obstack_compat;
124 compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
125 # endif
126 # endif
127
128 /* Define a macro that either calls functions with the traditional malloc/free
129 calling interface, or calls functions with the mmalloc/mfree interface
130 (that adds an extra first argument), based on the state of use_extra_arg.
131 For free, do not use ?:, since some compilers, like the MIPS compilers,
132 do not allow (expr) ? void : void. */
133
134 # define CALL_CHUNKFUN(h, size) \
135 (((h) -> use_extra_arg) \
136 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
137 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
138
139 # define CALL_FREEFUN(h, old_chunk) \
140 do { \
141 if ((h) -> use_extra_arg) \
142 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
143 else \
144 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
145 } while (0)
146
147
148 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
149 Objects start on multiples of ALIGNMENT (0 means use default).
150 CHUNKFUN is the function to use to allocate chunks,
151 and FREEFUN the function to free them.
152
153 Return nonzero if successful, calls obstack_alloc_failed_handler if
154 allocation fails. */
155
156 int
_obstack_begin(struct obstack * h,int size,int alignment,void * (* chunkfun)(long),void (* freefun)(void *))157 _obstack_begin (struct obstack *h,
158 int size, int alignment,
159 void *(*chunkfun) (long),
160 void (*freefun) (void *))
161 {
162 register struct _obstack_chunk *chunk; /* points to new chunk */
163
164 if (alignment == 0)
165 alignment = DEFAULT_ALIGNMENT;
166 if (size == 0)
167 /* Default size is what GNU malloc can fit in a 4096-byte block. */
168 {
169 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
170 Use the values for range checking, because if range checking is off,
171 the extra bytes won't be missed terribly, but if range checking is on
172 and we used a larger request, a whole extra 4096 bytes would be
173 allocated.
174
175 These number are irrelevant to the new GNU malloc. I suspect it is
176 less sensitive to the size of the request. */
177 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
178 + 4 + DEFAULT_ROUNDING - 1)
179 & ~(DEFAULT_ROUNDING - 1));
180 size = 4096 - extra;
181 }
182
183 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
184 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
185 h->chunk_size = size;
186 h->alignment_mask = alignment - 1;
187 h->use_extra_arg = 0;
188
189 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
190 if (!chunk)
191 (*obstack_alloc_failed_handler) ();
192 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
193 alignment - 1);
194 h->chunk_limit = chunk->limit
195 = (char *) chunk + h->chunk_size;
196 chunk->prev = 0;
197 /* The initial chunk now contains no empty object. */
198 h->maybe_empty_object = 0;
199 h->alloc_failed = 0;
200 return 1;
201 }
202
203 int
_obstack_begin_1(struct obstack * h,int size,int alignment,void * (* chunkfun)(void *,long),void (* freefun)(void *,void *),void * arg)204 _obstack_begin_1 (struct obstack *h, int size, int alignment,
205 void *(*chunkfun) (void *, long),
206 void (*freefun) (void *, void *),
207 void *arg)
208 {
209 register struct _obstack_chunk *chunk; /* points to new chunk */
210
211 if (alignment == 0)
212 alignment = DEFAULT_ALIGNMENT;
213 if (size == 0)
214 /* Default size is what GNU malloc can fit in a 4096-byte block. */
215 {
216 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
217 Use the values for range checking, because if range checking is off,
218 the extra bytes won't be missed terribly, but if range checking is on
219 and we used a larger request, a whole extra 4096 bytes would be
220 allocated.
221
222 These number are irrelevant to the new GNU malloc. I suspect it is
223 less sensitive to the size of the request. */
224 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
225 + 4 + DEFAULT_ROUNDING - 1)
226 & ~(DEFAULT_ROUNDING - 1));
227 size = 4096 - extra;
228 }
229
230 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
231 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
232 h->chunk_size = size;
233 h->alignment_mask = alignment - 1;
234 h->extra_arg = arg;
235 h->use_extra_arg = 1;
236
237 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
238 if (!chunk)
239 (*obstack_alloc_failed_handler) ();
240 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
241 alignment - 1);
242 h->chunk_limit = chunk->limit
243 = (char *) chunk + h->chunk_size;
244 chunk->prev = 0;
245 /* The initial chunk now contains no empty object. */
246 h->maybe_empty_object = 0;
247 h->alloc_failed = 0;
248 return 1;
249 }
250
251 /* Allocate a new current chunk for the obstack *H
252 on the assumption that LENGTH bytes need to be added
253 to the current object, or a new object of length LENGTH allocated.
254 Copies any partial object from the end of the old chunk
255 to the beginning of the new one. */
256
257 void
_obstack_newchunk(struct obstack * h,int length)258 _obstack_newchunk (struct obstack *h, int length)
259 {
260 register struct _obstack_chunk *old_chunk = h->chunk;
261 register struct _obstack_chunk *new_chunk;
262 register long new_size;
263 register long obj_size = h->next_free - h->object_base;
264 register long i;
265 long already;
266 char *object_base;
267
268 /* Compute size for new chunk. */
269 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
270 if (new_size < h->chunk_size)
271 new_size = h->chunk_size;
272
273 /* Allocate and initialize the new chunk. */
274 new_chunk = CALL_CHUNKFUN (h, new_size);
275 if (!new_chunk)
276 (*obstack_alloc_failed_handler) ();
277 h->chunk = new_chunk;
278 new_chunk->prev = old_chunk;
279 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
280
281 /* Compute an aligned object_base in the new chunk */
282 object_base =
283 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
284
285 /* Move the existing object to the new chunk.
286 Word at a time is fast and is safe if the object
287 is sufficiently aligned. */
288 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
289 {
290 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
291 i >= 0; i--)
292 ((COPYING_UNIT *)object_base)[i]
293 = ((COPYING_UNIT *)h->object_base)[i];
294 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
295 but that can cross a page boundary on a machine
296 which does not do strict alignment for COPYING_UNITS. */
297 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
298 }
299 else
300 already = 0;
301 /* Copy remaining bytes one by one. */
302 for (i = already; i < obj_size; i++)
303 object_base[i] = h->object_base[i];
304
305 /* If the object just copied was the only data in OLD_CHUNK,
306 free that chunk and remove it from the chain.
307 But not if that chunk might contain an empty object. */
308 if (! h->maybe_empty_object
309 && (h->object_base
310 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
311 h->alignment_mask)))
312 {
313 new_chunk->prev = old_chunk->prev;
314 CALL_FREEFUN (h, old_chunk);
315 }
316
317 h->object_base = object_base;
318 h->next_free = h->object_base + obj_size;
319 /* The new chunk certainly contains no empty object yet. */
320 h->maybe_empty_object = 0;
321 }
322 # ifdef _LIBC
323 libc_hidden_def (_obstack_newchunk)
324 # endif
325
326 /* Return nonzero if object OBJ has been allocated from obstack H.
327 This is here for debugging.
328 If you use it in a program, you are probably losing. */
329
330 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
331 obstack.h because it is just for debugging. */
332 int _obstack_allocated_p (struct obstack *h, void *obj);
333
334 int
_obstack_allocated_p(struct obstack * h,void * obj)335 _obstack_allocated_p (struct obstack *h, void *obj)
336 {
337 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
338 register struct _obstack_chunk *plp; /* point to previous chunk if any */
339
340 lp = (h)->chunk;
341 /* We use >= rather than > since the object cannot be exactly at
342 the beginning of the chunk but might be an empty object exactly
343 at the end of an adjacent chunk. */
344 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
345 {
346 plp = lp->prev;
347 lp = plp;
348 }
349 return lp != 0;
350 }
351
352 /* Free objects in obstack H, including OBJ and everything allocate
353 more recently than OBJ. If OBJ is zero, free everything in H. */
354
355 # undef obstack_free
356
357 void
obstack_free(struct obstack * h,void * obj)358 obstack_free (struct obstack *h, void *obj)
359 {
360 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
361 register struct _obstack_chunk *plp; /* point to previous chunk if any */
362
363 lp = h->chunk;
364 /* We use >= because there cannot be an object at the beginning of a chunk.
365 But there can be an empty object at that address
366 at the end of another chunk. */
367 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
368 {
369 plp = lp->prev;
370 CALL_FREEFUN (h, lp);
371 lp = plp;
372 /* If we switch chunks, we can't tell whether the new current
373 chunk contains an empty object, so assume that it may. */
374 h->maybe_empty_object = 1;
375 }
376 if (lp)
377 {
378 h->object_base = h->next_free = (char *) (obj);
379 h->chunk_limit = lp->limit;
380 h->chunk = lp;
381 }
382 else if (obj != 0)
383 /* obj is not in any of the chunks! */
384 abort ();
385 }
386
387 # ifdef _LIBC
388 /* Older versions of libc used a function _obstack_free intended to be
389 called by non-GCC compilers. */
strong_alias(obstack_free,_obstack_free)390 strong_alias (obstack_free, _obstack_free)
391 # endif
392
393 int
394 _obstack_memory_used (struct obstack *h)
395 {
396 register struct _obstack_chunk* lp;
397 register int nbytes = 0;
398
399 for (lp = h->chunk; lp != 0; lp = lp->prev)
400 {
401 nbytes += lp->limit - (char *) lp;
402 }
403 return nbytes;
404 }
405
406 /* Define the error handler. */
407 # ifdef _LIBC
408 # include <libintl.h>
409 # endif
410 # ifndef _
411 # define _(msgid) (msgid)
412 # endif
413
414 # ifdef _LIBC
415 # include <libio/iolibio.h>
416 # endif
417
418 # ifndef __attribute__
419 /* This feature is available in gcc versions 2.5 and later. */
420 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
421 # define __attribute__(Spec) /* empty */
422 # endif
423 # endif
424
425 static void
426 __attribute__ ((noreturn))
print_and_abort(void)427 print_and_abort (void)
428 {
429 /* Don't change any of these strings. Yes, it would be possible to add
430 the newline to the string and use fputs or so. But this must not
431 happen because the "memory exhausted" message appears in other places
432 like this and the translation should be reused instead of creating
433 a very similar string which requires a separate translation. */
434 # ifdef _LIBC
435 (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
436 # else
437 fprintf (stderr, "%s\n", _("memory exhausted"));
438 # endif
439 exit (obstack_exit_failure);
440 }
441
442 #endif /* !ELIDE_CODE */
443