Blame malloc/obstack.c

Packit 6c4009
/* obstack.c - subroutines used implicitly by object stack macros
Packit 6c4009
   Copyright (C) 1988-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
# include <obstack.h>
Packit 6c4009
# include <shlib-compat.h>
Packit 6c4009
#else
Packit 6c4009
# include <config.h>
Packit 6c4009
# include "obstack.h"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* NOTE BEFORE MODIFYING THIS FILE: This version number must be
Packit 6c4009
   incremented whenever callers compiled using an old obstack.h can no
Packit 6c4009
   longer properly call the functions in this obstack.c.  */
Packit 6c4009
#define OBSTACK_INTERFACE_VERSION 1
Packit 6c4009
Packit 6c4009
/* Comment out all this code if we are using the GNU C Library, and are not
Packit 6c4009
   actually compiling the library itself, and the installed library
Packit 6c4009
   supports the same library interface we do.  This code is part of the GNU
Packit 6c4009
   C Library, but also included in many other GNU distributions.  Compiling
Packit 6c4009
   and linking in this code is a waste when using the GNU C library
Packit 6c4009
   (especially if it is a shared library).  Rather than having every GNU
Packit 6c4009
   program understand 'configure --with-gnu-libc' and omit the object
Packit 6c4009
   files, it is simpler to just do this in the source for each such file.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>              /* Random thing to get __GNU_LIBRARY__.  */
Packit 6c4009
#if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
Packit 6c4009
# include <gnu-versions.h>
Packit 6c4009
# if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
Packit 6c4009
#  define ELIDE_CODE
Packit 6c4009
# endif
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <stddef.h>
Packit 6c4009
Packit 6c4009
#ifndef ELIDE_CODE
Packit 6c4009
Packit 6c4009
Packit 6c4009
# include <stdint.h>
Packit 6c4009
Packit 6c4009
/* Determine default alignment.  */
Packit 6c4009
union fooround
Packit 6c4009
{
Packit 6c4009
  uintmax_t i;
Packit 6c4009
  long double d;
Packit 6c4009
  void *p;
Packit 6c4009
};
Packit 6c4009
struct fooalign
Packit 6c4009
{
Packit 6c4009
  char c;
Packit 6c4009
  union fooround u;
Packit 6c4009
};
Packit 6c4009
/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
Packit 6c4009
   But in fact it might be less smart and round addresses to as much as
Packit 6c4009
   DEFAULT_ROUNDING.  So we prepare for it to do that.  */
Packit 6c4009
enum
Packit 6c4009
{
Packit 6c4009
  DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
Packit 6c4009
  DEFAULT_ROUNDING = sizeof (union fooround)
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* When we copy a long block of data, this is the unit to do it with.
Packit 6c4009
   On some machines, copying successive ints does not work;
Packit 6c4009
   in such a case, redefine COPYING_UNIT to 'long' (if that works)
Packit 6c4009
   or 'char' as a last resort.  */
Packit 6c4009
# ifndef COPYING_UNIT
Packit 6c4009
#  define COPYING_UNIT int
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The functions allocating more room by calling 'obstack_chunk_alloc'
Packit 6c4009
   jump to the handler pointed to by 'obstack_alloc_failed_handler'.
Packit 6c4009
   This can be set to a user defined function which should either
Packit 6c4009
   abort gracefully or use longjump - but shouldn't return.  This
Packit 6c4009
   variable by default points to the internal function
Packit 6c4009
   'print_and_abort'.  */
Packit 6c4009
static _Noreturn void print_and_abort (void);
Packit 6c4009
void (*obstack_alloc_failed_handler) (void) = print_and_abort;
Packit 6c4009
Packit 6c4009
/* Exit value used when 'print_and_abort' is used.  */
Packit 6c4009
# include <stdlib.h>
Packit 6c4009
# ifdef _LIBC
Packit 6c4009
int obstack_exit_failure = EXIT_FAILURE;
Packit 6c4009
# else
Packit 6c4009
#  include "exitfail.h"
Packit 6c4009
#  define obstack_exit_failure exit_failure
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# ifdef _LIBC
Packit 6c4009
#  if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
Packit 6c4009
/* A looong time ago (before 1994, anyway; we're not sure) this global variable
Packit 6c4009
   was used by non-GNU-C macros to avoid multiple evaluation.  The GNU C
Packit 6c4009
   library still exports it because somebody might use it.  */
Packit 6c4009
struct obstack *_obstack_compat = 0;
Packit 6c4009
compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
Packit 6c4009
#  endif
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
/* Define a macro that either calls functions with the traditional malloc/free
Packit 6c4009
   calling interface, or calls functions with the mmalloc/mfree interface
Packit 6c4009
   (that adds an extra first argument), based on the state of use_extra_arg.
Packit 6c4009
   For free, do not use ?:, since some compilers, like the MIPS compilers,
Packit 6c4009
   do not allow (expr) ? void : void.  */
Packit 6c4009
Packit 6c4009
# define CALL_CHUNKFUN(h, size) \
Packit 6c4009
  (((h)->use_extra_arg)							      \
Packit 6c4009
   ? (*(h)->chunkfun)((h)->extra_arg, (size))				      \
Packit 6c4009
   : (*(struct _obstack_chunk *(*)(long))(h)->chunkfun)((size)))
Packit 6c4009
Packit 6c4009
# define CALL_FREEFUN(h, old_chunk) \
Packit 6c4009
  do { \
Packit 6c4009
      if ((h)->use_extra_arg)						      \
Packit 6c4009
	(*(h)->freefun)((h)->extra_arg, (old_chunk));			      \
Packit 6c4009
      else								      \
Packit 6c4009
	(*(void (*)(void *))(h)->freefun)((old_chunk));			      \
Packit 6c4009
    } while (0)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
Packit 6c4009
   Objects start on multiples of ALIGNMENT (0 means use default).
Packit 6c4009
   CHUNKFUN is the function to use to allocate chunks,
Packit 6c4009
   and FREEFUN the function to free them.
Packit 6c4009
Packit 6c4009
   Return nonzero if successful, calls obstack_alloc_failed_handler if
Packit 6c4009
   allocation fails.  */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_obstack_begin (struct obstack *h,
Packit 6c4009
		int size, int alignment,
Packit 6c4009
		void *(*chunkfun) (long),
Packit 6c4009
		void (*freefun) (void *))
Packit 6c4009
{
Packit 6c4009
  struct _obstack_chunk *chunk; /* points to new chunk */
Packit 6c4009
Packit 6c4009
  if (alignment == 0)
Packit 6c4009
    alignment = DEFAULT_ALIGNMENT;
Packit 6c4009
  if (size == 0)
Packit 6c4009
    /* Default size is what GNU malloc can fit in a 4096-byte block.  */
Packit 6c4009
    {
Packit 6c4009
      /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
Packit 6c4009
	 Use the values for range checking, because if range checking is off,
Packit 6c4009
	 the extra bytes won't be missed terribly, but if range checking is on
Packit 6c4009
	 and we used a larger request, a whole extra 4096 bytes would be
Packit 6c4009
	 allocated.
Packit 6c4009
Packit 6c4009
	 These number are irrelevant to the new GNU malloc.  I suspect it is
Packit 6c4009
	 less sensitive to the size of the request.  */
Packit 6c4009
      int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
Packit 6c4009
		    + 4 + DEFAULT_ROUNDING - 1)
Packit 6c4009
		   & ~(DEFAULT_ROUNDING - 1));
Packit 6c4009
      size = 4096 - extra;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  h->chunkfun = (struct _obstack_chunk * (*) (void *, long)) chunkfun;
Packit 6c4009
  h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
Packit 6c4009
  h->chunk_size = size;
Packit 6c4009
  h->alignment_mask = alignment - 1;
Packit 6c4009
  h->use_extra_arg = 0;
Packit 6c4009
Packit 6c4009
  chunk = h->chunk = CALL_CHUNKFUN (h, h->chunk_size);
Packit 6c4009
  if (!chunk)
Packit 6c4009
    (*obstack_alloc_failed_handler) ();
Packit 6c4009
  h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
Packit 6c4009
					       alignment - 1);
Packit 6c4009
  h->chunk_limit = chunk->limit
Packit 6c4009
    = (char *) chunk + h->chunk_size;
Packit 6c4009
  chunk->prev = 0;
Packit 6c4009
  /* The initial chunk now contains no empty object.  */
Packit 6c4009
  h->maybe_empty_object = 0;
Packit 6c4009
  h->alloc_failed = 0;
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_obstack_begin_1 (struct obstack *h, int size, int alignment,
Packit 6c4009
		  void *(*chunkfun) (void *, long),
Packit 6c4009
		  void (*freefun) (void *, void *),
Packit 6c4009
		  void *arg)
Packit 6c4009
{
Packit 6c4009
  struct _obstack_chunk *chunk; /* points to new chunk */
Packit 6c4009
Packit 6c4009
  if (alignment == 0)
Packit 6c4009
    alignment = DEFAULT_ALIGNMENT;
Packit 6c4009
  if (size == 0)
Packit 6c4009
    /* Default size is what GNU malloc can fit in a 4096-byte block.  */
Packit 6c4009
    {
Packit 6c4009
      /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
Packit 6c4009
	 Use the values for range checking, because if range checking is off,
Packit 6c4009
	 the extra bytes won't be missed terribly, but if range checking is on
Packit 6c4009
	 and we used a larger request, a whole extra 4096 bytes would be
Packit 6c4009
	 allocated.
Packit 6c4009
Packit 6c4009
	 These number are irrelevant to the new GNU malloc.  I suspect it is
Packit 6c4009
	 less sensitive to the size of the request.  */
Packit 6c4009
      int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
Packit 6c4009
		    + 4 + DEFAULT_ROUNDING - 1)
Packit 6c4009
		   & ~(DEFAULT_ROUNDING - 1));
Packit 6c4009
      size = 4096 - extra;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
Packit 6c4009
  h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
Packit 6c4009
  h->chunk_size = size;
Packit 6c4009
  h->alignment_mask = alignment - 1;
Packit 6c4009
  h->extra_arg = arg;
Packit 6c4009
  h->use_extra_arg = 1;
Packit 6c4009
Packit 6c4009
  chunk = h->chunk = CALL_CHUNKFUN (h, h->chunk_size);
Packit 6c4009
  if (!chunk)
Packit 6c4009
    (*obstack_alloc_failed_handler) ();
Packit 6c4009
  h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
Packit 6c4009
					       alignment - 1);
Packit 6c4009
  h->chunk_limit = chunk->limit
Packit 6c4009
    = (char *) chunk + h->chunk_size;
Packit 6c4009
  chunk->prev = 0;
Packit 6c4009
  /* The initial chunk now contains no empty object.  */
Packit 6c4009
  h->maybe_empty_object = 0;
Packit 6c4009
  h->alloc_failed = 0;
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Allocate a new current chunk for the obstack *H
Packit 6c4009
   on the assumption that LENGTH bytes need to be added
Packit 6c4009
   to the current object, or a new object of length LENGTH allocated.
Packit 6c4009
   Copies any partial object from the end of the old chunk
Packit 6c4009
   to the beginning of the new one.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_obstack_newchunk (struct obstack *h, int length)
Packit 6c4009
{
Packit 6c4009
  struct _obstack_chunk *old_chunk = h->chunk;
Packit 6c4009
  struct _obstack_chunk *new_chunk;
Packit 6c4009
  long new_size;
Packit 6c4009
  long obj_size = h->next_free - h->object_base;
Packit 6c4009
  long i;
Packit 6c4009
  long already;
Packit 6c4009
  char *object_base;
Packit 6c4009
Packit 6c4009
  /* Compute size for new chunk.  */
Packit 6c4009
  new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
Packit 6c4009
  if (new_size < h->chunk_size)
Packit 6c4009
    new_size = h->chunk_size;
Packit 6c4009
Packit 6c4009
  /* Allocate and initialize the new chunk.  */
Packit 6c4009
  new_chunk = CALL_CHUNKFUN (h, new_size);
Packit 6c4009
  if (!new_chunk)
Packit 6c4009
    (*obstack_alloc_failed_handler)();
Packit 6c4009
  h->chunk = new_chunk;
Packit 6c4009
  new_chunk->prev = old_chunk;
Packit 6c4009
  new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
Packit 6c4009
Packit 6c4009
  /* Compute an aligned object_base in the new chunk */
Packit 6c4009
  object_base =
Packit 6c4009
    __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
Packit 6c4009
Packit 6c4009
  /* Move the existing object to the new chunk.
Packit 6c4009
     Word at a time is fast and is safe if the object
Packit 6c4009
     is sufficiently aligned.  */
Packit 6c4009
  if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
Packit 6c4009
    {
Packit 6c4009
      for (i = obj_size / sizeof (COPYING_UNIT) - 1;
Packit 6c4009
	   i >= 0; i--)
Packit 6c4009
	((COPYING_UNIT *) object_base)[i]
Packit 6c4009
	  = ((COPYING_UNIT *) h->object_base)[i];
Packit 6c4009
      /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
Packit 6c4009
	 but that can cross a page boundary on a machine
Packit 6c4009
	 which does not do strict alignment for COPYING_UNITS.  */
Packit 6c4009
      already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    already = 0;
Packit 6c4009
  /* Copy remaining bytes one by one.  */
Packit 6c4009
  for (i = already; i < obj_size; i++)
Packit 6c4009
    object_base[i] = h->object_base[i];
Packit 6c4009
Packit 6c4009
  /* If the object just copied was the only data in OLD_CHUNK,
Packit 6c4009
     free that chunk and remove it from the chain.
Packit 6c4009
     But not if that chunk might contain an empty object.  */
Packit 6c4009
  if (!h->maybe_empty_object
Packit 6c4009
      && (h->object_base
Packit 6c4009
	  == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
Packit 6c4009
			  h->alignment_mask)))
Packit 6c4009
    {
Packit 6c4009
      new_chunk->prev = old_chunk->prev;
Packit 6c4009
      CALL_FREEFUN (h, old_chunk);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  h->object_base = object_base;
Packit 6c4009
  h->next_free = h->object_base + obj_size;
Packit 6c4009
  /* The new chunk certainly contains no empty object yet.  */
Packit 6c4009
  h->maybe_empty_object = 0;
Packit 6c4009
}
Packit 6c4009
# ifdef _LIBC
Packit 6c4009
libc_hidden_def (_obstack_newchunk)
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
/* Return nonzero if object OBJ has been allocated from obstack H.
Packit 6c4009
   This is here for debugging.
Packit 6c4009
   If you use it in a program, you are probably losing.  */
Packit 6c4009
Packit 6c4009
/* Suppress -Wmissing-prototypes warning.  We don't want to declare this in
Packit 6c4009
   obstack.h because it is just for debugging.  */
Packit 6c4009
int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__;
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_obstack_allocated_p (struct obstack *h, void *obj)
Packit 6c4009
{
Packit 6c4009
  struct _obstack_chunk *lp;    /* below addr of any objects in this chunk */
Packit 6c4009
  struct _obstack_chunk *plp;   /* point to previous chunk if any */
Packit 6c4009
Packit 6c4009
  lp = (h)->chunk;
Packit 6c4009
  /* We use >= rather than > since the object cannot be exactly at
Packit 6c4009
     the beginning of the chunk but might be an empty object exactly
Packit 6c4009
     at the end of an adjacent chunk.  */
Packit 6c4009
  while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
Packit 6c4009
    {
Packit 6c4009
      plp = lp->prev;
Packit 6c4009
      lp = plp;
Packit 6c4009
    }
Packit 6c4009
  return lp != 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Free objects in obstack H, including OBJ and everything allocate
Packit 6c4009
   more recently than OBJ.  If OBJ is zero, free everything in H.  */
Packit 6c4009
Packit 6c4009
# undef obstack_free
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__obstack_free (struct obstack *h, void *obj)
Packit 6c4009
{
Packit 6c4009
  struct _obstack_chunk *lp;    /* below addr of any objects in this chunk */
Packit 6c4009
  struct _obstack_chunk *plp;   /* point to previous chunk if any */
Packit 6c4009
Packit 6c4009
  lp = h->chunk;
Packit 6c4009
  /* We use >= because there cannot be an object at the beginning of a chunk.
Packit 6c4009
     But there can be an empty object at that address
Packit 6c4009
     at the end of another chunk.  */
Packit 6c4009
  while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
Packit 6c4009
    {
Packit 6c4009
      plp = lp->prev;
Packit 6c4009
      CALL_FREEFUN (h, lp);
Packit 6c4009
      lp = plp;
Packit 6c4009
      /* If we switch chunks, we can't tell whether the new current
Packit 6c4009
	 chunk contains an empty object, so assume that it may.  */
Packit 6c4009
      h->maybe_empty_object = 1;
Packit 6c4009
    }
Packit 6c4009
  if (lp)
Packit 6c4009
    {
Packit 6c4009
      h->object_base = h->next_free = (char *) (obj);
Packit 6c4009
      h->chunk_limit = lp->limit;
Packit 6c4009
      h->chunk = lp;
Packit 6c4009
    }
Packit 6c4009
  else if (obj != 0)
Packit 6c4009
    /* obj is not in any of the chunks! */
Packit 6c4009
    abort ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# ifdef _LIBC
Packit 6c4009
/* Older versions of libc used a function _obstack_free intended to be
Packit 6c4009
   called by non-GCC compilers.  */
Packit 6c4009
strong_alias (obstack_free, _obstack_free)
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_obstack_memory_used (struct obstack *h)
Packit 6c4009
{
Packit 6c4009
  struct _obstack_chunk *lp;
Packit 6c4009
  int nbytes = 0;
Packit 6c4009
Packit 6c4009
  for (lp = h->chunk; lp != 0; lp = lp->prev)
Packit 6c4009
    {
Packit 6c4009
      nbytes += lp->limit - (char *) lp;
Packit 6c4009
    }
Packit 6c4009
  return nbytes;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Define the error handler.  */
Packit 6c4009
# ifdef _LIBC
Packit 6c4009
#  include <libintl.h>
Packit 6c4009
# else
Packit 6c4009
#  include "gettext.h"
Packit 6c4009
# endif
Packit 6c4009
# ifndef _
Packit 6c4009
#  define _(msgid) gettext (msgid)
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# ifdef _LIBC
Packit 6c4009
#  include <libio/iolibio.h>
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
static _Noreturn void
Packit 6c4009
print_and_abort (void)
Packit 6c4009
{
Packit 6c4009
  /* Don't change any of these strings.  Yes, it would be possible to add
Packit 6c4009
     the newline to the string and use fputs or so.  But this must not
Packit 6c4009
     happen because the "memory exhausted" message appears in other places
Packit 6c4009
     like this and the translation should be reused instead of creating
Packit 6c4009
     a very similar string which requires a separate translation.  */
Packit 6c4009
# ifdef _LIBC
Packit 6c4009
  (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
Packit 6c4009
# else
Packit 6c4009
  fprintf (stderr, "%s\n", _("memory exhausted"));
Packit 6c4009
# endif
Packit 6c4009
  exit (obstack_exit_failure);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif  /* !ELIDE_CODE */