From 0d4174849c4336a3e87c25c66b4cea504a7aad12 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 31 May 2024 14:57:18 -0300 Subject: src/vector.c: Make the "void **values" `const` --- src/vector.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/vector.c b/src/vector.c index 358036b..6708546 100644 --- a/src/vector.c +++ b/src/vector.c @@ -18,7 +18,7 @@ struct Vector { - void **values; + const void **values; size_t capacity; size_t count; const size_t max_capacity; @@ -46,15 +46,8 @@ vector_new_with( ) { int rc = -1; - struct Vector v = { - .values = NULL, - .capacity = capacity, - .count = 0U, - .max_capacity = max_capacity, - .multiplier = multiplier, - .value_size = value_size, - }; - struct Vector *restrict ret = NULL; + const struct Vector *ret = NULL; + void *values = NULL; assert(capacity > 0); if (capacity > max_capacity) { @@ -68,8 +61,8 @@ vector_new_with( } const size_t array_bytes_size = capacity * value_size; - v.values = malloc(array_bytes_size); - if (v.values == NULL) { + values = malloc(array_bytes_size); + if (values == NULL) { logerr("malloc(): %s", strerror(errno)); goto out; } @@ -79,17 +72,26 @@ vector_new_with( logerr("malloc(): %s", strerror(errno)); goto out; } - memcpy(ret, &v, sizeof(v)); + memcpy((void *)ret, &(struct Vector) { + .values = values, + .capacity = capacity, + .count = 0U, + .max_capacity = max_capacity, + .multiplier = multiplier, + .value_size = value_size, + }, sizeof(*ret)); *out = ret; rc = 0; out: if (rc) { if (ret != NULL) { - free(ret); + free((void *)ret); + ret = NULL; } - if (v.values != NULL) { - free(v.values); + if (values != NULL) { + free((void *)values); + values = NULL; } } return rc; @@ -196,6 +198,7 @@ out: if (rc) { if (new_values != NULL) { free(new_values); + new_values = NULL; } } return rc; -- cgit v1.2.3