From: Kevin Day Date: Sat, 9 May 2020 22:26:41 +0000 (-0500) Subject: Feature: add f_string_static as a non-allocated alternative to f_string_dynamic X-Git-Tag: 0.5.0~272 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=7dff78abd93a141de6f4e10871c67305863b4e26;p=fll Feature: add f_string_static as a non-allocated alternative to f_string_dynamic That is not to say that a dynamically allocated buffer cannot be placed in its string part. Instead, it communicates that this is not never be allocated or deallocated directly. This is implemented in such a way that f_string_dynamic is a sub-type of f_string_static. This should allow for easy type conversions. --- diff --git a/level_0/f_string/c/string.h b/level_0/f_string/c/string.h index 5fc1b8d..e8bb990 100644 --- a/level_0/f_string/c/string.h +++ b/level_0/f_string/c/string.h @@ -204,28 +204,48 @@ extern "C" { #endif // _di_f_string_ranges_ /** - * A string that supports contains a size attribute to handle dynamic allocations and deallocations. + * A string that is analogous to f_string_dynamic but intended for static-only uses. * - * Save the string size along with the string, so that strlen(..) commands can be avoided as much as possible. + * The f_string_static type should never be directly allocated or deallocated. * * string: the string. - * size: total amount of allocated space. - * used: total number of allocated spaces used. + * size: total amount of space available. + * used: total number of space used. */ -#ifndef _di_f_string_dynamic_ +#ifndef _di_f_string_static_ typedef struct { f_string string; f_string_length size; f_string_length used; - } f_string_dynamic; + } f_string_static; - #define f_string_dynamic_initialize { f_string_initialize, 0, 0 } + #define f_string_static_initialize { 0, 0, 0 } - #define f_macro_string_dynamic_clear(dynamic) \ - dynamic.string = 0; \ - dynamic.size = 0; \ - dynamic.used = 0; + #define f_macro_string_static_clear(string_static) \ + string_static.string = 0; \ + string_static.size = 0; \ + string_static.used = 0; +#endif // _di_f_string_static_ + +/** + * A string that supports contains a size attribute to handle dynamic allocations and deallocations. + * + * Save the string size along with the string, so that strlen(..) commands can be avoided as much as possible. + * + * This is a sub-type of f_string_static, allowing it to be passed into any f_string_static type. + * It is recommended that f_string_static are not otherwise casted into f_string_dynamic to avoid potential memory allocation issues. + * + * string: the string. + * size: total amount of allocated space. + * used: total number of allocated spaces used. + */ +#ifndef _di_f_string_dynamic_ + typedef f_string_static f_string_dynamic; + + #define f_string_dynamic_initialize f_string_static_initialize + + #define f_macro_string_dynamic_clear(dynamic) f_macro_string_static_clear(dynamic) #define f_macro_string_dynamic_new(status, dynamic, new_length) \ f_macro_string_dynamic_clear(dynamic) \