version 1.1, 2012/02/21 22:57:48
|
version 1.1.1.3, 2021/03/17 13:38:46
|
Line 1
|
Line 1
|
/* Safe automatic memory allocation. |
/* Safe automatic memory allocation. |
Copyright (C) 2003-2007 Free Software Foundation, Inc. | Copyright (C) 2003-2007, 2009-2019 Free Software Foundation, Inc. |
Written by Bruno Haible <bruno@clisp.org>, 2003. |
Written by Bruno Haible <bruno@clisp.org>, 2003. |
|
|
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
Line 13
|
Line 13
|
GNU General Public License for more details. |
GNU General Public License for more details. |
|
|
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software Foundation, | along with this program; if not, see <https://www.gnu.org/licenses/>. */ |
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | |
|
|
#ifndef _MALLOCA_H |
#ifndef _MALLOCA_H |
#define _MALLOCA_H |
#define _MALLOCA_H |
Line 22
|
Line 21
|
#include <alloca.h> |
#include <alloca.h> |
#include <stddef.h> |
#include <stddef.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <stdint.h> |
|
|
|
#include "xalloc-oversized.h" |
|
|
|
|
#ifdef __cplusplus |
#ifdef __cplusplus |
extern "C" { |
extern "C" { |
#endif |
#endif |
Line 43 extern "C" {
|
Line 45 extern "C" {
|
and a page size can be as small as 4096 bytes. So we cannot safely |
and a page size can be as small as 4096 bytes. So we cannot safely |
allocate anything larger than 4096 bytes. Also care for the possibility |
allocate anything larger than 4096 bytes. Also care for the possibility |
of a few compiler-allocated temporary stack slots. |
of a few compiler-allocated temporary stack slots. |
This must be a macro, not an inline function. */ | This must be a macro, not a function. */ |
# define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL) |
# define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL) |
#else |
#else |
# define safe_alloca(N) ((void) (N), NULL) |
# define safe_alloca(N) ((void) (N), NULL) |
Line 54 extern "C" {
|
Line 56 extern "C" {
|
the function returns. Upon failure, it returns NULL. */ |
the function returns. Upon failure, it returns NULL. */ |
#if HAVE_ALLOCA |
#if HAVE_ALLOCA |
# define malloca(N) \ |
# define malloca(N) \ |
((N) < 4032 - sa_increment \ | ((N) < 4032 - (2 * sa_alignment_max - 1) \ |
? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \ | ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \ |
| + (2 * sa_alignment_max - 1)) \ |
| & ~(uintptr_t)(2 * sa_alignment_max - 1)) \ |
: mmalloca (N)) |
: mmalloca (N)) |
#else |
#else |
# define malloca(N) \ |
# define malloca(N) \ |
Line 74 extern void freea (void *p);
|
Line 78 extern void freea (void *p);
|
It allocates an array of N objects, each with S bytes of memory, |
It allocates an array of N objects, each with S bytes of memory, |
on the stack. S must be positive and N must be nonnegative. |
on the stack. S must be positive and N must be nonnegative. |
The array must be freed using freea() before the function returns. */ |
The array must be freed using freea() before the function returns. */ |
#if 1 | #define nmalloca(n, s) (xalloc_oversized (n, s) ? NULL : malloca ((n) * (s))) |
/* Cf. the definition of xalloc_oversized. */ | |
# define nmalloca(n, s) \ | |
((n) > (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) \ | |
? NULL \ | |
: malloca ((n) * (s))) | |
#else | |
extern void * nmalloca (size_t n, size_t s); | |
#endif | |
|
|
|
|
#ifdef __cplusplus |
#ifdef __cplusplus |
Line 93 extern void * nmalloca (size_t n, size_t s);
|
Line 89 extern void * nmalloca (size_t n, size_t s);
|
/* ------------------- Auxiliary, non-public definitions ------------------- */ |
/* ------------------- Auxiliary, non-public definitions ------------------- */ |
|
|
/* Determine the alignment of a type at compile time. */ |
/* Determine the alignment of a type at compile time. */ |
#if defined __GNUC__ | #if defined __GNUC__ || defined __IBM__ALIGNOF__ |
# define sa_alignof __alignof__ |
# define sa_alignof __alignof__ |
#elif defined __cplusplus |
#elif defined __cplusplus |
template <class type> struct sa_alignof_helper { char __slot1; type __slot2; }; |
template <class type> struct sa_alignof_helper { char __slot1; type __slot2; }; |
Line 122 enum
|
Line 118 enum
|
sa_alignment_longdouble = sa_alignof (long double), |
sa_alignment_longdouble = sa_alignof (long double), |
sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1) |
sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1) |
#if HAVE_LONG_LONG_INT |
#if HAVE_LONG_LONG_INT |
| (sa_alignment_longlong - 1) | | (sa_alignment_longlong - 1) |
#endif |
#endif |
| (sa_alignment_longdouble - 1) | | (sa_alignment_longdouble - 1) |
) + 1, | ) + 1 |
/* The increment that guarantees room for a magic word must be >= sizeof (int) | |
and a multiple of sa_alignment_max. */ | |
sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max | |
}; |
}; |
|
|
#endif /* _MALLOCA_H */ |
#endif /* _MALLOCA_H */ |