7f51a271f4f1e6ab1b632025b4aa5e2d02ea3e94
[supertux.git] / external / obstack / obstack.c
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, write to the Free
18    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.  */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "obstack.h"
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 #include <stdio.h>              /* Random thing to get __GNU_LIBRARY__.  */
32 #include <stddef.h>
33 #include <stdint.h>
34
35 /* Determine default alignment.  */
36 union fooround
37 {
38   uintmax_t i;
39   long double d;
40   void *p;
41 };
42 struct fooalign
43 {
44   char c;
45   union fooround u;
46 };
47 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
48    But in fact it might be less smart and round addresses to as much as
49    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
50 enum
51   {
52     DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
53     DEFAULT_ROUNDING = sizeof (union fooround)
54   };
55
56 /* When we copy a long block of data, this is the unit to do it with.
57    On some machines, copying successive ints does not work;
58    in such a case, redefine COPYING_UNIT to `long' (if that works)
59    or `char' as a last resort.  */
60 # ifndef COPYING_UNIT
61 #  define COPYING_UNIT int
62 # endif
63
64
65 /* The functions allocating more room by calling `obstack_chunk_alloc'
66    jump to the handler pointed to by `obstack_alloc_failed_handler'.
67    This can be set to a user defined function which should either
68    abort gracefully or use longjump - but shouldn't return.  This
69    variable by default points to the internal function
70    `print_and_abort'.  */
71 static void print_and_abort (void);
72 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
73
74 /* Exit value used when `print_and_abort' is used.  */
75 # include <stdlib.h>
76 int obstack_exit_failure = EXIT_FAILURE;
77
78 /* Define a macro that either calls functions with the traditional malloc/free
79    calling interface, or calls functions with the mmalloc/mfree interface
80    (that adds an extra first argument), based on the state of use_extra_arg.
81    For free, do not use ?:, since some compilers, like the MIPS compilers,
82    do not allow (expr) ? void : void.  */
83
84 # define CALL_CHUNKFUN(h, size) \
85   (((h) -> use_extra_arg) \
86    ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
87    : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
88
89 # define CALL_FREEFUN(h, old_chunk) \
90   do { \
91     if ((h) -> use_extra_arg) \
92       (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
93     else \
94       (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
95   } while (0)
96
97 /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
98    Objects start on multiples of ALIGNMENT (0 means use default).
99    CHUNKFUN is the function to use to allocate chunks,
100    and FREEFUN the function to free them.
101
102    Return nonzero if successful, calls obstack_alloc_failed_handler if
103    allocation fails.  */
104
105 int
106 _obstack_begin (struct obstack *h,
107                 int size, int alignment,
108                 void *(*chunkfun) (long),
109                 void (*freefun) (void *))
110 {
111   register struct _obstack_chunk *chunk; /* points to new chunk */
112
113   if (alignment == 0)
114     alignment = DEFAULT_ALIGNMENT;
115   if (size == 0)
116     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
117     {
118       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
119          Use the values for range checking, because if range checking is off,
120          the extra bytes won't be missed terribly, but if range checking is on
121          and we used a larger request, a whole extra 4096 bytes would be
122          allocated.
123
124          These number are irrelevant to the new GNU malloc.  I suspect it is
125          less sensitive to the size of the request.  */
126       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
127                     + 4 + DEFAULT_ROUNDING - 1)
128                    & ~(DEFAULT_ROUNDING - 1));
129       size = 4096 - extra;
130     }
131
132   h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
133   h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
134   h->chunk_size = size;
135   h->alignment_mask = alignment - 1;
136   h->use_extra_arg = 0;
137
138   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
139   if (!chunk)
140     (*obstack_alloc_failed_handler) ();
141   h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
142                                                alignment - 1);
143   h->chunk_limit = chunk->limit
144     = (char *) chunk + h->chunk_size;
145   chunk->prev = 0;
146   /* The initial chunk now contains no empty object.  */
147   h->maybe_empty_object = 0;
148   h->alloc_failed = 0;
149   return 1;
150 }
151
152 int
153 _obstack_begin_1 (struct obstack *h, int size, int alignment,
154                   void *(*chunkfun) (void *, long),
155                   void (*freefun) (void *, void *),
156                   void *arg)
157 {
158   register struct _obstack_chunk *chunk; /* points to new chunk */
159
160   if (alignment == 0)
161     alignment = DEFAULT_ALIGNMENT;
162   if (size == 0)
163     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
164     {
165       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
166          Use the values for range checking, because if range checking is off,
167          the extra bytes won't be missed terribly, but if range checking is on
168          and we used a larger request, a whole extra 4096 bytes would be
169          allocated.
170
171          These number are irrelevant to the new GNU malloc.  I suspect it is
172          less sensitive to the size of the request.  */
173       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
174                     + 4 + DEFAULT_ROUNDING - 1)
175                    & ~(DEFAULT_ROUNDING - 1));
176       size = 4096 - extra;
177     }
178
179   h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
180   h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
181   h->chunk_size = size;
182   h->alignment_mask = alignment - 1;
183   h->extra_arg = arg;
184   h->use_extra_arg = 1;
185
186   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
187   if (!chunk)
188     (*obstack_alloc_failed_handler) ();
189   h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
190                                                alignment - 1);
191   h->chunk_limit = chunk->limit
192     = (char *) chunk + h->chunk_size;
193   chunk->prev = 0;
194   /* The initial chunk now contains no empty object.  */
195   h->maybe_empty_object = 0;
196   h->alloc_failed = 0;
197   return 1;
198 }
199
200 /* Allocate a new current chunk for the obstack *H
201    on the assumption that LENGTH bytes need to be added
202    to the current object, or a new object of length LENGTH allocated.
203    Copies any partial object from the end of the old chunk
204    to the beginning of the new one.  */
205
206 void
207 _obstack_newchunk (struct obstack *h, int length)
208 {
209   register struct _obstack_chunk *old_chunk = h->chunk;
210   register struct _obstack_chunk *new_chunk;
211   register long new_size;
212   register long obj_size = h->next_free - h->object_base;
213   register long i;
214   long already;
215   char *object_base;
216
217   /* Compute size for new chunk.  */
218   new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
219   if (new_size < h->chunk_size)
220     new_size = h->chunk_size;
221
222   /* Allocate and initialize the new chunk.  */
223   new_chunk = CALL_CHUNKFUN (h, new_size);
224   if (!new_chunk)
225     (*obstack_alloc_failed_handler) ();
226   h->chunk = new_chunk;
227   new_chunk->prev = old_chunk;
228   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
229
230   /* Compute an aligned object_base in the new chunk */
231   object_base =
232     __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
233
234   /* Move the existing object to the new chunk.
235      Word at a time is fast and is safe if the object
236      is sufficiently aligned.  */
237   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
238     {
239       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
240            i >= 0; i--)
241         ((COPYING_UNIT *)object_base)[i]
242           = ((COPYING_UNIT *)h->object_base)[i];
243       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
244          but that can cross a page boundary on a machine
245          which does not do strict alignment for COPYING_UNITS.  */
246       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
247     }
248   else
249     already = 0;
250   /* Copy remaining bytes one by one.  */
251   for (i = already; i < obj_size; i++)
252     object_base[i] = h->object_base[i];
253
254   /* If the object just copied was the only data in OLD_CHUNK,
255      free that chunk and remove it from the chain.
256      But not if that chunk might contain an empty object.  */
257   if (! h->maybe_empty_object
258       && (h->object_base
259           == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
260                           h->alignment_mask)))
261     {
262       new_chunk->prev = old_chunk->prev;
263       CALL_FREEFUN (h, old_chunk);
264     }
265
266   h->object_base = object_base;
267   h->next_free = h->object_base + obj_size;
268   /* The new chunk certainly contains no empty object yet.  */
269   h->maybe_empty_object = 0;
270 }
271
272 /* Return nonzero if object OBJ has been allocated from obstack H.
273    This is here for debugging.
274    If you use it in a program, you are probably losing.  */
275
276 /* Suppress -Wmissing-prototypes warning.  We don't want to declare this in
277    obstack.h because it is just for debugging.  */
278 int _obstack_allocated_p (struct obstack *h, void *obj);
279
280 int
281 _obstack_allocated_p (struct obstack *h, void *obj)
282 {
283   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
284   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
285
286   lp = (h)->chunk;
287   /* We use >= rather than > since the object cannot be exactly at
288      the beginning of the chunk but might be an empty object exactly
289      at the end of an adjacent chunk.  */
290   while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
291     {
292       plp = lp->prev;
293       lp = plp;
294     }
295   return lp != 0;
296 }
297
298 /* Free objects in obstack H, including OBJ and everything allocate
299    more recently than OBJ.  If OBJ is zero, free everything in H.  */
300
301 # undef obstack_free
302
303 void
304 obstack_free (struct obstack *h, void *obj)
305 {
306   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
307   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
308
309   lp = h->chunk;
310   /* We use >= because there cannot be an object at the beginning of a chunk.
311      But there can be an empty object at that address
312      at the end of another chunk.  */
313   while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
314     {
315       plp = lp->prev;
316       CALL_FREEFUN (h, lp);
317       lp = plp;
318       /* If we switch chunks, we can't tell whether the new current
319          chunk contains an empty object, so assume that it may.  */
320       h->maybe_empty_object = 1;
321     }
322   if (lp)
323     {
324       h->object_base = h->next_free = (char *) (obj);
325       h->chunk_limit = lp->limit;
326       h->chunk = lp;
327     }
328   else if (obj != 0)
329     /* obj is not in any of the chunks! */
330     abort ();
331 }
332
333 int
334 _obstack_memory_used (struct obstack *h)
335 {
336   register struct _obstack_chunk* lp;
337   register int nbytes = 0;
338
339   for (lp = h->chunk; lp != 0; lp = lp->prev)
340     {
341       nbytes += lp->limit - (char *) lp;
342     }
343   return nbytes;
344 }
345
346 static void
347 __attribute__ ((noreturn))
348 print_and_abort (void)
349 {
350   /* Don't change any of these strings.  Yes, it would be possible to add
351      the newline to the string and use fputs or so.  But this must not
352      happen because the "memory exhausted" message appears in other places
353      like this and the translation should be reused instead of creating
354      a very similar string which requires a separate translation.  */
355   fprintf (stderr, "%s\n", "memory exhausted");
356   exit (obstack_exit_failure);
357 }
358