summaryrefslogtreecommitdiffstats
path: root/gl/xmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/xmalloc.c')
-rw-r--r--gl/xmalloc.c36
1 files changed, 20 insertions, 16 deletions
diff --git a/gl/xmalloc.c b/gl/xmalloc.c
index 289cbd05..6adc43c0 100644
--- a/gl/xmalloc.c
+++ b/gl/xmalloc.c
@@ -1,6 +1,6 @@
1/* xmalloc.c -- malloc with out of memory checking 1/* xmalloc.c -- malloc with out of memory checking
2 2
3 Copyright (C) 1990-2000, 2002-2006, 2008-2023 Free Software Foundation, Inc. 3 Copyright (C) 1990-2000, 2002-2006, 2008-2026 Free Software Foundation, Inc.
4 4
5 This program is free software: you can redistribute it and/or modify 5 This program 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 General Public License as published by
@@ -15,10 +15,8 @@
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 17
18#include <config.h>
19
20#define XALLOC_INLINE _GL_EXTERN_INLINE 18#define XALLOC_INLINE _GL_EXTERN_INLINE
21 19#include <config.h>
22#include "xalloc.h" 20#include "xalloc.h"
23 21
24#include "ialloc.h" 22#include "ialloc.h"
@@ -29,8 +27,14 @@
29#include <stdint.h> 27#include <stdint.h>
30#include <string.h> 28#include <string.h>
31 29
32static void * _GL_ATTRIBUTE_PURE 30/* Pacify GCC up to at least 15.2, which otherwise would incorrectly
33nonnull (void *p) 31 complain about check_nonnull. */
32#if _GL_GNUC_PREREQ (4, 6)
33# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
34#endif
35
36static void *
37check_nonnull (void *p)
34{ 38{
35 if (!p) 39 if (!p)
36 xalloc_die (); 40 xalloc_die ();
@@ -42,13 +46,13 @@ nonnull (void *p)
42void * 46void *
43xmalloc (size_t s) 47xmalloc (size_t s)
44{ 48{
45 return nonnull (malloc (s)); 49 return check_nonnull (malloc (s));
46} 50}
47 51
48void * 52void *
49ximalloc (idx_t s) 53ximalloc (idx_t s)
50{ 54{
51 return nonnull (imalloc (s)); 55 return check_nonnull (imalloc (s));
52} 56}
53 57
54char * 58char *
@@ -64,7 +68,7 @@ void *
64xrealloc (void *p, size_t s) 68xrealloc (void *p, size_t s)
65{ 69{
66 void *r = realloc (p, s); 70 void *r = realloc (p, s);
67 if (!r && (!p || s)) 71 if (!r)
68 xalloc_die (); 72 xalloc_die ();
69 return r; 73 return r;
70} 74}
@@ -72,7 +76,7 @@ xrealloc (void *p, size_t s)
72void * 76void *
73xirealloc (void *p, idx_t s) 77xirealloc (void *p, idx_t s)
74{ 78{
75 return nonnull (irealloc (p, s)); 79 return check_nonnull (irealloc (p, s));
76} 80}
77 81
78/* Change the size of an allocated block of memory P to an array of N 82/* Change the size of an allocated block of memory P to an array of N
@@ -82,7 +86,7 @@ void *
82xreallocarray (void *p, size_t n, size_t s) 86xreallocarray (void *p, size_t n, size_t s)
83{ 87{
84 void *r = reallocarray (p, n, s); 88 void *r = reallocarray (p, n, s);
85 if (!r && (!p || (n && s))) 89 if (!r)
86 xalloc_die (); 90 xalloc_die ();
87 return r; 91 return r;
88} 92}
@@ -90,7 +94,7 @@ xreallocarray (void *p, size_t n, size_t s)
90void * 94void *
91xireallocarray (void *p, idx_t n, idx_t s) 95xireallocarray (void *p, idx_t n, idx_t s)
92{ 96{
93 return nonnull (ireallocarray (p, n, s)); 97 return check_nonnull (ireallocarray (p, n, s));
94} 98}
95 99
96/* Allocate an array of N objects, each with S bytes of memory, 100/* Allocate an array of N objects, each with S bytes of memory,
@@ -224,13 +228,13 @@ x2nrealloc (void *p, size_t *pn, size_t s)
224void * 228void *
225xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s) 229xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
226{ 230{
227 idx_t n0 = *pn;
228
229 /* The approximate size to use for initial small allocation 231 /* The approximate size to use for initial small allocation
230 requests. This is the largest "small" request for the GNU C 232 requests. This is the largest "small" request for the GNU C
231 library malloc. */ 233 library malloc. */
232 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; 234 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
233 235
236 idx_t n0 = *pn;
237
234 /* If the array is tiny, grow it to about (but no greater than) 238 /* If the array is tiny, grow it to about (but no greater than)
235 DEFAULT_MXFAST bytes. Otherwise, grow it by about 50%. 239 DEFAULT_MXFAST bytes. Otherwise, grow it by about 50%.
236 Adjust the growth according to three constraints: N_INCR_MIN, 240 Adjust the growth according to three constraints: N_INCR_MIN,
@@ -295,13 +299,13 @@ xizalloc (idx_t s)
295void * 299void *
296xcalloc (size_t n, size_t s) 300xcalloc (size_t n, size_t s)
297{ 301{
298 return nonnull (calloc (n, s)); 302 return check_nonnull (calloc (n, s));
299} 303}
300 304
301void * 305void *
302xicalloc (idx_t n, idx_t s) 306xicalloc (idx_t n, idx_t s)
303{ 307{
304 return nonnull (icalloc (n, s)); 308 return check_nonnull (icalloc (n, s));
305} 309}
306 310
307/* Clone an object P of size S, with error checking. There's no need 311/* Clone an object P of size S, with error checking. There's no need