A gnulib testdir of module 'array-oset' fails to compile with Solaris 10 cc:
cc -xarch=generic64 -O -D_STDC_C99= -g -L/home/haible/prefix-x86_64/lib -o test-array_oset test-array_oset.o libtests.a ../gllib/libgnu.a libtests.a ../gllib/libgnu.a Undefined first referenced symbol in file xrealloc test-array_oset.o xmalloc test-array_oset.o ld: fatal: symbol referencing errors. No output written to test-array_oset *** Error code 1 The reason is that apparently this compiler does not eliminate unused inline functions from xalloc.h, despite the option '-O'. This patch fixes it. 2020-10-19 Bruno Haible <br...@clisp.org> xalloc-die: Fix link error with Solaris cc (regression 2020-07-27). * lib/xalloc.h (xalloc_die): Don't declare if GNULIB_XALLOC_DIE is 0. (xmalloc, xzalloc, xcalloc, xrealloc, x2realloc, xmemdup, xstrdup, XMALLOC, XNMALLOC, XZALLOC, XCALLOC, xnmalloc, xnrealloc, x2nrealloc, xcharalloc): Don't declare/define if GNULIB_XALLOC is 0. * modules/xalloc (configure.ac): Define GNULIB_XALLOC. * modules/xalloc-die (configure.ac): Define GNULIB_XALLOC_DIE. diff --git a/lib/xalloc.h b/lib/xalloc.h index 24273ff..d4f96bf 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -31,11 +31,14 @@ _GL_INLINE_HEADER_BEGIN # define XALLOC_INLINE _GL_INLINE #endif + #ifdef __cplusplus extern "C" { #endif +#if GNULIB_XALLOC_DIE + /* This function is always triggered when memory is exhausted. It must be defined by the application, either explicitly or by using gnulib's xalloc-die module. This is the @@ -43,6 +46,10 @@ extern "C" { memory allocation failure. */ /*extern*/ _Noreturn void xalloc_die (void); +#endif /* GNULIB_XALLOC_DIE */ + +#if GNULIB_XALLOC + void *xmalloc (size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); void *xzalloc (size_t s) @@ -64,23 +71,23 @@ char *xstrdup (char const *str) /* Allocate an object of type T dynamically, with error checking. */ /* extern t *XMALLOC (typename t); */ -#define XMALLOC(t) ((t *) xmalloc (sizeof (t))) +# define XMALLOC(t) ((t *) xmalloc (sizeof (t))) /* Allocate memory for N elements of type T, with error checking. */ /* extern t *XNMALLOC (size_t n, typename t); */ -#define XNMALLOC(n, t) \ - ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t)))) +# define XNMALLOC(n, t) \ + ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t)))) /* Allocate an object of type T dynamically, with error checking, and zero it. */ /* extern t *XZALLOC (typename t); */ -#define XZALLOC(t) ((t *) xzalloc (sizeof (t))) +# define XZALLOC(t) ((t *) xzalloc (sizeof (t))) /* Allocate memory for N elements of type T, with error checking, and zero it. */ /* extern t *XCALLOC (size_t n, typename t); */ -#define XCALLOC(n, t) \ - ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t)))) +# define XCALLOC(n, t) \ + ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t)))) /* Allocate an array of N objects, each with S bytes of memory, @@ -211,8 +218,15 @@ xcharalloc (size_t n) return XNMALLOC (n, char); } +#endif /* GNULIB_XALLOC */ + + #ifdef __cplusplus } +#endif + + +#if GNULIB_XALLOC && defined __cplusplus /* C++ does not allow conversions from void * to other pointer types without a cast. Use templates to work around the problem when @@ -248,7 +262,8 @@ xmemdup (T const *p, size_t s) return (T *) xmemdup ((void const *) p, s); } -#endif +#endif /* GNULIB_XALLOC && C++ */ + _GL_INLINE_HEADER_END diff --git a/modules/xalloc b/modules/xalloc index c6e3cfd..6500756 100644 --- a/modules/xalloc +++ b/modules/xalloc @@ -15,6 +15,7 @@ xalloc-oversized configure.ac: gl_XALLOC +gl_MODULE_INDICATOR([xalloc]) Makefile.am: lib_SOURCES += xmalloc.c diff --git a/modules/xalloc-die b/modules/xalloc-die index 23ade89..f53de5a 100644 --- a/modules/xalloc-die +++ b/modules/xalloc-die @@ -14,6 +14,7 @@ stdint xalloc-oversized configure.ac: +gl_MODULE_INDICATOR([xalloc-die]) Makefile.am: lib_SOURCES += xalloc-die.c