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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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 #define POINTER void * 56 57 /* Determine default alignment. */ 58 struct fooalign {char x; double d;}; 59 #define DEFAULT_ALIGNMENT \ 60 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0)) 61 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. 62 But in fact it might be less smart and round addresses to as much as 63 DEFAULT_ROUNDING. So we prepare for it to do that. */ 64 union fooround {long x; double d;}; 65 #define DEFAULT_ROUNDING (sizeof (union fooround)) 66 67 /* When we copy a long block of data, this is the unit to do it with. 68 On some machines, copying successive ints does not work; 69 in such a case, redefine COPYING_UNIT to `long' (if that works) 70 or `char' as a last resort. */ 71 #ifndef COPYING_UNIT 72 #define COPYING_UNIT int 73 #endif 74 75 76 /* The functions allocating more room by calling `obstack_chunk_alloc' 77 jump to the handler pointed to by `obstack_alloc_failed_handler'. 78 This variable by default points to the internal function 79 `print_and_abort'. */ 80 static void print_and_abort (void); 81 void (*obstack_alloc_failed_handler) (void) = print_and_abort; 82 83 /* Exit value used when `print_and_abort' is used. */ 84 #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H 85 #include <stdlib.h> 86 #endif 87 #ifndef EXIT_FAILURE 88 #define EXIT_FAILURE 1 89 #endif 90 int obstack_exit_failure = EXIT_FAILURE; 91 92 /* The non-GNU-C macros copy the obstack into this global variable 93 to avoid multiple evaluation. */ 94 95 struct obstack *_obstack; 96 97 /* Define a macro that either calls functions with the traditional malloc/free 98 calling interface, or calls functions with the mmalloc/mfree interface 99 (that adds an extra first argument), based on the state of use_extra_arg. 100 For free, do not use ?:, since some compilers, like the MIPS compilers, 101 do not allow (expr) ? void : void. */ 102 103 #if defined (__STDC__) && __STDC__ 104 #define CALL_CHUNKFUN(h, size) \ 105 (((h) -> use_extra_arg) \ 106 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \ 107 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size))) 108 109 #define CALL_FREEFUN(h, old_chunk) \ 110 do { \ 111 if ((h) -> use_extra_arg) \ 112 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \ 113 else \ 114 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \ 115 } while (0) 116 #else 117 #define CALL_CHUNKFUN(h, size) \ 118 (((h) -> use_extra_arg) \ 119 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \ 120 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size))) 121 122 #define CALL_FREEFUN(h, old_chunk) \ 123 do { \ 124 if ((h) -> use_extra_arg) \ 125 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \ 126 else \ 127 (*(void (*) ()) (h)->freefun) ((old_chunk)); \ 128 } while (0) 129 #endif 130 131 132 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default). 133 Objects start on multiples of ALIGNMENT (0 means use default). 134 CHUNKFUN is the function to use to allocate chunks, 135 and FREEFUN the function to free them. 136 137 Return nonzero if successful, zero if out of memory. 138 To recover from an out of memory error, 139 free up some memory, then call this again. */ 140 141 int 142 _obstack_begin (struct obstack *h, int size, int alignment, 143 POINTER (*chunkfun) (long), void (*freefun) (void *)) 144 { 145 register struct _obstack_chunk *chunk; /* points to new chunk */ 146 147 if (alignment == 0) 148 alignment = (int) DEFAULT_ALIGNMENT; 149 if (size == 0) 150 /* Default size is what GNU malloc can fit in a 4096-byte block. */ 151 { 152 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. 153 Use the values for range checking, because if range checking is off, 154 the extra bytes won't be missed terribly, but if range checking is on 155 and we used a larger request, a whole extra 4096 bytes would be 156 allocated. 157 158 These number are irrelevant to the new GNU malloc. I suspect it is 159 less sensitive to the size of the request. */ 160 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) 161 + 4 + DEFAULT_ROUNDING - 1) 162 & ~(DEFAULT_ROUNDING - 1)); 163 size = 4096 - extra; 164 } 165 166 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun; 167 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun; 168 h->chunk_size = size; 169 h->alignment_mask = alignment - 1; 170 h->use_extra_arg = 0; 171 172 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); 173 if (!chunk) 174 (*obstack_alloc_failed_handler) (); 175 h->next_free = h->object_base = chunk->contents; 176 h->chunk_limit = chunk->limit 177 = (char *) chunk + h->chunk_size; 178 chunk->prev = 0; 179 /* The initial chunk now contains no empty object. */ 180 h->maybe_empty_object = 0; 181 h->alloc_failed = 0; 182 return 1; 183 } 184 185 int 186 _obstack_begin_1 (struct obstack *h, int size, int alignment, 187 POINTER (*chunkfun) (POINTER, long), 188 void (*freefun) (POINTER, POINTER), POINTER arg) 189 { 190 register struct _obstack_chunk *chunk; /* points to new chunk */ 191 192 if (alignment == 0) 193 alignment = (int) DEFAULT_ALIGNMENT; 194 if (size == 0) 195 /* Default size is what GNU malloc can fit in a 4096-byte block. */ 196 { 197 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. 198 Use the values for range checking, because if range checking is off, 199 the extra bytes won't be missed terribly, but if range checking is on 200 and we used a larger request, a whole extra 4096 bytes would be 201 allocated. 202 203 These number are irrelevant to the new GNU malloc. I suspect it is 204 less sensitive to the size of the request. */ 205 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) 206 + 4 + DEFAULT_ROUNDING - 1) 207 & ~(DEFAULT_ROUNDING - 1)); 208 size = 4096 - extra; 209 } 210 211 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun; 212 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun; 213 h->chunk_size = size; 214 h->alignment_mask = alignment - 1; 215 h->extra_arg = arg; 216 h->use_extra_arg = 1; 217 218 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); 219 if (!chunk) 220 (*obstack_alloc_failed_handler) (); 221 h->next_free = h->object_base = chunk->contents; 222 h->chunk_limit = chunk->limit 223 = (char *) chunk + h->chunk_size; 224 chunk->prev = 0; 225 /* The initial chunk now contains no empty object. */ 226 h->maybe_empty_object = 0; 227 h->alloc_failed = 0; 228 return 1; 229 } 230 231 /* Allocate a new current chunk for the obstack *H 232 on the assumption that LENGTH bytes need to be added 233 to the current object, or a new object of length LENGTH allocated. 234 Copies any partial object from the end of the old chunk 235 to the beginning of the new one. */ 236 237 void 238 _obstack_newchunk (struct obstack *h, int length) 239 { 240 register struct _obstack_chunk *old_chunk = h->chunk; 241 register struct _obstack_chunk *new_chunk; 242 register long new_size; 243 register long obj_size = h->next_free - h->object_base; 244 register long i; 245 long already; 246 247 /* Compute size for new chunk. */ 248 new_size = (obj_size + length) + (obj_size >> 3) + 100; 249 if (new_size < h->chunk_size) 250 new_size = h->chunk_size; 251 252 /* Allocate and initialize the new chunk. */ 253 new_chunk = CALL_CHUNKFUN (h, new_size); 254 if (!new_chunk) 255 (*obstack_alloc_failed_handler) (); 256 h->chunk = new_chunk; 257 new_chunk->prev = old_chunk; 258 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size; 259 260 /* Move the existing object to the new chunk. 261 Word at a time is fast and is safe if the object 262 is sufficiently aligned. */ 263 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT) 264 { 265 for (i = obj_size / sizeof (COPYING_UNIT) - 1; 266 i >= 0; i--) 267 ((COPYING_UNIT *)new_chunk->contents)[i] 268 = ((COPYING_UNIT *)h->object_base)[i]; 269 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT, 270 but that can cross a page boundary on a machine 271 which does not do strict alignment for COPYING_UNITS. */ 272 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT); 273 } 274 else 275 already = 0; 276 /* Copy remaining bytes one by one. */ 277 for (i = already; i < obj_size; i++) 278 new_chunk->contents[i] = h->object_base[i]; 279 280 /* If the object just copied was the only data in OLD_CHUNK, 281 free that chunk and remove it from the chain. 282 But not if that chunk might contain an empty object. */ 283 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object) 284 { 285 new_chunk->prev = old_chunk->prev; 286 CALL_FREEFUN (h, old_chunk); 287 } 288 289 h->object_base = new_chunk->contents; 290 h->next_free = h->object_base + obj_size; 291 /* The new chunk certainly contains no empty object yet. */ 292 h->maybe_empty_object = 0; 293 } 294 295 /* Return nonzero if object OBJ has been allocated from obstack H. 296 This is here for debugging. 297 If you use it in a program, you are probably losing. */ 298 299 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in 300 obstack.h because it is just for debugging. */ 301 int _obstack_allocated_p (struct obstack *h, POINTER obj); 302 303 int 304 _obstack_allocated_p (struct obstack *h, POINTER obj) 305 { 306 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ 307 register struct _obstack_chunk *plp; /* point to previous chunk if any */ 308 309 lp = (h)->chunk; 310 /* We use >= rather than > since the object cannot be exactly at 311 the beginning of the chunk but might be an empty object exactly 312 at the end of an adjacent chunk. */ 313 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj)) 314 { 315 plp = lp->prev; 316 lp = plp; 317 } 318 return lp != 0; 319 } 320 321 /* Free objects in obstack H, including OBJ and everything allocate 322 more recently than OBJ. If OBJ is zero, free everything in H. */ 323 324 #undef obstack_free 325 326 /* This function has two names with identical definitions. 327 This is the first one, called from non-ANSI code. */ 328 329 void 330 _obstack_free (struct obstack *h, POINTER obj) 331 { 332 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ 333 register struct _obstack_chunk *plp; /* point to previous chunk if any */ 334 335 lp = h->chunk; 336 /* We use >= because there cannot be an object at the beginning of a chunk. 337 But there can be an empty object at that address 338 at the end of another chunk. */ 339 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj)) 340 { 341 plp = lp->prev; 342 CALL_FREEFUN (h, lp); 343 lp = plp; 344 /* If we switch chunks, we can't tell whether the new current 345 chunk contains an empty object, so assume that it may. */ 346 h->maybe_empty_object = 1; 347 } 348 if (lp) 349 { 350 h->object_base = h->next_free = (char *) (obj); 351 h->chunk_limit = lp->limit; 352 h->chunk = lp; 353 } 354 else if (obj != 0) 355 /* obj is not in any of the chunks! */ 356 abort (); 357 } 358 359 /* This function is used from ANSI code. */ 360 361 void 362 obstack_free (struct obstack *h, POINTER obj) 363 { 364 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ 365 register struct _obstack_chunk *plp; /* point to previous chunk if any */ 366 367 lp = h->chunk; 368 /* We use >= because there cannot be an object at the beginning of a chunk. 369 But there can be an empty object at that address 370 at the end of another chunk. */ 371 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj)) 372 { 373 plp = lp->prev; 374 CALL_FREEFUN (h, lp); 375 lp = plp; 376 /* If we switch chunks, we can't tell whether the new current 377 chunk contains an empty object, so assume that it may. */ 378 h->maybe_empty_object = 1; 379 } 380 if (lp) 381 { 382 h->object_base = h->next_free = (char *) (obj); 383 h->chunk_limit = lp->limit; 384 h->chunk = lp; 385 } 386 else if (obj != 0) 387 /* obj is not in any of the chunks! */ 388 abort (); 389 } 390 391 int 392 _obstack_memory_used (struct obstack *h) 393 { 394 register struct _obstack_chunk* lp; 395 register int nbytes = 0; 396 397 for (lp = h->chunk; lp != 0; lp = lp->prev) 398 { 399 nbytes += lp->limit - (char *) lp; 400 } 401 return nbytes; 402 } 403 404 /* Define the error handler. */ 405 #ifndef _ 406 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC 407 # include <libintl.h> 408 # ifndef _ 409 # define _(Str) gettext (Str) 410 # endif 411 # else 412 # define _(Str) (Str) 413 # endif 414 #endif 415 416 static void 417 print_and_abort (void) 418 { 419 fputs (_("memory exhausted\n"), stderr); 420 exit (obstack_exit_failure); 421 } 422 423 #if 0 424 /* These are now turned off because the applications do not use it 425 and it uses bcopy via obstack_grow, which causes trouble on sysV. */ 426 427 /* Now define the functional versions of the obstack macros. 428 Define them to simply use the corresponding macros to do the job. */ 429 430 /* The function names appear in parentheses in order to prevent 431 the macro-definitions of the names from being expanded there. */ 432 433 POINTER (obstack_base) (struct obstack *obstack) 434 { 435 return obstack_base (obstack); 436 } 437 438 POINTER (obstack_next_free) (struct obstack *obstack) 439 { 440 return obstack_next_free (obstack); 441 } 442 443 int (obstack_object_size) (struct obstack *obstack) 444 { 445 return obstack_object_size (obstack); 446 } 447 448 int (obstack_room) (struct obstack *obstack) 449 { 450 return obstack_room (obstack); 451 } 452 453 int (obstack_make_room) (struct obstack *obstack, int length) 454 { 455 return obstack_make_room (obstack, length); 456 } 457 458 void (obstack_grow) (struct obstack *obstack, POINTER pointer, int length) 459 { 460 obstack_grow (obstack, pointer, length); 461 } 462 463 void (obstack_grow0) (struct obstack *obstack, POINTER pointer, int length) 464 { 465 obstack_grow0 (obstack, pointer, length); 466 } 467 468 void (obstack_1grow) (struct obstack *obstack, int character) 469 { 470 obstack_1grow (obstack, character); 471 } 472 473 void (obstack_blank) (struct obstack *obstack, int length) 474 { 475 obstack_blank (obstack, length); 476 } 477 478 void (obstack_1grow_fast) (struct obstack *obstack, int character) 479 { 480 obstack_1grow_fast (obstack, character); 481 } 482 483 void (obstack_blank_fast) (struct obstack *obstack, int length) 484 { 485 obstack_blank_fast (obstack, length); 486 } 487 488 POINTER (obstack_finish) (struct obstack *obstack) 489 { 490 return obstack_finish (obstack); 491 } 492 493 POINTER (obstack_alloc) (struct obstack *obstack, int length) 494 { 495 return obstack_alloc (obstack, length); 496 } 497 498 POINTER (obstack_copy) (struct obstack *obstack, POINTER pointer, int length) 499 { 500 return obstack_copy (obstack, pointer, length); 501 } 502 503 POINTER (obstack_copy0) (struct obstack *obstack, POINTER pointer, int length) 504 { 505 return obstack_copy0 (obstack, pointer, length); 506 } 507 508 #endif /* 0 */ 509 510 #endif /* !ELIDE_CODE */ 511