]> git.saurik.com Git - bison.git/blob - lib/obstack.c
Update.
[bison.git] / lib / obstack.c
1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988-1994, 1996-1999, 2000-2002 Free Software Foundation, Inc.
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.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
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,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
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__. */
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
43 #endif
44
45 #if defined _LIBC && defined USE_IN_LIBIO
46 # include <wchar.h>
47 #endif
48
49 #ifndef ELIDE_CODE
50
51
52 # if defined __STDC__ && __STDC__
53 # define POINTER void *
54 # else
55 # define POINTER char *
56 # endif
57
58 /* Determine default alignment. */
59 struct fooalign {char x; double d;};
60 # define DEFAULT_ALIGNMENT \
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. */
65 union fooround {long x; double d;};
66 # define DEFAULT_ROUNDING (sizeof (union fooround))
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. */
72 # ifndef COPYING_UNIT
73 # define COPYING_UNIT int
74 # endif
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'. */
83 # if defined __STDC__ && __STDC__
84 static void print_and_abort (void);
85 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
86 # else
87 static void print_and_abort ();
88 void (*obstack_alloc_failed_handler) () = print_and_abort;
89 # endif
90
91 /* Exit value used when `print_and_abort' is used. */
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
98 int 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
103 struct 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
111 # if defined __STDC__ && __STDC__
112 # define CALL_CHUNKFUN(h, size) \
113 (((h) -> use_extra_arg) \
114 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
115 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
116
117 # define CALL_FREEFUN(h, old_chunk) \
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)
124 # else
125 # define CALL_CHUNKFUN(h, size) \
126 (((h) -> use_extra_arg) \
127 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
128 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
129
130 # define CALL_FREEFUN(h, old_chunk) \
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)
137 # endif
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
148 int
149 _obstack_begin (h, size, alignment, chunkfun, freefun)
150 struct obstack *h;
151 int size;
152 int alignment;
153 # if defined __STDC__ && __STDC__
154 POINTER (*chunkfun) (long);
155 void (*freefun) (void *);
156 # else
157 POINTER (*chunkfun) ();
158 void (*freefun) ();
159 # endif
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
182 # if defined __STDC__ && __STDC__
183 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
184 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
185 # else
186 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
187 h->freefun = freefun;
188 # endif
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
206 int
207 _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
208 struct obstack *h;
209 int size;
210 int alignment;
211 # if defined __STDC__ && __STDC__
212 POINTER (*chunkfun) (POINTER, long);
213 void (*freefun) (POINTER, POINTER);
214 # else
215 POINTER (*chunkfun) ();
216 void (*freefun) ();
217 # endif
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
241 # if defined __STDC__ && __STDC__
242 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
243 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
244 # else
245 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
246 h->freefun = freefun;
247 # endif
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
272 void
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
342 # if defined __STDC__ && __STDC__
343 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
344 obstack.h because it is just for debugging. */
345 int _obstack_allocated_p (struct obstack *h, POINTER obj);
346 # endif
347
348 int
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
371 # undef obstack_free
372
373 /* This function has two names with identical definitions.
374 This is the first one, called from non-ANSI code. */
375
376 void
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
410 void
411 obstack_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
442 int
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. */
457 # ifdef _LIBC
458 # include <libintl.h>
459 # else
460 # include "gettext.h"
461 # endif
462 # define _(msgid) gettext (msgid)
463
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 */
473 # endif
474 # endif
475
476 static void
477 __attribute__ ((noreturn))
478 print_and_abort ()
479 {
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"));
491 exit (obstack_exit_failure);
492 }
493 \f
494 # if 0
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
501 # if defined __STDC__ && __STDC__
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
508 POINTER (obstack_base) (obstack)
509 struct obstack *obstack;
510 {
511 return obstack_base (obstack);
512 }
513
514 POINTER (obstack_next_free) (obstack)
515 struct obstack *obstack;
516 {
517 return obstack_next_free (obstack);
518 }
519
520 int (obstack_object_size) (obstack)
521 struct obstack *obstack;
522 {
523 return obstack_object_size (obstack);
524 }
525
526 int (obstack_room) (obstack)
527 struct obstack *obstack;
528 {
529 return obstack_room (obstack);
530 }
531
532 int (obstack_make_room) (obstack, length)
533 struct obstack *obstack;
534 int length;
535 {
536 return obstack_make_room (obstack, length);
537 }
538
539 void (obstack_grow) (obstack, data, length)
540 struct obstack *obstack;
541 const POINTER data;
542 int length;
543 {
544 obstack_grow (obstack, data, length);
545 }
546
547 void (obstack_grow0) (obstack, data, length)
548 struct obstack *obstack;
549 const POINTER data;
550 int length;
551 {
552 obstack_grow0 (obstack, data, length);
553 }
554
555 void (obstack_1grow) (obstack, character)
556 struct obstack *obstack;
557 int character;
558 {
559 obstack_1grow (obstack, character);
560 }
561
562 void (obstack_blank) (obstack, length)
563 struct obstack *obstack;
564 int length;
565 {
566 obstack_blank (obstack, length);
567 }
568
569 void (obstack_1grow_fast) (obstack, character)
570 struct obstack *obstack;
571 int character;
572 {
573 obstack_1grow_fast (obstack, character);
574 }
575
576 void (obstack_blank_fast) (obstack, length)
577 struct obstack *obstack;
578 int length;
579 {
580 obstack_blank_fast (obstack, length);
581 }
582
583 POINTER (obstack_finish) (obstack)
584 struct obstack *obstack;
585 {
586 return obstack_finish (obstack);
587 }
588
589 POINTER (obstack_alloc) (obstack, length)
590 struct obstack *obstack;
591 int length;
592 {
593 return obstack_alloc (obstack, length);
594 }
595
596 POINTER (obstack_copy) (obstack, address, length)
597 struct obstack *obstack;
598 const POINTER address;
599 int length;
600 {
601 return obstack_copy (obstack, address, length);
602 }
603
604 POINTER (obstack_copy0) (obstack, address, length)
605 struct obstack *obstack;
606 const POINTER address;
607 int length;
608 {
609 return obstack_copy0 (obstack, address, length);
610 }
611
612 # endif /* __STDC__ */
613
614 # endif /* 0 */
615
616 #endif /* !ELIDE_CODE */