From 7e4e56d8e28805d8ce38ffe5c1c1db4e304ba07c Mon Sep 17 00:00:00 2001 From: Vftdan Date: Mon, 19 Aug 2024 20:59:56 +0200 Subject: [PATCH] Also wrap reallocarray into a type-safe macro --- defs.h | 5 +++-- event_predicate.c | 2 +- graph.c | 2 +- processing.c | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/defs.h b/defs.h index ab7e382..1eb290e 100644 --- a/defs.h +++ b/defs.h @@ -12,8 +12,9 @@ // So for structs it is usually actual downcast, but for unions it is an upcast #define DOWNCAST(contype, basename, ptr) containerof(ptr, contype, as_##basename) // Expects ptr to be of type srctype* or void*, returns (dsttype*)ptr -#define IMPLICIT_CAST(dsttype, srctype, ptr) (((union { typeof(srctype) *src; typeof(dsttype) *dst; }){.src = ptr}).dst) -#define T_ALLOC(count, T) ((T*)calloc(count, sizeof(T))) +#define IMPLICIT_CAST(dsttype, srctype, ptr) (((union { typeof(srctype) *src; typeof(dsttype) *dst; }){.src = (ptr)}).dst) +#define T_ALLOC(count, T) ((T*)calloc((count), sizeof(T))) +#define T_REALLOC(ptr, count, T) ((T*)reallocarray(IMPLICIT_CAST(void, T, ptr), (count), sizeof(T))) #define MODULE_CONSTRUCTOR(name) __attribute__((constructor)) static void name(void) diff --git a/event_predicate.c b/event_predicate.c index 256c0e5..30a850f 100644 --- a/event_predicate.c +++ b/event_predicate.c @@ -21,7 +21,7 @@ event_predicate_list_extend(EventPredicateList * lst) EventPredicate *new_values; if (lst->values) { - new_values = reallocarray(lst->values, capacity, sizeof(EventPredicate)); + new_values = T_REALLOC(lst->values, capacity, EventPredicate); } else { new_values = T_ALLOC(capacity, EventPredicate); } diff --git a/graph.c b/graph.c index e3c980d..48b698b 100644 --- a/graph.c +++ b/graph.c @@ -5,7 +5,7 @@ static void graph_channel_list_resize(GraphChannelList * lst, size_t target) { if (target > lst->length) { - GraphChannel **new_elements = reallocarray(lst->elements, target, sizeof(GraphChannel*)); + GraphChannel **new_elements = T_REALLOC(lst->elements, target, GraphChannel*); if (!new_elements) { return; } diff --git a/processing.c b/processing.c index 122f32f..b6e3258 100644 --- a/processing.c +++ b/processing.c @@ -10,13 +10,13 @@ io_subscription_list_extend(IOSubscriptionList * lst) size_t capacity = lst->capacity; capacity = capacity + (capacity >> 1) + 1; - int * new_fds = reallocarray(lst->fds, capacity, sizeof(int)); + int * new_fds = T_REALLOC(lst->fds, capacity, int); if (!new_fds) { return false; } lst->fds = new_fds; - IOHandling ** new_subscribers = reallocarray(lst->subscribers, capacity, sizeof(IOHandling*)); + IOHandling ** new_subscribers = T_REALLOC(lst->subscribers, capacity, IOHandling*); if (!new_subscribers) { return false; }