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