Typesafe wrapper around calloc

This commit is contained in:
Vftdan 2024-08-15 16:53:02 +02:00
parent a0825d1d58
commit 63dabd6fcd
9 changed files with 13 additions and 12 deletions

View File

@ -35,7 +35,7 @@ load_nodes_section(const config_setting_t *config_section)
if (length <= 0) { if (length <= 0) {
return result; return result;
} }
result.items = calloc(length, sizeof(GraphNodeConfig)); result.items = T_ALLOC(length, GraphNodeConfig);
if (!result.items) { if (!result.items) {
return result; return result;
} }
@ -112,7 +112,7 @@ load_channels_section(const config_setting_t *config_section)
if (length <= 0) { if (length <= 0) {
return result; return result;
} }
result.items = calloc(length, sizeof(GraphChannelConfig)); result.items = T_ALLOC(length, GraphChannelConfig);
if (!result.items) { if (!result.items) {
return result; return result;
} }

3
defs.h
View File

@ -6,11 +6,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#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))) #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 // 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 // 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 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) #define DEBUG_PRINT_VALUE(x, fmt) fprintf(stderr, #x " = " fmt "\n", x); fflush(stderr)

View File

@ -32,7 +32,7 @@ event_replicate(EventNode * source, size_t count)
EventNode * EventNode *
event_create(const EventData * content) event_create(const EventData * content)
{ {
EventNode * event = calloc(1, sizeof(EventNode)); EventNode * event = T_ALLOC(1, EventNode);
if (content) { if (content) {
event->data = *content; event->data = *content;
event->data.modifiers = modifier_set_copy(content->modifiers); event->data.modifiers = modifier_set_copy(content->modifiers);

View File

@ -23,7 +23,7 @@ event_filter_list_extend(EventFilterList * lst)
if (lst->values) { if (lst->values) {
new_values = reallocarray(lst->values, capacity, sizeof(EventFilter)); new_values = reallocarray(lst->values, capacity, sizeof(EventFilter));
} else { } else {
new_values = calloc(capacity, sizeof(EventFilter)); new_values = T_ALLOC(capacity, EventFilter);
} }
if (!new_values) { if (!new_values) {
return false; return false;

4
main.c
View File

@ -32,7 +32,7 @@ main(int argc, char ** argv)
exit(1); 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) { for (size_t i = 0; i < loaded_config.nodes.length; ++i) {
const char* type_name = loaded_config.nodes.items[i].type; const char* type_name = loaded_config.nodes.items[i].type;
if (!type_name) { 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) { for (size_t i = 0; i < loaded_config.channels.length; ++i) {
const char *node_names[2]; const char *node_names[2];
GraphNode *end_nodes[2] = {NULL, NULL}; GraphNode *end_nodes[2] = {NULL, NULL};

View File

@ -70,7 +70,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output)
static GraphNode * static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config) create(GraphNodeSpecification * spec, GraphNodeConfig * config)
{ {
EvdevGraphNode * node = calloc(1, sizeof(EvdevGraphNode)); EvdevGraphNode * node = T_ALLOC(1, EvdevGraphNode);
if (!node) { if (!node) {
return NULL; return NULL;
} }

View File

@ -50,7 +50,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output)
static GraphNode * static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config) create(GraphNodeSpecification * spec, GraphNodeConfig * config)
{ {
GetcharGraphNode * node = calloc(1, sizeof(GetcharGraphNode)); GetcharGraphNode * node = T_ALLOC(1, GetcharGraphNode);
if (!node) { if (!node) {
return NULL; return NULL;
} }

View File

@ -30,7 +30,7 @@ static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config) create(GraphNodeSpecification * spec, GraphNodeConfig * config)
{ {
(void) config; (void) config;
GraphNode * node = calloc(1, sizeof(GraphNode)); GraphNode * node = T_ALLOC(1, GraphNode);
if (!node) { if (!node) {
return node; return node;
} }

View File

@ -35,8 +35,8 @@ io_subscription_list_init(IOSubscriptionList * lst, size_t capacity)
.fds = NULL, .fds = NULL,
.subscribers = NULL, .subscribers = NULL,
}; };
result.fds = calloc(capacity, sizeof(int)); result.fds = T_ALLOC(capacity, int);
result.subscribers = calloc(capacity, sizeof(IOHandling*)); result.subscribers = T_ALLOC(capacity, IOHandling*);
if (!result.fds || !result.subscribers) if (!result.fds || !result.subscribers)
capacity = 0; capacity = 0;
result.capacity = capacity; result.capacity = capacity;