1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
17
18
19 #ifdef _LIBC
20 # include <obstack.h>
21 #else
22 # include <config.h>
23 # include "obstack.h"
24 #endif
25
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. */
29
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__
34 # include <alignof.h>
35 # define __alignof__(type) alignof_type (type)
36 #endif
37 #include <stdlib.h>
38 #include <stdint.h>
39
40 #ifndef MAX
41 # define MAX(a,b) ((a) > (b) ? (a) : (b))
42 #endif
43
44 /* Determine default alignment. */
45
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.
49
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), \
56 sizeof (void *)))
57
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. */
61
62 static void *
63 call_chunkfun (struct obstack *h, size_t size)
64 {
65 if (h->use_extra_arg)
66 return h->chunkfun.extra (h->extra_arg, size);
67 else
68 return h->chunkfun.plain (size);
69 }
70
71 static void
72 call_freefun (struct obstack *h, void *old_chunk)
73 {
74 if (h->use_extra_arg)
75 h->freefun.extra (h->extra_arg, old_chunk);
76 else
77 h->freefun.plain (old_chunk);
78 }
79
80
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).
83
84 Return nonzero if successful, calls obstack_alloc_failed_handler if
85 allocation fails. */
86
87 static int
88 _obstack_begin_worker (struct obstack *h,
89 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment)
90 {
91 struct _obstack_chunk *chunk; /* points to new chunk */
92
93 if (alignment == 0)
94 alignment = DEFAULT_ALIGNMENT;
95 if (size == 0)
96 /* Default size is what GNU malloc can fit in a 4096-byte block. */
97 {
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
102 allocated.
103
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));
109 size = 4096 - extra;
110 }
111
112 h->chunk_size = size;
113 h->alignment_mask = alignment - 1;
114
115 chunk = h->chunk = call_chunkfun (h, h->chunk_size);
116 if (!chunk)
117 (*obstack_alloc_failed_handler) ();
118 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
119 alignment - 1);
120 h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size;
121 chunk->prev = 0;
122 /* The initial chunk now contains no empty object. */
123 h->maybe_empty_object = 0;
124 h->alloc_failed = 0;
125 return 1;
126 }
127
128 int
129 _obstack_begin (struct obstack *h,
130 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
131 void *(*chunkfun) (size_t),
132 void (*freefun) (void *))
133 {
134 h->chunkfun.plain = chunkfun;
135 h->freefun.plain = freefun;
136 h->use_extra_arg = 0;
137 return _obstack_begin_worker (h, size, alignment);
138 }
139
140 int
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 *),
145 void *arg)
146 {
147 h->chunkfun.extra = chunkfun;
148 h->freefun.extra = freefun;
149 h->extra_arg = arg;
150 h->use_extra_arg = 1;
151 return _obstack_begin_worker (h, size, alignment);
152 }
153
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. */
159
160 void
161 _obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length)
162 {
163 struct _obstack_chunk *old_chunk = h->chunk;
164 struct _obstack_chunk *new_chunk = 0;
165 size_t obj_size = h->next_free - h->object_base;
166 char *object_base;
167
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;
172 if (new_size < sum2)
173 new_size = sum2;
174 if (new_size < h->chunk_size)
175 new_size = h->chunk_size;
176
177 /* Allocate and initialize the new chunk. */
178 if (obj_size <= sum1 && sum1 <= sum2)
179 new_chunk = call_chunkfun (h, new_size);
180 if (!new_chunk)
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;
185
186 /* Compute an aligned object_base in the new chunk */
187 object_base =
188 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
189
190 /* Move the existing object to the new chunk. */
191 memcpy (object_base, h->object_base, obj_size);
192
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
197 && (h->object_base
198 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
199 h->alignment_mask)))
200 {
201 new_chunk->prev = old_chunk->prev;
202 call_freefun (h, old_chunk);
203 }
204
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;
209 }
210
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. */
214
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__;
218
219 int
220 _obstack_allocated_p (struct obstack *h, void *obj)
221 {
222 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
223 struct _obstack_chunk *plp; /* point to previous chunk if any */
224
225 lp = (h)->chunk;
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 != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
230 {
231 plp = lp->prev;
232 lp = plp;
233 }
234 return lp != 0;
235 }
236
237 /* Free objects in obstack H, including OBJ and everything allocate
238 more recently than OBJ. If OBJ is zero, free everything in H. */
239
240 void
241 _obstack_free (struct obstack *h, void *obj)
242 {
243 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
244 struct _obstack_chunk *plp; /* point to previous chunk if any */
245
246 lp = h->chunk;
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 != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
251 {
252 plp = lp->prev;
253 call_freefun (h, lp);
254 lp = plp;
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;
258 }
259 if (lp)
260 {
261 h->object_base = h->next_free = (char *) (obj);
262 h->chunk_limit = lp->limit;
263 h->chunk = lp;
264 }
265 else if (obj != 0)
266 /* obj is not in any of the chunks! */
267 abort ();
268 }
269
270 _OBSTACK_SIZE_T
271 _obstack_memory_used (struct obstack *h)
272 {
273 struct _obstack_chunk *lp;
274 _OBSTACK_SIZE_T nbytes = 0;
275
276 for (lp = h->chunk; lp != 0; lp = lp->prev)
277 {
278 nbytes += lp->limit - (char *) lp;
279 }
280 return nbytes;
281 }
282
283 #ifndef _OBSTACK_NO_ERROR_HANDLER
284 /* Define the error handler. */
285 # include <stdio.h>
286
287 /* Exit value used when 'print_and_abort' is used. */
288 # ifdef _LIBC
289 int obstack_exit_failure = EXIT_FAILURE;
290 # else
291 # include "exitfail.h"
292 # define obstack_exit_failure exit_failure
293 # endif
294
295 # ifdef _LIBC
296 # include <libintl.h>
297 # else
298 # include "gettext.h"
299 # endif
300 # ifndef _
301 # define _(msgid) gettext (msgid)
302 # endif
303
304 # ifdef _LIBC
305 # include <libio/iolibio.h>
306 # endif
307
308 static __attribute_noreturn__ void
309 print_and_abort (void)
310 {
311 /* Don't change any of these strings. Yes, it would be possible to add
312 the newline to the string and use fputs or so. But this must not
313 happen because the "memory exhausted" message appears in other places
314 like this and the translation should be reused instead of creating
315 a very similar string which requires a separate translation. */
316 # ifdef _LIBC
317 (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
318 # else
319 fprintf (stderr, "%s\n", _("memory exhausted"));
320 # endif
321 exit (obstack_exit_failure);
322 }
323
324 /* The functions allocating more room by calling 'obstack_chunk_alloc'
325 jump to the handler pointed to by 'obstack_alloc_failed_handler'.
326 This can be set to a user defined function which should either
327 abort gracefully or use longjump - but shouldn't return. This
328 variable by default points to the internal function
329 'print_and_abort'. */
330 __attribute_noreturn__ void (*obstack_alloc_failed_handler) (void)
331 = print_and_abort;
332 #endif /* !_OBSTACK_NO_ERROR_HANDLER */