1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-2014 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 <http://www.gnu.org/licenses/>. */
22 # include <shlib-compat.h>
28 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
29 incremented whenever callers compiled using an old obstack.h can no
30 longer properly call the functions in this obstack.c. */
31 #define OBSTACK_INTERFACE_VERSION 1
33 /* Comment out all this code if we are using the GNU C Library, and are not
34 actually compiling the library itself, and the installed library
35 supports the same library interface we do. This code is part of the GNU
36 C Library, but also included in many other GNU distributions. Compiling
37 and linking in this code is a waste when using the GNU C library
38 (especially if it is a shared library). Rather than having every GNU
39 program understand 'configure --with-gnu-libc' and omit the object
40 files, it is simpler to just do this in the source for each such file. */
42 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
43 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
44 # include <gnu-versions.h>
45 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
57 /* Determine default alignment. */
69 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
70 But in fact it might be less smart and round addresses to as much as
71 DEFAULT_ROUNDING. So we prepare for it to do that. */
74 DEFAULT_ALIGNMENT
= offsetof (struct fooalign
, u
),
75 DEFAULT_ROUNDING
= sizeof (union fooround
)
78 /* When we copy a long block of data, this is the unit to do it with.
79 On some machines, copying successive ints does not work;
80 in such a case, redefine COPYING_UNIT to 'long' (if that works)
81 or 'char' as a last resort. */
83 # define COPYING_UNIT int
87 /* The functions allocating more room by calling 'obstack_chunk_alloc'
88 jump to the handler pointed to by 'obstack_alloc_failed_handler'.
89 This can be set to a user defined function which should either
90 abort gracefully or use longjump - but shouldn't return. This
91 variable by default points to the internal function
93 static _Noreturn
void print_and_abort (void);
94 void (*obstack_alloc_failed_handler
) (void) = print_and_abort
;
96 /* Exit value used when 'print_and_abort' is used. */
99 int obstack_exit_failure
= EXIT_FAILURE
;
101 # include "exitfail.h"
102 # define obstack_exit_failure exit_failure
106 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
107 /* A looong time ago (before 1994, anyway; we're not sure) this global variable
108 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
109 library still exports it because somebody might use it. */
110 struct obstack
*_obstack_compat
= 0;
111 compat_symbol (libc
, _obstack_compat
, _obstack
, GLIBC_2_0
);
115 /* Define a macro that either calls functions with the traditional malloc/free
116 calling interface, or calls functions with the mmalloc/mfree interface
117 (that adds an extra first argument), based on the state of use_extra_arg.
118 For free, do not use ?:, since some compilers, like the MIPS compilers,
119 do not allow (expr) ? void : void. */
121 # define CALL_CHUNKFUN(h, size) \
122 (((h)->use_extra_arg) \
123 ? (*(h)->chunkfun)((h)->extra_arg, (size)) \
124 : (*(struct _obstack_chunk *(*)(long))(h)->chunkfun)((size)))
126 # define CALL_FREEFUN(h, old_chunk) \
128 if ((h)->use_extra_arg) \
129 (*(h)->freefun)((h)->extra_arg, (old_chunk)); \
131 (*(void (*)(void *))(h)->freefun)((old_chunk)); \
135 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
136 Objects start on multiples of ALIGNMENT (0 means use default).
137 CHUNKFUN is the function to use to allocate chunks,
138 and FREEFUN the function to free them.
140 Return nonzero if successful, calls obstack_alloc_failed_handler if
144 _obstack_begin (struct obstack
*h
,
145 int size
, int alignment
,
146 void *(*chunkfun
) (long),
147 void (*freefun
) (void *))
149 struct _obstack_chunk
*chunk
; /* points to new chunk */
152 alignment
= DEFAULT_ALIGNMENT
;
154 /* Default size is what GNU malloc can fit in a 4096-byte block. */
156 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
157 Use the values for range checking, because if range checking is off,
158 the extra bytes won't be missed terribly, but if range checking is on
159 and we used a larger request, a whole extra 4096 bytes would be
162 These number are irrelevant to the new GNU malloc. I suspect it is
163 less sensitive to the size of the request. */
164 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
165 + 4 + DEFAULT_ROUNDING
- 1)
166 & ~(DEFAULT_ROUNDING
- 1));
170 h
->chunkfun
= (struct _obstack_chunk
* (*) (void *, long)) chunkfun
;
171 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
172 h
->chunk_size
= size
;
173 h
->alignment_mask
= alignment
- 1;
174 h
->use_extra_arg
= 0;
176 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
->chunk_size
);
178 (*obstack_alloc_failed_handler
) ();
179 h
->next_free
= h
->object_base
= __PTR_ALIGN ((char *) chunk
, chunk
->contents
,
181 h
->chunk_limit
= chunk
->limit
182 = (char *) chunk
+ h
->chunk_size
;
184 /* The initial chunk now contains no empty object. */
185 h
->maybe_empty_object
= 0;
191 _obstack_begin_1 (struct obstack
*h
, int size
, int alignment
,
192 void *(*chunkfun
) (void *, long),
193 void (*freefun
) (void *, void *),
196 struct _obstack_chunk
*chunk
; /* points to new chunk */
199 alignment
= DEFAULT_ALIGNMENT
;
201 /* Default size is what GNU malloc can fit in a 4096-byte block. */
203 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
204 Use the values for range checking, because if range checking is off,
205 the extra bytes won't be missed terribly, but if range checking is on
206 and we used a larger request, a whole extra 4096 bytes would be
209 These number are irrelevant to the new GNU malloc. I suspect it is
210 less sensitive to the size of the request. */
211 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
212 + 4 + DEFAULT_ROUNDING
- 1)
213 & ~(DEFAULT_ROUNDING
- 1));
217 h
->chunkfun
= (struct _obstack_chunk
* (*)(void *,long)) chunkfun
;
218 h
->freefun
= (void (*) (void *, struct _obstack_chunk
*)) freefun
;
219 h
->chunk_size
= size
;
220 h
->alignment_mask
= alignment
- 1;
222 h
->use_extra_arg
= 1;
224 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
->chunk_size
);
226 (*obstack_alloc_failed_handler
) ();
227 h
->next_free
= h
->object_base
= __PTR_ALIGN ((char *) chunk
, chunk
->contents
,
229 h
->chunk_limit
= chunk
->limit
230 = (char *) chunk
+ h
->chunk_size
;
232 /* The initial chunk now contains no empty object. */
233 h
->maybe_empty_object
= 0;
238 /* Allocate a new current chunk for the obstack *H
239 on the assumption that LENGTH bytes need to be added
240 to the current object, or a new object of length LENGTH allocated.
241 Copies any partial object from the end of the old chunk
242 to the beginning of the new one. */
245 _obstack_newchunk (struct obstack
*h
, int length
)
247 struct _obstack_chunk
*old_chunk
= h
->chunk
;
248 struct _obstack_chunk
*new_chunk
;
250 long obj_size
= h
->next_free
- h
->object_base
;
255 /* Compute size for new chunk. */
256 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + h
->alignment_mask
+ 100;
257 if (new_size
< h
->chunk_size
)
258 new_size
= h
->chunk_size
;
260 /* Allocate and initialize the new chunk. */
261 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
263 (*obstack_alloc_failed_handler
)();
264 h
->chunk
= new_chunk
;
265 new_chunk
->prev
= old_chunk
;
266 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
268 /* Compute an aligned object_base in the new chunk */
270 __PTR_ALIGN ((char *) new_chunk
, new_chunk
->contents
, h
->alignment_mask
);
272 /* Move the existing object to the new chunk.
273 Word at a time is fast and is safe if the object
274 is sufficiently aligned. */
275 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
277 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
279 ((COPYING_UNIT
*) object_base
)[i
]
280 = ((COPYING_UNIT
*) h
->object_base
)[i
];
281 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
282 but that can cross a page boundary on a machine
283 which does not do strict alignment for COPYING_UNITS. */
284 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
288 /* Copy remaining bytes one by one. */
289 for (i
= already
; i
< obj_size
; i
++)
290 object_base
[i
] = h
->object_base
[i
];
292 /* If the object just copied was the only data in OLD_CHUNK,
293 free that chunk and remove it from the chain.
294 But not if that chunk might contain an empty object. */
295 if (!h
->maybe_empty_object
297 == __PTR_ALIGN ((char *) old_chunk
, old_chunk
->contents
,
300 new_chunk
->prev
= old_chunk
->prev
;
301 CALL_FREEFUN (h
, old_chunk
);
304 h
->object_base
= object_base
;
305 h
->next_free
= h
->object_base
+ obj_size
;
306 /* The new chunk certainly contains no empty object yet. */
307 h
->maybe_empty_object
= 0;
310 libc_hidden_def (_obstack_newchunk
)
313 /* Return nonzero if object OBJ has been allocated from obstack H.
314 This is here for debugging.
315 If you use it in a program, you are probably losing. */
317 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
318 obstack.h because it is just for debugging. */
319 int _obstack_allocated_p (struct obstack
*h
, void *obj
) __attribute_pure__
;
322 _obstack_allocated_p (struct obstack
*h
, void *obj
)
324 struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
325 struct _obstack_chunk
*plp
; /* point to previous chunk if any */
328 /* We use >= rather than > since the object cannot be exactly at
329 the beginning of the chunk but might be an empty object exactly
330 at the end of an adjacent chunk. */
331 while (lp
!= 0 && ((void *) lp
>= obj
|| (void *) (lp
)->limit
< obj
))
339 /* Free objects in obstack H, including OBJ and everything allocate
340 more recently than OBJ. If OBJ is zero, free everything in H. */
345 __obstack_free (struct obstack
*h
, void *obj
)
347 struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
348 struct _obstack_chunk
*plp
; /* point to previous chunk if any */
351 /* We use >= because there cannot be an object at the beginning of a chunk.
352 But there can be an empty object at that address
353 at the end of another chunk. */
354 while (lp
!= 0 && ((void *) lp
>= obj
|| (void *) (lp
)->limit
< obj
))
357 CALL_FREEFUN (h
, lp
);
359 /* If we switch chunks, we can't tell whether the new current
360 chunk contains an empty object, so assume that it may. */
361 h
->maybe_empty_object
= 1;
365 h
->object_base
= h
->next_free
= (char *) (obj
);
366 h
->chunk_limit
= lp
->limit
;
370 /* obj is not in any of the chunks! */
375 /* Older versions of libc used a function _obstack_free intended to be
376 called by non-GCC compilers. */
377 strong_alias (obstack_free
, _obstack_free
)
381 _obstack_memory_used (struct obstack
*h
)
383 struct _obstack_chunk
*lp
;
386 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
388 nbytes
+= lp
->limit
- (char *) lp
;
393 /* Define the error handler. */
395 # include <libintl.h>
397 # include "gettext.h"
400 # define _(msgid) gettext (msgid)
404 # include <libio/iolibio.h>
407 static _Noreturn
void
408 print_and_abort (void)
410 /* Don't change any of these strings. Yes, it would be possible to add
411 the newline to the string and use fputs or so. But this must not
412 happen because the "memory exhausted" message appears in other places
413 like this and the translation should be reused instead of creating
414 a very similar string which requires a separate translation. */
416 (void) __fxprintf (NULL
, "%s\n", _("memory exhausted"));
418 fprintf (stderr
, "%s\n", _("memory exhausted"));
420 exit (obstack_exit_failure
);
423 #endif /* !ELIDE_CODE */