root/maint/gnulib/lib/obstack.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. call_chunkfun
  2. call_freefun
  3. _obstack_begin_worker
  4. _obstack_begin
  5. _obstack_begin_1
  6. _obstack_newchunk
  7. _obstack_allocated_p
  8. _obstack_free
  9. _obstack_memory_used
  10. print_and_abort

   1 /* obstack.c - subroutines used implicitly by object stack macros
   2    Copyright (C) 1988-2021 Free Software Foundation, Inc.
   3    This file is part of the GNU C Library.
   4 
   5    This file is free software: you can redistribute it and/or modify
   6    it under the terms of the GNU Lesser General Public License as
   7    published by the Free Software Foundation; either version 3 of the
   8    License, or (at your option) any later version.
   9 
  10    This file is distributed in the hope that it will be useful,
  11    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13    GNU Lesser General Public License for more details.
  14 
  15    You should have received a copy of the GNU Lesser General Public License
  16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  17 
  18 
  19 #ifdef _LIBC
  20 # include <obstack.h>
  21 #else
  22 # include <config.h>
  23 # include "obstack.h"
  24 #endif
  25 
  26 /* NOTE BEFORE MODIFYING THIS FILE: _OBSTACK_INTERFACE_VERSION in
  27    obstack.h must be incremented whenever callers compiled using an old
  28    obstack.h can no longer properly call the functions in this file.  */
  29 
  30 /* Comment out all this code if we are using the GNU C Library, and are not
  31    actually compiling the library itself, and the installed library
  32    supports the same library interface we do.  This code is part of the GNU
  33    C Library, but also included in many other GNU distributions.  Compiling
  34    and linking in this code is a waste when using the GNU C library
  35    (especially if it is a shared library).  Rather than having every GNU
  36    program understand 'configure --with-gnu-libc' and omit the object
  37    files, it is simpler to just do this in the source for each such file.  */
  38 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
  39 # include <gnu-versions.h>
  40 # if (_GNU_OBSTACK_INTERFACE_VERSION == _OBSTACK_INTERFACE_VERSION            \
  41       || (_GNU_OBSTACK_INTERFACE_VERSION == 1                                 \
  42           && _OBSTACK_INTERFACE_VERSION == 2                                  \
  43           && defined SIZEOF_INT && defined SIZEOF_SIZE_T                      \
  44           && SIZEOF_INT == SIZEOF_SIZE_T))
  45 #  define _OBSTACK_ELIDE_CODE
  46 # endif
  47 #endif
  48 
  49 #ifndef _OBSTACK_ELIDE_CODE
  50 /* If GCC, or if an oddball (testing?) host that #defines __alignof__,
  51    use the already-supplied __alignof__.  Otherwise, this must be Gnulib
  52    (as glibc assumes GCC); defer to Gnulib's alignof_type.  */
  53 # if !defined __GNUC__ && !defined __alignof__
  54 #  include <alignof.h>
  55 #  define __alignof__(type) alignof_type (type)
  56 # endif
  57 # include <stdlib.h>
  58 # include <stdint.h>
  59 
  60 # ifndef MAX
  61 #  define MAX(a,b) ((a) > (b) ? (a) : (b))
  62 # endif
  63 
  64 /* Determine default alignment.  */
  65 
  66 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  67    But in fact it might be less smart and round addresses to as much as
  68    DEFAULT_ROUNDING.  So we prepare for it to do that.
  69 
  70    DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h.  */
  71 #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double),                     \
  72                                MAX (__alignof__ (uintmax_t),                  \
  73                                     __alignof__ (void *)))
  74 #define DEFAULT_ROUNDING MAX (sizeof (long double),                           \
  75                                MAX (sizeof (uintmax_t),                       \
  76                                     sizeof (void *)))
  77 
  78 /* Call functions with either the traditional malloc/free calling
  79    interface, or the mmalloc/mfree interface (that adds an extra first
  80    argument), based on the value of use_extra_arg.  */
  81 
  82 static void *
  83 call_chunkfun (struct obstack *h, size_t size)
     /* [previous][next][first][last][top][bottom][index][help] */
  84 {
  85   if (h->use_extra_arg)
  86     return h->chunkfun.extra (h->extra_arg, size);
  87   else
  88     return h->chunkfun.plain (size);
  89 }
  90 
  91 static void
  92 call_freefun (struct obstack *h, void *old_chunk)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94   if (h->use_extra_arg)
  95     h->freefun.extra (h->extra_arg, old_chunk);
  96   else
  97     h->freefun.plain (old_chunk);
  98 }
  99 
 100 
 101 /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
 102    Objects start on multiples of ALIGNMENT (0 means use default).
 103 
 104    Return nonzero if successful, calls obstack_alloc_failed_handler if
 105    allocation fails.  */
 106 
 107 static int
 108 _obstack_begin_worker (struct obstack *h,
     /* [previous][next][first][last][top][bottom][index][help] */
 109                        _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment)
 110 {
 111   struct _obstack_chunk *chunk; /* points to new chunk */
 112 
 113   if (alignment == 0)
 114     alignment = DEFAULT_ALIGNMENT;
 115   if (size == 0)
 116     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
 117     {
 118       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
 119          Use the values for range checking, because if range checking is off,
 120          the extra bytes won't be missed terribly, but if range checking is on
 121          and we used a larger request, a whole extra 4096 bytes would be
 122          allocated.
 123 
 124          These number are irrelevant to the new GNU malloc.  I suspect it is
 125          less sensitive to the size of the request.  */
 126       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
 127                     + 4 + DEFAULT_ROUNDING - 1)
 128                    & ~(DEFAULT_ROUNDING - 1));
 129       size = 4096 - extra;
 130     }
 131 
 132   h->chunk_size = size;
 133   h->alignment_mask = alignment - 1;
 134 
 135   chunk = h->chunk = call_chunkfun (h, h->chunk_size);
 136   if (!chunk)
 137     (*obstack_alloc_failed_handler) ();
 138   h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
 139                                                alignment - 1);
 140   h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size;
 141   chunk->prev = 0;
 142   /* The initial chunk now contains no empty object.  */
 143   h->maybe_empty_object = 0;
 144   h->alloc_failed = 0;
 145   return 1;
 146 }
 147 
 148 int
 149 _obstack_begin (struct obstack *h,
     /* [previous][next][first][last][top][bottom][index][help] */
 150                 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
 151                 void *(*chunkfun) (size_t),
 152                 void (*freefun) (void *))
 153 {
 154   h->chunkfun.plain = chunkfun;
 155   h->freefun.plain = freefun;
 156   h->use_extra_arg = 0;
 157   return _obstack_begin_worker (h, size, alignment);
 158 }
 159 
 160 int
 161 _obstack_begin_1 (struct obstack *h,
     /* [previous][next][first][last][top][bottom][index][help] */
 162                   _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
 163                   void *(*chunkfun) (void *, size_t),
 164                   void (*freefun) (void *, void *),
 165                   void *arg)
 166 {
 167   h->chunkfun.extra = chunkfun;
 168   h->freefun.extra = freefun;
 169   h->extra_arg = arg;
 170   h->use_extra_arg = 1;
 171   return _obstack_begin_worker (h, size, alignment);
 172 }
 173 
 174 /* Allocate a new current chunk for the obstack *H
 175    on the assumption that LENGTH bytes need to be added
 176    to the current object, or a new object of length LENGTH allocated.
 177    Copies any partial object from the end of the old chunk
 178    to the beginning of the new one.  */
 179 
 180 void
 181 _obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length)
     /* [previous][next][first][last][top][bottom][index][help] */
 182 {
 183   struct _obstack_chunk *old_chunk = h->chunk;
 184   struct _obstack_chunk *new_chunk = 0;
 185   size_t obj_size = h->next_free - h->object_base;
 186   char *object_base;
 187 
 188   /* Compute size for new chunk.  */
 189   size_t sum1 = obj_size + length;
 190   size_t sum2 = sum1 + h->alignment_mask;
 191   size_t new_size = sum2 + (obj_size >> 3) + 100;
 192   if (new_size < sum2)
 193     new_size = sum2;
 194   if (new_size < h->chunk_size)
 195     new_size = h->chunk_size;
 196 
 197   /* Allocate and initialize the new chunk.  */
 198   if (obj_size <= sum1 && sum1 <= sum2)
 199     new_chunk = call_chunkfun (h, new_size);
 200   if (!new_chunk)
 201     (*obstack_alloc_failed_handler)();
 202   h->chunk = new_chunk;
 203   new_chunk->prev = old_chunk;
 204   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
 205 
 206   /* Compute an aligned object_base in the new chunk */
 207   object_base =
 208     __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
 209 
 210   /* Move the existing object to the new chunk.  */
 211   memcpy (object_base, h->object_base, obj_size);
 212 
 213   /* If the object just copied was the only data in OLD_CHUNK,
 214      free that chunk and remove it from the chain.
 215      But not if that chunk might contain an empty object.  */
 216   if (!h->maybe_empty_object
 217       && (h->object_base
 218           == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
 219                           h->alignment_mask)))
 220     {
 221       new_chunk->prev = old_chunk->prev;
 222       call_freefun (h, old_chunk);
 223     }
 224 
 225   h->object_base = object_base;
 226   h->next_free = h->object_base + obj_size;
 227   /* The new chunk certainly contains no empty object yet.  */
 228   h->maybe_empty_object = 0;
 229 }
 230 
 231 /* Return nonzero if object OBJ has been allocated from obstack H.
 232    This is here for debugging.
 233    If you use it in a program, you are probably losing.  */
 234 
 235 /* Suppress -Wmissing-prototypes warning.  We don't want to declare this in
 236    obstack.h because it is just for debugging.  */
 237 int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__;
     /* [previous][next][first][last][top][bottom][index][help] */
 238 
 239 int
 240 _obstack_allocated_p (struct obstack *h, void *obj)
 241 {
 242   struct _obstack_chunk *lp;    /* below addr of any objects in this chunk */
 243   struct _obstack_chunk *plp;   /* point to previous chunk if any */
 244 
 245   lp = (h)->chunk;
 246   /* We use >= rather than > since the object cannot be exactly at
 247      the beginning of the chunk but might be an empty object exactly
 248      at the end of an adjacent chunk.  */
 249   while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
 250     {
 251       plp = lp->prev;
 252       lp = plp;
 253     }
 254   return lp != 0;
 255 }
 256 
 257 /* Free objects in obstack H, including OBJ and everything allocate
 258    more recently than OBJ.  If OBJ is zero, free everything in H.  */
 259 
 260 void
 261 _obstack_free (struct obstack *h, void *obj)
     /* [previous][next][first][last][top][bottom][index][help] */
 262 {
 263   struct _obstack_chunk *lp;    /* below addr of any objects in this chunk */
 264   struct _obstack_chunk *plp;   /* point to previous chunk if any */
 265 
 266   lp = h->chunk;
 267   /* We use >= because there cannot be an object at the beginning of a chunk.
 268      But there can be an empty object at that address
 269      at the end of another chunk.  */
 270   while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
 271     {
 272       plp = lp->prev;
 273       call_freefun (h, lp);
 274       lp = plp;
 275       /* If we switch chunks, we can't tell whether the new current
 276          chunk contains an empty object, so assume that it may.  */
 277       h->maybe_empty_object = 1;
 278     }
 279   if (lp)
 280     {
 281       h->object_base = h->next_free = (char *) (obj);
 282       h->chunk_limit = lp->limit;
 283       h->chunk = lp;
 284     }
 285   else if (obj != 0)
 286     /* obj is not in any of the chunks! */
 287     abort ();
 288 }
 289 
 290 _OBSTACK_SIZE_T
 291 _obstack_memory_used (struct obstack *h)
     /* [previous][next][first][last][top][bottom][index][help] */
 292 {
 293   struct _obstack_chunk *lp;
 294   _OBSTACK_SIZE_T nbytes = 0;
 295 
 296   for (lp = h->chunk; lp != 0; lp = lp->prev)
 297     {
 298       nbytes += lp->limit - (char *) lp;
 299     }
 300   return nbytes;
 301 }
 302 
 303 # ifndef _OBSTACK_NO_ERROR_HANDLER
 304 /* Define the error handler.  */
 305 #  include <stdio.h>
 306 
 307 /* Exit value used when 'print_and_abort' is used.  */
 308 #  ifdef _LIBC
 309 int obstack_exit_failure = EXIT_FAILURE;
 310 #  else
 311 #   include "exitfail.h"
 312 #   define obstack_exit_failure exit_failure
 313 #  endif
 314 
 315 #  ifdef _LIBC
 316 #   include <libintl.h>
 317 #  else
 318 #   include "gettext.h"
 319 #  endif
 320 #  ifndef _
 321 #   define _(msgid) gettext (msgid)
 322 #  endif
 323 
 324 #  ifdef _LIBC
 325 #   include <libio/iolibio.h>
 326 #  endif
 327 
 328 static __attribute_noreturn__ void
 329 print_and_abort (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 330 {
 331   /* Don't change any of these strings.  Yes, it would be possible to add
 332      the newline to the string and use fputs or so.  But this must not
 333      happen because the "memory exhausted" message appears in other places
 334      like this and the translation should be reused instead of creating
 335      a very similar string which requires a separate translation.  */
 336 #  ifdef _LIBC
 337   (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
 338 #  else
 339   fprintf (stderr, "%s\n", _("memory exhausted"));
 340 #  endif
 341   exit (obstack_exit_failure);
 342 }
 343 
 344 /* The functions allocating more room by calling 'obstack_chunk_alloc'
 345    jump to the handler pointed to by 'obstack_alloc_failed_handler'.
 346    This can be set to a user defined function which should either
 347    abort gracefully or use longjump - but shouldn't return.  This
 348    variable by default points to the internal function
 349    'print_and_abort'.  */
 350 __attribute_noreturn__ void (*obstack_alloc_failed_handler) (void)
 351   = print_and_abort;
 352 # endif /* !_OBSTACK_NO_ERROR_HANDLER */
 353 #endif /* !_OBSTACK_ELIDE_CODE */

/* [previous][next][first][last][top][bottom][index][help] */