From 26c9dd37157353092e8709255a6a601915057c05 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 10 May 2024 00:58:23 +0200 Subject: [PATCH] lib/alloc.h: Reimplement [X]REALLOC[F]() macros with _Generic(3) Instead of GNU builtins and extensions, these macros can be implemented with C11's _Generic(3), and the result is much simpler (and safer, since it's now an error, not just a warning). Signed-off-by: Alejandro Colomar --- lib/alloc.h | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/lib/alloc.h b/lib/alloc.h index 0e048849..39405a56 100644 --- a/lib/alloc.h +++ b/lib/alloc.h @@ -1,8 +1,5 @@ -/* - * SPDX-FileCopyrightText: 2023, Alejandro Colomar - * - * SPDX-License-Identifier: BSD-3-Clause - */ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause #ifndef SHADOW_INCLUDE_LIB_MALLOC_H_ @@ -27,31 +24,19 @@ #define XMALLOC(n, type) ((type *) xmallocarray(n, sizeof(type))) #define REALLOC(ptr, n, type) \ -({ \ - __auto_type p_ = (ptr); \ - \ - static_assert(__builtin_types_compatible_p(typeof(p_), type *), ""); \ - \ - (type *) reallocarray(p_, n, sizeof(type)); \ -}) +( \ + _Generic(ptr, type *: (type *) reallocarray(ptr, n, sizeof(type))) \ +) #define REALLOCF(ptr, n, type) \ -({ \ - __auto_type p_ = (ptr); \ - \ - static_assert(__builtin_types_compatible_p(typeof(p_), type *), ""); \ - \ - (type *) reallocarrayf(p_, n, sizeof(type)); \ -}) +( \ + _Generic(ptr, type *: (type *) reallocarrayf(ptr, n, sizeof(type))) \ +) #define XREALLOC(ptr, n, type) \ -({ \ - __auto_type p_ = (ptr); \ - \ - static_assert(__builtin_types_compatible_p(typeof(p_), type *), ""); \ - \ - (type *) xreallocarray(p_, n, sizeof(type)); \ -}) +( \ + _Generic(ptr, type *: (type *) xreallocarray(ptr, n, sizeof(type))) \ +) ATTR_MALLOC(free)