From 63dabd6fcd76d183d5ba50d311488ee02ed6113f Mon Sep 17 00:00:00 2001 From: Vftdan Date: Thu, 15 Aug 2024 16:53:02 +0200 Subject: [PATCH] Typesafe wrapper around calloc --- config.c | 4 ++-- defs.h | 3 ++- events.c | 2 +- filters.c | 2 +- main.c | 4 ++-- nodes/evdev.c | 2 +- nodes/getchar.c | 2 +- nodes/print.c | 2 +- processing.c | 4 ++-- 9 files changed, 13 insertions(+), 12 deletions(-) diff --git a/config.c b/config.c index 9ccc3f2..afeb762 100644 --- a/config.c +++ b/config.c @@ -35,7 +35,7 @@ load_nodes_section(const config_setting_t *config_section) if (length <= 0) { return result; } - result.items = calloc(length, sizeof(GraphNodeConfig)); + result.items = T_ALLOC(length, GraphNodeConfig); if (!result.items) { return result; } @@ -112,7 +112,7 @@ load_channels_section(const config_setting_t *config_section) if (length <= 0) { return result; } - result.items = calloc(length, sizeof(GraphChannelConfig)); + result.items = T_ALLOC(length, GraphChannelConfig); if (!result.items) { return result; } diff --git a/defs.h b/defs.h index 82c3f92..fc87fe8 100644 --- a/defs.h +++ b/defs.h @@ -6,11 +6,12 @@ #include #include +#define lengthof(arr) (sizeof(arr) / sizeof(*arr)) #define containerof(ptr, contype, membpath) ((contype*)(0 ? (void)(((contype*)NULL)->membpath = *(ptr)) : (void)0, ((char *)(ptr)) - offsetof(contype, membpath))) // Assuming child type has a field for the base type // 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) -#define lengthof(arr) (sizeof(arr) / sizeof(*arr)) +#define T_ALLOC(count, T) ((T*)calloc(count, sizeof(T))) #define DEBUG_PRINT_VALUE(x, fmt) fprintf(stderr, #x " = " fmt "\n", x); fflush(stderr) diff --git a/events.c b/events.c index 27572cb..81fe063 100644 --- a/events.c +++ b/events.c @@ -32,7 +32,7 @@ event_replicate(EventNode * source, size_t count) EventNode * event_create(const EventData * content) { - EventNode * event = calloc(1, sizeof(EventNode)); + EventNode * event = T_ALLOC(1, EventNode); if (content) { event->data = *content; event->data.modifiers = modifier_set_copy(content->modifiers); diff --git a/filters.c b/filters.c index 883aa94..067e9c7 100644 --- a/filters.c +++ b/filters.c @@ -23,7 +23,7 @@ event_filter_list_extend(EventFilterList * lst) if (lst->values) { new_values = reallocarray(lst->values, capacity, sizeof(EventFilter)); } else { - new_values = calloc(capacity, sizeof(EventFilter)); + new_values = T_ALLOC(capacity, EventFilter); } if (!new_values) { return false; diff --git a/main.c b/main.c index 2cfa696..b0873f5 100644 --- a/main.c +++ b/main.c @@ -32,7 +32,7 @@ main(int argc, char ** argv) exit(1); } - GraphNode **nodes = calloc(loaded_config.nodes.length, sizeof(GraphNode*)); + GraphNode **nodes = T_ALLOC(loaded_config.nodes.length, GraphNode*); for (size_t i = 0; i < loaded_config.nodes.length; ++i) { const char* type_name = loaded_config.nodes.items[i].type; if (!type_name) { @@ -57,7 +57,7 @@ main(int argc, char ** argv) } } - GraphChannel *channels = calloc(loaded_config.channels.length, sizeof(GraphChannel)); + GraphChannel *channels = T_ALLOC(loaded_config.channels.length, GraphChannel); for (size_t i = 0; i < loaded_config.channels.length; ++i) { const char *node_names[2]; GraphNode *end_nodes[2] = {NULL, NULL}; diff --git a/nodes/evdev.c b/nodes/evdev.c index 9e065f0..849addc 100644 --- a/nodes/evdev.c +++ b/nodes/evdev.c @@ -70,7 +70,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output) static GraphNode * create(GraphNodeSpecification * spec, GraphNodeConfig * config) { - EvdevGraphNode * node = calloc(1, sizeof(EvdevGraphNode)); + EvdevGraphNode * node = T_ALLOC(1, EvdevGraphNode); if (!node) { return NULL; } diff --git a/nodes/getchar.c b/nodes/getchar.c index 4bfff2f..3b3b7c8 100644 --- a/nodes/getchar.c +++ b/nodes/getchar.c @@ -50,7 +50,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output) static GraphNode * create(GraphNodeSpecification * spec, GraphNodeConfig * config) { - GetcharGraphNode * node = calloc(1, sizeof(GetcharGraphNode)); + GetcharGraphNode * node = T_ALLOC(1, GetcharGraphNode); if (!node) { return NULL; } diff --git a/nodes/print.c b/nodes/print.c index 4da2aa3..693f8f3 100644 --- a/nodes/print.c +++ b/nodes/print.c @@ -30,7 +30,7 @@ static GraphNode * create(GraphNodeSpecification * spec, GraphNodeConfig * config) { (void) config; - GraphNode * node = calloc(1, sizeof(GraphNode)); + GraphNode * node = T_ALLOC(1, GraphNode); if (!node) { return node; } diff --git a/processing.c b/processing.c index 1138250..122f32f 100644 --- a/processing.c +++ b/processing.c @@ -35,8 +35,8 @@ io_subscription_list_init(IOSubscriptionList * lst, size_t capacity) .fds = NULL, .subscribers = NULL, }; - result.fds = calloc(capacity, sizeof(int)); - result.subscribers = calloc(capacity, sizeof(IOHandling*)); + result.fds = T_ALLOC(capacity, int); + result.subscribers = T_ALLOC(capacity, IOHandling*); if (!result.fds || !result.subscribers) capacity = 0; result.capacity = capacity;