diff options
author | EuAndreh <eu@euandre.org> | 2024-05-31 15:22:47 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-05-31 15:22:47 -0300 |
commit | a4362fc06770f2ff86ef7226b0f0907b8063b5b2 (patch) | |
tree | 7ea89f69f069140faf5f2b39c8d67052b024f77a | |
parent | src/vector.h: Revert back to doing vector_free(&v) (diff) | |
download | pindaiba-a4362fc06770f2ff86ef7226b0f0907b8063b5b2.tar.gz pindaiba-a4362fc06770f2ff86ef7226b0f0907b8063b5b2.tar.xz |
Use freeit() everywhere
-rw-r--r-- | deps.mk | 20 | ||||
-rw-r--r-- | src/random.c | 3 | ||||
-rw-r--r-- | src/string.c | 12 | ||||
-rw-r--r-- | src/util.c | 17 | ||||
-rw-r--r-- | src/util.h | 6 | ||||
-rw-r--r-- | src/vector.c | 12 | ||||
-rw-r--r-- | tests/catalog.c | 11 | ||||
-rw-r--r-- | tests/logerr.c | 20 | ||||
-rw-r--r-- | tests/util.c | 70 |
9 files changed, 81 insertions, 90 deletions
@@ -76,30 +76,30 @@ src/i18n.o: src/catalog.h src/lib.o: src/logerr.h src/logerr.o: src/math.o: -src/random.o: src/logerr.h +src/random.o: src/logerr.h src/util.h src/string.o: src/logerr.h src/math.h src/util.h src/testing.o: src/util.o: src/logerr.h -src/vector.o: src/logerr.h src/catalog.h src/i18n.h src/math.h +src/vector.o: src/catalog.h src/i18n.h src/logerr.h src/math.h src/util.h -tests/catalog.o: src/logerr.h src/testing.h tests/slurp.h +tests/catalog.o: src/logerr.h src/testing.h src/util.h tests/slurp.h tests/i18n.o: src/catalog.h src/logerr.h tests/lib.o: src/logerr.h -tests/logerr.o: src/testing.h tests/slurp.h +tests/logerr.o: src/testing.h src/util.h tests/slurp.h tests/math.o: src/testing.h -tests/random.o: src/logerr.h src/testing.h +tests/random.o: src/logerr.h src/util.h src/testing.h tests/string.o: src/logerr.h src/math.h src/util.h src/testing.h tests/testing.o: tests/util.o: src/logerr.h src/testing.h tests/slurp.h -tests/vector.o: src/logerr.h src/catalog.h src/i18n.h src/math.h src/testing.h +tests/vector.o: src/catalog.h src/i18n.h src/logerr.h src/math.h src/util.h src/testing.h -tests/catalog.a: src/logerr.o src/testing.o tests/slurp.o +tests/catalog.a: src/logerr.o src/testing.o src/util.o tests/slurp.o tests/i18n.a: src/catalog.o src/logerr.o tests/lib.a: src/logerr.o -tests/logerr.a: src/testing.o tests/slurp.o +tests/logerr.a: src/testing.o src/util.o tests/slurp.o tests/math.a: src/testing.o -tests/random.a: src/logerr.o src/testing.o +tests/random.a: src/logerr.o src/util.o src/testing.o tests/string.a: src/logerr.o src/math.o src/util.o src/testing.o tests/testing.a: tests/util.a: src/logerr.o src/testing.o tests/slurp.o -tests/vector.a: src/logerr.o src/catalog.o src/i18n.o src/math.o src/testing.o +tests/vector.a: src/catalog.o src/i18n.o src/logerr.o src/math.o src/util.o src/testing.o diff --git a/src/random.c b/src/random.c index 2b41645..df2d3ff 100644 --- a/src/random.c +++ b/src/random.c @@ -8,6 +8,7 @@ #include <string.h> #include "logerr.h" +#include "util.h" #include "random.h" @@ -54,7 +55,7 @@ out: } } if (ret != NULL) { - free(ret); + freeit((void *)&ret); } return rc; } diff --git a/src/string.c b/src/string.c index dce00f3..5b06519 100644 --- a/src/string.c +++ b/src/string.c @@ -57,12 +57,10 @@ string_new_with( out: if (rc) { if (bytes != NULL) { - free((void *)bytes); - ret = NULL; + freeit((void *)&bytes); } if (ret != NULL) { - free((void *)ret); - ret = NULL; + freeit((void *)&ret); } } return rc; @@ -144,12 +142,10 @@ string_append( out: if (rc) { if (bytes != NULL) { - free((void *)bytes); - bytes = NULL; + freeit((void *)&bytes); } if (ret != NULL) { - free((void *)ret); - ret = NULL; + freeit((void *)&ret); } } return rc; @@ -10,12 +10,21 @@ #include "util.h" + const size_t NULL_TERMINATOR = sizeof((char)'\0'); const int EXIT_USAGE = 2; + + +void +freeit(const void **const ptr) { + free((void *)*ptr); + *ptr = NULL; +} + int slurp(const char *const filename, char **out) { int rc = -1; @@ -71,14 +80,8 @@ out: } if (rc) { if (str != NULL) { - free(str); + freeit((void *)&str); } } return rc; } - -void -freeit(const void **const ptr) { - free((void *)*ptr); - *ptr = NULL; -} @@ -4,8 +4,10 @@ NULL_TERMINATOR; extern const int EXIT_USAGE; -int -slurp(const char *const filename, char **out); + void freeit(const void **const ptr); + +int +slurp(const char *const filename, char **out); diff --git a/src/vector.c b/src/vector.c index b65ee2f..e5f5949 100644 --- a/src/vector.c +++ b/src/vector.c @@ -9,10 +9,11 @@ #include <stdio.h> #include <string.h> -#include "logerr.h" #include "catalog.h" #include "i18n.h" +#include "logerr.h" #include "math.h" +#include "util.h" #include "vector.h" @@ -86,12 +87,10 @@ vector_new_with( out: if (rc) { if (ret != NULL) { - free((void *)ret); - ret = NULL; + freeit((void *)&ret); } if (values != NULL) { - free((void *)values); - values = NULL; + freeit((void *)&values); } } return rc; @@ -198,8 +197,7 @@ vector_push_back(const struct Vector *const v, const void *const value) { out: if (rc) { if (new_values != NULL) { - free(new_values); - new_values = NULL; + freeit((void *)&new_values); } } return rc; diff --git a/tests/catalog.c b/tests/catalog.c index fbccfe5..7192135 100644 --- a/tests/catalog.c +++ b/tests/catalog.c @@ -1,6 +1,7 @@ #include "../src/catalog.c" #include "../src/testing.h" +#include "../src/util.h" #include "slurp.h" @@ -103,9 +104,7 @@ test_s_print_msgs(void) { assert(strcmp(expected, str) == 0); - free(str); - str = NULL; - + freeit((void *)&str); test_ok(); } { @@ -139,8 +138,7 @@ test_s_print_msgs(void) { assert(strcmp(expected, str) == 0); - free(str); - str = NULL; + freeit((void *)&str); test_ok(); } @@ -258,8 +256,7 @@ test_s_print_msg(void) { assert(strcmp(expected, str) == 0); - free(str); - str = NULL; + freeit((void *)&str); test_ok(); } diff --git a/tests/logerr.c b/tests/logerr.c index 3dd700e..806f6d0 100644 --- a/tests/logerr.c +++ b/tests/logerr.c @@ -5,6 +5,7 @@ #include <string.h> #include "../src/testing.h" +#include "../src/util.h" #include "slurp.h" @@ -48,8 +49,7 @@ test_vlogerr(void) { "tests/logerr.c:test_vlogerr:100: \n"; assert(strcmp(expected, str) == 0); - free(str); - str = NULL; + freeit((void *)&str); test_ok(); } @@ -82,8 +82,7 @@ test_vlogerr(void) { "tests/logerr.c:test_vlogerr:200: \n\n"; assert(strcmp(expected, str) == 0); - free(str); - str = NULL; + freeit((void *)&str); test_ok(); } @@ -116,8 +115,7 @@ test_vlogerr(void) { "tests/logerr.c:test_vlogerr:300: some static string\n"; assert(strcmp(expected, str) == 0); - free(str); - str = NULL; + freeit((void *)&str); test_ok(); } @@ -150,8 +148,7 @@ test_vlogerr(void) { "tests/logerr.c:test_vlogerr:400: fn(an-arg)\n"; assert(strcmp(expected, str) == 0); - free(str); - str = NULL; + freeit((void *)&str); test_ok(); } @@ -188,16 +185,15 @@ test_vlogerr(void) { "int (123), string (another-str) and char (z)\n"; assert(strcmp(expected, str) == 0); - free(str); - str = NULL; + freeit((void *)&str); test_ok(); } rc = 0; out: - if (str) { - free(str); + if (str != NULL) { + freeit((void *)&str); } if (file) { if (fclose(file)) { diff --git a/tests/util.c b/tests/util.c index bda7606..7b48568 100644 --- a/tests/util.c +++ b/tests/util.c @@ -19,6 +19,38 @@ test_EXIT_USAGE(void) { } static int +test_freeit(void) { + int rc = -1; + + test_start("freeit()"); + + const char *ptr = NULL; + + { + testing("ptr is NULL afterwards"); + + ptr = malloc(1234U); + if (ptr == NULL) { + logerr("malloc(): %s", strerror(errno)); + goto out; + } + + assert(ptr != NULL); + freeit((void *)&ptr); + assert(ptr == NULL); + + test_ok(); + } + + rc = 0; +out: + if (ptr != NULL) { + freeit((void *)&ptr); + } + return rc; +} + +static int test_slurp(void) { int rc = -1; @@ -55,10 +87,8 @@ test_slurp(void) { assert(expected != NULL); assert(strcmp(given, expected) == 0); - free(given); - given = NULL; - free(expected); - expected = NULL; + freeit((void *)&given); + freeit((void *)&expected); test_ok(); } @@ -74,38 +104,6 @@ out: return rc; } -static int -test_freeit(void) { - int rc = -1; - - test_start("freeit()"); - - const char *ptr = NULL; - - { - testing("ptr is NULL afterwards"); - - ptr = malloc(1234U); - if (ptr == NULL) { - logerr("malloc(): %s", strerror(errno)); - goto out; - } - - assert(ptr != NULL); - freeit((void *)&ptr); - assert(ptr == NULL); - - test_ok(); - } - - rc = 0; -out: - if (ptr != NULL) { - freeit((void *)&ptr); - } - return rc; -} - int main(void) { |