diff --git a/lib/alloc/calloc.h b/lib/alloc/calloc.h index db10a685bb..a9b64a2a29 100644 --- a/lib/alloc/calloc.h +++ b/lib/alloc/calloc.h @@ -10,16 +10,12 @@ #include +#include "cast.h" #include "exit_if_null.h" -#include "sizeof.h" // calloc_T - calloc type-safe -#define calloc_T(n, T) calloc_T_(n, typeas(T)) -#define calloc_T_(n, T) \ -({ \ - (T *){calloc(n, sizeof(T))}; \ -}) +#define calloc_T(n, T) ptr_cast(T, calloc(n, sizeof(T))) // xcalloc_T - exit-on-error calloc type-safe diff --git a/lib/alloc/malloc.h b/lib/alloc/malloc.h index 1e3a69291a..70a7f522bd 100644 --- a/lib/alloc/malloc.h +++ b/lib/alloc/malloc.h @@ -16,11 +16,7 @@ // malloc_T - malloc type-safe -#define malloc_T(n, T) malloc_T_(n, typeas(T)) -#define malloc_T_(n, T) \ -({ \ - (T *){mallocarray(n, sizeof(T))}; \ -}) +#define malloc_T(n, T) ptr_cast(T, mallocarray(n, sizeof(T))) // xmalloc_T - exit-on-error malloc type-safe diff --git a/lib/alloc/realloc.h b/lib/alloc/realloc.h index ac9f046ec6..d52d152472 100644 --- a/lib/alloc/realloc.h +++ b/lib/alloc/realloc.h @@ -17,10 +17,10 @@ // realloc_T - realloc type-safe #define realloc_T(p, n, T) realloc_T_(p, n, typeas(T)) #define realloc_T_(p, n, T) \ -({ \ - _Generic(p, T *: (void)0); \ - (T *){reallocarray_(p, n, sizeof(T))}; \ -}) +( \ + _Generic(p, T *: (void)0), \ + ptr_cast(T, reallocarray_(p, n, sizeof(T))) \ +) #define reallocarray_(p, n, size) reallocarray(p, (n) ?: 1, (size) ?: 1) diff --git a/lib/alloc/reallocf.h b/lib/alloc/reallocf.h index c3522829d6..717987e521 100644 --- a/lib/alloc/reallocf.h +++ b/lib/alloc/reallocf.h @@ -18,10 +18,10 @@ // reallocf_T - realloc free-on-error type-safe #define reallocf_T(p, n, T) reallocf_T_(p, n, typeas(T)) #define reallocf_T_(p, n, T) \ -({ \ - _Generic(p, T *: (void)0); \ - (T *){reallocarrayf_(p, n, sizeof(T))}; \ -}) +( \ + _Generic(p, T *: (void)0), \ + ptr_cast(T, reallocarrayf_(p, n, sizeof(T))) \ +) #define reallocarrayf_(p, n, size) reallocarrayf(p, (n) ?: 1, (size) ?: 1) diff --git a/lib/cast.h b/lib/cast.h index e8e42e1a0b..6678220e04 100644 --- a/lib/cast.h +++ b/lib/cast.h @@ -10,6 +10,9 @@ #define const_cast(T, p) _Generic(p, const T: (T) (p)) +#define ptr_cast(T, p) rvalue((typeas(T) *){(p)}) + +#define rvalue(lv) ((void)0, (lv)) #endif // include guard diff --git a/lib/search/l/lfind.h b/lib/search/l/lfind.h index 7bbd16e0e3..6c0945b19d 100644 --- a/lib/search/l/lfind.h +++ b/lib/search/l/lfind.h @@ -18,11 +18,11 @@ // lfind_T - linear find type-safe #define lfind_T(T, ...) lfind_T_(typeas(T), __VA_ARGS__) #define lfind_T_(T, k, a, n, cmp) \ -({ \ - _Generic(k, T *: (void)0, const T *: (void)0); \ - _Generic(a, T *: (void)0, const T *: (void)0); \ - (T *){lfind_(k, a, n, sizeof(T), cmp)}; \ -}) +( \ + _Generic(k, T *: (void)0, const T *: (void)0), \ + _Generic(a, T *: (void)0, const T *: (void)0), \ + ptr_cast(T, lfind_(k, a, n, sizeof(T), cmp)) \ +) #define LFIND(T, ...) lfind_T(T, __VA_ARGS__, CMP(T)) diff --git a/lib/sizeof.h b/lib/sizeof.h index 1fc38873dd..e9aa343a0b 100644 --- a/lib/sizeof.h +++ b/lib/sizeof.h @@ -14,10 +14,12 @@ #endif #include +#include "cast.h" + #define typeas(T) typeof((T){0}) -#define ssizeof(x) ({(ssize_t){sizeof(x)};}) +#define ssizeof(x) rvalue((ssize_t){sizeof(x)}) #define memberof(T, member) ((T){}.member) #define WIDTHOF(x) (sizeof(x) * CHAR_BIT)