From 7fb1907595f79a78fa402f2cb6749e126142da72 Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Thu, 6 Nov 2025 11:56:54 -0500 Subject: [PATCH] Normalize memory allocation in C code to use ALLOC/REALLOC/FREE macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Convert direct malloc() calls to ALLOC() macro in cstringstream.c - Convert direct realloc() calls to REALLOC() macro in cstringstream.c - Convert direct free() calls to FREE() macro in cstringstream.c - Add util.h include to cstringstream.c for macro availability - Ensures consistent memory management and error handling across codebase - All existing tests continue to pass Changes: - 3 malloc() → ALLOC() conversions - 1 realloc() → REALLOC() conversion - 2 free() → FREE() conversions This change standardizes memory allocation to use the centralized memory management macros, providing consistent error handling through MMalloc/MMrealloc when USE_MM is not defined, and automatic pointer zeroing with the FREE() macro. Signed-off-by: Alan Jowett --- src/cstringstream.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cstringstream.c b/src/cstringstream.c index 9af649b3..eb03cd59 100644 --- a/src/cstringstream.c +++ b/src/cstringstream.c @@ -46,6 +46,7 @@ #include #include #include +#include "util.h" #include "cstringstream.h" /** @@ -59,13 +60,13 @@ struct _cstringstream { cstringstream newStringStream(void) { cstringstream ss; - ss = (cstringstream) malloc(sizeof(struct _cstringstream)); + ss = ALLOC(struct _cstringstream, 1); if (!ss) return NULL; ss->capacity = 1; /* parsimonious */ ss->inUse = 0; - ss->data = (char *) malloc(sizeof(char) * ss->capacity); + ss->data = ALLOC(char, ss->capacity); if (!ss->data) { - free(ss); + FREE(ss); return NULL; } return ss; @@ -73,8 +74,8 @@ cstringstream newStringStream(void) { void deleteStringStream(cstringstream ss) { if (ss) { - free(ss->data); - free(ss); + FREE(ss->data); + FREE(ss); } } @@ -104,7 +105,7 @@ int resizeStringStream(cstringstream ss, size_t newSize) { size_t newCapacity = 2 * ss->capacity; if (newCapacity < newSize) newCapacity = newSize; - char * tmp = (char *) realloc(ss->data, newCapacity * sizeof(char)); + char * tmp = REALLOC(char, ss->data, newCapacity); /* If the allocation fails, leave the array alone. */ if (!tmp) return -1; ss->data = tmp; @@ -187,7 +188,7 @@ int putStringStream(cstringstream ss, size_t index, char c) { char * stringFromStringStream(const_cstringstream ss) { if (!ss) return 0; - char * str = (char *) malloc(sizeof(char) * (ss->inUse + 1)); + char * str = ALLOC(char, ss->inUse + 1); if (!str) return 0; strncpy(str, ss->data, ss->inUse); str[ss->inUse] = '\0';