From: Kevin Day Date: Sun, 12 Dec 2021 05:50:23 +0000 (-0600) Subject: Update: Use C11's aligned_alloc() by default, but keep posix_memalign() via macro... X-Git-Tag: 0.5.7~43 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=de4615ea5e4ec8210ca6b7bfc4006ac173156f2e;p=fll Update: Use C11's aligned_alloc() by default, but keep posix_memalign() via macro _f_memory_USE_posix_memalign_. The C11 standard introduced aligned_alloc() making it better practice than posix_memalign(). In case the compiler being used doesn't have aligned_alloc() or the user compiling just wants to posix_memalign() this behavior is preserved via _f_memory_USE_posix_memalign_ macro. I didn't actually test this beyond confirming that it compiles. I'm flying blind here. --- diff --git a/level_0/f_memory/c/memory.c b/level_0/f_memory/c/memory.c index f195196..1d03bda 100644 --- a/level_0/f_memory/c/memory.c +++ b/level_0/f_memory/c/memory.c @@ -115,15 +115,29 @@ extern "C" { return F_data_not; } - const int result = posix_memalign(pointer, alignment, length); + #ifdef _f_memory_USE_posix_memalign_ + const int result = posix_memalign(pointer, alignment, length); - if (result) { - if (result == EINVAL) { - return F_status_set_error(F_parameter); + if (result) { + if (result == EINVAL) { + return F_status_set_error(F_parameter); + } + + return F_status_set_error(F_memory_not); } + #else + void *result = aligned_alloc(alignment, length); - return F_status_set_error(F_memory_not); - } + if (result) { + *pointer = result; + } + else if (errno == EINVAL) { + return F_status_set_error(F_parameter); + } + else { + return F_status_set_error(F_memory_not); + } + #endif // _f_memory_USE_posix_memalign_ // 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. diff --git a/level_0/f_memory/c/memory.h b/level_0/f_memory/c/memory.h index 9274b1e..4280c3c 100644 --- a/level_0/f_memory/c/memory.h +++ b/level_0/f_memory/c/memory.h @@ -6,10 +6,6 @@ * Licenses: lgpl-2.1-or-later * * Provide means to use memory routines, with error checking. - * - * @todo consider adding f_memory_scramble() and f_memory_juggle(). - * f_memory_scramble() is like f_destroy() but it writes random data instead of 0. - * f_memory_juggle() is like f_adjust() but it writes random data instead of 0. */ #ifndef _F_memory_h #define _F_memory_h @@ -163,6 +159,7 @@ extern "C" { * F_memory_not (with error bit) on allocation error. * F_parameter (with error bit) if a parameter is invalid. * + * @see aligned_alloc() * @see posix_memalign() * @see memset() */ diff --git a/level_0/f_memory/data/build/defines b/level_0/f_memory/data/build/defines index 13ab9ee..2c64298 100644 --- a/level_0/f_memory/data/build/defines +++ b/level_0/f_memory/data/build/defines @@ -1,3 +1,4 @@ # 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).