1 /* Substitute for and wrapper around <malloc.h>.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 @PRAGMA_SYSTEM_HEADER@
22 #if defined _GL_ALREADY_INCLUDING_MALLOC_H
23 /* Special invocation convention:
24 - On Android we have a sequence of nested includes
25 <malloc.h> -> <stdio.h> -> <sys/stat.h> -> <time.h> -> <sys/time.h> ->
26 <sys/select.h> -> <signal.h> -> <pthread.h> -> <stdlib.h> -> "malloc.h"
27 In this situation, in C++ mode, the declaration of memalign might be used
28 before it actually occurs. */
30 #@INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ @NEXT_AS_FIRST_DIRECTIVE_MALLOC_H@
33 /* Normal invocation convention. */
35 #ifndef _@GUARD_PREFIX@_MALLOC_H
37 #define _GL_ALREADY_INCLUDING_MALLOC_H
39 /* The include_next requires a split double-inclusion guard. */
41 # @INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ @NEXT_AS_FIRST_DIRECTIVE_MALLOC_H@
44 #undef _GL_ALREADY_INCLUDING_MALLOC_H
46 #ifndef _@GUARD_PREFIX@_MALLOC_H
47 #define _@GUARD_PREFIX@_MALLOC_H
49 /* This file uses _GL_ATTRIBUTE_DEALLOC, _GL_ATTRIBUTE_NOTHROW,
50 GNULIB_POSIXCHECK, HAVE_RAW_DECL_*. */
51 #if !_GL_CONFIG_H_INCLUDED
52 #error "Please include config.h first."
55 /* Solaris declares memalign() in <stdlib.h>, not in <malloc.h>. */
56 #if defined __sun || defined __hpux
64 /* _GL_ATTRIBUTE_NOTHROW declares that the function does not throw exceptions.
66 #ifndef _GL_ATTRIBUTE_NOTHROW
67 # if defined __cplusplus
68 # if (__GNUC__ + (__GNUC_MINOR__ >= 8) > 2) || __clang_major >= 4
69 # if __cplusplus >= 201103L
70 # define _GL_ATTRIBUTE_NOTHROW noexcept (true)
72 # define _GL_ATTRIBUTE_NOTHROW throw ()
75 # define _GL_ATTRIBUTE_NOTHROW
78 # if (__GNUC__ + (__GNUC_MINOR__ >= 3) > 3) || defined __clang__
79 # define _GL_ATTRIBUTE_NOTHROW __attribute__ ((__nothrow__))
81 # define _GL_ATTRIBUTE_NOTHROW
86 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
88 /* The definition of _GL_WARN_ON_USE is copied here. */
91 /* Make _GL_ATTRIBUTE_DEALLOC_FREE work, even though <stdlib.h> may not have
93 #if @GNULIB_FREE_POSIX@
94 # if (@REPLACE_FREE@ && !defined free \
95 && !(defined __cplusplus && defined GNULIB_NAMESPACE))
96 /* We can't do '#define free rpl_free' here. */
97 # if defined __cplusplus && (__GLIBC__ + (__GLIBC_MINOR__ >= 14) > 2)
98 _GL_EXTERN_C
void rpl_free (void *) _GL_ATTRIBUTE_NOTHROW
;
100 _GL_EXTERN_C
void rpl_free (void *);
102 # undef _GL_ATTRIBUTE_DEALLOC_FREE
103 # define _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC (rpl_free, 1)
105 # if defined _MSC_VER && !defined free
108 __declspec (dllimport
)
110 void __cdecl
free (void *);
112 # if defined __cplusplus && (__GLIBC__ + (__GLIBC_MINOR__ >= 14) > 2)
113 _GL_EXTERN_C
void free (void *) _GL_ATTRIBUTE_NOTHROW
;
115 _GL_EXTERN_C
void free (void *);
120 # if defined _MSC_VER && !defined free
123 __declspec (dllimport
)
125 void __cdecl
free (void *);
127 # if defined __cplusplus && (__GLIBC__ + (__GLIBC_MINOR__ >= 14) > 2)
128 _GL_EXTERN_C
void free (void *) _GL_ATTRIBUTE_NOTHROW
;
130 _GL_EXTERN_C
void free (void *);
136 /* Declare overridden functions. */
138 #if @GNULIB_MEMALIGN@
139 # if @REPLACE_MEMALIGN@
140 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
142 # define memalign rpl_memalign
144 _GL_FUNCDECL_RPL (memalign
, void *,
145 (size_t alignment
, size_t size
)
146 _GL_ATTRIBUTE_DEALLOC_FREE
);
147 _GL_CXXALIAS_RPL (memalign
, void *, (size_t alignment
, size_t size
));
151 /* For -Wmismatched-dealloc: Associate memalign with free or rpl_free. */
152 # if __GLIBC__ + (__GLIBC_MINOR__ >= 14) > 2
153 _GL_FUNCDECL_SYS (memalign
, void *,
154 (size_t alignment
, size_t size
)
155 _GL_ATTRIBUTE_NOTHROW
156 _GL_ATTRIBUTE_DEALLOC_FREE
);
158 _GL_FUNCDECL_SYS (memalign
, void *,
159 (size_t alignment
, size_t size
)
160 _GL_ATTRIBUTE_DEALLOC_FREE
);
163 _GL_CXXALIAS_SYS (memalign
, void *, (size_t alignment
, size_t size
));
167 _GL_CXXALIASWARN (memalign
);
170 # if __GNUC__ >= 11 && !defined memalign
171 /* For -Wmismatched-dealloc: Associate memalign with free or rpl_free. */
172 # if __GLIBC__ + (__GLIBC_MINOR__ >= 14) > 2
173 _GL_FUNCDECL_SYS (memalign
, void *,
174 (size_t alignment
, size_t size
)
175 _GL_ATTRIBUTE_NOTHROW
176 _GL_ATTRIBUTE_DEALLOC_FREE
);
178 _GL_FUNCDECL_SYS (memalign
, void *,
179 (size_t alignment
, size_t size
)
180 _GL_ATTRIBUTE_DEALLOC_FREE
);
183 # if defined GNULIB_POSIXCHECK
185 # if HAVE_RAW_DECL_MEMALIGN
186 _GL_WARN_ON_USE (memalign
, "memalign is not portable - "
187 "use gnulib module memalign for portability");
193 #endif /* _@GUARD_PREFIX@_MALLOC_H */
194 #endif /* _@GUARD_PREFIX@_MALLOC_H */