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