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