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