1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-1994, 1996-1999, 2000-2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. Its master source is NOT part of
4 the C library, however. The master source lives in /gd/gnu/lib.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 # include <shlib-compat.h>
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. */
35 #define OBSTACK_INTERFACE_VERSION 1
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. */
46 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
47 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
48 # include <gnu-versions.h>
49 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
54 #if defined _LIBC && defined USE_IN_LIBIO
64 # include <inttypes.h>
66 # if HAVE_STDINT_H || defined _LIBC
70 /* Determine default alignment. */
82 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
83 But in fact it might be less smart and round addresses to as much as
84 DEFAULT_ROUNDING. So we prepare for it to do that. */
87 DEFAULT_ALIGNMENT
= offsetof (struct fooalign
, u
),
88 DEFAULT_ROUNDING
= sizeof (union fooround
)
91 /* When we copy a long block of data, this is the unit to do it with.
92 On some machines, copying successive ints does not work;
93 in such a case, redefine COPYING_UNIT to `long' (if that works)
94 or `char' as a last resort. */
96 # define COPYING_UNIT int
100 /* The functions allocating more room by calling `obstack_chunk_alloc'
101 jump to the handler pointed to by `obstack_alloc_failed_handler'.
102 This can be set to a user defined function which should either
103 abort gracefully or use longjump - but shouldn't return. This
104 variable by default points to the internal function
105 `print_and_abort'. */
106 static void print_and_abort (void);
107 void (*obstack_alloc_failed_handler
) (void) = print_and_abort
;
109 /* Exit value used when `print_and_abort' is used. */
112 int obstack_exit_failure
= EXIT_FAILURE
;
114 # include "exitfail.h"
115 # define obstack_exit_failure exit_failure
119 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
120 /* A looong time ago (before 1994, anyway; we're not sure) this global variable
121 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
122 library still exports it because somebody might use it. */
123 struct obstack
*_obstack_compat
;
124 compat_symbol (libc
, _obstack_compat
, _obstack
, GLIBC_2_0
);
128 /* Define a macro that either calls functions with the traditional malloc/free
129 calling interface, or calls functions with the mmalloc/mfree interface
130 (that adds an extra first argument), based on the state of use_extra_arg.
131 For free, do not use ?:, since some compilers, like the MIPS compilers,
132 do not allow (expr) ? void : void. */
134 # define CALL_CHUNKFUN(h, size) \
135 (((h) -> use_extra_arg) \
136 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
137 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
139 # define CALL_FREEFUN(h, old_chunk) \
141 if ((h) -> use_extra_arg) \
142 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
144 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
148 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
149 Objects start on multiples of ALIGNMENT (0 means use default).
150 CHUNKFUN is the function to use to allocate chunks,
151 and FREEFUN the function to free them.
153 Return nonzero if successful, calls obstack_alloc_failed_handler if
157 _obstack_begin (struct obstack
*h
,
158 int size
, int alignment
,
159 void *(*chunkfun
) (long),
160 void (*freefun
) (void *))
162 register struct _obstack_chunk
*chunk
; /* points to new chunk */
165 alignment
= DEFAULT_ALIGNMENT
;
167 /* Default size is what GNU malloc can fit in a 4096-byte block. */
169 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
170 Use the values for range checking, because if range checking is off,
171 the extra bytes won't be missed terribly, but if range checking is on
172 and we used a larger request, a whole extra 4096 bytes would be
175 These number are irrelevant to the new GNU malloc. I suspect it is
176 less sensitive to the size of the request. */
177 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
178 + 4 + DEFAULT_ROUNDING
- 1)
179 & ~(DEFAULT_ROUNDING
- 1));
183 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *, long)) chunkfun
;
184 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
185 h
->chunk_size
= size
;
186 h
->alignment_mask
= alignment
- 1;
187 h
->use_extra_arg
= 0;
189 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
191 (*obstack_alloc_failed_handler
) ();
192 h
->next_free
= h
->object_base
= __PTR_ALIGN ((char *) chunk
, chunk
->contents
,
194 h
->chunk_limit
= chunk
->limit
195 = (char *) chunk
+ h
->chunk_size
;
197 /* The initial chunk now contains no empty object. */
198 h
->maybe_empty_object
= 0;
204 _obstack_begin_1 (struct obstack
*h
, int size
, int alignment
,
205 void *(*chunkfun
) (void *, long),
206 void (*freefun
) (void *, void *),
209 register struct _obstack_chunk
*chunk
; /* points to new chunk */
212 alignment
= DEFAULT_ALIGNMENT
;
214 /* Default size is what GNU malloc can fit in a 4096-byte block. */
216 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
217 Use the values for range checking, because if range checking is off,
218 the extra bytes won't be missed terribly, but if range checking is on
219 and we used a larger request, a whole extra 4096 bytes would be
222 These number are irrelevant to the new GNU malloc. I suspect it is
223 less sensitive to the size of the request. */
224 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
225 + 4 + DEFAULT_ROUNDING
- 1)
226 & ~(DEFAULT_ROUNDING
- 1));
230 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *,long)) chunkfun
;
231 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
232 h
->chunk_size
= size
;
233 h
->alignment_mask
= alignment
- 1;
235 h
->use_extra_arg
= 1;
237 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
239 (*obstack_alloc_failed_handler
) ();
240 h
->next_free
= h
->object_base
= __PTR_ALIGN ((char *) chunk
, chunk
->contents
,
242 h
->chunk_limit
= chunk
->limit
243 = (char *) chunk
+ h
->chunk_size
;
245 /* The initial chunk now contains no empty object. */
246 h
->maybe_empty_object
= 0;
251 /* Allocate a new current chunk for the obstack *H
252 on the assumption that LENGTH bytes need to be added
253 to the current object, or a new object of length LENGTH allocated.
254 Copies any partial object from the end of the old chunk
255 to the beginning of the new one. */
258 _obstack_newchunk (struct obstack
*h
, int length
)
260 register struct _obstack_chunk
*old_chunk
= h
->chunk
;
261 register struct _obstack_chunk
*new_chunk
;
262 register long new_size
;
263 register long obj_size
= h
->next_free
- h
->object_base
;
268 /* Compute size for new chunk. */
269 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + h
->alignment_mask
+ 100;
270 if (new_size
< h
->chunk_size
)
271 new_size
= h
->chunk_size
;
273 /* Allocate and initialize the new chunk. */
274 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
276 (*obstack_alloc_failed_handler
) ();
277 h
->chunk
= new_chunk
;
278 new_chunk
->prev
= old_chunk
;
279 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
281 /* Compute an aligned object_base in the new chunk */
283 __PTR_ALIGN ((char *) new_chunk
, new_chunk
->contents
, h
->alignment_mask
);
285 /* Move the existing object to the new chunk.
286 Word at a time is fast and is safe if the object
287 is sufficiently aligned. */
288 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
290 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
292 ((COPYING_UNIT
*)object_base
)[i
]
293 = ((COPYING_UNIT
*)h
->object_base
)[i
];
294 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
295 but that can cross a page boundary on a machine
296 which does not do strict alignment for COPYING_UNITS. */
297 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
301 /* Copy remaining bytes one by one. */
302 for (i
= already
; i
< obj_size
; i
++)
303 object_base
[i
] = h
->object_base
[i
];
305 /* If the object just copied was the only data in OLD_CHUNK,
306 free that chunk and remove it from the chain.
307 But not if that chunk might contain an empty object. */
308 if (! h
->maybe_empty_object
310 == __PTR_ALIGN ((char *) old_chunk
, old_chunk
->contents
,
313 new_chunk
->prev
= old_chunk
->prev
;
314 CALL_FREEFUN (h
, old_chunk
);
317 h
->object_base
= object_base
;
318 h
->next_free
= h
->object_base
+ obj_size
;
319 /* The new chunk certainly contains no empty object yet. */
320 h
->maybe_empty_object
= 0;
323 libc_hidden_def (_obstack_newchunk
)
326 /* Return nonzero if object OBJ has been allocated from obstack H.
327 This is here for debugging.
328 If you use it in a program, you are probably losing. */
330 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
331 obstack.h because it is just for debugging. */
332 int _obstack_allocated_p (struct obstack
*h
, void *obj
);
335 _obstack_allocated_p (struct obstack
*h
, void *obj
)
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 */
341 /* We use >= rather than > since the object cannot be exactly at
342 the beginning of the chunk but might be an empty object exactly
343 at the end of an adjacent chunk. */
344 while (lp
!= 0 && ((void *) lp
>= obj
|| (void *) (lp
)->limit
< obj
))
352 /* Free objects in obstack H, including OBJ and everything allocate
353 more recently than OBJ. If OBJ is zero, free everything in H. */
358 obstack_free (struct obstack
*h
, void *obj
)
360 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
361 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
364 /* We use >= because there cannot be an object at the beginning of a chunk.
365 But there can be an empty object at that address
366 at the end of another chunk. */
367 while (lp
!= 0 && ((void *) lp
>= obj
|| (void *) (lp
)->limit
< obj
))
370 CALL_FREEFUN (h
, lp
);
372 /* If we switch chunks, we can't tell whether the new current
373 chunk contains an empty object, so assume that it may. */
374 h
->maybe_empty_object
= 1;
378 h
->object_base
= h
->next_free
= (char *) (obj
);
379 h
->chunk_limit
= lp
->limit
;
383 /* obj is not in any of the chunks! */
388 /* Older versions of libc used a function _obstack_free intended to be
389 called by non-GCC compilers. */
390 strong_alias (obstack_free
, _obstack_free
)
394 _obstack_memory_used (struct obstack
*h
)
396 register struct _obstack_chunk
* lp
;
397 register int nbytes
= 0;
399 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
401 nbytes
+= lp
->limit
- (char *) lp
;
406 /* Define the error handler. */
408 # include <libintl.h>
410 # include "gettext.h"
413 # define _(msgid) gettext (msgid)
417 # include <libio/iolibio.h>
420 # ifndef __attribute__
421 /* This feature is available in gcc versions 2.5 and later. */
422 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
423 # define __attribute__(Spec) /* empty */
428 __attribute__ ((noreturn
))
429 print_and_abort (void)
431 /* Don't change any of these strings. Yes, it would be possible to add
432 the newline to the string and use fputs or so. But this must not
433 happen because the "memory exhausted" message appears in other places
434 like this and the translation should be reused instead of creating
435 a very similar string which requires a separate translation. */
436 # if defined _LIBC && defined USE_IN_LIBIO
437 if (_IO_fwide (stderr
, 0) > 0)
438 __fwprintf (stderr
, L
"%s\n", _("memory exhausted"));
441 fprintf (stderr
, "%s\n", _("memory exhausted"));
442 exit (obstack_exit_failure
);
445 #endif /* !ELIDE_CODE */