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