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