1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-2025 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
26 /* NOTE BEFORE MODIFYING THIS FILE IN GNU LIBC: _OBSTACK_INTERFACE_VERSION in
27 gnu-versions.h must be incremented whenever callers compiled using an old
28 obstack.h can no longer properly call the functions in this file. */
30 /* If GCC, or if an oddball (testing?) host that #defines __alignof__,
31 use the already-supplied __alignof__. Otherwise, this must be Gnulib
32 (as glibc assumes GCC); defer to Gnulib's alignof_type. */
33 #if !defined __GNUC__ && !defined __alignof__
35 # define __alignof__(type) alignof_type (type)
41 # define MAX(a,b) ((a) > (b) ? (a) : (b))
44 /* Determine default alignment. */
46 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
47 But in fact it might be less smart and round addresses to as much as
48 DEFAULT_ROUNDING. So we prepare for it to do that.
50 DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h. */
51 #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \
52 MAX (__alignof__ (uintmax_t), \
53 __alignof__ (void *)))
54 #define DEFAULT_ROUNDING MAX (sizeof (long double), \
55 MAX (sizeof (uintmax_t), \
58 /* Call functions with either the traditional malloc/free calling
59 interface, or the mmalloc/mfree interface (that adds an extra first
60 argument), based on the value of use_extra_arg. */
63 call_chunkfun (struct obstack
*h
, size_t size
)
66 return h
->chunkfun
.extra (h
->extra_arg
, size
);
68 return h
->chunkfun
.plain (size
);
72 call_freefun (struct obstack
*h
, void *old_chunk
)
75 h
->freefun
.extra (h
->extra_arg
, old_chunk
);
77 h
->freefun
.plain (old_chunk
);
81 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
82 Objects start on multiples of ALIGNMENT (0 means use default).
84 Return nonzero if successful, calls obstack_alloc_failed_handler if
88 _obstack_begin_worker (struct obstack
*h
,
89 _OBSTACK_SIZE_T size
, _OBSTACK_SIZE_T alignment
)
91 struct _obstack_chunk
*chunk
; /* points to new chunk */
94 alignment
= DEFAULT_ALIGNMENT
;
96 /* Default size is what GNU malloc can fit in a 4096-byte block. */
98 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
99 Use the values for range checking, because if range checking is off,
100 the extra bytes won't be missed terribly, but if range checking is on
101 and we used a larger request, a whole extra 4096 bytes would be
104 These number are irrelevant to the new GNU malloc. I suspect it is
105 less sensitive to the size of the request. */
106 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
107 + 4 + DEFAULT_ROUNDING
- 1)
108 & ~(DEFAULT_ROUNDING
- 1));
112 h
->chunk_size
= size
;
113 h
->alignment_mask
= alignment
- 1;
115 chunk
= h
->chunk
= call_chunkfun (h
, h
->chunk_size
);
117 (*obstack_alloc_failed_handler
) ();
118 h
->next_free
= h
->object_base
= __PTR_ALIGN ((char *) chunk
, chunk
->contents
,
120 h
->chunk_limit
= chunk
->limit
= (char *) chunk
+ h
->chunk_size
;
122 /* The initial chunk now contains no empty object. */
123 h
->maybe_empty_object
= 0;
129 _obstack_begin (struct obstack
*h
,
130 _OBSTACK_SIZE_T size
, _OBSTACK_SIZE_T alignment
,
131 void *(*chunkfun
) (size_t),
132 void (*freefun
) (void *))
134 h
->chunkfun
.plain
= chunkfun
;
135 h
->freefun
.plain
= freefun
;
136 h
->use_extra_arg
= 0;
137 return _obstack_begin_worker (h
, size
, alignment
);
141 _obstack_begin_1 (struct obstack
*h
,
142 _OBSTACK_SIZE_T size
, _OBSTACK_SIZE_T alignment
,
143 void *(*chunkfun
) (void *, size_t),
144 void (*freefun
) (void *, void *),
147 h
->chunkfun
.extra
= chunkfun
;
148 h
->freefun
.extra
= freefun
;
150 h
->use_extra_arg
= 1;
151 return _obstack_begin_worker (h
, size
, alignment
);
154 /* Allocate a new current chunk for the obstack *H
155 on the assumption that LENGTH bytes need to be added
156 to the current object, or a new object of length LENGTH allocated.
157 Copies any partial object from the end of the old chunk
158 to the beginning of the new one. */
161 _obstack_newchunk (struct obstack
*h
, _OBSTACK_SIZE_T length
)
163 struct _obstack_chunk
*old_chunk
= h
->chunk
;
164 struct _obstack_chunk
*new_chunk
= NULL
;
165 size_t obj_size
= h
->next_free
- h
->object_base
;
168 /* Compute size for new chunk. */
169 size_t sum1
= obj_size
+ length
;
170 size_t sum2
= sum1
+ h
->alignment_mask
;
171 size_t new_size
= sum2
+ (obj_size
>> 3) + 100;
174 if (new_size
< h
->chunk_size
)
175 new_size
= h
->chunk_size
;
177 /* Allocate and initialize the new chunk. */
178 if (obj_size
<= sum1
&& sum1
<= sum2
)
179 new_chunk
= call_chunkfun (h
, new_size
);
181 (*obstack_alloc_failed_handler
)();
182 h
->chunk
= new_chunk
;
183 new_chunk
->prev
= old_chunk
;
184 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
186 /* Compute an aligned object_base in the new chunk */
188 __PTR_ALIGN ((char *) new_chunk
, new_chunk
->contents
, h
->alignment_mask
);
190 /* Move the existing object to the new chunk. */
191 memcpy (object_base
, h
->object_base
, obj_size
);
193 /* If the object just copied was the only data in OLD_CHUNK,
194 free that chunk and remove it from the chain.
195 But not if that chunk might contain an empty object. */
196 if (!h
->maybe_empty_object
198 == __PTR_ALIGN ((char *) old_chunk
, old_chunk
->contents
,
201 new_chunk
->prev
= old_chunk
->prev
;
202 call_freefun (h
, old_chunk
);
205 h
->object_base
= object_base
;
206 h
->next_free
= h
->object_base
+ obj_size
;
207 /* The new chunk certainly contains no empty object yet. */
208 h
->maybe_empty_object
= 0;
211 /* Return nonzero if object OBJ has been allocated from obstack H.
212 This is here for debugging.
213 If you use it in a program, you are probably losing. */
215 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
216 obstack.h because it is just for debugging. */
217 int _obstack_allocated_p (struct obstack
*h
, void *obj
) __attribute_pure__
;
220 _obstack_allocated_p (struct obstack
*h
, void *obj
)
222 struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
223 struct _obstack_chunk
*plp
; /* point to previous chunk if any */
226 /* We use >= rather than > since the object cannot be exactly at
227 the beginning of the chunk but might be an empty object exactly
228 at the end of an adjacent chunk. */
229 while (lp
!= NULL
&& ((void *) lp
>= obj
|| (void *) (lp
)->limit
< obj
))
237 /* Free objects in obstack H, including OBJ and everything allocate
238 more recently than OBJ. If OBJ is zero, free everything in H. */
241 _obstack_free (struct obstack
*h
, void *obj
)
243 struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
244 struct _obstack_chunk
*plp
; /* point to previous chunk if any */
247 /* We use >= because there cannot be an object at the beginning of a chunk.
248 But there can be an empty object at that address
249 at the end of another chunk. */
250 while (lp
!= NULL
&& ((void *) lp
>= obj
|| (void *) (lp
)->limit
< obj
))
253 call_freefun (h
, lp
);
255 /* If we switch chunks, we can't tell whether the new current
256 chunk contains an empty object, so assume that it may. */
257 h
->maybe_empty_object
= 1;
261 h
->object_base
= h
->next_free
= (char *) (obj
);
262 h
->chunk_limit
= lp
->limit
;
265 else if (obj
!= NULL
)
266 /* obj is not in any of the chunks! */
271 _obstack_memory_used (struct obstack
*h
)
273 struct _obstack_chunk
*lp
;
274 _OBSTACK_SIZE_T nbytes
= 0;
276 for (lp
= h
->chunk
; lp
!= NULL
; lp
= lp
->prev
)
278 nbytes
+= lp
->limit
- (char *) lp
;
283 #ifndef _OBSTACK_NO_ERROR_HANDLER
284 /* Define the error handler. */
287 /* Exit value used when 'print_and_abort' is used. */
289 int obstack_exit_failure
= EXIT_FAILURE
;
291 # include "exitfail.h"
292 # define obstack_exit_failure exit_failure
296 # include <libintl.h>
298 # define _(msgid) gettext (msgid)
301 # include "gettext.h"
303 # define _(msgid) dgettext ("gnulib", msgid)
308 # include <libio/iolibio.h>
311 static __attribute_noreturn__
void
312 print_and_abort (void)
314 /* Don't change any of these strings. Yes, it would be possible to add
315 the newline to the string and use fputs or so. But this must not
316 happen because the "memory exhausted" message appears in other places
317 like this and the translation should be reused instead of creating
318 a very similar string which requires a separate translation. */
320 (void) __fxprintf (NULL
, "%s\n", _("memory exhausted"));
322 fprintf (stderr
, "%s\n", _("memory exhausted"));
324 exit (obstack_exit_failure
);
327 /* The functions allocating more room by calling 'obstack_chunk_alloc'
328 jump to the handler pointed to by 'obstack_alloc_failed_handler'.
329 This can be set to a user defined function which should either
330 abort gracefully or use longjump - but shouldn't return. This
331 variable by default points to the internal function
332 'print_and_abort'. */
333 __attribute_noreturn__
void (*obstack_alloc_failed_handler
) (void)
335 #endif /* !_OBSTACK_NO_ERROR_HANDLER */