From 8317c3b33b9b1c70e5ae4c87c7efe33d9b9ce09a Mon Sep 17 00:00:00 2001 From: Mosder Date: Thu, 23 Jul 2026 17:35:04 +0200 Subject: feat: added helper functions for dealing with memory management and refactored the codebase to use them --- analyzer/utils/memory.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 analyzer/utils/memory.c (limited to 'analyzer/utils/memory.c') diff --git a/analyzer/utils/memory.c b/analyzer/utils/memory.c new file mode 100644 index 0000000..0a43bd3 --- /dev/null +++ b/analyzer/utils/memory.c @@ -0,0 +1,62 @@ +#include "memory.h" + +#include +#include +#include +#include + +void *safe_malloc(size_t size) { + for (size_t i = 0; i < MAX_ALLOC_ATTEMPTS; i++) { + void *p = malloc(size); + if (p) + return p; + if (i < MAX_ALLOC_ATTEMPTS - 1) { + printf("Failed malloc. Retrying in 1s...\n"); + sleep(1); + } + } + + printf("ERROR: Failed malloc %d times. Exiting...", MAX_ALLOC_ATTEMPTS); + exit(EXIT_FAILURE); +} + +void *safe_realloc(void *ptr, size_t size) { + if (!ptr) + return safe_malloc(size); + + for (size_t i = 0; i < MAX_ALLOC_ATTEMPTS; i++) { + void *p = realloc(ptr, size); + if (p) + return p; + if (i < MAX_ALLOC_ATTEMPTS - 1) { + printf("Failed realloc. Retrying in 1s...\n"); + sleep(1); + } + } + + printf("ERROR: Failed realloc %d times. Exiting...", MAX_ALLOC_ATTEMPTS); + exit(EXIT_FAILURE); +} + +// expand an array +// returns pointer to the expanded array +void *array_expand(void *arr, size_t el_size, size_t *cap, size_t to_fit) { + while (*cap < to_fit) + *cap *= 2; + return safe_realloc(arr, *cap * el_size); +} + +void *array_append(void *arr, size_t el_size, size_t *len, size_t *cap, void *val) { + if (*len >= *cap) + arr = array_expand(arr, el_size, cap, *len + 1); + memcpy((char *)arr + (*len)++ * el_size, val, el_size); + return arr; +} + +void *array_concat(void *arr1, size_t el_size, size_t *len1, size_t *cap, void *arr2, size_t len2) { + if (*len1 + len2 > *cap) + arr1 = array_expand(arr1, el_size, cap, *len1 + len2); + memcpy((char *)arr1 + *len1 * el_size, arr2, len2 * el_size); + *len1 += len2; + return arr1; +} -- cgit v1.2.3 From 4add8c9c34c48f01de58b3b41e79a768063f7acf Mon Sep 17 00:00:00 2001 From: Mosder Date: Thu, 23 Jul 2026 20:04:32 +0200 Subject: feat: redirected runtime errors to stderr --- analyzer/analyzer.c | 2 +- analyzer/utils/memory.c | 8 ++++---- csv_table_gen.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'analyzer/utils/memory.c') diff --git a/analyzer/analyzer.c b/analyzer/analyzer.c index bd24613..aa4a645 100644 --- a/analyzer/analyzer.c +++ b/analyzer/analyzer.c @@ -59,7 +59,7 @@ void analyze_file(char *path) { // handle errors if (handle == NULL) { - printf("Couldn't open the specified file:\n%s\n", errbuf); + fprintf(stderr, "Couldn't open the specified file:\n%s\n", errbuf); return; } diff --git a/analyzer/utils/memory.c b/analyzer/utils/memory.c index 0a43bd3..91c5f18 100644 --- a/analyzer/utils/memory.c +++ b/analyzer/utils/memory.c @@ -11,12 +11,12 @@ void *safe_malloc(size_t size) { if (p) return p; if (i < MAX_ALLOC_ATTEMPTS - 1) { - printf("Failed malloc. Retrying in 1s...\n"); + fprintf(stderr, "Failed malloc. Retrying in 1s...\n"); sleep(1); } } - printf("ERROR: Failed malloc %d times. Exiting...", MAX_ALLOC_ATTEMPTS); + fprintf(stderr, "FATAL: Failed malloc %d times. Exiting...", MAX_ALLOC_ATTEMPTS); exit(EXIT_FAILURE); } @@ -29,12 +29,12 @@ void *safe_realloc(void *ptr, size_t size) { if (p) return p; if (i < MAX_ALLOC_ATTEMPTS - 1) { - printf("Failed realloc. Retrying in 1s...\n"); + fprintf(stderr, "Failed realloc. Retrying in 1s...\n"); sleep(1); } } - printf("ERROR: Failed realloc %d times. Exiting...", MAX_ALLOC_ATTEMPTS); + fprintf(stderr, "FATAL: Failed realloc %d times. Exiting...", MAX_ALLOC_ATTEMPTS); exit(EXIT_FAILURE); } diff --git a/csv_table_gen.c b/csv_table_gen.c index 0bfd51a..ae80b78 100644 --- a/csv_table_gen.c +++ b/csv_table_gen.c @@ -80,7 +80,7 @@ void create_c(char *path, char *h_path, char *from, char *to, char from_to_array fprintf(fp, "const char *get_%s_from_%s(int %s) {\n", to, from, from); fprintf(fp, " if (%s >= 0 && %s < %s)\n", from, from, arr_len_name); fprintf(fp, " return %s[%s];\n", arr_name, from); - fprintf(fp, " printf(\"%s = %%d is outside of table range [0; %%d]\\n\", %s, %s - 1);\n", from, from, arr_len_name); + fprintf(fp, " fprintf(stderr, \"%s = %%d is outside of table range [0; %%d]\\n\", %s, %s - 1);\n", from, from, arr_len_name); fprintf(fp, " return \"null\";\n"); fprintf(fp, "}"); -- cgit v1.2.3