From: Kevin Day Date: Fri, 9 Feb 2024 04:34:55 +0000 (-0600) Subject: Feature: Add _f_memory_USE_reallocarray_ and _f_memory_NO_zeroing_on_larger_ macros. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=5c16170c3e04b2b9879d26f380544a1cac21a58a;p=fll Feature: Add _f_memory_USE_reallocarray_ and _f_memory_NO_zeroing_on_larger_ macros. The _f_memory_USE_reallocarray_ designates to use reallocarray() rather than realloc() in the f_memory project. The _f_memory_NO_zeroing_on_larger_ designates to not perform the memset() to 0 for malloc() or realloc() calls where the new size is larger than the old size. The programs provided by this project might expect the memory to be zeroed. Please use _f_memory_NO_zeroing_on_larger_ carefully. --- diff --git a/level_0/f_memory/c/private-memory.c b/level_0/f_memory/c/private-memory.c index c25b2e7..039fda1 100644 --- a/level_0/f_memory/c/private-memory.c +++ b/level_0/f_memory/c/private-memory.c @@ -14,25 +14,33 @@ extern "C" { } if (*pointer) { - if (length_old) { - if (length_new < length_old) { + #ifndef _f_memory_NO_zeroing_on_larger_ + if (length_old) { + if (length_new < length_old) { - // uint8_t * is of a data size size of 1, casting it to uint8_t should result in a single-length increment. - // This is done to avoid problems with (void *) having arithmetic issues. - memset((void *) (((uint8_t *) *pointer) + length_new), 0, type_size * (length_old - length_new)); + // uint8_t * is of a data size size of 1, casting it to uint8_t should result in a single-length increment. + // This is done to avoid problems with (void *) having arithmetic issues. + memset((void *) (((uint8_t *) *pointer) + length_new), 0, type_size * (length_old - length_new)); + } } - } + #endif // _f_memory_NO_zeroing_on_larger_ if (length_new) { - void * const new_pointer = realloc(*pointer, type_size * length_new); + #ifdef _f_memory_USE_reallocarray_ + void * const new_pointer = reallocarray(*pointer, length_new, type_size); + #else + void * const new_pointer = realloc(*pointer, type_size * length_new); + #endif // _f_memory_USE_reallocarray_ if (new_pointer) { - if (length_new > length_old) { + #ifndef _f_memory_NO_zeroing_on_larger_ + if (length_new > length_old) { - // uint8_t * is of a data size size of 1, casting it to bool should result in a single-length increment. - // This is done to avoid problems with (void *) having arithmetic issues. - memset((void *) (((uint8_t *) new_pointer) + (type_size * length_old)), 0, type_size * (length_new - length_old)); - } + // uint8_t * is of a data size size of 1, casting it to bool should result in a single-length increment. + // This is done to avoid problems with (void *) having arithmetic issues. + memset((void *) (((uint8_t *) new_pointer) + (type_size * length_old)), 0, type_size * (length_new - length_old)); + } + #endif // _f_memory_NO_zeroing_on_larger_ *pointer = new_pointer; @@ -70,15 +78,21 @@ extern "C" { if (*pointer) { if (length_new) { - void * const new_pointer = realloc(*pointer, type_size * length_new); + #ifdef _f_memory_USE_reallocarray_ + void * const new_pointer = reallocarray(*pointer, length_new, type_size); + #else + void * const new_pointer = realloc(*pointer, type_size * length_new); + #endif // _f_memory_USE_reallocarray_ if (new_pointer) { - if (length_new > length_old) { - - // uint8_t * is of a data size size of 1, casting it to bool should result in a single-length increment. - // This is done to avoid problems with (void *) having arithmetic issues. - memset((void *) (((uint8_t *) new_pointer) + (type_size * length_old)), 0, type_size * (length_new - length_old)); - } + #ifndef _f_memory_NO_zeroing_on_larger_ + if (length_new > length_old) { + + // uint8_t * is of a data size size of 1, casting it to bool should result in a single-length increment. + // This is done to avoid problems with (void *) having arithmetic issues. + memset((void *) (((uint8_t *) new_pointer) + (type_size * length_old)), 0, type_size * (length_new - length_old)); + } + #endif // _f_memory_NO_zeroing_on_larger_ *pointer = new_pointer; diff --git a/level_0/f_memory/data/build/defines b/level_0/f_memory/data/build/defines index 2c64298..8428f9d 100644 --- a/level_0/f_memory/data/build/defines +++ b/level_0/f_memory/data/build/defines @@ -1,4 +1,6 @@ # fss-0000 -_f_memory_FORCE_secure_memory_ all to-be removed address spaces are set to 0 before freeing or resizing. -_f_memory_FORCE_fast_memory_ all to-be removed address spaces remain unchanged before freeing or resizing. -_f_memory_USE_posix_memalign_ use POSIX memalign instead of aligned_alloc() (aligned_alloc() is in C as of C11 standard). +_f_memory_FORCE_secure_memory_ All to-be removed address spaces are set to 0 before freeing or resizing. +_f_memory_FORCE_fast_memory_ All to-be removed address spaces remain unchanged before freeing or resizing. +_f_memory_USE_posix_memalign_ Use POSIX memalign instead of aligned_alloc() (aligned_alloc() is in C as of C11 standard). +_f_memory_USE_reallocarray_ Use reallocarray() instead of realloc(). +_f_memory_NO_zeroing_on_larger_ Do not call memset() to zero out memory when the new size is larger for the newly added memory.