1 /* obstack.c - subroutines used implicitly by object stack macros 2 3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software 5 Foundation, Inc. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2, or (at your option) 10 any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program; if not, write to the Free Software Foundation, 19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 20 21 #ifdef _LIBC 22 # include <obstack.h> 23 # include <shlib-compat.h> 24 #else 25 # include <config.h> 26 # include "obstack.h" 27 #endif 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 #include <stddef.h> 52 53 #ifndef ELIDE_CODE 54 55 # include <stdint.h> 56 57 /* Determine default alignment. */ 58 union fooround 59 { 60 uintmax_t i; 61 long double d; 62 void *p; 63 }; 64 struct fooalign 65 { 66 char c; 67 union fooround u; 68 }; 69 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. 70 But in fact it might be less smart and round addresses to as much as 71 DEFAULT_ROUNDING. So we prepare for it to do that. */ 72 enum 73 { 74 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u), 75 DEFAULT_ROUNDING = sizeof (union fooround) 76 }; 77 78 /* When we copy a long block of data, this is the unit to do it with. 79 On some machines, copying successive ints does not work; 80 in such a case, redefine COPYING_UNIT to `long' (if that works) 81 or `char' as a last resort. */ 82 # ifndef COPYING_UNIT 83 # define COPYING_UNIT int 84 # endif 85 86 87 /* The functions allocating more room by calling `obstack_chunk_alloc' 88 jump to the handler pointed to by `obstack_alloc_failed_handler'. 89 This can be set to a user defined function which should either 90 abort gracefully or use longjump - but shouldn't return. This 91 variable by default points to the internal function 92 `print_and_abort'. */ 93 static void print_and_abort (void); 94 void (*obstack_alloc_failed_handler) (void) = print_and_abort; 95 96 /* Exit value used when `print_and_abort' is used. */ 97 # include <stdlib.h> 98 # ifdef _LIBC 99 int obstack_exit_failure = EXIT_FAILURE; 100 # else 101 # include "exitfail.h" 102 # define obstack_exit_failure exit_failure 103 # endif 104 105 # ifdef _LIBC 106 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4) 107 /* A looong time ago (before 1994, anyway; we're not sure) this global variable 108 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C 109 library still exports it because somebody might use it. */ 110 struct obstack *_obstack_compat; 111 compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0); 112 # endif 113 # endif 114 115 /* Define a macro that either calls functions with the traditional malloc/free 116 calling interface, or calls functions with the mmalloc/mfree interface 117 (that adds an extra first argument), based on the state of use_extra_arg. 118 For free, do not use ?:, since some compilers, like the MIPS compilers, 119 do not allow (expr) ? void : void. */ 120 121 # define CALL_CHUNKFUN(h, size) \ 122 (((h) -> use_extra_arg) \ 123 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \ 124 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size))) 125 126 # define CALL_FREEFUN(h, old_chunk) \ 127 do { \ 128 if ((h) -> use_extra_arg) \ 129 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \ 130 else \ 131 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \ 132 } while (0) 133 134 135 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default). 137 Objects start on multiples of ALIGNMENT (0 means use default). 138 CHUNKFUN is the function to use to allocate chunks, 139 and FREEFUN the function to free them. 140 141 Return nonzero if successful, calls obstack_alloc_failed_handler if 142 allocation fails. */ 143 144 int 145 _obstack_begin (struct obstack *h, 146 int size, int alignment, 147 void *(*chunkfun) (long), 148 void (*freefun) (void *)) 149 { 150 register struct _obstack_chunk *chunk; /* points to new chunk */ 151 152 if (alignment == 0) 153 alignment = DEFAULT_ALIGNMENT; 154 if (size == 0) 155 /* Default size is what GNU malloc can fit in a 4096-byte block. */ 156 { 157 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. 158 Use the values for range checking, because if range checking is off, 159 the extra bytes won't be missed terribly, but if range checking is on 160 and we used a larger request, a whole extra 4096 bytes would be 161 allocated. 162 163 These number are irrelevant to the new GNU malloc. I suspect it is 164 less sensitive to the size of the request. */ 165 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) 166 + 4 + DEFAULT_ROUNDING - 1) 167 & ~(DEFAULT_ROUNDING - 1)); 168 size = 4096 - extra; 169 } 170 171 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun; 172 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun; 173 h->chunk_size = size; 174 h->alignment_mask = alignment - 1; 175 h->use_extra_arg = 0; 176 177 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); 178 if (!chunk) 179 (*obstack_alloc_failed_handler) (); 180 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents, 181 alignment - 1); 182 h->chunk_limit = chunk->limit 183 = (char *) chunk + h->chunk_size; 184 chunk->prev = 0; 185 /* The initial chunk now contains no empty object. */ 186 h->maybe_empty_object = 0; 187 h->alloc_failed = 0; 188 return 1; 189 } 190 191 int 192 _obstack_begin_1 (struct obstack *h, int size, int alignment, 193 void *(*chunkfun) (void *, long), 194 void (*freefun) (void *, void *), 195 void *arg) 196 { 197 register struct _obstack_chunk *chunk; /* points to new chunk */ 198 199 if (alignment == 0) 200 alignment = DEFAULT_ALIGNMENT; 201 if (size == 0) 202 /* Default size is what GNU malloc can fit in a 4096-byte block. */ 203 { 204 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. 205 Use the values for range checking, because if range checking is off, 206 the extra bytes won't be missed terribly, but if range checking is on 207 and we used a larger request, a whole extra 4096 bytes would be 208 allocated. 209 210 These number are irrelevant to the new GNU malloc. I suspect it is 211 less sensitive to the size of the request. */ 212 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) 213 + 4 + DEFAULT_ROUNDING - 1) 214 & ~(DEFAULT_ROUNDING - 1)); 215 size = 4096 - extra; 216 } 217 218 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun; 219 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun; 220 h->chunk_size = size; 221 h->alignment_mask = alignment - 1; 222 h->extra_arg = arg; 223 h->use_extra_arg = 1; 224 225 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); 226 if (!chunk) 227 (*obstack_alloc_failed_handler) (); 228 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents, 229 alignment - 1); 230 h->chunk_limit = chunk->limit 231 = (char *) chunk + h->chunk_size; 232 chunk->prev = 0; 233 /* The initial chunk now contains no empty object. */ 234 h->maybe_empty_object = 0; 235 h->alloc_failed = 0; 236 return 1; 237 } 238 239 /* Allocate a new current chunk for the obstack *H 240 on the assumption that LENGTH bytes need to be added 241 to the current object, or a new object of length LENGTH allocated. 242 Copies any partial object from the end of the old chunk 243 to the beginning of the new one. */ 244 245 void 246 _obstack_newchunk (struct obstack *h, int length) 247 { 248 register struct _obstack_chunk *old_chunk = h->chunk; 249 register struct _obstack_chunk *new_chunk; 250 register long new_size; 251 register long obj_size = h->next_free - h->object_base; 252 register long i; 253 long already; 254 char *object_base; 255 256 /* Compute size for new chunk. */ 257 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100; 258 if (new_size < h->chunk_size) 259 new_size = h->chunk_size; 260 261 /* Allocate and initialize the new chunk. */ 262 new_chunk = CALL_CHUNKFUN (h, new_size); 263 if (!new_chunk) 264 (*obstack_alloc_failed_handler) (); 265 h->chunk = new_chunk; 266 new_chunk->prev = old_chunk; 267 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size; 268 269 /* Compute an aligned object_base in the new chunk */ 270 object_base = 271 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask); 272 273 /* Move the existing object to the new chunk. 274 Word at a time is fast and is safe if the object 275 is sufficiently aligned. */ 276 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT) 277 { 278 for (i = obj_size / sizeof (COPYING_UNIT) - 1; 279 i >= 0; i--) 280 ((COPYING_UNIT *)object_base)[i] 281 = ((COPYING_UNIT *)h->object_base)[i]; 282 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT, 283 but that can cross a page boundary on a machine 284 which does not do strict alignment for COPYING_UNITS. */ 285 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT); 286 } 287 else 288 already = 0; 289 /* Copy remaining bytes one by one. */ 290 for (i = already; i < obj_size; i++) 291 object_base[i] = h->object_base[i]; 292 293 /* If the object just copied was the only data in OLD_CHUNK, 294 free that chunk and remove it from the chain. 295 But not if that chunk might contain an empty object. */ 296 if (! h->maybe_empty_object 297 && (h->object_base 298 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents, 299 h->alignment_mask))) 300 { 301 new_chunk->prev = old_chunk->prev; 302 CALL_FREEFUN (h, old_chunk); 303 } 304 305 h->object_base = object_base; 306 h->next_free = h->object_base + obj_size; 307 /* The new chunk certainly contains no empty object yet. */ 308 h->maybe_empty_object = 0; 309 } 310 # ifdef _LIBC 311 libc_hidden_def (_obstack_newchunk) 312 # endif 313 314 /* Return nonzero if object OBJ has been allocated from obstack H. 315 This is here for debugging. 316 If you use it in a program, you are probably losing. */ 317 318 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in 319 obstack.h because it is just for debugging. */ 320 int _obstack_allocated_p (struct obstack *h, void *obj); 321 322 int 323 _obstack_allocated_p (struct obstack *h, void *obj) 324 { 325 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ 326 register struct _obstack_chunk *plp; /* point to previous chunk if any */ 327 328 lp = (h)->chunk; 329 /* We use >= rather than > since the object cannot be exactly at 330 the beginning of the chunk but might be an empty object exactly 331 at the end of an adjacent chunk. */ 332 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj)) 333 { 334 plp = lp->prev; 335 lp = plp; 336 } 337 return lp != 0; 338 } 339 340 /* Free objects in obstack H, including OBJ and everything allocate 342 more recently than OBJ. If OBJ is zero, free everything in H. */ 343 344 # undef obstack_free 345 346 void 347 __obstack_free (struct obstack *h, void *obj) 348 { 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 */ 351 352 lp = h->chunk; 353 /* We use >= because there cannot be an object at the beginning of a chunk. 354 But there can be an empty object at that address 355 at the end of another chunk. */ 356 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj)) 357 { 358 plp = lp->prev; 359 CALL_FREEFUN (h, lp); 360 lp = plp; 361 /* If we switch chunks, we can't tell whether the new current 362 chunk contains an empty object, so assume that it may. */ 363 h->maybe_empty_object = 1; 364 } 365 if (lp) 366 { 367 h->object_base = h->next_free = (char *) (obj); 368 h->chunk_limit = lp->limit; 369 h->chunk = lp; 370 } 371 else if (obj != 0) 372 /* obj is not in any of the chunks! */ 373 abort (); 374 } 375 376 # ifdef _LIBC 377 /* Older versions of libc used a function _obstack_free intended to be 378 called by non-GCC compilers. */ 379 strong_alias (obstack_free, _obstack_free) 380 # endif 381 382 int 384 _obstack_memory_used (struct obstack *h) 385 { 386 register struct _obstack_chunk* lp; 387 register int nbytes = 0; 388 389 for (lp = h->chunk; lp != 0; lp = lp->prev) 390 { 391 nbytes += lp->limit - (char *) lp; 392 } 393 return nbytes; 394 } 395 396 /* Define the error handler. */ 398 # ifdef _LIBC 399 # include <libintl.h> 400 # else 401 # include "gettext.h" 402 # endif 403 # ifndef _ 404 # define _(msgid) gettext (msgid) 405 # endif 406 407 # ifdef _LIBC 408 # include <libio/iolibio.h> 409 # endif 410 411 # ifndef __attribute__ 412 /* This feature is available in gcc versions 2.5 and later. */ 413 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) 414 # define __attribute__(Spec) /* empty */ 415 # endif 416 # endif 417 418 static void 419 __attribute__ ((noreturn)) 420 print_and_abort (void) 421 { 422 /* Don't change any of these strings. Yes, it would be possible to add 423 the newline to the string and use fputs or so. But this must not 424 happen because the "memory exhausted" message appears in other places 425 like this and the translation should be reused instead of creating 426 a very similar string which requires a separate translation. */ 427 # ifdef _LIBC 428 (void) __fxprintf (NULL, "%s\n", _("memory exhausted")); 429 # else 430 fprintf (stderr, "%s\n", _("memory exhausted")); 431 # endif 432 exit (obstack_exit_failure); 433 } 434 435 #endif /* !ELIDE_CODE */ 436