Blame gnulib/lib/malloca.c

Packit 06dd63
/* Safe automatic memory allocation.
Packit 06dd63
   Copyright (C) 2003, 2006-2007, 2009-2019 Free Software Foundation, Inc.
Packit 06dd63
   Written by Bruno Haible <bruno@clisp.org>, 2003, 2018.
Packit 06dd63
Packit 06dd63
   This program is free software; you can redistribute it and/or modify
Packit 06dd63
   it under the terms of the GNU Lesser General Public License as published by
Packit 06dd63
   the Free Software Foundation; either version 2.1, or (at your option)
Packit 06dd63
   any later version.
Packit 06dd63
Packit 06dd63
   This program is distributed in the hope that it will be useful,
Packit 06dd63
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 06dd63
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 06dd63
   GNU Lesser General Public License for more details.
Packit 06dd63
Packit 06dd63
   You should have received a copy of the GNU Lesser General Public License
Packit 06dd63
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit 06dd63
Packit 06dd63
#define _GL_USE_STDLIB_ALLOC 1
Packit 06dd63
#include <config.h>
Packit 06dd63
Packit 06dd63
/* Specification.  */
Packit 06dd63
#include "malloca.h"
Packit 06dd63
Packit 06dd63
#include "verify.h"
Packit 06dd63
Packit 06dd63
/* The speed critical point in this file is freea() applied to an alloca()
Packit 06dd63
   result: it must be fast, to match the speed of alloca().  The speed of
Packit 06dd63
   mmalloca() and freea() in the other case are not critical, because they
Packit 06dd63
   are only invoked for big memory sizes.
Packit 06dd63
   Here we use a bit in the address as an indicator, an idea by Ondřej Bílka.
Packit 06dd63
   malloca() can return three types of pointers:
Packit 06dd63
     - Pointers ≡ 0 mod 2*sa_alignment_max come from stack allocation.
Packit 06dd63
     - Pointers ≡ sa_alignment_max mod 2*sa_alignment_max come from heap
Packit 06dd63
       allocation.
Packit 06dd63
     - NULL comes from a failed heap allocation.  */
Packit 06dd63
Packit 06dd63
/* Type for holding very small pointer differences.  */
Packit 06dd63
typedef unsigned char small_t;
Packit 06dd63
/* Verify that it is wide enough.  */
Packit 06dd63
verify (2 * sa_alignment_max - 1 <= (small_t) -1);
Packit 06dd63
Packit 06dd63
void *
Packit 06dd63
mmalloca (size_t n)
Packit 06dd63
{
Packit 06dd63
#if HAVE_ALLOCA
Packit 06dd63
  /* Allocate one more word, used to determine the address to pass to freea(),
Packit 06dd63
     and room for the alignment ≡ sa_alignment_max mod 2*sa_alignment_max.  */
Packit 06dd63
  size_t nplus = n + sizeof (small_t) + 2 * sa_alignment_max - 1;
Packit 06dd63
Packit 06dd63
  if (nplus >= n)
Packit 06dd63
    {
Packit 06dd63
      char *mem = (char *) malloc (nplus);
Packit 06dd63
Packit 06dd63
      if (mem != NULL)
Packit 06dd63
        {
Packit 06dd63
          char *p =
Packit 06dd63
            (char *)((((uintptr_t)mem + sizeof (small_t) + sa_alignment_max - 1)
Packit 06dd63
                      & ~(uintptr_t)(2 * sa_alignment_max - 1))
Packit 06dd63
                     + sa_alignment_max);
Packit 06dd63
          /* Here p >= mem + sizeof (small_t),
Packit 06dd63
             and p <= mem + sizeof (small_t) + 2 * sa_alignment_max - 1
Packit 06dd63
             hence p + n <= mem + nplus.
Packit 06dd63
             So, the memory range [p, p+n) lies in the allocated memory range
Packit 06dd63
             [mem, mem + nplus).  */
Packit 06dd63
          ((small_t *) p)[-1] = p - mem;
Packit 06dd63
          /* p ≡ sa_alignment_max mod 2*sa_alignment_max.  */
Packit 06dd63
          return p;
Packit 06dd63
        }
Packit 06dd63
    }
Packit 06dd63
  /* Out of memory.  */
Packit 06dd63
  return NULL;
Packit 06dd63
#else
Packit 06dd63
# if !MALLOC_0_IS_NONNULL
Packit 06dd63
  if (n == 0)
Packit 06dd63
    n = 1;
Packit 06dd63
# endif
Packit 06dd63
  return malloc (n);
Packit 06dd63
#endif
Packit 06dd63
}
Packit 06dd63
Packit 06dd63
#if HAVE_ALLOCA
Packit 06dd63
void
Packit 06dd63
freea (void *p)
Packit 06dd63
{
Packit 06dd63
  /* Check argument.  */
Packit 06dd63
  if ((uintptr_t) p & (sa_alignment_max - 1))
Packit 06dd63
    {
Packit 06dd63
      /* p was not the result of a malloca() call.  Invalid argument.  */
Packit 06dd63
      abort ();
Packit 06dd63
    }
Packit 06dd63
  /* Determine whether p was a non-NULL pointer returned by mmalloca().  */
Packit 06dd63
  if ((uintptr_t) p & sa_alignment_max)
Packit 06dd63
    {
Packit 06dd63
      void *mem = (char *) p - ((small_t *) p)[-1];
Packit 06dd63
      free (mem);
Packit 06dd63
    }
Packit 06dd63
}
Packit 06dd63
#endif
Packit 06dd63
Packit 06dd63
/*
Packit 06dd63
 * Hey Emacs!
Packit 06dd63
 * Local Variables:
Packit 06dd63
 * coding: utf-8
Packit 06dd63
 * End:
Packit 06dd63
 */