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