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