b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | --- a/lib/ialloc.h |
| 2 | +++ b/lib/ialloc.h |
| 3 | @@ -106,6 +106,8 @@ icalloc (idx_t n, idx_t s) |
| 4 | return calloc (n, s); |
| 5 | } |
| 6 | |
| 7 | +#if GNULIB_REALLOCARRAY |
| 8 | + |
| 9 | /* ireallocarray (ptr, num, size) is like reallocarray (ptr, num, size). |
| 10 | It returns a non-NULL pointer to num * size bytes of memory. |
| 11 | Upon failure, it returns NULL with errno set. */ |
| 12 | @@ -131,6 +133,8 @@ ireallocarray (void *p, idx_t n, idx_t s |
| 13 | return _gl_alloc_nomem (); |
| 14 | } |
| 15 | |
| 16 | +#endif /* GNULIB_REALLOCARRAY */ |
| 17 | + |
| 18 | #ifdef __cplusplus |
| 19 | } |
| 20 | #endif |
| 21 | --- a/lib/xmalloc.c |
| 22 | +++ b/lib/xmalloc.c |
| 23 | @@ -51,12 +51,16 @@ ximalloc (idx_t s) |
| 24 | return nonnull (imalloc (s)); |
| 25 | } |
| 26 | |
| 27 | +#if GNULIB_REALLOCARRAY |
| 28 | + |
| 29 | char * |
| 30 | xcharalloc (size_t n) |
| 31 | { |
| 32 | return XNMALLOC (n, char); |
| 33 | } |
| 34 | |
| 35 | +#endif /* GNULIB_REALLOCARRAY */ |
| 36 | + |
| 37 | /* Change the size of an allocated block of memory P to S bytes, |
| 38 | with error checking. */ |
| 39 | |
| 40 | @@ -75,6 +79,8 @@ xirealloc (void *p, idx_t s) |
| 41 | return nonnull (irealloc (p, s)); |
| 42 | } |
| 43 | |
| 44 | +#if GNULIB_REALLOCARRAY |
| 45 | + |
| 46 | /* Change the size of an allocated block of memory P to an array of N |
| 47 | objects each of S bytes, with error checking. */ |
| 48 | |
| 49 | @@ -205,6 +211,8 @@ x2nrealloc (void *p, size_t *pn, size_t |
| 50 | return p; |
| 51 | } |
| 52 | |
| 53 | +#endif /* GNULIB_REALLOCARRAY */ |
| 54 | + |
| 55 | /* Grow PA, which points to an array of *PN items, and return the |
| 56 | location of the reallocated array, updating *PN to reflect its |
| 57 | new size. The new array will contain at least N_INCR_MIN more |
| 58 | --- a/lib/xalloc.h |
| 59 | +++ b/lib/xalloc.h |
| 60 | @@ -129,6 +129,7 @@ char *xstrdup (char const *str) |
| 61 | # define XCALLOC(n, t) \ |
| 62 | ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t)))) |
| 63 | |
| 64 | +# if GNULIB_REALLOCARRAY |
| 65 | |
| 66 | /* Allocate an array of N objects, each with S bytes of memory, |
| 67 | dynamically, with error checking. S must be nonzero. */ |
| 68 | @@ -156,6 +157,8 @@ char *xcharalloc (size_t n) |
| 69 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE |
| 70 | _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL; |
| 71 | |
| 72 | +# endif /* GNULIB_REALLOCARRAY */ |
| 73 | + |
| 74 | #endif /* GNULIB_XALLOC */ |
| 75 | |
| 76 | |
| 77 | --- a/lib/safe-alloc.h |
| 78 | +++ b/lib/safe-alloc.h |
| 79 | @@ -36,6 +36,8 @@ _GL_INLINE_HEADER_BEGIN |
| 80 | # define SAFE_ALLOC_INLINE _GL_INLINE |
| 81 | #endif |
| 82 | |
| 83 | +#if GNULIB_REALLOCARRAY |
| 84 | + |
| 85 | /* Don't call these directly - use the macros below. */ |
| 86 | SAFE_ALLOC_INLINE void * |
| 87 | safe_alloc_realloc_n (void *ptr, size_t count, size_t size) |
| 88 | @@ -51,6 +53,9 @@ safe_alloc_realloc_n (void *ptr, size_t |
| 89 | #endif |
| 90 | return ptr; |
| 91 | } |
| 92 | + |
| 93 | +#endif /* GNULIB_REALLOCARRAY */ |
| 94 | + |
| 95 | _GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int |
| 96 | safe_alloc_check (void *ptr) |
| 97 | { |
| 98 | @@ -84,6 +89,8 @@ safe_alloc_check (void *ptr) |
| 99 | #define ALLOC_N(ptr, count) \ |
| 100 | safe_alloc_check ((ptr) = calloc (count, sizeof *(ptr))) |
| 101 | |
| 102 | +#if GNULIB_REALLOCARRAY |
| 103 | + |
| 104 | /** |
| 105 | * ALLOC_N_UNINITIALIZED: |
| 106 | * @ptr: pointer to allocated memory |
| 107 | @@ -112,6 +119,8 @@ safe_alloc_check (void *ptr) |
| 108 | #define REALLOC_N(ptr, count) \ |
| 109 | safe_alloc_check ((ptr) = safe_alloc_realloc_n (ptr, count, sizeof *(ptr))) |
| 110 | |
| 111 | +#endif /* GNULIB_REALLOCARRAY */ |
| 112 | + |
| 113 | /** |
| 114 | * FREE: |
| 115 | * @ptr: pointer holding address to be freed |