1/* -*- buffer-read-only: t -*- vi: set ro: */ 2/* DO NOT EDIT! GENERATED AUTOMATICALLY! */ 3#line 1 4/* Safe automatic memory allocation. 5 Copyright (C) 2003-2007, 2009-2010 Free Software Foundation, Inc. 6 Written by Bruno Haible <bruno@clisp.org>, 2003. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3, or (at your option) 11 any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software Foundation, 20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 21 22#ifndef _MALLOCA_H 23#define _MALLOCA_H 24 25#include <alloca.h> 26#include <stddef.h> 27#include <stdlib.h> 28 29 30#ifdef __cplusplus 31extern "C" { 32#endif 33 34 35/* safe_alloca(N) is equivalent to alloca(N) when it is safe to call 36 alloca(N); otherwise it returns NULL. It either returns N bytes of 37 memory allocated on the stack, that lasts until the function returns, 38 or NULL. 39 Use of safe_alloca should be avoided: 40 - inside arguments of function calls - undefined behaviour, 41 - in inline functions - the allocation may actually last until the 42 calling function returns. 43*/ 44#if HAVE_ALLOCA 45/* The OS usually guarantees only one guard page at the bottom of the stack, 46 and a page size can be as small as 4096 bytes. So we cannot safely 47 allocate anything larger than 4096 bytes. Also care for the possibility 48 of a few compiler-allocated temporary stack slots. 49 This must be a macro, not an inline function. */ 50# define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL) 51#else 52# define safe_alloca(N) ((void) (N), NULL) 53#endif 54 55/* malloca(N) is a safe variant of alloca(N). It allocates N bytes of 56 memory allocated on the stack, that must be freed using freea() before 57 the function returns. Upon failure, it returns NULL. */ 58#if HAVE_ALLOCA 59# define malloca(N) \ 60 ((N) < 4032 - sa_increment \ 61 ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \ 62 : mmalloca (N)) 63#else 64# define malloca(N) \ 65 mmalloca (N) 66#endif 67extern void * mmalloca (size_t n); 68 69/* Free a block of memory allocated through malloca(). */ 70#if HAVE_ALLOCA 71extern void freea (void *p); 72#else 73# define freea free 74#endif 75 76/* nmalloca(N,S) is an overflow-safe variant of malloca (N * S). 77 It allocates an array of N objects, each with S bytes of memory, 78 on the stack. S must be positive and N must be nonnegative. 79 The array must be freed using freea() before the function returns. */ 80#if 1 81/* Cf. the definition of xalloc_oversized. */ 82# define nmalloca(n, s) \ 83 ((n) > (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) \ 84 ? NULL \ 85 : malloca ((n) * (s))) 86#else 87extern void * nmalloca (size_t n, size_t s); 88#endif 89 90 91#ifdef __cplusplus 92} 93#endif 94 95 96/* ------------------- Auxiliary, non-public definitions ------------------- */ 97 98/* Determine the alignment of a type at compile time. */ 99#if defined __GNUC__ 100# define sa_alignof __alignof__ 101#elif defined __cplusplus 102 template <class type> struct sa_alignof_helper { char __slot1; type __slot2; }; 103# define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2) 104#elif defined __hpux 105 /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof 106 values. */ 107# define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8) 108#elif defined _AIX 109 /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof 110 values. */ 111# define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8) 112#else 113# define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2) 114#endif 115 116enum 117{ 118/* The desired alignment of memory allocations is the maximum alignment 119 among all elementary types. */ 120 sa_alignment_long = sa_alignof (long), 121 sa_alignment_double = sa_alignof (double), 122#if HAVE_LONG_LONG_INT 123 sa_alignment_longlong = sa_alignof (long long), 124#endif 125 sa_alignment_longdouble = sa_alignof (long double), 126 sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1) 127#if HAVE_LONG_LONG_INT 128 | (sa_alignment_longlong - 1) 129#endif 130 | (sa_alignment_longdouble - 1) 131 ) + 1, 132/* The increment that guarantees room for a magic word must be >= sizeof (int) 133 and a multiple of sa_alignment_max. */ 134 sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max 135}; 136 137#endif /* _MALLOCA_H */ 138