Annotation of embedaddon/libiconv/srclib/malloca.h, revision 1.1.1.1
1.1 misho 1: /* Safe automatic memory allocation.
2: Copyright (C) 2003-2007 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 3, 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 _MALLOCA_H
20: #define _MALLOCA_H
21:
22: #include <alloca.h>
23: #include <stddef.h>
24: #include <stdlib.h>
25:
26:
27: #ifdef __cplusplus
28: extern "C" {
29: #endif
30:
31:
32: /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
33: alloca(N); otherwise it returns NULL. It either returns N bytes of
34: memory allocated on the stack, that lasts until the function returns,
35: or NULL.
36: Use of safe_alloca should be avoided:
37: - inside arguments of function calls - undefined behaviour,
38: - in inline functions - the allocation may actually last until the
39: calling function returns.
40: */
41: #if HAVE_ALLOCA
42: /* The OS usually guarantees only one guard page at the bottom of the stack,
43: and a page size can be as small as 4096 bytes. So we cannot safely
44: allocate anything larger than 4096 bytes. Also care for the possibility
45: of a few compiler-allocated temporary stack slots.
46: This must be a macro, not an inline function. */
47: # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
48: #else
49: # define safe_alloca(N) ((void) (N), NULL)
50: #endif
51:
52: /* malloca(N) is a safe variant of alloca(N). It allocates N bytes of
53: memory allocated on the stack, that must be freed using freea() before
54: the function returns. Upon failure, it returns NULL. */
55: #if HAVE_ALLOCA
56: # define malloca(N) \
57: ((N) < 4032 - sa_increment \
58: ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \
59: : mmalloca (N))
60: #else
61: # define malloca(N) \
62: mmalloca (N)
63: #endif
64: extern void * mmalloca (size_t n);
65:
66: /* Free a block of memory allocated through malloca(). */
67: #if HAVE_ALLOCA
68: extern void freea (void *p);
69: #else
70: # define freea free
71: #endif
72:
73: /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
74: It allocates an array of N objects, each with S bytes of memory,
75: on the stack. S must be positive and N must be nonnegative.
76: The array must be freed using freea() before the function returns. */
77: #if 1
78: /* Cf. the definition of xalloc_oversized. */
79: # define nmalloca(n, s) \
80: ((n) > (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) \
81: ? NULL \
82: : malloca ((n) * (s)))
83: #else
84: extern void * nmalloca (size_t n, size_t s);
85: #endif
86:
87:
88: #ifdef __cplusplus
89: }
90: #endif
91:
92:
93: /* ------------------- Auxiliary, non-public definitions ------------------- */
94:
95: /* Determine the alignment of a type at compile time. */
96: #if defined __GNUC__
97: # define sa_alignof __alignof__
98: #elif defined __cplusplus
99: template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
100: # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
101: #elif defined __hpux
102: /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
103: values. */
104: # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
105: #elif defined _AIX
106: /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
107: values. */
108: # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
109: #else
110: # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
111: #endif
112:
113: enum
114: {
115: /* The desired alignment of memory allocations is the maximum alignment
116: among all elementary types. */
117: sa_alignment_long = sa_alignof (long),
118: sa_alignment_double = sa_alignof (double),
119: #if HAVE_LONG_LONG_INT
120: sa_alignment_longlong = sa_alignof (long long),
121: #endif
122: sa_alignment_longdouble = sa_alignof (long double),
123: sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
124: #if HAVE_LONG_LONG_INT
125: | (sa_alignment_longlong - 1)
126: #endif
127: | (sa_alignment_longdouble - 1)
128: ) + 1,
129: /* The increment that guarantees room for a magic word must be >= sizeof (int)
130: and a multiple of sa_alignment_max. */
131: sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max
132: };
133:
134: #endif /* _MALLOCA_H */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>