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