Blame lib/malloca.h

Packit Service fdd496
/* Safe automatic memory allocation.
Packit Service fdd496
   Copyright (C) 2003-2007, 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
   Written by Bruno Haible <bruno@clisp.org>, 2003.
Packit Service fdd496
Packit Service fdd496
   This program is free software; you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3, or (at your option)
Packit Service fdd496
   any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
#ifndef _MALLOCA_H
Packit Service fdd496
#define _MALLOCA_H
Packit Service fdd496
Packit Service fdd496
#include <alloca.h>
Packit Service fdd496
#include <stddef.h>
Packit Service fdd496
#include <stdlib.h>
Packit Service fdd496
#include <stdint.h>
Packit Service fdd496
Packit Service fdd496
#include "xalloc-oversized.h"
Packit Service fdd496
Packit Service fdd496
Packit Service fdd496
#ifdef __cplusplus
Packit Service fdd496
extern "C" {
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
Packit Service fdd496
/* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
Packit Service fdd496
   alloca(N); otherwise it returns NULL.  It either returns N bytes of
Packit Service fdd496
   memory allocated on the stack, that lasts until the function returns,
Packit Service fdd496
   or NULL.
Packit Service fdd496
   Use of safe_alloca should be avoided:
Packit Service fdd496
     - inside arguments of function calls - undefined behaviour,
Packit Service fdd496
     - in inline functions - the allocation may actually last until the
Packit Service fdd496
       calling function returns.
Packit Service fdd496
*/
Packit Service fdd496
#if HAVE_ALLOCA
Packit Service fdd496
/* The OS usually guarantees only one guard page at the bottom of the stack,
Packit Service fdd496
   and a page size can be as small as 4096 bytes.  So we cannot safely
Packit Service fdd496
   allocate anything larger than 4096 bytes.  Also care for the possibility
Packit Service fdd496
   of a few compiler-allocated temporary stack slots.
Packit Service fdd496
   This must be a macro, not a function.  */
Packit Service fdd496
# define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
Packit Service fdd496
#else
Packit Service fdd496
# define safe_alloca(N) ((void) (N), NULL)
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* malloca(N) is a safe variant of alloca(N).  It allocates N bytes of
Packit Service fdd496
   memory allocated on the stack, that must be freed using freea() before
Packit Service fdd496
   the function returns.  Upon failure, it returns NULL.  */
Packit Service fdd496
#if HAVE_ALLOCA
Packit Service fdd496
# define malloca(N) \
Packit Service fdd496
  ((N) < 4032 - sa_increment                                        \
Packit Service fdd496
   ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \
Packit Service fdd496
   : mmalloca (N))
Packit Service fdd496
#else
Packit Service fdd496
# define malloca(N) \
Packit Service fdd496
  mmalloca (N)
Packit Service fdd496
#endif
Packit Service fdd496
extern void * mmalloca (size_t n);
Packit Service fdd496
Packit Service fdd496
/* Free a block of memory allocated through malloca().  */
Packit Service fdd496
#if HAVE_ALLOCA
Packit Service fdd496
extern void freea (void *p);
Packit Service fdd496
#else
Packit Service fdd496
# define freea free
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
Packit Service fdd496
   It allocates an array of N objects, each with S bytes of memory,
Packit Service fdd496
   on the stack.  S must be positive and N must be nonnegative.
Packit Service fdd496
   The array must be freed using freea() before the function returns.  */
Packit Service fdd496
#define nmalloca(n, s) (xalloc_oversized (n, s) ? NULL : malloca ((n) * (s)))
Packit Service fdd496
Packit Service fdd496
Packit Service fdd496
#ifdef __cplusplus
Packit Service fdd496
}
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
Packit Service fdd496
/* ------------------- Auxiliary, non-public definitions ------------------- */
Packit Service fdd496
Packit Service fdd496
/* Determine the alignment of a type at compile time.  */
Packit Service fdd496
#if defined __GNUC__ || defined __IBM__ALIGNOF__
Packit Service fdd496
# define sa_alignof __alignof__
Packit Service fdd496
#elif defined __cplusplus
Packit Service fdd496
  template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
Packit Service fdd496
# define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
Packit Service fdd496
#elif defined __hpux
Packit Service fdd496
  /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
Packit Service fdd496
     values.  */
Packit Service fdd496
# define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
Packit Service fdd496
#elif defined _AIX
Packit Service fdd496
  /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
Packit Service fdd496
     values.  */
Packit Service fdd496
# define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
Packit Service fdd496
#else
Packit Service fdd496
# define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
enum
Packit Service fdd496
{
Packit Service fdd496
/* The desired alignment of memory allocations is the maximum alignment
Packit Service fdd496
   among all elementary types.  */
Packit Service fdd496
  sa_alignment_long = sa_alignof (long),
Packit Service fdd496
  sa_alignment_double = sa_alignof (double),
Packit Service fdd496
#if HAVE_LONG_LONG_INT
Packit Service fdd496
  sa_alignment_longlong = sa_alignof (long long),
Packit Service fdd496
#endif
Packit Service fdd496
  sa_alignment_longdouble = sa_alignof (long double),
Packit Service fdd496
  sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
Packit Service fdd496
#if HAVE_LONG_LONG_INT
Packit Service fdd496
                      | (sa_alignment_longlong - 1)
Packit Service fdd496
#endif
Packit Service fdd496
                      | (sa_alignment_longdouble - 1)
Packit Service fdd496
                     ) + 1,
Packit Service fdd496
/* The increment that guarantees room for a magic word must be >= sizeof (int)
Packit Service fdd496
   and a multiple of sa_alignment_max.  */
Packit Service fdd496
  sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max
Packit Service fdd496
};
Packit Service fdd496
Packit Service fdd496
#endif /* _MALLOCA_H */