obstack.c revision 38889
1/* obstack.c - subroutines used implicitly by object stack macros 2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc. 3 4This program is free software; you can redistribute it and/or modify it 5under the terms of the GNU General Public License as published by the 6Free Software Foundation; either version 2, or (at your option) any 7later version. 8 9This program is distributed in the hope that it will be useful, 10but WITHOUT ANY WARRANTY; without even the implied warranty of 11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12GNU General Public License for more details. 13 14You should have received a copy of the GNU General Public License 15along with this program; if not, write to the Free Software 16Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ 17 18#include "obstack.h" 19 20/* NOTE BEFORE MODIFYING THIS FILE: This version number must be 21 incremented whenever callers compiled using an old obstack.h can no 22 longer properly call the functions in this obstack.c. */ 23#define OBSTACK_INTERFACE_VERSION 1 24 25/* Comment out all this code if we are using the GNU C Library, and are not 26 actually compiling the library itself, and the installed library 27 supports the same library interface we do. This code is part of the GNU 28 C Library, but also included in many other GNU distributions. Compiling 29 and linking in this code is a waste when using the GNU C library 30 (especially if it is a shared library). Rather than having every GNU 31 program understand `configure --with-gnu-libc' and omit the object 32 files, it is simpler to just do this in the source for each such file. */ 33 34#include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */ 35#if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1 36#include <gnu-versions.h> 37#if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION 38#define ELIDE_CODE 39#endif 40#endif 41 42/* CYGNUS LOCAL (not to be elided!) */ 43 44int 45_obstack_memory_used (h) 46 struct obstack *h; 47{ 48 register struct _obstack_chunk* lp; 49 register int nbytes = 0; 50 51 for (lp = h->chunk; lp != 0; lp = lp->prev) 52 { 53 nbytes += lp->limit - (char *) lp; 54 } 55 return nbytes; 56} 57 58/* END CYGNUS LOCAL */ 59 60#ifndef ELIDE_CODE 61 62 63#if defined (__STDC__) && __STDC__ 64#define POINTER void * 65#else 66#define POINTER char * 67#endif 68 69/* Determine default alignment. */ 70struct fooalign {char x; double d;}; 71#define DEFAULT_ALIGNMENT \ 72 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0)) 73/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. 74 But in fact it might be less smart and round addresses to as much as 75 DEFAULT_ROUNDING. So we prepare for it to do that. */ 76union fooround {long x; double d;}; 77#define DEFAULT_ROUNDING (sizeof (union fooround)) 78 79/* When we copy a long block of data, this is the unit to do it with. 80 On some machines, copying successive ints does not work; 81 in such a case, redefine COPYING_UNIT to `long' (if that works) 82 or `char' as a last resort. */ 83#ifndef COPYING_UNIT 84#define COPYING_UNIT int 85#endif 86 87/* The non-GNU-C macros copy the obstack into this global variable 88 to avoid multiple evaluation. */ 89 90struct obstack *_obstack; 91 92/* Define a macro that either calls functions with the traditional malloc/free 93 calling interface, or calls functions with the mmalloc/mfree interface 94 (that adds an extra first argument), based on the state of use_extra_arg. 95 For free, do not use ?:, since some compilers, like the MIPS compilers, 96 do not allow (expr) ? void : void. */ 97 98#define CALL_CHUNKFUN(h, size) \ 99 (((h) -> use_extra_arg) \ 100 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \ 101 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size))) 102 103#define CALL_FREEFUN(h, old_chunk) \ 104 do { \ 105 if ((h) -> use_extra_arg) \ 106 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \ 107 else \ 108 (*(void (*) ()) (h)->freefun) ((old_chunk)); \ 109 } while (0) 110 111 112/* Initialize an obstack H for use. Specify chunk size SIZE (0 means default). 113 Objects start on multiples of ALIGNMENT (0 means use default). 114 CHUNKFUN is the function to use to allocate chunks, 115 and FREEFUN the function to free them. 116 117 Return nonzero if successful, zero if out of memory. 118 To recover from an out of memory error, 119 free up some memory, then call this again. */ 120 121int 122_obstack_begin (h, size, alignment, chunkfun, freefun) 123 struct obstack *h; 124 int size; 125 int alignment; 126 POINTER (*chunkfun) (); 127 void (*freefun) (); 128{ 129 register struct _obstack_chunk *chunk; /* points to new chunk */ 130 131 if (alignment == 0) 132 alignment = DEFAULT_ALIGNMENT; 133 if (size == 0) 134 /* Default size is what GNU malloc can fit in a 4096-byte block. */ 135 { 136 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. 137 Use the values for range checking, because if range checking is off, 138 the extra bytes won't be missed terribly, but if range checking is on 139 and we used a larger request, a whole extra 4096 bytes would be 140 allocated. 141 142 These number are irrelevant to the new GNU malloc. I suspect it is 143 less sensitive to the size of the request. */ 144 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) 145 + 4 + DEFAULT_ROUNDING - 1) 146 & ~(DEFAULT_ROUNDING - 1)); 147 size = 4096 - extra; 148 } 149 150 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun; 151 h->freefun = freefun; 152 h->chunk_size = size; 153 h->alignment_mask = alignment - 1; 154 h->use_extra_arg = 0; 155 156 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); 157 if (!chunk) 158 { 159 h->alloc_failed = 1; 160 return 0; 161 } 162 h->alloc_failed = 0; 163 h->next_free = h->object_base = chunk->contents; 164 h->chunk_limit = chunk->limit 165 = (char *) chunk + h->chunk_size; 166 chunk->prev = 0; 167 /* The initial chunk now contains no empty object. */ 168 h->maybe_empty_object = 0; 169 return 1; 170} 171 172int 173_obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg) 174 struct obstack *h; 175 int size; 176 int alignment; 177 POINTER (*chunkfun) (); 178 void (*freefun) (); 179 POINTER arg; 180{ 181 register struct _obstack_chunk *chunk; /* points to new chunk */ 182 183 if (alignment == 0) 184 alignment = DEFAULT_ALIGNMENT; 185 if (size == 0) 186 /* Default size is what GNU malloc can fit in a 4096-byte block. */ 187 { 188 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. 189 Use the values for range checking, because if range checking is off, 190 the extra bytes won't be missed terribly, but if range checking is on 191 and we used a larger request, a whole extra 4096 bytes would be 192 allocated. 193 194 These number are irrelevant to the new GNU malloc. I suspect it is 195 less sensitive to the size of the request. */ 196 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) 197 + 4 + DEFAULT_ROUNDING - 1) 198 & ~(DEFAULT_ROUNDING - 1)); 199 size = 4096 - extra; 200 } 201 202 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun; 203 h->freefun = freefun; 204 h->chunk_size = size; 205 h->alignment_mask = alignment - 1; 206 h->extra_arg = arg; 207 h->use_extra_arg = 1; 208 209 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); 210 if (!chunk) 211 { 212 h->alloc_failed = 1; 213 return 0; 214 } 215 h->alloc_failed = 0; 216 h->next_free = h->object_base = chunk->contents; 217 h->chunk_limit = chunk->limit 218 = (char *) chunk + h->chunk_size; 219 chunk->prev = 0; 220 /* The initial chunk now contains no empty object. */ 221 h->maybe_empty_object = 0; 222 return 1; 223} 224 225/* Allocate a new current chunk for the obstack *H 226 on the assumption that LENGTH bytes need to be added 227 to the current object, or a new object of length LENGTH allocated. 228 Copies any partial object from the end of the old chunk 229 to the beginning of the new one. */ 230 231void 232_obstack_newchunk (h, length) 233 struct obstack *h; 234 int length; 235{ 236 register struct _obstack_chunk *old_chunk = h->chunk; 237 register struct _obstack_chunk *new_chunk; 238 register long new_size; 239 register int obj_size = h->next_free - h->object_base; 240 register int i; 241 int already; 242 243 /* Compute size for new chunk. */ 244 new_size = (obj_size + length) + (obj_size >> 3) + 100; 245 if (new_size < h->chunk_size) 246 new_size = h->chunk_size; 247 248 /* Allocate and initialize the new chunk. */ 249 new_chunk = CALL_CHUNKFUN (h, new_size); 250 if (!new_chunk) 251 { 252 h->alloc_failed = 1; 253 return; 254 } 255 h->alloc_failed = 0; 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#if defined (__STDC__) && __STDC__ 300/* Suppress -Wmissing-prototypes warning. We don't want to declare this in 301 obstack.h because it is just for debugging. */ 302int _obstack_allocated_p (struct obstack *h, POINTER obj); 303#endif 304 305int 306_obstack_allocated_p (h, obj) 307 struct obstack *h; 308 POINTER obj; 309{ 310 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ 311 register struct _obstack_chunk *plp; /* point to previous chunk if any */ 312 313 lp = (h)->chunk; 314 /* We use >= rather than > since the object cannot be exactly at 315 the beginning of the chunk but might be an empty object exactly 316 at the end of an adjacent chunk. */ 317 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj)) 318 { 319 plp = lp->prev; 320 lp = plp; 321 } 322 return lp != 0; 323} 324 325/* Free objects in obstack H, including OBJ and everything allocate 326 more recently than OBJ. If OBJ is zero, free everything in H. */ 327 328#undef obstack_free 329 330/* This function has two names with identical definitions. 331 This is the first one, called from non-ANSI code. */ 332 333void 334_obstack_free (h, obj) 335 struct obstack *h; 336 POINTER obj; 337{ 338 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ 339 register struct _obstack_chunk *plp; /* point to previous chunk if any */ 340 341 lp = h->chunk; 342 /* We use >= because there cannot be an object at the beginning of a chunk. 343 But there can be an empty object at that address 344 at the end of another chunk. */ 345 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj)) 346 { 347 plp = lp->prev; 348 CALL_FREEFUN (h, lp); 349 lp = plp; 350 /* If we switch chunks, we can't tell whether the new current 351 chunk contains an empty object, so assume that it may. */ 352 h->maybe_empty_object = 1; 353 } 354 if (lp) 355 { 356 h->object_base = h->next_free = (char *) (obj); 357 h->chunk_limit = lp->limit; 358 h->chunk = lp; 359 } 360 else if (obj != 0) 361 /* obj is not in any of the chunks! */ 362 abort (); 363} 364 365/* This function is used from ANSI code. */ 366 367void 368obstack_free (h, obj) 369 struct obstack *h; 370 POINTER obj; 371{ 372 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ 373 register struct _obstack_chunk *plp; /* point to previous chunk if any */ 374 375 lp = h->chunk; 376 /* We use >= because there cannot be an object at the beginning of a chunk. 377 But there can be an empty object at that address 378 at the end of another chunk. */ 379 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj)) 380 { 381 plp = lp->prev; 382 CALL_FREEFUN (h, lp); 383 lp = plp; 384 /* If we switch chunks, we can't tell whether the new current 385 chunk contains an empty object, so assume that it may. */ 386 h->maybe_empty_object = 1; 387 } 388 if (lp) 389 { 390 h->object_base = h->next_free = (char *) (obj); 391 h->chunk_limit = lp->limit; 392 h->chunk = lp; 393 } 394 else if (obj != 0) 395 /* obj is not in any of the chunks! */ 396 abort (); 397} 398 399#if 0 400/* These are now turned off because the applications do not use it 401 and it uses bcopy via obstack_grow, which causes trouble on sysV. */ 402 403/* Now define the functional versions of the obstack macros. 404 Define them to simply use the corresponding macros to do the job. */ 405 406#if defined (__STDC__) && __STDC__ 407/* These function definitions do not work with non-ANSI preprocessors; 408 they won't pass through the macro names in parentheses. */ 409 410/* The function names appear in parentheses in order to prevent 411 the macro-definitions of the names from being expanded there. */ 412 413POINTER (obstack_base) (obstack) 414 struct obstack *obstack; 415{ 416 return obstack_base (obstack); 417} 418 419POINTER (obstack_next_free) (obstack) 420 struct obstack *obstack; 421{ 422 return obstack_next_free (obstack); 423} 424 425int (obstack_object_size) (obstack) 426 struct obstack *obstack; 427{ 428 return obstack_object_size (obstack); 429} 430 431int (obstack_room) (obstack) 432 struct obstack *obstack; 433{ 434 return obstack_room (obstack); 435} 436 437void (obstack_grow) (obstack, pointer, length) 438 struct obstack *obstack; 439 POINTER pointer; 440 int length; 441{ 442 obstack_grow (obstack, pointer, length); 443} 444 445void (obstack_grow0) (obstack, pointer, length) 446 struct obstack *obstack; 447 POINTER pointer; 448 int length; 449{ 450 obstack_grow0 (obstack, pointer, length); 451} 452 453void (obstack_1grow) (obstack, character) 454 struct obstack *obstack; 455 int character; 456{ 457 obstack_1grow (obstack, character); 458} 459 460void (obstack_blank) (obstack, length) 461 struct obstack *obstack; 462 int length; 463{ 464 obstack_blank (obstack, length); 465} 466 467void (obstack_1grow_fast) (obstack, character) 468 struct obstack *obstack; 469 int character; 470{ 471 obstack_1grow_fast (obstack, character); 472} 473 474void (obstack_blank_fast) (obstack, length) 475 struct obstack *obstack; 476 int length; 477{ 478 obstack_blank_fast (obstack, length); 479} 480 481POINTER (obstack_finish) (obstack) 482 struct obstack *obstack; 483{ 484 return obstack_finish (obstack); 485} 486 487POINTER (obstack_alloc) (obstack, length) 488 struct obstack *obstack; 489 int length; 490{ 491 return obstack_alloc (obstack, length); 492} 493 494POINTER (obstack_copy) (obstack, pointer, length) 495 struct obstack *obstack; 496 POINTER pointer; 497 int length; 498{ 499 return obstack_copy (obstack, pointer, length); 500} 501 502POINTER (obstack_copy0) (obstack, pointer, length) 503 struct obstack *obstack; 504 POINTER pointer; 505 int length; 506{ 507 return obstack_copy0 (obstack, pointer, length); 508} 509 510#endif /* __STDC__ */ 511 512#endif /* 0 */ 513 514#endif /* !ELIDE_CODE */ 515