]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/alloc.h: Reimplement [X]REALLOC[F]() macros with _Generic(3)
authorAlejandro Colomar <alx@kernel.org>
Thu, 9 May 2024 22:58:23 +0000 (00:58 +0200)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Wed, 15 May 2024 10:08:00 +0000 (12:08 +0200)
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 <alx@kernel.org>
lib/alloc.h

index 0e04884947cae5bbb5e1686bd1ed1f0acc34b2d0..39405a56fc1d7f602f2b4ba0685fd268f07aae96 100644 (file)
@@ -1,8 +1,5 @@
-/*
- * SPDX-FileCopyrightText:  2023, Alejandro Colomar <alx@kernel.org>
- *
- * SPDX-License-Identifier:  BSD-3-Clause
- */
+// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
 
 
 #ifndef SHADOW_INCLUDE_LIB_MALLOC_H_
 #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)