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