]> git.saurik.com Git - bison.git/blame - lib/obstack.c
(EXTRA_DIST): Add stdbool_.h.
[bison.git] / lib / obstack.c
CommitLineData
8c7ebe49 1/* obstack.c - subroutines used implicitly by object stack macros
fc659dfd 2 Copyright (C) 1988-1994, 1996-1999, 2000-2002 Free Software Foundation, Inc.
16cb098b
PE
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8c7ebe49 7
16cb098b 8 This program is distributed in the hope that it will be useful,
8c7ebe49 9 but WITHOUT ANY WARRANTY; without even the implied warranty of
16cb098b
PE
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
8c7ebe49 12
fc659dfd
PE
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation,
16cb098b 15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
8c7ebe49
AD
16
17#ifdef HAVE_CONFIG_H
4a0d8936 18# include <config.h>
8c7ebe49
AD
19#endif
20
21#include "obstack.h"
22
23/* NOTE BEFORE MODIFYING THIS FILE: This version number must be
24 incremented whenever callers compiled using an old obstack.h can no
25 longer properly call the functions in this obstack.c. */
26#define OBSTACK_INTERFACE_VERSION 1
27
28/* Comment out all this code if we are using the GNU C Library, and are not
29 actually compiling the library itself, and the installed library
30 supports the same library interface we do. This code is part of the GNU
31 C Library, but also included in many other GNU distributions. Compiling
32 and linking in this code is a waste when using the GNU C library
33 (especially if it is a shared library). Rather than having every GNU
34 program understand `configure --with-gnu-libc' and omit the object
35 files, it is simpler to just do this in the source for each such file. */
36
37#include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
4a0d8936
PE
38#if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
39# include <gnu-versions.h>
40# if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
41# define ELIDE_CODE
42# endif
8c7ebe49
AD
43#endif
44
4a0d8936
PE
45#if defined _LIBC && defined USE_IN_LIBIO
46# include <wchar.h>
47#endif
8c7ebe49
AD
48
49#ifndef ELIDE_CODE
50
51
fc659dfd 52# if defined __STDC__ && __STDC__
4a0d8936
PE
53# define POINTER void *
54# else
55# define POINTER char *
56# endif
8c7ebe49
AD
57
58/* Determine default alignment. */
59struct fooalign {char x; double d;};
4a0d8936 60# define DEFAULT_ALIGNMENT \
8c7ebe49
AD
61 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
62/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
63 But in fact it might be less smart and round addresses to as much as
64 DEFAULT_ROUNDING. So we prepare for it to do that. */
65union fooround {long x; double d;};
4a0d8936 66# define DEFAULT_ROUNDING (sizeof (union fooround))
8c7ebe49
AD
67
68/* When we copy a long block of data, this is the unit to do it with.
69 On some machines, copying successive ints does not work;
70 in such a case, redefine COPYING_UNIT to `long' (if that works)
71 or `char' as a last resort. */
4a0d8936
PE
72# ifndef COPYING_UNIT
73# define COPYING_UNIT int
74# endif
8c7ebe49
AD
75
76
77/* The functions allocating more room by calling `obstack_chunk_alloc'
78 jump to the handler pointed to by `obstack_alloc_failed_handler'.
79 This can be set to a user defined function which should either
80 abort gracefully or use longjump - but shouldn't return. This
81 variable by default points to the internal function
82 `print_and_abort'. */
fc659dfd 83# if defined __STDC__ && __STDC__
8c7ebe49
AD
84static void print_and_abort (void);
85void (*obstack_alloc_failed_handler) (void) = print_and_abort;
4a0d8936 86# else
8c7ebe49
AD
87static void print_and_abort ();
88void (*obstack_alloc_failed_handler) () = print_and_abort;
4a0d8936 89# endif
8c7ebe49
AD
90
91/* Exit value used when `print_and_abort' is used. */
4a0d8936
PE
92# if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
93# include <stdlib.h>
94# endif
95# ifndef EXIT_FAILURE
96# define EXIT_FAILURE 1
97# endif
8c7ebe49
AD
98int obstack_exit_failure = EXIT_FAILURE;
99
100/* The non-GNU-C macros copy the obstack into this global variable
101 to avoid multiple evaluation. */
102
103struct obstack *_obstack;
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
fc659dfd 111# if defined __STDC__ && __STDC__
4a0d8936 112# define CALL_CHUNKFUN(h, size) \
8c7ebe49
AD
113 (((h) -> use_extra_arg) \
114 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
115 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
116
4a0d8936 117# define CALL_FREEFUN(h, old_chunk) \
8c7ebe49
AD
118 do { \
119 if ((h) -> use_extra_arg) \
120 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
121 else \
122 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
123 } while (0)
4a0d8936
PE
124# else
125# define CALL_CHUNKFUN(h, size) \
8c7ebe49
AD
126 (((h) -> use_extra_arg) \
127 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
128 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
129
4a0d8936 130# define CALL_FREEFUN(h, old_chunk) \
8c7ebe49
AD
131 do { \
132 if ((h) -> use_extra_arg) \
133 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
134 else \
135 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
136 } while (0)
4a0d8936 137# endif
8c7ebe49
AD
138
139\f
140/* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
141 Objects start on multiples of ALIGNMENT (0 means use default).
142 CHUNKFUN is the function to use to allocate chunks,
143 and FREEFUN the function to free them.
144
145 Return nonzero if successful, calls obstack_alloc_failed_handler if
146 allocation fails. */
147
148int
149_obstack_begin (h, size, alignment, chunkfun, freefun)
150 struct obstack *h;
151 int size;
152 int alignment;
fc659dfd 153# if defined __STDC__ && __STDC__
8c7ebe49
AD
154 POINTER (*chunkfun) (long);
155 void (*freefun) (void *);
4a0d8936 156# else
8c7ebe49
AD
157 POINTER (*chunkfun) ();
158 void (*freefun) ();
4a0d8936 159# endif
8c7ebe49
AD
160{
161 register struct _obstack_chunk *chunk; /* points to new chunk */
162
163 if (alignment == 0)
164 alignment = (int) DEFAULT_ALIGNMENT;
165 if (size == 0)
166 /* Default size is what GNU malloc can fit in a 4096-byte block. */
167 {
168 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
169 Use the values for range checking, because if range checking is off,
170 the extra bytes won't be missed terribly, but if range checking is on
171 and we used a larger request, a whole extra 4096 bytes would be
172 allocated.
173
174 These number are irrelevant to the new GNU malloc. I suspect it is
175 less sensitive to the size of the request. */
176 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
177 + 4 + DEFAULT_ROUNDING - 1)
178 & ~(DEFAULT_ROUNDING - 1));
179 size = 4096 - extra;
180 }
181
fc659dfd 182# if defined __STDC__ && __STDC__
8c7ebe49
AD
183 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
184 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
4a0d8936 185# else
8c7ebe49
AD
186 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
187 h->freefun = freefun;
4a0d8936 188# endif
8c7ebe49
AD
189 h->chunk_size = size;
190 h->alignment_mask = alignment - 1;
191 h->use_extra_arg = 0;
192
193 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
194 if (!chunk)
195 (*obstack_alloc_failed_handler) ();
196 h->next_free = h->object_base = chunk->contents;
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
206int
207_obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
208 struct obstack *h;
209 int size;
210 int alignment;
fc659dfd 211# if defined __STDC__ && __STDC__
8c7ebe49
AD
212 POINTER (*chunkfun) (POINTER, long);
213 void (*freefun) (POINTER, POINTER);
4a0d8936 214# else
8c7ebe49
AD
215 POINTER (*chunkfun) ();
216 void (*freefun) ();
4a0d8936 217# endif
8c7ebe49
AD
218 POINTER arg;
219{
220 register struct _obstack_chunk *chunk; /* points to new chunk */
221
222 if (alignment == 0)
223 alignment = (int) DEFAULT_ALIGNMENT;
224 if (size == 0)
225 /* Default size is what GNU malloc can fit in a 4096-byte block. */
226 {
227 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
228 Use the values for range checking, because if range checking is off,
229 the extra bytes won't be missed terribly, but if range checking is on
230 and we used a larger request, a whole extra 4096 bytes would be
231 allocated.
232
233 These number are irrelevant to the new GNU malloc. I suspect it is
234 less sensitive to the size of the request. */
235 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
236 + 4 + DEFAULT_ROUNDING - 1)
237 & ~(DEFAULT_ROUNDING - 1));
238 size = 4096 - extra;
239 }
240
fc659dfd 241# if defined __STDC__ && __STDC__
8c7ebe49
AD
242 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
243 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
4a0d8936 244# else
8c7ebe49
AD
245 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
246 h->freefun = freefun;
4a0d8936 247# endif
8c7ebe49
AD
248 h->chunk_size = size;
249 h->alignment_mask = alignment - 1;
250 h->extra_arg = arg;
251 h->use_extra_arg = 1;
252
253 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
254 if (!chunk)
255 (*obstack_alloc_failed_handler) ();
256 h->next_free = h->object_base = chunk->contents;
257 h->chunk_limit = chunk->limit
258 = (char *) chunk + h->chunk_size;
259 chunk->prev = 0;
260 /* The initial chunk now contains no empty object. */
261 h->maybe_empty_object = 0;
262 h->alloc_failed = 0;
263 return 1;
264}
265
266/* Allocate a new current chunk for the obstack *H
267 on the assumption that LENGTH bytes need to be added
268 to the current object, or a new object of length LENGTH allocated.
269 Copies any partial object from the end of the old chunk
270 to the beginning of the new one. */
271
272void
273_obstack_newchunk (h, length)
274 struct obstack *h;
275 int length;
276{
277 register struct _obstack_chunk *old_chunk = h->chunk;
278 register struct _obstack_chunk *new_chunk;
279 register long new_size;
280 register long obj_size = h->next_free - h->object_base;
281 register long i;
282 long already;
283 char *object_base;
284
285 /* Compute size for new chunk. */
286 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
287 if (new_size < h->chunk_size)
288 new_size = h->chunk_size;
289
290 /* Allocate and initialize the new chunk. */
291 new_chunk = CALL_CHUNKFUN (h, new_size);
292 if (!new_chunk)
293 (*obstack_alloc_failed_handler) ();
294 h->chunk = new_chunk;
295 new_chunk->prev = old_chunk;
296 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
297
298 /* Compute an aligned object_base in the new chunk */
299 object_base =
300 __INT_TO_PTR ((__PTR_TO_INT (new_chunk->contents) + h->alignment_mask)
301 & ~ (h->alignment_mask));
302
303 /* Move the existing object to the new chunk.
304 Word at a time is fast and is safe if the object
305 is sufficiently aligned. */
306 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
307 {
308 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
309 i >= 0; i--)
310 ((COPYING_UNIT *)object_base)[i]
311 = ((COPYING_UNIT *)h->object_base)[i];
312 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
313 but that can cross a page boundary on a machine
314 which does not do strict alignment for COPYING_UNITS. */
315 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
316 }
317 else
318 already = 0;
319 /* Copy remaining bytes one by one. */
320 for (i = already; i < obj_size; i++)
321 object_base[i] = h->object_base[i];
322
323 /* If the object just copied was the only data in OLD_CHUNK,
324 free that chunk and remove it from the chain.
325 But not if that chunk might contain an empty object. */
326 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
327 {
328 new_chunk->prev = old_chunk->prev;
329 CALL_FREEFUN (h, old_chunk);
330 }
331
332 h->object_base = object_base;
333 h->next_free = h->object_base + obj_size;
334 /* The new chunk certainly contains no empty object yet. */
335 h->maybe_empty_object = 0;
336}
337
338/* Return nonzero if object OBJ has been allocated from obstack H.
339 This is here for debugging.
340 If you use it in a program, you are probably losing. */
341
fc659dfd 342# if defined __STDC__ && __STDC__
8c7ebe49
AD
343/* Suppress -Wmissing-prototypes warning. We don't want to declare this in
344 obstack.h because it is just for debugging. */
345int _obstack_allocated_p (struct obstack *h, POINTER obj);
4a0d8936 346# endif
8c7ebe49
AD
347
348int
349_obstack_allocated_p (h, obj)
350 struct obstack *h;
351 POINTER obj;
352{
353 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
354 register struct _obstack_chunk *plp; /* point to previous chunk if any */
355
356 lp = (h)->chunk;
357 /* We use >= rather than > since the object cannot be exactly at
358 the beginning of the chunk but might be an empty object exactly
359 at the end of an adjacent chunk. */
360 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
361 {
362 plp = lp->prev;
363 lp = plp;
364 }
365 return lp != 0;
366}
367\f
368/* Free objects in obstack H, including OBJ and everything allocate
369 more recently than OBJ. If OBJ is zero, free everything in H. */
370
4a0d8936 371# undef obstack_free
8c7ebe49
AD
372
373/* This function has two names with identical definitions.
374 This is the first one, called from non-ANSI code. */
375
376void
377_obstack_free (h, obj)
378 struct obstack *h;
379 POINTER obj;
380{
381 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
382 register struct _obstack_chunk *plp; /* point to previous chunk if any */
383
384 lp = h->chunk;
385 /* We use >= because there cannot be an object at the beginning of a chunk.
386 But there can be an empty object at that address
387 at the end of another chunk. */
388 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
389 {
390 plp = lp->prev;
391 CALL_FREEFUN (h, lp);
392 lp = plp;
393 /* If we switch chunks, we can't tell whether the new current
394 chunk contains an empty object, so assume that it may. */
395 h->maybe_empty_object = 1;
396 }
397 if (lp)
398 {
399 h->object_base = h->next_free = (char *) (obj);
400 h->chunk_limit = lp->limit;
401 h->chunk = lp;
402 }
403 else if (obj != 0)
404 /* obj is not in any of the chunks! */
405 abort ();
406}
407
408/* This function is used from ANSI code. */
409
410void
411obstack_free (h, obj)
412 struct obstack *h;
413 POINTER obj;
414{
415 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
416 register struct _obstack_chunk *plp; /* point to previous chunk if any */
417
418 lp = h->chunk;
419 /* We use >= because there cannot be an object at the beginning of a chunk.
420 But there can be an empty object at that address
421 at the end of another chunk. */
422 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
423 {
424 plp = lp->prev;
425 CALL_FREEFUN (h, lp);
426 lp = plp;
427 /* If we switch chunks, we can't tell whether the new current
428 chunk contains an empty object, so assume that it may. */
429 h->maybe_empty_object = 1;
430 }
431 if (lp)
432 {
433 h->object_base = h->next_free = (char *) (obj);
434 h->chunk_limit = lp->limit;
435 h->chunk = lp;
436 }
437 else if (obj != 0)
438 /* obj is not in any of the chunks! */
439 abort ();
440}
441\f
442int
443_obstack_memory_used (h)
444 struct obstack *h;
445{
446 register struct _obstack_chunk* lp;
447 register int nbytes = 0;
448
449 for (lp = h->chunk; lp != 0; lp = lp->prev)
450 {
451 nbytes += lp->limit - (char *) lp;
452 }
453 return nbytes;
454}
455\f
456/* Define the error handler. */
fc659dfd
PE
457# ifdef _LIBC
458# include <libintl.h>
459# else
460# include "gettext.h"
4a0d8936 461# endif
fc659dfd
PE
462# define _(msgid) gettext (msgid)
463
4a0d8936
PE
464# if defined _LIBC && defined USE_IN_LIBIO
465# include <libio/iolibio.h>
466# define fputs(s, f) _IO_fputs (s, f)
467# endif
468
469# ifndef __attribute__
470/* This feature is available in gcc versions 2.5 and later. */
471# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
472# define __attribute__(Spec) /* empty */
8c7ebe49 473# endif
8c7ebe49 474# endif
8c7ebe49
AD
475
476static void
4a0d8936 477__attribute__ ((noreturn))
8c7ebe49
AD
478print_and_abort ()
479{
4a0d8936
PE
480 /* Don't change any of these strings. Yes, it would be possible to add
481 the newline to the string and use fputs or so. But this must not
482 happen because the "memory exhausted" message appears in other places
483 like this and the translation should be reused instead of creating
484 a very similar string which requires a separate translation. */
485# if defined _LIBC && defined USE_IN_LIBIO
486 if (_IO_fwide (stderr, 0) > 0)
487 __fwprintf (stderr, L"%s\n", _("memory exhausted"));
488 else
489# endif
490 fprintf (stderr, "%s\n", _("memory exhausted"));
8c7ebe49
AD
491 exit (obstack_exit_failure);
492}
493\f
4a0d8936 494# if 0
8c7ebe49
AD
495/* These are now turned off because the applications do not use it
496 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
497
498/* Now define the functional versions of the obstack macros.
499 Define them to simply use the corresponding macros to do the job. */
500
fc659dfd 501# if defined __STDC__ && __STDC__
8c7ebe49
AD
502/* These function definitions do not work with non-ANSI preprocessors;
503 they won't pass through the macro names in parentheses. */
504
505/* The function names appear in parentheses in order to prevent
506 the macro-definitions of the names from being expanded there. */
507
508POINTER (obstack_base) (obstack)
509 struct obstack *obstack;
510{
511 return obstack_base (obstack);
512}
513
514POINTER (obstack_next_free) (obstack)
515 struct obstack *obstack;
516{
517 return obstack_next_free (obstack);
518}
519
520int (obstack_object_size) (obstack)
521 struct obstack *obstack;
522{
523 return obstack_object_size (obstack);
524}
525
526int (obstack_room) (obstack)
527 struct obstack *obstack;
528{
529 return obstack_room (obstack);
530}
531
532int (obstack_make_room) (obstack, length)
533 struct obstack *obstack;
534 int length;
535{
536 return obstack_make_room (obstack, length);
537}
538
8a4f41d6 539void (obstack_grow) (obstack, data, length)
8c7ebe49 540 struct obstack *obstack;
8a4f41d6 541 const POINTER data;
8c7ebe49
AD
542 int length;
543{
8a4f41d6 544 obstack_grow (obstack, data, length);
8c7ebe49
AD
545}
546
8a4f41d6 547void (obstack_grow0) (obstack, data, length)
8c7ebe49 548 struct obstack *obstack;
8a4f41d6 549 const POINTER data;
8c7ebe49
AD
550 int length;
551{
8a4f41d6 552 obstack_grow0 (obstack, data, length);
8c7ebe49
AD
553}
554
555void (obstack_1grow) (obstack, character)
556 struct obstack *obstack;
557 int character;
558{
559 obstack_1grow (obstack, character);
560}
561
562void (obstack_blank) (obstack, length)
563 struct obstack *obstack;
564 int length;
565{
566 obstack_blank (obstack, length);
567}
568
569void (obstack_1grow_fast) (obstack, character)
570 struct obstack *obstack;
571 int character;
572{
573 obstack_1grow_fast (obstack, character);
574}
575
576void (obstack_blank_fast) (obstack, length)
577 struct obstack *obstack;
578 int length;
579{
580 obstack_blank_fast (obstack, length);
581}
582
583POINTER (obstack_finish) (obstack)
584 struct obstack *obstack;
585{
586 return obstack_finish (obstack);
587}
588
589POINTER (obstack_alloc) (obstack, length)
590 struct obstack *obstack;
591 int length;
592{
593 return obstack_alloc (obstack, length);
594}
595
8a4f41d6 596POINTER (obstack_copy) (obstack, address, length)
8c7ebe49 597 struct obstack *obstack;
8a4f41d6 598 const POINTER address;
8c7ebe49
AD
599 int length;
600{
8a4f41d6 601 return obstack_copy (obstack, address, length);
8c7ebe49
AD
602}
603
8a4f41d6 604POINTER (obstack_copy0) (obstack, address, length)
8c7ebe49 605 struct obstack *obstack;
8a4f41d6 606 const POINTER address;
8c7ebe49
AD
607 int length;
608{
8a4f41d6 609 return obstack_copy0 (obstack, address, length);
8c7ebe49
AD
610}
611
fc659dfd 612# endif /* __STDC__ */
8c7ebe49 613
4a0d8936 614# endif /* 0 */
8c7ebe49
AD
615
616#endif /* !ELIDE_CODE */