From 046cc478e58756ec413059e893b7439ec2587ba5 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 1 Jan 2021 19:15:06 -0600 Subject: [PATCH] Update: fix length calculation (cannot be 0), add f_memory_new_aligned(), and other changes. The length calculation is documented to not be allowed to be 0. Make sure the parameter checkers assure this behavior. Provide f_memory_new_aligned() as a wrapper to posix_memalign(). The calloc() function actually accepts the length arguments as the first parameter. Remove memory times and just stick with size_t. --- level_0/f_memory/c/memory.c | 41 ++++++++++++++++++++++++++++++++++------- level_0/f_memory/c/memory.h | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/level_0/f_memory/c/memory.c b/level_0/f_memory/c/memory.c index deaac92..ccd5915 100644 --- a/level_0/f_memory/c/memory.c +++ b/level_0/f_memory/c/memory.c @@ -5,10 +5,10 @@ extern "C" { #endif #ifndef _di_f_memory_new_ - f_return_status f_memory_new(void **pointer, const f_memory_size_t size, const f_memory_length length) { + f_return_status f_memory_new(void **pointer, const size_t size, const size_t 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_ @@ -17,7 +17,7 @@ extern "C" { // 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); + *pointer = calloc(length, size); if (*pointer) { @@ -32,8 +32,35 @@ extern "C" { } #endif // _di_f_memory_new_ +#ifndef _di_f_memory_new_aligned_ + f_return_status f_memory_new_aligned(void **pointer, const size_t alignment, const size_t length) { + #ifndef _di_level_0_parameter_checking_ + if (alignment <= 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 || length == 0) return F_none; + + const int result = posix_memalign(pointer, alignment, length); + + if (result) { + if (result == EINVAL) return F_status_set_error(F_parameter); + + return F_status_set_error(F_memory_allocation); + } + + // 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, length); + + return F_none; + } +#endif // _di_f_memory_new_aligned_ + #if !(defined(_di_f_memory_delete_) || defined(_f_memory_FORCE_secure_memory_)) - f_return_status f_memory_delete(void **pointer, const f_memory_size_t size, const f_memory_length length) { + f_return_status f_memory_delete(void **pointer, const size_t size, const size_t length) { #ifndef _di_level_0_parameter_checking_ if (!pointer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ @@ -51,7 +78,7 @@ extern "C" { #endif // !(defined(_di_f_memory_delete_) || defined(_f_memory_FORCE_secure_memory_)) #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) { + f_return_status f_memory_destroy(void **pointer, const size_t size, const size_t length) { #ifndef _di_level_0_parameter_checking_ if (length < 0) return F_status_set_error(F_parameter); if (size <= 0) return F_status_set_error(F_parameter); @@ -75,7 +102,7 @@ extern "C" { #endif // !(defined(_di_f_memory_destroy_) || defined(_f_memory_FORCE_fast_memory_)) #if !(defined(_di_f_memory_resize_) || defined(_f_memory_FORCE_secure_memory_)) - f_return_status f_memory_resize(void **pointer, const f_memory_size_t size, const f_memory_length old_length, const f_memory_length new_length) { + f_return_status f_memory_resize(void **pointer, const size_t size, const size_t old_length, const size_t new_length) { #ifndef _di_level_0_parameter_checking_ if (size <= 0) return F_status_set_error(F_parameter); if (old_length < 0) return F_status_set_error(F_parameter); @@ -132,7 +159,7 @@ extern "C" { #endif // !(defined(_di_f_memory_resize_) || defined(_f_memory_FORCE_secure_memory_)) #if !(defined(_di_f_memory_adjust_) || defined(_f_memory_FORCE_fast_memory_)) - f_return_status f_memory_adjust(void **pointer, const f_memory_size_t size, const f_memory_length old_length, const f_memory_length new_length) { + f_return_status f_memory_adjust(void **pointer, const size_t size, const size_t old_length, const size_t new_length) { #ifndef _di_level_0_parameter_checking_ if (size <= 0) return F_status_set_error(F_parameter); if (old_length < 0) return F_status_set_error(F_parameter); diff --git a/level_0/f_memory/c/memory.h b/level_0/f_memory/c/memory.h index 332842e..37f95b9 100644 --- a/level_0/f_memory/c/memory.h +++ b/level_0/f_memory/c/memory.h @@ -68,14 +68,6 @@ extern "C" { #endif // _di_f_memory_default_allocation_step_ /** - * Memory types. - */ -#ifndef _di_f_memory_types_ - #define f_memory_size_t size_t - #define f_memory_length size_t -#endif // _di_f_memory_types_ - -/** * Create some dynamically allocated array of some length. * * @param pointer @@ -96,10 +88,35 @@ extern "C" { * @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); + extern f_return_status f_memory_new(void **pointer, const size_t size, const size_t length); #endif // _di_f_memory_new_ /** + * Create some dynamically allocated array of some length, guaranteeing aligned memory. + * + * @param pointer + * A pointer that will be updated to the address of the newly allocated memory. + * @param alignment + * The size of the alignment, such as sizeof(void *). + * Must be greater than 0. + * @param length + * The total number of blocks to be allocated. + * Must be greater than 0. + * Must be a multiple of alignment. + * + * @return + * F_none on success. + * F_memory_allocation (with error bit) on allocation error. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see posix_memalign() + * @see memset() + */ +#ifndef _di_f_memory_new_aligned_ + extern f_return_status f_memory_new_aligned(void **pointer, const size_t alignment, const size_t length); +#endif // _di_f_memory_new_aligned_ + +/** * Delete dynamically allocated data. * * Will not change any of the data to 0 prior to deallocation. @@ -122,7 +139,7 @@ extern "C" { * @see free() */ #if !(defined(_di_f_memory_delete_) || defined(_f_memory_FORCE_secure_memory_)) - extern f_return_status f_memory_delete(void **pointer, const f_memory_size_t size, const f_memory_length length); + extern f_return_status f_memory_delete(void **pointer, const size_t size, const size_t length); #endif // !(defined(_di_f_memory_delete_) || defined(_f_memory_FORCE_secure_memory_)) /** @@ -147,7 +164,7 @@ extern "C" { * @see memset() */ #if !(defined(_di_f_memory_destroy_) || defined(_f_memory_FORCE_fast_memory_)) - extern f_return_status f_memory_destroy(void **pointer, const f_memory_size_t size, const f_memory_length length); + extern f_return_status f_memory_destroy(void **pointer, const size_t size, const size_t length); #endif // !(defined(_di_f_memory_destroy_) || defined(_f_memory_FORCE_fast_memory_)) /** @@ -174,7 +191,7 @@ extern "C" { * @see realloc() */ #if !(defined(_di_f_memory_resize_) || defined(_f_memory_FORCE_secure_memory_)) - extern f_return_status f_memory_resize(void **pointer, const f_memory_size_t size, const f_memory_length old_length, const f_memory_length new_length); + extern f_return_status f_memory_resize(void **pointer, const size_t size, const size_t old_length, const size_t new_length); #endif // !(defined(_di_f_memory_resize_) || defined(_f_memory_FORCE_secure_memory_)) /** @@ -201,7 +218,7 @@ extern "C" { * @see realloc() */ #if !(defined(_di_f_memory_adjust_) || defined(_f_memory_FORCE_fast_memory_)) - extern f_return_status f_memory_adjust(void **pointer, const f_memory_size_t size, const f_memory_length old_length, const f_memory_length new_length); + extern f_return_status f_memory_adjust(void **pointer, const size_t size, const size_t old_length, const size_t new_length); #endif // _di_f_memory_adjust_ #ifdef __cplusplus -- 1.8.3.1