annotate lib/malloca.h @ 9454:265fad13afeb

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