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