From 6b002e7c6adc75fb3467d36ea970e7c281aca743 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 14 Jun 2024 22:21:54 -0500 Subject: [PATCH] Update: Change f_memory_array_increase() and f_memory_array_increase_by() design. Don't bother setting the *used size to F_number_t_size_unsigned_d on large size. Instead, just return an error of F_array_too_large. Add an unsigned overflow check such that if the increment results in an overflow, then the resulting length will be smaller than the original length. Update the documentation comments. --- level_0/f_memory/c/memory/array.c | 16 ++++------------ level_0/f_memory/c/memory/array.h | 17 ++++++----------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/level_0/f_memory/c/memory/array.c b/level_0/f_memory/c/memory/array.c index 3622718..15530f9 100644 --- a/level_0/f_memory/c/memory/array.c +++ b/level_0/f_memory/c/memory/array.c @@ -125,15 +125,9 @@ extern "C" { #endif // _di_level_0_parameter_checking_ if (step && *used + 1 > *size) { - if (*used >= F_number_t_size_unsigned_d) return F_status_set_error(F_array_too_large); + const f_number_unsigned_t length = *used + step; - f_number_unsigned_t length = *used + step; - - if (length > F_number_t_size_unsigned_d) { - if (*used + 1 > F_number_t_size_unsigned_d) return F_status_set_error(F_array_too_large); - - length = F_number_t_size_unsigned_d; - } + if (length > F_number_t_size_unsigned_d || length < *used) return F_status_set_error(F_array_too_large); return private_f_memory_array_resize(length, width, array, used, size); } @@ -152,13 +146,11 @@ extern "C" { #endif // _di_level_0_parameter_checking_ if (amount) { - if (*used >= F_number_t_size_unsigned_d) return F_status_set_error(F_array_too_large); - const f_number_unsigned_t length = *used + amount; - if (length > *size) { - if (length > F_number_t_size_unsigned_d) return F_status_set_error(F_array_too_large); + if (length > F_number_t_size_unsigned_d || length < *used) return F_status_set_error(F_array_too_large); + if (length > *size) { return private_f_memory_array_resize(length, width, array, used, size); } } diff --git a/level_0/f_memory/c/memory/array.h b/level_0/f_memory/c/memory/array.h index 466e15e..86e8482 100644 --- a/level_0/f_memory/c/memory/array.h +++ b/level_0/f_memory/c/memory/array.h @@ -152,7 +152,7 @@ extern "C" { * This will not shrink the size to less than 0. * * This function is only useful for simple structures of the form "{ array, used, size }" where the array is a simple type. - * If the simple type that is "array" requires additional memory manipulation on allocation or de-allocation, then do not use this function. + * If the simple type that is "array" requires additional memory manipulation on de-allocation, then do not use this function. * * @param amount * A positive number representing how much to decimate the size by. @@ -185,7 +185,7 @@ extern "C" { * This will not shrink the size to less than 0. * * This function is only useful for simple structures of the form "{ array, used, size }" where the array is a simple type. - * If the simple type that is "array" requires additional memory manipulation on allocation or de-allocation, then do not use this function. + * If the simple type that is "array" requires additional memory manipulation on de-allocation, then do not use this function. * * @param amount * A positive number representing how much to decrease the size by. @@ -211,13 +211,10 @@ extern "C" { #endif // _di_f_memory_array_decrease_by_ /** - * Increase the size of the memory array, but only if necessary. - * - * If the given length is too large for the buffer, then attempt to set max buffer size (F_number_t_size_unsigned_d). - * If already set to the maximum buffer size, then the resize will fail. + * Increase the size of the memory array if and only if (used + 1 > size). * * This function is only useful for simple structures of the form "{ array, used, size }" where the array is a simple type. - * If the simple type that is "array" requires additional memory manipulation on allocation or de-allocation, then do not use this function. + * If the simple type that is "array" requires additional memory manipulation on allocation, then do not use this function. * * @param step * The allocation step to use. @@ -246,14 +243,12 @@ extern "C" { #endif // _di_f_memory_array_increase_ /** - * Resize the memory array to a larger size. + * Increase the size of the memory array by the given amount. * * This will resize making the array larger based on the given length. - * If the given length is too large for the buffer, then attempt to set max buffer size (F_number_t_size_unsigned_d). - * If already set to the maximum buffer size, then the resize will fail. * * This function is only useful for simple structures of the form "{ array, used, size }" where the array is a simple type. - * If the simple type that is "array" requires additional memory manipulation on allocation or de-allocation, then do not use this function. + * If the simple type that is "array" requires additional memory manipulation on allocation, then do not use this function. * * @param amount * A positive number representing how much to increase the size by. -- 1.8.3.1