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