From: Kevin Day Date: Fri, 9 Feb 2024 03:55:08 +0000 (-0600) Subject: Update: Add restrict optimization to f_memory functions. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=f351ee5711680eda0ab5e8a005e12702af400a47;p=fll Update: Add restrict optimization to f_memory functions. The design of the memory operations require that array, used, and size do not point to the same memory address. Help prevent potential aliasing by adding restrict keyword. The functions with only a single pointer in its parameters still has the restrict keyword added. I opted to avoid adding the restrict keyword to some of the append and append all functions. Those would require more attention before I am certain whether or not they should be allowed to have overlapping addresses. I think they should not, but just in case, I put doing that on hold. The callback could also use restrict but I have decided not to do that as well due to the number of files that I will need to change. I will do this later. --- diff --git a/level_0/f_memory/c/memory.c b/level_0/f_memory/c/memory.c index 3f47cf3..377e1c6 100644 --- a/level_0/f_memory/c/memory.c +++ b/level_0/f_memory/c/memory.c @@ -6,7 +6,7 @@ extern "C" { #endif #ifndef _di_f_memory_adjust_ - f_status_t f_memory_adjust(const size_t length_old, const size_t length_new, const size_t size, void ** const pointer) { + f_status_t f_memory_adjust(const size_t length_old, const size_t length_new, const size_t size, void ** restrict const pointer) { #ifndef _di_level_0_parameter_checking_ if (!size) return F_status_set_error(F_parameter); if (!pointer) return F_status_set_error(F_parameter); @@ -24,7 +24,7 @@ extern "C" { #endif // _di_f_memory_adjust_ #ifndef _di_f_memory_delete_ - f_status_t f_memory_delete(const size_t length, const size_t size, void ** const pointer) { + f_status_t f_memory_delete(const size_t length, const size_t size, void ** restrict const pointer) { #ifndef _di_level_0_parameter_checking_ if (!size) return F_status_set_error(F_parameter); if (!pointer) return F_status_set_error(F_parameter); @@ -49,7 +49,7 @@ extern "C" { #endif // _di_f_memory_delete_ #ifndef _di_f_memory_destroy_ - f_status_t f_memory_destroy(const size_t length, const size_t size, void ** const pointer) { + f_status_t f_memory_destroy(const size_t length, const size_t size, void ** restrict const pointer) { #ifndef _di_level_0_parameter_checking_ if (!size) return F_status_set_error(F_parameter); if (!pointer) return F_status_set_error(F_parameter); @@ -74,7 +74,7 @@ extern "C" { #endif // _di_f_memory_destroy_ #ifndef _di_f_memory_new_ - f_status_t f_memory_new(const size_t length, const size_t size, void ** const pointer) { + f_status_t f_memory_new(const size_t length, const size_t size, void ** restrict const pointer) { #ifndef _di_level_0_parameter_checking_ if (!size) return F_status_set_error(F_parameter); if (!pointer) return F_status_set_error(F_parameter); @@ -91,7 +91,7 @@ extern "C" { #ifndef _di_f_memory_new_aligned_ - f_status_t f_memory_new_aligned(const size_t length, const size_t alignment, void ** const pointer) { + f_status_t f_memory_new_aligned(const size_t length, const size_t alignment, void ** restrict const pointer) { #ifndef _di_level_0_parameter_checking_ if (!alignment) return F_status_set_error(F_parameter); if (!pointer) return F_status_set_error(F_parameter); @@ -129,7 +129,7 @@ extern "C" { #endif // _di_f_memory_new_aligned_ #ifndef _di_f_memory_resize_ - f_status_t f_memory_resize(const size_t length_old, const size_t length_new, const size_t size, void ** const pointer) { + f_status_t f_memory_resize(const size_t length_old, const size_t length_new, const size_t size, void ** restrict const pointer) { #ifndef _di_level_0_parameter_checking_ if (!size) return F_status_set_error(F_parameter); if (!pointer) 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 b58f1c9..4a2dffc 100644 --- a/level_0/f_memory/c/memory.h +++ b/level_0/f_memory/c/memory.h @@ -56,7 +56,7 @@ extern "C" { * @see realloc() */ #ifndef _di_f_memory_adjust_ - extern f_status_t f_memory_adjust(const size_t length_old, const size_t length_new, const size_t size, void ** const pointer); + extern f_status_t f_memory_adjust(const size_t length_old, const size_t length_new, const size_t size, void ** restrict const pointer); #endif // _di_f_memory_adjust_ /** @@ -84,7 +84,7 @@ extern "C" { * @see free() */ #ifndef _di_f_memory_delete_ - extern f_status_t f_memory_delete(const size_t length, const size_t size, void ** const pointer); + extern f_status_t f_memory_delete(const size_t length, const size_t size, void ** restrict const pointer); #endif // _di_f_memory_delete_ /** @@ -111,7 +111,7 @@ extern "C" { * @see memset() */ #ifndef _di_f_memory_destroy_ - extern f_status_t f_memory_destroy(const size_t length, const size_t size, void ** const pointer); + extern f_status_t f_memory_destroy(const size_t length, const size_t size, void ** restrict const pointer); #endif // _di_f_memory_destroy_ /** @@ -137,7 +137,7 @@ extern "C" { * @see memset() */ #ifndef _di_f_memory_new_ - extern f_status_t f_memory_new(const size_t length, const size_t size, void ** const pointer); + extern f_status_t f_memory_new(const size_t length, const size_t size, void ** restrict const pointer); #endif // _di_f_memory_new_ /** @@ -165,7 +165,7 @@ extern "C" { * @see memset() */ #ifndef _di_f_memory_new_aligned_ - extern f_status_t f_memory_new_aligned(const size_t length, const size_t alignment, void ** const pointer); + extern f_status_t f_memory_new_aligned(const size_t length, const size_t alignment, void ** restrict const pointer); #endif // _di_f_memory_new_aligned_ /** @@ -196,7 +196,7 @@ extern "C" { * @see realloc() */ #ifndef _di_f_memory_resize_ - extern f_status_t f_memory_resize(const size_t length_old, const size_t length_new, const size_t size, void ** const pointer); + extern f_status_t f_memory_resize(const size_t length_old, const size_t length_new, const size_t size, void ** restrict const pointer); #endif // _di_f_memory_resize_ #ifdef __cplusplus diff --git a/level_0/f_memory/c/memory/array.c b/level_0/f_memory/c/memory/array.c index cb7b471..ad1b561 100644 --- a/level_0/f_memory/c/memory/array.c +++ b/level_0/f_memory/c/memory/array.c @@ -6,7 +6,7 @@ extern "C" { #endif #ifndef _di_f_memory_array_adjust_ - f_status_t f_memory_array_adjust(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t f_memory_array_adjust(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { #ifndef _di_level_0_parameter_checking_ if (!width) return F_status_set_error(F_parameter); if (!array) return F_status_set_error(F_parameter); @@ -21,7 +21,7 @@ extern "C" { #endif // _di_f_memory_array_adjust_ #ifndef _di_f_memory_array_append_ - f_status_t f_memory_array_append(const void * const source, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t f_memory_array_append(const void * const source, const size_t width, void ** const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { #ifndef _di_level_0_parameter_checking_ if (!source) return F_status_set_error(F_parameter); if (!width) return F_status_set_error(F_parameter); @@ -54,7 +54,7 @@ extern "C" { #endif // _di_f_memory_array_append_ #ifndef _di_f_memory_array_append_all_ - f_status_t f_memory_array_append_all(const void * const sources, const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t f_memory_array_append_all(const void * const sources, const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { #ifndef _di_level_0_parameter_checking_ if (!sources) return F_status_set_error(F_parameter); if (!width) return F_status_set_error(F_parameter); @@ -88,7 +88,7 @@ extern "C" { #endif // _di_f_memory_array_append_all_ #ifndef _di_f_memory_array_decimate_by_ - f_status_t f_memory_array_decimate_by(const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t f_memory_array_decimate_by(const f_number_unsigned_t amount, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { #ifndef _di_level_0_parameter_checking_ if (!width) return F_status_set_error(F_parameter); if (!array) return F_status_set_error(F_parameter); @@ -103,7 +103,7 @@ extern "C" { #endif // _di_f_memory_array_decimate_by_ #ifndef _di_f_memory_array_decrease_by_ - f_status_t f_memory_array_decrease_by(const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t f_memory_array_decrease_by(const f_number_unsigned_t amount, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { #ifndef _di_level_0_parameter_checking_ if (!width) return F_status_set_error(F_parameter); if (!array) return F_status_set_error(F_parameter); @@ -118,7 +118,7 @@ extern "C" { #endif // _di_f_memory_array_decrease_by_ #ifndef _di_f_memory_array_increase_ - f_status_t f_memory_array_increase(const f_number_unsigned_t step, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t f_memory_array_increase(const f_number_unsigned_t step, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { #ifndef _di_level_0_parameter_checking_ if (!width) return F_status_set_error(F_parameter); if (!array) return F_status_set_error(F_parameter); @@ -145,7 +145,7 @@ extern "C" { #endif // _di_f_memory_array_increase_ #ifndef _di_f_memory_array_increase_by_ - f_status_t f_memory_array_increase_by(const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t f_memory_array_increase_by(const f_number_unsigned_t amount, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { #ifndef _di_level_0_parameter_checking_ if (!width) return F_status_set_error(F_parameter); if (!array) return F_status_set_error(F_parameter); @@ -170,7 +170,7 @@ extern "C" { #endif // _di_f_memory_array_increase_by_ #ifndef _di_f_memory_array_resize_ - f_status_t f_memory_array_resize(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t f_memory_array_resize(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { #ifndef _di_level_0_parameter_checking_ if (!width) return F_status_set_error(F_parameter); if (!array) return F_status_set_error(F_parameter); diff --git a/level_0/f_memory/c/memory/array.h b/level_0/f_memory/c/memory/array.h index 646e791..466e15e 100644 --- a/level_0/f_memory/c/memory/array.h +++ b/level_0/f_memory/c/memory/array.h @@ -43,7 +43,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. */ #ifndef _di_f_memory_array_adjust_ - extern f_status_t f_memory_array_adjust(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size); + extern f_status_t f_memory_array_adjust(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); #endif // _di_f_memory_array_adjust_ /** @@ -88,7 +88,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. */ #ifndef _di_f_memory_array_append_ - extern f_status_t f_memory_array_append(const void * const source, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size); + extern f_status_t f_memory_array_append(const void * const source, const size_t width, void ** const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); #endif // _di_f_memory_array_append_ /** @@ -141,7 +141,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. */ #ifndef _di_f_memory_array_append_all_ - extern f_status_t f_memory_array_append_all(const void * const sources, const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size); + extern f_status_t f_memory_array_append_all(const void * const sources, const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); #endif // _di_f_memory_array_append_all_ /** @@ -174,7 +174,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. */ #ifndef _di_f_memory_array_decimate_by_ - extern f_status_t f_memory_array_decimate_by(const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size); + extern f_status_t f_memory_array_decimate_by(const f_number_unsigned_t amount, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); #endif // _di_f_memory_array_decimate_by_ /** @@ -207,7 +207,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. */ #ifndef _di_f_memory_array_decrease_by_ - extern f_status_t f_memory_array_decrease_by(const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size); + extern f_status_t f_memory_array_decrease_by(const f_number_unsigned_t amount, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); #endif // _di_f_memory_array_decrease_by_ /** @@ -242,7 +242,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. */ #ifndef _di_f_memory_array_increase_ - extern f_status_t f_memory_array_increase(const f_number_unsigned_t step, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size); + extern f_status_t f_memory_array_increase(const f_number_unsigned_t step, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); #endif // _di_f_memory_array_increase_ /** @@ -277,7 +277,7 @@ extern "C" { * F_array_too_large (with error bit) if the new array length is too large. */ #ifndef _di_f_memory_array_increase_by_ - extern f_status_t f_memory_array_increase_by(const f_number_unsigned_t amount, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size); + extern f_status_t f_memory_array_increase_by(const f_number_unsigned_t amount, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); #endif // _di_f_memory_array_increase_by_ /** @@ -307,7 +307,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. */ #ifndef _di_f_memory_array_resize_ - extern f_status_t f_memory_array_resize(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size); + extern f_status_t f_memory_array_resize(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size); #endif // _di_f_memory_array_resize_ #ifdef __cplusplus diff --git a/level_0/f_memory/c/memory/arrays.c b/level_0/f_memory/c/memory/arrays.c index 0e7d37b..c329d39 100644 --- a/level_0/f_memory/c/memory/arrays.c +++ b/level_0/f_memory/c/memory/arrays.c @@ -6,7 +6,7 @@ extern "C" { #endif #ifndef _di_f_memory_arrays_adjust_ - f_status_t f_memory_arrays_adjust(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size, f_status_t (*callback)(const f_number_unsigned_t start, const f_number_unsigned_t size, void * const array)) { + f_status_t f_memory_arrays_adjust(const f_number_unsigned_t length, const size_t width, void ** const restrict array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size, f_status_t (*callback)(const f_number_unsigned_t start, const f_number_unsigned_t size, void * const array)) { #ifndef _di_level_0_parameter_checking_ if (!width) return F_status_set_error(F_parameter); if (!array) return F_status_set_error(F_parameter); @@ -27,7 +27,7 @@ extern "C" { #endif // _di_f_memory_arrays_adjust_ #ifndef _di_f_memory_arrays_resize_ - f_status_t f_memory_arrays_resize(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size, f_status_t (*callback)(const f_number_unsigned_t start, const f_number_unsigned_t size, void * const array)) { + f_status_t f_memory_arrays_resize(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size, f_status_t (*callback)(const f_number_unsigned_t start, const f_number_unsigned_t size, void * const array)) { #ifndef _di_level_0_parameter_checking_ if (!width) return F_status_set_error(F_parameter); if (!array) return F_status_set_error(F_parameter); diff --git a/level_0/f_memory/c/memory/arrays.h b/level_0/f_memory/c/memory/arrays.h index a2460e5..4ecac14 100644 --- a/level_0/f_memory/c/memory/arrays.h +++ b/level_0/f_memory/c/memory/arrays.h @@ -109,7 +109,7 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. */ #ifndef _di_f_memory_arrays_resize_ - extern f_status_t f_memory_arrays_resize(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size, f_status_t (*callback)(const f_number_unsigned_t start, const f_number_unsigned_t size, void * const array)); + extern f_status_t f_memory_arrays_resize(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size, f_status_t (*callback)(const f_number_unsigned_t start, const f_number_unsigned_t size, void * const array)); #endif // _di_f_memory_arrays_resize_ #ifdef __cplusplus diff --git a/level_0/f_memory/c/memory/private-array.c b/level_0/f_memory/c/memory/private-array.c index 893f816..348e356 100644 --- a/level_0/f_memory/c/memory/private-array.c +++ b/level_0/f_memory/c/memory/private-array.c @@ -7,7 +7,7 @@ extern "C" { #endif #if !defined(_di_f_memory_array_adjust_) || !defined(_di_f_memory_array_decimate_by_) - f_status_t private_f_memory_array_adjust(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t private_f_memory_array_adjust(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) { { const f_status_t status = private_f_memory_adjust(*size, length, width, array); @@ -25,7 +25,7 @@ extern "C" { #endif // !defined(_di_f_memory_array_adjust_) || !defined(_di_f_memory_array_decimate_by_) #if !defined(_di_f_memory_array_append_) || !defined(_di_f_memory_array_append_all_) || !defined(_di_f_memory_array_decrease_by_) || !defined(_di_f_memory_array_increase_) || !defined(_di_f_memory_array_increase_by_) || !defined(_di_f_memory_array_resize_) - f_status_t private_f_memory_array_resize(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) { + f_status_t private_f_memory_array_resize(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * restrict const size) { { const f_status_t status = private_f_memory_resize(*size, length, width, array); diff --git a/level_0/f_memory/c/memory/private-array.h b/level_0/f_memory/c/memory/private-array.h index 6c28a7d..b86a26d 100644 --- a/level_0/f_memory/c/memory/private-array.h +++ b/level_0/f_memory/c/memory/private-array.h @@ -42,7 +42,7 @@ extern "C" { * @see f_memory_adjust() */ #if !defined(_di_f_memory_array_adjust_) || !defined(_di_f_memory_array_decimate_by_) - extern f_status_t private_f_memory_array_adjust(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) F_attribute_visibility_internal_d; + extern f_status_t private_f_memory_array_adjust(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) F_attribute_visibility_internal_d; #endif // !defined(_di_f_memory_array_adjust_) || !defined(_di_f_memory_array_decimate_by_) /** @@ -76,7 +76,7 @@ extern "C" { * @see f_memory_resize() */ #if !defined(_di_f_memory_array_append_) || !defined(_di_f_memory_array_append_all_) || !defined(_di_f_memory_array_decrease_by_) || !defined(_di_f_memory_array_increase_) || !defined(_di_f_memory_array_increase_by_) || !defined(_di_f_memory_array_resize_) - extern f_status_t private_f_memory_array_resize(const f_number_unsigned_t length, const size_t width, void ** const array, f_number_unsigned_t * const used, f_number_unsigned_t * const size) F_attribute_visibility_internal_d; + extern f_status_t private_f_memory_array_resize(const f_number_unsigned_t length, const size_t width, void ** restrict const array, f_number_unsigned_t * restrict const used, f_number_unsigned_t * restrict const size) F_attribute_visibility_internal_d; #endif // !defined(_di_f_memory_array_append_) || !defined(_di_f_memory_array_append_all_) || !defined(_di_f_memory_array_decrease_by_) || !defined(_di_f_memory_array_increase_) || !defined(_di_f_memory_array_increase_by_) || !defined(_di_f_memory_array_resize_) #ifdef __cplusplus