1 /* ialloc.h -- malloc with idx_t rather than size_t
3 Copyright 2021-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_ATTRIBUTE_COLD,
22 _GL_ATTRIBUTE_MALLOC. */
23 #if !_GL_CONFIG_H_INCLUDED
24 #error "Please include config.h first."
33 _GL_INLINE_HEADER_BEGIN
35 # define IALLOC_INLINE _GL_INLINE
42 IALLOC_INLINE
void * _GL_ATTRIBUTE_COLD
43 _gl_alloc_nomem (void)
49 /* imalloc (size) is like malloc (size).
50 It returns a non-NULL pointer to size bytes of memory.
51 Upon failure, it returns NULL with errno set. */
53 _GL_ATTRIBUTE_MALLOC
/*_GL_ATTRIBUTE_DEALLOC_FREE*/
57 return s
<= SIZE_MAX
? malloc (s
) : _gl_alloc_nomem ();
60 /* irealloc (ptr, size) is like realloc (ptr, size).
61 It returns a non-NULL pointer to size bytes of memory.
62 Upon failure, it returns NULL with errno set. */
64 /*_GL_ATTRIBUTE_DEALLOC_FREE*/
66 irealloc (void *p
, idx_t s
)
68 return s
<= SIZE_MAX
? realloc (p
, s
) : _gl_alloc_nomem ();
71 /* icalloc (num, size) is like calloc (num, size).
72 It returns a non-NULL pointer to num * size bytes of memory.
73 Upon failure, it returns NULL with errno set. */
75 _GL_ATTRIBUTE_MALLOC
/*_GL_ATTRIBUTE_DEALLOC_FREE*/
77 icalloc (idx_t n
, idx_t s
)
82 return _gl_alloc_nomem ();
88 return _gl_alloc_nomem ();
94 /* ireallocarray (ptr, num, size) is like reallocarray (ptr, num, size).
95 It returns a non-NULL pointer to num * size bytes of memory.
96 Upon failure, it returns NULL with errno set. */
98 ireallocarray (void *p
, idx_t n
, idx_t s
)
100 return (n
<= SIZE_MAX
&& s
<= SIZE_MAX
101 ? reallocarray (p
, n
, s
)
102 : _gl_alloc_nomem ());
109 _GL_INLINE_HEADER_END