1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
5 NOTE: This source is derived from an old version taken from the GNU C
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
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. */
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
55 #if defined (__STDC__) && __STDC__
56 #define POINTER void *
58 #define POINTER char *
61 /* Determine default alignment. */
62 struct fooalign {char x; double d;};
63 #define DEFAULT_ALIGNMENT \
64 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
65 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
66 But in fact it might be less smart and round addresses to as much as
67 DEFAULT_ROUNDING. So we prepare for it to do that. */
68 union fooround {long x; double d;};
69 #define DEFAULT_ROUNDING (sizeof (union fooround))
71 /* When we copy a long block of data, this is the unit to do it with.
72 On some machines, copying successive ints does not work;
73 in such a case, redefine COPYING_UNIT to `long' (if that works)
74 or `char' as a last resort. */
76 #define COPYING_UNIT int
80 /* The functions allocating more room by calling `obstack_chunk_alloc'
81 jump to the handler pointed to by `obstack_alloc_failed_handler'.
82 This variable by default points to the internal function
84 #if defined (__STDC__) && __STDC__
85 static void print_and_abort (void);
86 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
88 static void print_and_abort ();
89 void (*obstack_alloc_failed_handler) () = print_and_abort;
92 /* Exit value used when `print_and_abort' is used. */
93 #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
97 #define EXIT_FAILURE 1
99 int obstack_exit_failure = EXIT_FAILURE;
101 /* The non-GNU-C macros copy the obstack into this global variable
102 to avoid multiple evaluation. */
104 struct obstack *_obstack;
106 /* Define a macro that either calls functions with the traditional malloc/free
107 calling interface, or calls functions with the mmalloc/mfree interface
108 (that adds an extra first argument), based on the state of use_extra_arg.
109 For free, do not use ?:, since some compilers, like the MIPS compilers,
110 do not allow (expr) ? void : void. */
112 #if defined (__STDC__) && __STDC__
113 #define CALL_CHUNKFUN(h, size) \
114 (((h) -> use_extra_arg) \
115 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
116 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
118 #define CALL_FREEFUN(h, old_chunk) \
120 if ((h) -> use_extra_arg) \
121 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
123 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
126 #define CALL_CHUNKFUN(h, size) \
127 (((h) -> use_extra_arg) \
128 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
129 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
131 #define CALL_FREEFUN(h, old_chunk) \
133 if ((h) -> use_extra_arg) \
134 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
136 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
141 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
142 Objects start on multiples of ALIGNMENT (0 means use default).
143 CHUNKFUN is the function to use to allocate chunks,
144 and FREEFUN the function to free them.
146 Return nonzero if successful, zero if out of memory.
147 To recover from an out of memory error,
148 free up some memory, then call this again. */
151 _obstack_begin (h, size, alignment, chunkfun, freefun)
155 #if defined (__STDC__) && __STDC__
156 POINTER (*chunkfun) (long);
157 void (*freefun) (void *);
159 POINTER (*chunkfun) ();
163 register struct _obstack_chunk *chunk; /* points to new chunk */
166 alignment = (int) DEFAULT_ALIGNMENT;
168 /* Default size is what GNU malloc can fit in a 4096-byte block. */
170 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
171 Use the values for range checking, because if range checking is off,
172 the extra bytes won't be missed terribly, but if range checking is on
173 and we used a larger request, a whole extra 4096 bytes would be
176 These number are irrelevant to the new GNU malloc. I suspect it is
177 less sensitive to the size of the request. */
178 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
179 + 4 + DEFAULT_ROUNDING - 1)
180 & ~(DEFAULT_ROUNDING - 1));
184 #if defined (__STDC__) && __STDC__
185 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
186 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
188 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
189 h->freefun = freefun;
191 h->chunk_size = size;
192 h->alignment_mask = alignment - 1;
193 h->use_extra_arg = 0;
195 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
197 (*obstack_alloc_failed_handler) ();
198 h->next_free = h->object_base = chunk->contents;
199 h->chunk_limit = chunk->limit
200 = (char *) chunk + h->chunk_size;
202 /* The initial chunk now contains no empty object. */
203 h->maybe_empty_object = 0;
209 _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
213 #if defined (__STDC__) && __STDC__
214 POINTER (*chunkfun) (POINTER, long);
215 void (*freefun) (POINTER, POINTER);
217 POINTER (*chunkfun) ();
222 register struct _obstack_chunk *chunk; /* points to new chunk */
225 alignment = (int) DEFAULT_ALIGNMENT;
227 /* Default size is what GNU malloc can fit in a 4096-byte block. */
229 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
230 Use the values for range checking, because if range checking is off,
231 the extra bytes won't be missed terribly, but if range checking is on
232 and we used a larger request, a whole extra 4096 bytes would be
235 These number are irrelevant to the new GNU malloc. I suspect it is
236 less sensitive to the size of the request. */
237 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
238 + 4 + DEFAULT_ROUNDING - 1)
239 & ~(DEFAULT_ROUNDING - 1));
243 #if defined(__STDC__) && __STDC__
244 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
245 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
247 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
248 h->freefun = freefun;
250 h->chunk_size = size;
251 h->alignment_mask = alignment - 1;
253 h->use_extra_arg = 1;
255 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
257 (*obstack_alloc_failed_handler) ();
258 h->next_free = h->object_base = chunk->contents;
259 h->chunk_limit = chunk->limit
260 = (char *) chunk + h->chunk_size;
262 /* The initial chunk now contains no empty object. */
263 h->maybe_empty_object = 0;
268 /* Allocate a new current chunk for the obstack *H
269 on the assumption that LENGTH bytes need to be added
270 to the current object, or a new object of length LENGTH allocated.
271 Copies any partial object from the end of the old chunk
272 to the beginning of the new one. */
275 _obstack_newchunk (h, length)
279 register struct _obstack_chunk *old_chunk = h->chunk;
280 register struct _obstack_chunk *new_chunk;
281 register long new_size;
282 register long obj_size = h->next_free - h->object_base;
286 /* Compute size for new chunk. */
287 new_size = (obj_size + length) + (obj_size >> 3) + 100;
288 if (new_size < h->chunk_size)
289 new_size = h->chunk_size;
291 /* Allocate and initialize the new chunk. */
292 new_chunk = CALL_CHUNKFUN (h, new_size);
294 (*obstack_alloc_failed_handler) ();
295 h->chunk = new_chunk;
296 new_chunk->prev = old_chunk;
297 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
299 /* Move the existing object to the new chunk.
300 Word at a time is fast and is safe if the object
301 is sufficiently aligned. */
302 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
304 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
306 ((COPYING_UNIT *)new_chunk->contents)[i]
307 = ((COPYING_UNIT *)h->object_base)[i];
308 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
309 but that can cross a page boundary on a machine
310 which does not do strict alignment for COPYING_UNITS. */
311 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
315 /* Copy remaining bytes one by one. */
316 for (i = already; i < obj_size; i++)
317 new_chunk->contents[i] = h->object_base[i];
319 /* If the object just copied was the only data in OLD_CHUNK,
320 free that chunk and remove it from the chain.
321 But not if that chunk might contain an empty object. */
322 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
324 new_chunk->prev = old_chunk->prev;
325 CALL_FREEFUN (h, old_chunk);
328 h->object_base = new_chunk->contents;
329 h->next_free = h->object_base + obj_size;
330 /* The new chunk certainly contains no empty object yet. */
331 h->maybe_empty_object = 0;
334 /* Return nonzero if object OBJ has been allocated from obstack H.
335 This is here for debugging.
336 If you use it in a program, you are probably losing. */
338 #if defined (__STDC__) && __STDC__
339 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
340 obstack.h because it is just for debugging. */
341 int _obstack_allocated_p (struct obstack *h, POINTER obj);
345 _obstack_allocated_p (h, obj)
349 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
350 register struct _obstack_chunk *plp; /* point to previous chunk if any */
353 /* We use >= rather than > since the object cannot be exactly at
354 the beginning of the chunk but might be an empty object exactly
355 at the end of an adjacent chunk. */
356 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
364 /* Free objects in obstack H, including OBJ and everything allocate
365 more recently than OBJ. If OBJ is zero, free everything in H. */
369 /* This function has two names with identical definitions.
370 This is the first one, called from non-ANSI code. */
373 _obstack_free (h, obj)
377 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
378 register struct _obstack_chunk *plp; /* point to previous chunk if any */
381 /* We use >= because there cannot be an object at the beginning of a chunk.
382 But there can be an empty object at that address
383 at the end of another chunk. */
384 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
387 CALL_FREEFUN (h, lp);
389 /* If we switch chunks, we can't tell whether the new current
390 chunk contains an empty object, so assume that it may. */
391 h->maybe_empty_object = 1;
395 h->object_base = h->next_free = (char *) (obj);
396 h->chunk_limit = lp->limit;
400 /* obj is not in any of the chunks! */
404 /* This function is used from ANSI code. */
407 obstack_free (h, obj)
411 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
412 register struct _obstack_chunk *plp; /* point to previous chunk if any */
415 /* We use >= because there cannot be an object at the beginning of a chunk.
416 But there can be an empty object at that address
417 at the end of another chunk. */
418 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
421 CALL_FREEFUN (h, lp);
423 /* If we switch chunks, we can't tell whether the new current
424 chunk contains an empty object, so assume that it may. */
425 h->maybe_empty_object = 1;
429 h->object_base = h->next_free = (char *) (obj);
430 h->chunk_limit = lp->limit;
434 /* obj is not in any of the chunks! */
439 _obstack_memory_used (h)
442 register struct _obstack_chunk* lp;
443 register int nbytes = 0;
445 for (lp = h->chunk; lp != 0; lp = lp->prev)
447 nbytes += lp->limit - (char *) lp;
452 /* Define the error handler. */
454 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
455 # include <libintl.h>
457 # define _(Str) gettext (Str)
460 # define _(Str) (Str)
467 fputs (_("memory exhausted\n"), stderr);
468 exit (obstack_exit_failure);
472 /* These are now turned off because the applications do not use it
473 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
475 /* Now define the functional versions of the obstack macros.
476 Define them to simply use the corresponding macros to do the job. */
478 #if defined (__STDC__) && __STDC__
479 /* These function definitions do not work with non-ANSI preprocessors;
480 they won't pass through the macro names in parentheses. */
482 /* The function names appear in parentheses in order to prevent
483 the macro-definitions of the names from being expanded there. */
485 POINTER (obstack_base) (obstack)
486 struct obstack *obstack;
488 return obstack_base (obstack);
491 POINTER (obstack_next_free) (obstack)
492 struct obstack *obstack;
494 return obstack_next_free (obstack);
497 int (obstack_object_size) (obstack)
498 struct obstack *obstack;
500 return obstack_object_size (obstack);
503 int (obstack_room) (obstack)
504 struct obstack *obstack;
506 return obstack_room (obstack);
509 int (obstack_make_room) (obstack, length)
510 struct obstack *obstack;
513 return obstack_make_room (obstack, length);
516 void (obstack_grow) (obstack, pointer, length)
517 struct obstack *obstack;
521 obstack_grow (obstack, pointer, length);
524 void (obstack_grow0) (obstack, pointer, length)
525 struct obstack *obstack;
529 obstack_grow0 (obstack, pointer, length);
532 void (obstack_1grow) (obstack, character)
533 struct obstack *obstack;
536 obstack_1grow (obstack, character);
539 void (obstack_blank) (obstack, length)
540 struct obstack *obstack;
543 obstack_blank (obstack, length);
546 void (obstack_1grow_fast) (obstack, character)
547 struct obstack *obstack;
550 obstack_1grow_fast (obstack, character);
553 void (obstack_blank_fast) (obstack, length)
554 struct obstack *obstack;
557 obstack_blank_fast (obstack, length);
560 POINTER (obstack_finish) (obstack)
561 struct obstack *obstack;
563 return obstack_finish (obstack);
566 POINTER (obstack_alloc) (obstack, length)
567 struct obstack *obstack;
570 return obstack_alloc (obstack, length);
573 POINTER (obstack_copy) (obstack, pointer, length)
574 struct obstack *obstack;
578 return obstack_copy (obstack, pointer, length);
581 POINTER (obstack_copy0) (obstack, pointer, length)
582 struct obstack *obstack;
586 return obstack_copy0 (obstack, pointer, length);
589 #endif /* __STDC__ */
593 #endif /* !ELIDE_CODE */