| 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.
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 | NOTE: This source is derived from an old version taken from the GNU C
|
|---|
| 6 | Library (glibc).
|
|---|
| 7 |
|
|---|
| 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
|
|---|
| 11 | later version.
|
|---|
| 12 |
|
|---|
| 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.
|
|---|
| 17 |
|
|---|
| 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,
|
|---|
| 21 | 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 |
|
|---|
| 52 | #ifndef ELIDE_CODE
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | #if defined (__STDC__) && __STDC__
|
|---|
| 56 | #define POINTER void *
|
|---|
| 57 | #else
|
|---|
| 58 | #define POINTER char *
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 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))
|
|---|
| 70 |
|
|---|
| 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. */
|
|---|
| 75 | #ifndef COPYING_UNIT
|
|---|
| 76 | #define COPYING_UNIT int
|
|---|
| 77 | #endif
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 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
|
|---|
| 83 | `print_and_abort'. */
|
|---|
| 84 | #if defined (__STDC__) && __STDC__
|
|---|
| 85 | static void print_and_abort (void);
|
|---|
| 86 | void (*obstack_alloc_failed_handler) (void) = print_and_abort;
|
|---|
| 87 | #else
|
|---|
| 88 | static void print_and_abort ();
|
|---|
| 89 | void (*obstack_alloc_failed_handler) () = print_and_abort;
|
|---|
| 90 | #endif
|
|---|
| 91 |
|
|---|
| 92 | /* Exit value used when `print_and_abort' is used. */
|
|---|
| 93 | #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
|
|---|
| 94 | #include <stdlib.h>
|
|---|
| 95 | #endif
|
|---|
| 96 | #ifndef EXIT_FAILURE
|
|---|
| 97 | #define EXIT_FAILURE 1
|
|---|
| 98 | #endif
|
|---|
| 99 | int obstack_exit_failure = EXIT_FAILURE;
|
|---|
| 100 |
|
|---|
| 101 | /* The non-GNU-C macros copy the obstack into this global variable
|
|---|
| 102 | to avoid multiple evaluation. */
|
|---|
| 103 |
|
|---|
| 104 | struct obstack *_obstack;
|
|---|
| 105 |
|
|---|
| 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. */
|
|---|
| 111 |
|
|---|
| 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)))
|
|---|
| 117 |
|
|---|
| 118 | #define CALL_FREEFUN(h, old_chunk) \
|
|---|
| 119 | do { \
|
|---|
| 120 | if ((h) -> use_extra_arg) \
|
|---|
| 121 | (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
|
|---|
| 122 | else \
|
|---|
| 123 | (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
|
|---|
| 124 | } while (0)
|
|---|
| 125 | #else
|
|---|
| 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)))
|
|---|
| 130 |
|
|---|
| 131 | #define CALL_FREEFUN(h, old_chunk) \
|
|---|
| 132 | do { \
|
|---|
| 133 | if ((h) -> use_extra_arg) \
|
|---|
| 134 | (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
|
|---|
| 135 | else \
|
|---|
| 136 | (*(void (*) ()) (h)->freefun) ((old_chunk)); \
|
|---|
| 137 | } while (0)
|
|---|
| 138 | #endif
|
|---|
| 139 |
|
|---|
| 140 | |
|---|
| 141 |
|
|---|
| 142 | /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
|
|---|
| 143 | Objects start on multiples of ALIGNMENT (0 means use default).
|
|---|
| 144 | CHUNKFUN is the function to use to allocate chunks,
|
|---|
| 145 | and FREEFUN the function to free them.
|
|---|
| 146 |
|
|---|
| 147 | Return nonzero if successful, zero if out of memory.
|
|---|
| 148 | To recover from an out of memory error,
|
|---|
| 149 | free up some memory, then call this again. */
|
|---|
| 150 |
|
|---|
| 151 | int
|
|---|
| 152 | _obstack_begin (h, size, alignment, chunkfun, freefun)
|
|---|
| 153 | struct obstack *h;
|
|---|
| 154 | int size;
|
|---|
| 155 | int alignment;
|
|---|
| 156 | #if defined (__STDC__) && __STDC__
|
|---|
| 157 | POINTER (*chunkfun) (long);
|
|---|
| 158 | void (*freefun) (void *);
|
|---|
| 159 | #else
|
|---|
| 160 | POINTER (*chunkfun) ();
|
|---|
| 161 | void (*freefun) ();
|
|---|
| 162 | #endif
|
|---|
| 163 | {
|
|---|
| 164 | register struct _obstack_chunk *chunk; /* points to new chunk */
|
|---|
| 165 |
|
|---|
| 166 | if (alignment == 0)
|
|---|
| 167 | alignment = (int) DEFAULT_ALIGNMENT;
|
|---|
| 168 | if (size == 0)
|
|---|
| 169 | /* Default size is what GNU malloc can fit in a 4096-byte block. */
|
|---|
| 170 | {
|
|---|
| 171 | /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
|
|---|
| 172 | Use the values for range checking, because if range checking is off,
|
|---|
| 173 | the extra bytes won't be missed terribly, but if range checking is on
|
|---|
| 174 | and we used a larger request, a whole extra 4096 bytes would be
|
|---|
| 175 | allocated.
|
|---|
| 176 |
|
|---|
| 177 | These number are irrelevant to the new GNU malloc. I suspect it is
|
|---|
| 178 | less sensitive to the size of the request. */
|
|---|
| 179 | int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
|
|---|
| 180 | + 4 + DEFAULT_ROUNDING - 1)
|
|---|
| 181 | & ~(DEFAULT_ROUNDING - 1));
|
|---|
| 182 | size = 4096 - extra;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | #if defined (__STDC__) && __STDC__
|
|---|
| 186 | h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
|
|---|
| 187 | h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
|
|---|
| 188 | #else
|
|---|
| 189 | h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
|
|---|
| 190 | h->freefun = freefun;
|
|---|
| 191 | #endif
|
|---|
| 192 | h->chunk_size = size;
|
|---|
| 193 | h->alignment_mask = alignment - 1;
|
|---|
| 194 | h->use_extra_arg = 0;
|
|---|
| 195 |
|
|---|
| 196 | chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
|
|---|
| 197 | if (!chunk)
|
|---|
| 198 | (*obstack_alloc_failed_handler) ();
|
|---|
| 199 | h->next_free = h->object_base = chunk->contents;
|
|---|
| 200 | h->chunk_limit = chunk->limit
|
|---|
| 201 | = (char *) chunk + h->chunk_size;
|
|---|
| 202 | chunk->prev = 0;
|
|---|
| 203 | /* The initial chunk now contains no empty object. */
|
|---|
| 204 | h->maybe_empty_object = 0;
|
|---|
| 205 | h->alloc_failed = 0;
|
|---|
| 206 | return 1;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | int
|
|---|
| 210 | _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
|
|---|
| 211 | struct obstack *h;
|
|---|
| 212 | int size;
|
|---|
| 213 | int alignment;
|
|---|
| 214 | #if defined (__STDC__) && __STDC__
|
|---|
| 215 | POINTER (*chunkfun) (POINTER, long);
|
|---|
| 216 | void (*freefun) (POINTER, POINTER);
|
|---|
| 217 | #else
|
|---|
| 218 | POINTER (*chunkfun) ();
|
|---|
| 219 | void (*freefun) ();
|
|---|
| 220 | #endif
|
|---|
| 221 | POINTER arg;
|
|---|
| 222 | {
|
|---|
| 223 | register struct _obstack_chunk *chunk; /* points to new chunk */
|
|---|
| 224 |
|
|---|
| 225 | if (alignment == 0)
|
|---|
| 226 | alignment = (int) DEFAULT_ALIGNMENT;
|
|---|
| 227 | if (size == 0)
|
|---|
| 228 | /* Default size is what GNU malloc can fit in a 4096-byte block. */
|
|---|
| 229 | {
|
|---|
| 230 | /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
|
|---|
| 231 | Use the values for range checking, because if range checking is off,
|
|---|
| 232 | the extra bytes won't be missed terribly, but if range checking is on
|
|---|
| 233 | and we used a larger request, a whole extra 4096 bytes would be
|
|---|
| 234 | allocated.
|
|---|
| 235 |
|
|---|
| 236 | These number are irrelevant to the new GNU malloc. I suspect it is
|
|---|
| 237 | less sensitive to the size of the request. */
|
|---|
| 238 | int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
|
|---|
| 239 | + 4 + DEFAULT_ROUNDING - 1)
|
|---|
| 240 | & ~(DEFAULT_ROUNDING - 1));
|
|---|
| 241 | size = 4096 - extra;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | #if defined(__STDC__) && __STDC__
|
|---|
| 245 | h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
|
|---|
| 246 | h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
|
|---|
| 247 | #else
|
|---|
| 248 | h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
|
|---|
| 249 | h->freefun = freefun;
|
|---|
| 250 | #endif
|
|---|
| 251 | h->chunk_size = size;
|
|---|
| 252 | h->alignment_mask = alignment - 1;
|
|---|
| 253 | h->extra_arg = arg;
|
|---|
| 254 | h->use_extra_arg = 1;
|
|---|
| 255 |
|
|---|
| 256 | chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
|
|---|
| 257 | if (!chunk)
|
|---|
| 258 | (*obstack_alloc_failed_handler) ();
|
|---|
| 259 | h->next_free = h->object_base = chunk->contents;
|
|---|
| 260 | h->chunk_limit = chunk->limit
|
|---|
| 261 | = (char *) chunk + h->chunk_size;
|
|---|
| 262 | chunk->prev = 0;
|
|---|
| 263 | /* The initial chunk now contains no empty object. */
|
|---|
| 264 | h->maybe_empty_object = 0;
|
|---|
| 265 | h->alloc_failed = 0;
|
|---|
| 266 | return 1;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /* Allocate a new current chunk for the obstack *H
|
|---|
| 270 | on the assumption that LENGTH bytes need to be added
|
|---|
| 271 | to the current object, or a new object of length LENGTH allocated.
|
|---|
| 272 | Copies any partial object from the end of the old chunk
|
|---|
| 273 | to the beginning of the new one. */
|
|---|
| 274 |
|
|---|
| 275 | void
|
|---|
| 276 | _obstack_newchunk (h, length)
|
|---|
| 277 | struct obstack *h;
|
|---|
| 278 | int length;
|
|---|
| 279 | {
|
|---|
| 280 | register struct _obstack_chunk *old_chunk = h->chunk;
|
|---|
| 281 | register struct _obstack_chunk *new_chunk;
|
|---|
| 282 | register long new_size;
|
|---|
| 283 | register long obj_size = h->next_free - h->object_base;
|
|---|
| 284 | register long i;
|
|---|
| 285 | long already;
|
|---|
| 286 |
|
|---|
| 287 | /* Compute size for new chunk. */
|
|---|
| 288 | new_size = (obj_size + length) + (obj_size >> 3) + 100;
|
|---|
| 289 | if (new_size < h->chunk_size)
|
|---|
| 290 | new_size = h->chunk_size;
|
|---|
| 291 |
|
|---|
| 292 | /* Allocate and initialize the new chunk. */
|
|---|
| 293 | new_chunk = CALL_CHUNKFUN (h, new_size);
|
|---|
| 294 | if (!new_chunk)
|
|---|
| 295 | (*obstack_alloc_failed_handler) ();
|
|---|
| 296 | h->chunk = new_chunk;
|
|---|
| 297 | new_chunk->prev = old_chunk;
|
|---|
| 298 | new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
|
|---|
| 299 |
|
|---|
| 300 | /* Move the existing object to the new chunk.
|
|---|
| 301 | Word at a time is fast and is safe if the object
|
|---|
| 302 | is sufficiently aligned. */
|
|---|
| 303 | if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
|
|---|
| 304 | {
|
|---|
| 305 | for (i = obj_size / sizeof (COPYING_UNIT) - 1;
|
|---|
| 306 | i >= 0; i--)
|
|---|
| 307 | ((COPYING_UNIT *)new_chunk->contents)[i]
|
|---|
| 308 | = ((COPYING_UNIT *)h->object_base)[i];
|
|---|
| 309 | /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
|
|---|
| 310 | but that can cross a page boundary on a machine
|
|---|
| 311 | which does not do strict alignment for COPYING_UNITS. */
|
|---|
| 312 | already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
|
|---|
| 313 | }
|
|---|
| 314 | else
|
|---|
| 315 | already = 0;
|
|---|
| 316 | /* Copy remaining bytes one by one. */
|
|---|
| 317 | for (i = already; i < obj_size; i++)
|
|---|
| 318 | new_chunk->contents[i] = h->object_base[i];
|
|---|
| 319 |
|
|---|
| 320 | /* If the object just copied was the only data in OLD_CHUNK,
|
|---|
| 321 | free that chunk and remove it from the chain.
|
|---|
| 322 | But not if that chunk might contain an empty object. */
|
|---|
| 323 | if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
|
|---|
| 324 | {
|
|---|
| 325 | new_chunk->prev = old_chunk->prev;
|
|---|
| 326 | CALL_FREEFUN (h, old_chunk);
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | h->object_base = new_chunk->contents;
|
|---|
| 330 | h->next_free = h->object_base + obj_size;
|
|---|
| 331 | /* The new chunk certainly contains no empty object yet. */
|
|---|
| 332 | h->maybe_empty_object = 0;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
|
|---|