1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
27 /* NOTE BEFORE MODIFYING THIS FILE: _OBSTACK_INTERFACE_VERSION in
28 obstack.h must be incremented whenever callers compiled using an old
29 obstack.h can no longer properly call the functions in this file. */
31 /* Comment out all this code if we are using the GNU C Library, and are not
32 actually compiling the library itself, and the installed library
33 supports the same library interface we do. This code is part of the GNU
34 C Library, but also included in many other GNU distributions. Compiling
35 and linking in this code is a waste when using the GNU C library
36 (especially if it is a shared library). Rather than having every GNU
37 program understand 'configure --with-gnu-libc' and omit the object
38 files, it is simpler to just do this in the source for each such file. */
39 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
40 # include <gnu-versions.h>
41 # if (_GNU_OBSTACK_INTERFACE_VERSION == _OBSTACK_INTERFACE_VERSION \
42 || (_GNU_OBSTACK_INTERFACE_VERSION == 1 \
43 && _OBSTACK_INTERFACE_VERSION == 2 \
44 && defined SIZEOF_INT && defined SIZEOF_SIZE_T \
45 && SIZEOF_INT == SIZEOF_SIZE_T))
46 # define _OBSTACK_ELIDE_CODE
50 #ifndef _OBSTACK_ELIDE_CODE
51 /* If GCC, or if an oddball (testing?) host that #defines __alignof__,
52 use the already-supplied __alignof__. Otherwise, this must be Gnulib
53 (as glibc assumes GCC); defer to Gnulib's alignof_type. */
54 # if !defined __GNUC__ && !defined __alignof__
56 # define __alignof__(type) alignof_type (type)
62 # define MAX(a,b) ((a) > (b) ? (a) : (b))
65 /* Determine default alignment. */
67 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
68 But in fact it might be less smart and round addresses to as much as
69 DEFAULT_ROUNDING. So we prepare for it to do that.
71 DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h. */
72 #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \
73 MAX (__alignof__ (uintmax_t), \
74 __alignof__ (void *)))
75 #define DEFAULT_ROUNDING MAX (sizeof (long double), \
76 MAX (sizeof (uintmax_t), \
79 /* Call functions with either the traditional malloc/free calling
80 interface, or the mmalloc/mfree interface (that adds an extra first
81 argument), based on the value of use_extra_arg. */
84 call_chunkfun (struct obstack
*h
, size_t size
)
87 return h
->chunkfun
.extra (h
->extra_arg
, size
);
89 return h
->chunkfun
.plain (size
);
93 call_freefun (struct obstack
*h
, void *old_chunk
)
96 h
->freefun
.extra (h
->extra_arg
, old_chunk
);
98 h
->freefun
.plain (old_chunk
);
102 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
103 Objects start on multiples of ALIGNMENT (0 means use default).
105 Return nonzero if successful, calls obstack_alloc_failed_handler if
109 _obstack_begin_worker (struct obstack
*h
,
110 _OBSTACK_SIZE_T size
, _OBSTACK_SIZE_T alignment
)
112 struct _obstack_chunk
*chunk
; /* points to new chunk */
115 alignment
= DEFAULT_ALIGNMENT
;
117 /* Default size is what GNU malloc can fit in a 4096-byte block. */
119 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
120 Use the values for range checking, because if range checking is off,
121 the extra bytes won't be missed terribly, but if range checking is on
122 and we used a larger request, a whole extra 4096 bytes would be
125 These number are irrelevant to the new GNU malloc. I suspect it is
126 less sensitive to the size of the request. */
127 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
128 + 4 + DEFAULT_ROUNDING
- 1)
129 & ~(DEFAULT_ROUNDING
- 1));
133 h
->chunk_size
= size
;
134 h
->alignment_mask
= alignment
- 1;
136 chunk
= h
->chunk
= call_chunkfun (h
, h
->chunk_size
);
138 (*obstack_alloc_failed_handler
) ();
139 h
->next_free
= h
->object_base
= __PTR_ALIGN ((char *) chunk
, chunk
->contents
,
141 h
->chunk_limit
= chunk
->limit
= (char *) chunk
+ h
->chunk_size
;
143 /* The initial chunk now contains no empty object. */
144 h
->maybe_empty_object
= 0;
150 _obstack_begin (struct obstack
*h
,
151 _OBSTACK_SIZE_T size
, _OBSTACK_SIZE_T alignment
,
152 void *(*chunkfun
) (size_t),
153 void (*freefun
) (void *))
155 h
->chunkfun
.plain
= chunkfun
;
156 h
->freefun
.plain
= freefun
;
157 h
->use_extra_arg
= 0;
158 return _obstack_begin_worker (h
, size
, alignment
);
162 _obstack_begin_1 (struct obstack
*h
,
163 _OBSTACK_SIZE_T size
, _OBSTACK_SIZE_T alignment
,
164 void *(*chunkfun
) (void *, size_t),
165 void (*freefun
) (void *, void *),
168 h
->chunkfun
.extra
= chunkfun
;
169 h
->freefun
.extra
= freefun
;
171 h
->use_extra_arg
= 1;
172 return _obstack_begin_worker (h
, size
, alignment
);
175 /* Allocate a new current chunk for the obstack *H
176 on the assumption that LENGTH bytes need to be added
177 to the current object, or a new object of length LENGTH allocated.
178 Copies any partial object from the end of the old chunk
179 to the beginning of the new one. */
182 _obstack_newchunk (struct obstack
*h
, _OBSTACK_SIZE_T length
)
184 struct _obstack_chunk
*old_chunk
= h
->chunk
;
185 struct _obstack_chunk
*new_chunk
= 0;
186 size_t obj_size
= h
->next_free
- h
->object_base
;
189 /* Compute size for new chunk. */
190 size_t sum1
= obj_size
+ length
;
191 size_t sum2
= sum1
+ h
->alignment_mask
;
192 size_t new_size
= sum2
+ (obj_size
>> 3) + 100;
195 if (new_size
< h
->chunk_size
)
196 new_size
= h
->chunk_size
;
198 /* Allocate and initialize the new chunk. */
199 if (obj_size
<= sum1
&& sum1
<= sum2
)
200 new_chunk
= call_chunkfun (h
, new_size
);
202 (*obstack_alloc_failed_handler
)();
203 h
->chunk
= new_chunk
;
204 new_chunk
->prev
= old_chunk
;
205 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
207 /* Compute an aligned object_base in the new chunk */
209 __PTR_ALIGN ((char *) new_chunk
, new_chunk
->contents
, h
->alignment_mask
);
211 /* Move the existing object to the new chunk. */
212 memcpy (object_base
, h
->object_base
, obj_size
);
214 /* If the object just copied was the only data in OLD_CHUNK,
215 free that chunk and remove it from the chain.
216 But not if that chunk might contain an empty object. */
217 if (!h
->maybe_empty_object
219 == __PTR_ALIGN ((char *) old_chunk
, old_chunk
->contents
,
222 new_chunk
->prev
= old_chunk
->prev
;
223 call_freefun (h
, old_chunk
);
226 h
->object_base
= object_base
;
227 h
->next_free
= h
->object_base
+ obj_size
;
228 /* The new chunk certainly contains no empty object yet. */
229 h
->maybe_empty_object
= 0;
232 /* Return nonzero if object OBJ has been allocated from obstack H.
233 This is here for debugging.
234 If you use it in a program, you are probably losing. */
236 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
237 obstack.h because it is just for debugging. */
238 int _obstack_allocated_p (struct obstack
*h
, void *obj
) __attribute_pure__
;
241 _obstack_allocated_p (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 >= rather than > since the object cannot be exactly at
248 the beginning of the chunk but might be an empty object exactly
249 at the end of an adjacent chunk. */
250 while (lp
!= 0 && ((void *) lp
>= obj
|| (void *) (lp
)->limit
< obj
))
258 /* Free objects in obstack H, including OBJ and everything allocate
259 more recently than OBJ. If OBJ is zero, free everything in H. */
262 _obstack_free (struct obstack
*h
, void *obj
)
264 struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
265 struct _obstack_chunk
*plp
; /* point to previous chunk if any */
268 /* We use >= because there cannot be an object at the beginning of a chunk.
269 But there can be an empty object at that address
270 at the end of another chunk. */
271 while (lp
!= 0 && ((void *) lp
>= obj
|| (void *) (lp
)->limit
< obj
))
274 call_freefun (h
, lp
);
276 /* If we switch chunks, we can't tell whether the new current
277 chunk contains an empty object, so assume that it may. */
278 h
->maybe_empty_object
= 1;
282 h
->object_base
= h
->next_free
= (char *) (obj
);
283 h
->chunk_limit
= lp
->limit
;
287 /* obj is not in any of the chunks! */
292 _obstack_memory_used (struct obstack
*h
)
294 struct _obstack_chunk
*lp
;
295 _OBSTACK_SIZE_T nbytes
= 0;
297 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
299 nbytes
+= lp
->limit
- (char *) lp
;
304 # ifndef _OBSTACK_NO_ERROR_HANDLER
305 /* Define the error handler. */
308 /* Exit value used when 'print_and_abort' is used. */
310 int obstack_exit_failure
= EXIT_FAILURE
;
312 # include "exitfail.h"
313 # define obstack_exit_failure exit_failure
317 # include <libintl.h>
319 # include "gettext.h"
322 # define _(msgid) gettext (msgid)
326 # include <libio/iolibio.h>
329 static _Noreturn
void
330 print_and_abort (void)
332 /* Don't change any of these strings. Yes, it would be possible to add
333 the newline to the string and use fputs or so. But this must not
334 happen because the "memory exhausted" message appears in other places
335 like this and the translation should be reused instead of creating
336 a very similar string which requires a separate translation. */
338 (void) __fxprintf (NULL
, "%s\n", _("memory exhausted"));
340 fprintf (stderr
, "%s\n", _("memory exhausted"));
342 exit (obstack_exit_failure
);
345 /* The functions allocating more room by calling 'obstack_chunk_alloc'
346 jump to the handler pointed to by 'obstack_alloc_failed_handler'.
347 This can be set to a user defined function which should either
348 abort gracefully or use longjump - but shouldn't return. This
349 variable by default points to the internal function
350 'print_and_abort'. */
351 __attribute_noreturn__
void (*obstack_alloc_failed_handler
) (void)
353 # endif /* !_OBSTACK_NO_ERROR_HANDLER */
354 #endif /* !_OBSTACK_ELIDE_CODE */