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