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