1 /* Safe automatic memory allocation. 2 Copyright (C) 2003-2004 Free Software Foundation, Inc. 3 Written by Bruno Haible <bruno@clisp.org>, 2003. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2, or (at your option) 8 any later version. 9 10 This program 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 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 19 #ifndef _ALLOCSA_H 20 #define _ALLOCSA_H 21 22 #ifdef HAVE_ALLOCA_H 23 #include <alloca.h> 24 #endif 25 #include <stddef.h> 26 #include <stdlib.h> 27 28 /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call 29 alloca(N); otherwise it returns NULL. It either returns N bytes of 30 memory allocated on the stack, that lasts until the function returns, 31 or NULL. 32 Use of safe_alloca should be avoided: 33 - inside arguments of function calls - undefined behaviour, 34 - in inline functions - the allocation may actually last until the 35 calling function returns. 36 */ 37 #if HAVE_ALLOCA 38 /* The OS usually guarantees only one guard page at the bottom of the stack, 39 and a page size can be as small as 4096 bytes. So we cannot safely 40 allocate anything larger than 4096 bytes. Also care for the possibility 41 of a few compiler-allocated temporary stack slots. 42 This must be a macro, not an inline function. */ 43 # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL) 44 #else 45 # define safe_alloca(N) ((N), NULL) 46 #endif 47 48 /* allocsa(N) is a safe variant of alloca(N). It allocates N bytes of 49 memory allocated on the stack, that must be freed using freesa() before 50 the function returns. Upon failure, it returns NULL. */ 51 #if HAVE_ALLOCA 52 # define allocsa(N) \ 53 ((N) < 4032 - sa_increment \ 54 ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \ 55 : mallocsa (N)) 56 #else 57 # define allocsa(N) \ 58 mallocsa (N) 59 #endif 60 extern void * mallocsa (size_t n); 61 62 /* Free a block of memory allocated through allocsa(). */ 63 #if HAVE_ALLOCA 64 extern void freesa (void *p); 65 #else 66 # define freesa free 67 #endif 68 69 /* Maybe we should also define a variant 70 nallocsa (size_t n, size_t s) - behaves like allocsa (n * s) 71 If this would be useful in your application. please speak up. */ 72 73 74 /* ------------------- Auxiliary, non-public definitions ------------------- */ 75 76 /* Determine the alignment of a type at compile time. */ 77 #if defined __GNUC__ 78 # define sa_alignof __alignof__ 79 #elif defined __cplusplus 80 template <class type> struct sa_alignof_helper { char __slot1; type __slot2; }; 81 # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2) 82 #elif defined __hpux 83 /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof 84 values. */ 85 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8) 86 #else 87 # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2) 88 #endif 89 90 enum 91 { 92 /* The desired alignment of memory allocations is the maximum alignment 93 among all elementary types. */ 94 sa_alignment_long = sa_alignof (long), 95 sa_alignment_double = sa_alignof (double), 96 #ifdef HAVE_LONG_LONG 97 sa_alignment_longlong = sa_alignof (long long), 98 #endif 99 #ifdef HAVE_LONG_DOUBLE 100 sa_alignment_longdouble = sa_alignof (long double), 101 #endif 102 sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1) 103 #ifdef HAVE_LONG_LONG 104 | (sa_alignment_longlong - 1) 105 #endif 106 #ifdef HAVE_LONG_DOUBLE 107 | (sa_alignment_longdouble - 1) 108 #endif 109 ) + 1, 110 /* The increment that guarantees room for a magic word must be >= sizeof (int) 111 and a multiple of sa_alignment_max. */ 112 sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max 113 }; 114 115 #endif /* _ALLOCSA_H */ 116