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