summaryrefslogtreecommitdiffstats
path: root/gl/malloca.h
diff options
context:
space:
mode:
Diffstat (limited to 'gl/malloca.h')
-rw-r--r--gl/malloca.h69
1 files changed, 31 insertions, 38 deletions
diff --git a/gl/malloca.h b/gl/malloca.h
index 6fbe45ea..325c7277 100644
--- a/gl/malloca.h
+++ b/gl/malloca.h
@@ -1,19 +1,19 @@
1/* Safe automatic memory allocation. 1/* Safe automatic memory allocation.
2 Copyright (C) 2003-2007, 2009-2013 Free Software Foundation, Inc. 2 Copyright (C) 2003-2007, 2009-2023 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003. 3 Written by Bruno Haible <bruno@clisp.org>, 2003.
4 4
5 This program is free software; you can redistribute it and/or modify 5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU Lesser General Public License as
7 the Free Software Foundation; either version 3, or (at your option) 7 published by the Free Software Foundation; either version 2.1 of the
8 any later version. 8 License, or (at your option) any later version.
9 9
10 This program is distributed in the hope that it will be useful, 10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 13 GNU Lesser General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */ 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 17
18#ifndef _MALLOCA_H 18#ifndef _MALLOCA_H
19#define _MALLOCA_H 19#define _MALLOCA_H
@@ -21,6 +21,9 @@
21#include <alloca.h> 21#include <alloca.h>
22#include <stddef.h> 22#include <stddef.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <stdint.h>
25
26#include "xalloc-oversized.h"
24 27
25 28
26#ifdef __cplusplus 29#ifdef __cplusplus
@@ -48,40 +51,37 @@ extern "C" {
48# define safe_alloca(N) ((void) (N), NULL) 51# define safe_alloca(N) ((void) (N), NULL)
49#endif 52#endif
50 53
54/* Free a block of memory allocated through malloca(). */
55#if HAVE_ALLOCA
56extern void freea (void *p);
57#else
58# define freea free
59#endif
60
51/* malloca(N) is a safe variant of alloca(N). It allocates N bytes of 61/* malloca(N) is a safe variant of alloca(N). It allocates N bytes of
52 memory allocated on the stack, that must be freed using freea() before 62 memory allocated on the stack, that must be freed using freea() before
53 the function returns. Upon failure, it returns NULL. */ 63 the function returns. Upon failure, it returns NULL. */
54#if HAVE_ALLOCA 64#if HAVE_ALLOCA
55# define malloca(N) \ 65# define malloca(N) \
56 ((N) < 4032 - sa_increment \ 66 ((N) < 4032 - (2 * sa_alignment_max - 1) \
57 ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \ 67 ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \
68 + (2 * sa_alignment_max - 1)) \
69 & ~(uintptr_t)(2 * sa_alignment_max - 1)) \
58 : mmalloca (N)) 70 : mmalloca (N))
59#else 71#else
60# define malloca(N) \ 72# define malloca(N) \
61 mmalloca (N) 73 mmalloca (N)
62#endif 74#endif
63extern void * mmalloca (size_t n); 75extern void *mmalloca (size_t n)
64 76 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (freea, 1)
65/* Free a block of memory allocated through malloca(). */ 77 _GL_ATTRIBUTE_ALLOC_SIZE ((1));
66#if HAVE_ALLOCA
67extern void freea (void *p);
68#else
69# define freea free
70#endif
71 78
72/* nmalloca(N,S) is an overflow-safe variant of malloca (N * S). 79/* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
73 It allocates an array of N objects, each with S bytes of memory, 80 It allocates an array of N objects, each with S bytes of memory,
74 on the stack. S must be positive and N must be nonnegative. 81 on the stack. N and S should be nonnegative and free of side effects.
75 The array must be freed using freea() before the function returns. */ 82 The array must be freed using freea() before the function returns. */
76#if 1 83#define nmalloca(n, s) \
77/* Cf. the definition of xalloc_oversized. */ 84 (xalloc_oversized (n, s) ? NULL : malloca ((n) * (size_t) (s)))
78# define nmalloca(n, s) \
79 ((n) > (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) \
80 ? NULL \
81 : malloca ((n) * (s)))
82#else
83extern void * nmalloca (size_t n, size_t s);
84#endif
85 85
86 86
87#ifdef __cplusplus 87#ifdef __cplusplus
@@ -92,7 +92,7 @@ extern void * nmalloca (size_t n, size_t s);
92/* ------------------- Auxiliary, non-public definitions ------------------- */ 92/* ------------------- Auxiliary, non-public definitions ------------------- */
93 93
94/* Determine the alignment of a type at compile time. */ 94/* Determine the alignment of a type at compile time. */
95#if defined __GNUC__ || defined __IBM__ALIGNOF__ 95#if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__
96# define sa_alignof __alignof__ 96# define sa_alignof __alignof__
97#elif defined __cplusplus 97#elif defined __cplusplus
98 template <class type> struct sa_alignof_helper { char __slot1; type __slot2; }; 98 template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
@@ -115,19 +115,12 @@ enum
115 among all elementary types. */ 115 among all elementary types. */
116 sa_alignment_long = sa_alignof (long), 116 sa_alignment_long = sa_alignof (long),
117 sa_alignment_double = sa_alignof (double), 117 sa_alignment_double = sa_alignof (double),
118#if HAVE_LONG_LONG_INT
119 sa_alignment_longlong = sa_alignof (long long), 118 sa_alignment_longlong = sa_alignof (long long),
120#endif
121 sa_alignment_longdouble = sa_alignof (long double), 119 sa_alignment_longdouble = sa_alignof (long double),
122 sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1) 120 sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
123#if HAVE_LONG_LONG_INT
124 | (sa_alignment_longlong - 1) 121 | (sa_alignment_longlong - 1)
125#endif
126 | (sa_alignment_longdouble - 1) 122 | (sa_alignment_longdouble - 1)
127 ) + 1, 123 ) + 1
128/* The increment that guarantees room for a magic word must be >= sizeof (int)
129 and a multiple of sa_alignment_max. */
130 sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max
131}; 124};
132 125
133#endif /* _MALLOCA_H */ 126#endif /* _MALLOCA_H */