From: Kevin Day Date: Mon, 14 Dec 2020 01:46:11 +0000 (-0600) Subject: Update: f_memory tweaks, always call memset() on new allocations. X-Git-Tag: 0.5.2~16 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=aec6fa9a4f7c683044e9098fdb085e8aa4c6119a;p=fll Update: f_memory tweaks, always call memset() on new allocations. This is already done with reallocations. Be consistent and also do this for allocations. The size parameters represent the size of the memory structure, which should always be non-zero and positive. The length parameter, on the other hand, is just the amount of time the structure is to be allocation, which should always be non-negative. The length parameter, therefore, can be 0 and if it is then do nothing (this relaxes the parameter restrictions). --- diff --git a/level_0/f_memory/c/memory.c b/level_0/f_memory/c/memory.c index 7552a35..deaac92 100644 --- a/level_0/f_memory/c/memory.c +++ b/level_0/f_memory/c/memory.c @@ -8,18 +8,23 @@ extern "C" { f_return_status f_memory_new(void **pointer, const f_memory_size_t size, const f_memory_length length) { #ifndef _di_level_0_parameter_checking_ if (size <= 0) return F_status_set_error(F_parameter); - if (length <= 0) return F_status_set_error(F_parameter); + if (length < 0) return F_status_set_error(F_parameter); if (!pointer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ // prevent double-allocations. - if (*pointer) return F_none; + if (*pointer || length == 0) return F_none; // Some people use malloc(size * length) to produce the same results. // This has been observed to sometimes causes an increase in L1/L2 cache misses (0.02% L1 increase, 0.01% L2 increase). *pointer = calloc(size, length); if (*pointer) { + + // 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(((uint8_t *) *pointer), 0, size * length); + return F_none; } @@ -48,13 +53,13 @@ extern "C" { #if !(defined(_di_f_memory_destroy_) || defined(_f_memory_FORCE_fast_memory_)) f_return_status f_memory_destroy(void **pointer, const f_memory_size_t size, const f_memory_length length) { #ifndef _di_level_0_parameter_checking_ - if (length < 0) return F_status_set_error(F_parameter); + if (length < 0) return F_status_set_error(F_parameter); if (size <= 0) return F_status_set_error(F_parameter); if (!pointer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ // prevent double-frees. - if (!*pointer || !size || !length) return F_none; + if (!*pointer || !length) return F_none; if (length > 0) { memset(*pointer, 0, size * length); diff --git a/level_0/f_memory/c/memory.h b/level_0/f_memory/c/memory.h index 221b9f2..332842e 100644 --- a/level_0/f_memory/c/memory.h +++ b/level_0/f_memory/c/memory.h @@ -93,6 +93,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. * * @see calloc() + * @see memset() */ #ifndef _di_f_memory_new_ extern f_return_status f_memory_new(void **pointer, const f_memory_size_t size, const f_memory_length length);