1
0
Fork 0
mirror of https://git.openwrt.org/openwrt/openwrt.git synced 2024-05-06 15:46:27 +02:00

tools/gnulib: add macros to skip reallocarray() functions

For modules that depend on the reallocarray module, like ialloc, xalloc,
and safe-alloc, it was not possible to skip importing the reallocarray
module as they all contained at least one function that called
reallocarray() and would cause build failure if the host system didn't
declare it.

This upstreamable patch adds macros that toggle whether to define
functions that depend on reallocarray() based on whether the reallocarray
module is being imported.

Tested-by: Georgi Valkov <gvalkov@gmail.com> # MacOS
Signed-off-by: Michael Pratt <mcpratt@pm.me>
Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
This commit is contained in:
Michael Pratt 2024-04-13 15:04:54 -04:00 committed by Robert Marko
parent 4de8c0e1d8
commit 5ade7ee60e

View File

@ -0,0 +1,115 @@
--- a/lib/ialloc.h
+++ b/lib/ialloc.h
@@ -106,6 +106,8 @@ icalloc (idx_t n, idx_t s)
return calloc (n, s);
}
+#if GNULIB_REALLOCARRAY
+
/* ireallocarray (ptr, num, size) is like reallocarray (ptr, num, size).
It returns a non-NULL pointer to num * size bytes of memory.
Upon failure, it returns NULL with errno set. */
@@ -131,6 +133,8 @@ ireallocarray (void *p, idx_t n, idx_t s
return _gl_alloc_nomem ();
}
+#endif /* GNULIB_REALLOCARRAY */
+
#ifdef __cplusplus
}
#endif
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -51,12 +51,16 @@ ximalloc (idx_t s)
return nonnull (imalloc (s));
}
+#if GNULIB_REALLOCARRAY
+
char *
xcharalloc (size_t n)
{
return XNMALLOC (n, char);
}
+#endif /* GNULIB_REALLOCARRAY */
+
/* Change the size of an allocated block of memory P to S bytes,
with error checking. */
@@ -75,6 +79,8 @@ xirealloc (void *p, idx_t s)
return nonnull (irealloc (p, s));
}
+#if GNULIB_REALLOCARRAY
+
/* Change the size of an allocated block of memory P to an array of N
objects each of S bytes, with error checking. */
@@ -205,6 +211,8 @@ x2nrealloc (void *p, size_t *pn, size_t
return p;
}
+#endif /* GNULIB_REALLOCARRAY */
+
/* Grow PA, which points to an array of *PN items, and return the
location of the reallocated array, updating *PN to reflect its
new size. The new array will contain at least N_INCR_MIN more
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -129,6 +129,7 @@ char *xstrdup (char const *str)
# define XCALLOC(n, t) \
((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
+# if GNULIB_REALLOCARRAY
/* Allocate an array of N objects, each with S bytes of memory,
dynamically, with error checking. S must be nonzero. */
@@ -156,6 +157,8 @@ char *xcharalloc (size_t n)
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
_GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
+# endif /* GNULIB_REALLOCARRAY */
+
#endif /* GNULIB_XALLOC */
--- a/lib/safe-alloc.h
+++ b/lib/safe-alloc.h
@@ -36,6 +36,8 @@ _GL_INLINE_HEADER_BEGIN
# define SAFE_ALLOC_INLINE _GL_INLINE
#endif
+#if GNULIB_REALLOCARRAY
+
/* Don't call these directly - use the macros below. */
SAFE_ALLOC_INLINE void *
safe_alloc_realloc_n (void *ptr, size_t count, size_t size)
@@ -51,6 +53,9 @@ safe_alloc_realloc_n (void *ptr, size_t
#endif
return ptr;
}
+
+#endif /* GNULIB_REALLOCARRAY */
+
_GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int
safe_alloc_check (void *ptr)
{
@@ -84,6 +89,8 @@ safe_alloc_check (void *ptr)
#define ALLOC_N(ptr, count) \
safe_alloc_check ((ptr) = calloc (count, sizeof *(ptr)))
+#if GNULIB_REALLOCARRAY
+
/**
* ALLOC_N_UNINITIALIZED:
* @ptr: pointer to allocated memory
@@ -112,6 +119,8 @@ safe_alloc_check (void *ptr)
#define REALLOC_N(ptr, count) \
safe_alloc_check ((ptr) = safe_alloc_realloc_n (ptr, count, sizeof *(ptr)))
+#endif /* GNULIB_REALLOCARRAY */
+
/**
* FREE:
* @ptr: pointer holding address to be freed