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