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