From: Kevin Day Date: Sun, 21 Jul 2019 20:58:23 +0000 (-0500) Subject: Update: make sure f_new_* calls exist and add f_clear_* calls X-Git-Tag: 0.4.3~28 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=2870e42fc0261eb37d3fa71b19482745472cf9fb;p=fll Update: make sure f_new_* calls exist and add f_clear_* calls --- diff --git a/level_0/f_memory/c/memory.h b/level_0/f_memory/c/memory.h index cb44db2..03a4e18 100644 --- a/level_0/f_memory/c/memory.h +++ b/level_0/f_memory/c/memory.h @@ -96,7 +96,30 @@ extern "C"{ #endif // _di_f_adjust_ -// the delete, destroy, resize, and adjust structure defines are mean to centralize allocation for all FLL structures that follow the size+used approach. +// centralize allocation for all FLL structures that follow the size+used approach. +#ifndef _di_f_clear_structure_ + // structure: the structure to operate on + #define f_clear_structure(structure) \ + structure.array = 0; \ + structure.size = 0; \ + structure.used = 0; +#endif // _di_f_clear_structure_ + +#ifndef _di_f_new_structure_ + // status: the status to return + // structure: the structure to operate on + // type: the structure type + #define f_new_structure(status, structure, type, length) \ + structure.array = 0; \ + structure.size = 0; \ + structure.used = 0; \ + status = f_new_array((void **) & structure.array, sizeof(type), length); \ + if (status == f_none) { \ + structure.size = length; \ + structure.used = 0; \ + } +#endif // _di_f_new_structure_ + // improper use of these defines can lead to memory leaks and compilation errors #ifndef _di_f_delete_structure_ // status: the status to return @@ -146,9 +169,32 @@ extern "C"{ } #endif // _di_f_adjust_structure_ -// the delete, destroy, resize, and adjust structures defines function in the same way that the delete, destroy, resize, and adjust structure defines do +// Structures defines function in the same way that the structure defines do // however, these hold an array of structure // improper use of these defines can lead to memory leaks and compilation errors +#ifndef _di_f_clear_structures_ + // structure: the structure to operate on + #define f_clear_structures(structures) \ + structures.array = 0; \ + structures.size = 0; \ + structures.used = 0; +#endif // _di_f_clear_structures_ + +#ifndef _di_f_new_structures_ + // status: the status to return + // structures: the structure to operate on + // type: the structure type + #define f_new_structures(status, structures, type, new_length) \ + structures.array = 0; \ + structures.size = 0; \ + structures.used = 0; \ + status = f_new_array((void **) & structures.array, sizeof(type), new_length); \ + if (status == f_none) { \ + structures.size = new_length; \ + structures.used = 0; \ + } +#endif // _di_f_new_structures_ + #ifndef _di_f_delete_structures_ // status: the status to return // structures: the structure to operate on diff --git a/level_0/f_strings/c/strings.h b/level_0/f_strings/c/strings.h index 6f9201a..036fe5d 100644 --- a/level_0/f_strings/c/strings.h +++ b/level_0/f_strings/c/strings.h @@ -90,9 +90,9 @@ extern "C"{ #define f_string_length_printf string_format_long_integer - #define f_new_string_length(status, string, length) status = f_new_array((void **) & string, sizeof(f_string_length), length) - #define f_delete_string_length(status, string) status = f_delete((void **) & string) - #define f_destroy_string_length(status, string, size) status = f_destroy((void **) & string, sizeof(f_string_length), size) + #define f_new_string_length(status, string, length) status = f_new_array((void **) & string, sizeof(f_string_length), length) + #define f_delete_string_length(status, string, length) status = f_delete((void **) & string, sizeof(f_string_length), length) + #define f_destroy_string_length(status, string, size) status = f_destroy((void **) & string, sizeof(f_string_length), size) #define f_resize_string_length(status, length, old_length, new_length) \ status = f_resize((void **) & length, sizeof(f_string_length), old_length, new_length) @@ -110,6 +110,9 @@ extern "C"{ #define f_string_lengths_initialize { 0, 0, 0 } + #define f_new_string_lengths(status, lengths) \ + f_new_structure(status, lengths, f_string_length) + #define f_delete_string_lengths(status, lengths) \ f_delete_structure(status, lengths, f_string_length) @@ -154,6 +157,12 @@ extern "C"{ #define f_string_locations_initialize {0, 0, 0} + #define f_clear_string_locations(locations) \ + f_clear_structure(locations) + + #define f_new_string_locations(status, locations, length) \ + f_new_structure(status, locations, f_string_location, length) + #define f_delete_string_locations(status, locations) \ f_delete_structure(status, locations, f_string_location) @@ -178,6 +187,19 @@ extern "C"{ #define f_dynamic_string_initialize { f_string_initialize, 0, 0 } + #define f_clear_dynamic_string(dynamic) \ + dynamic.string = f_null; \ + dynamic.size = 0; \ + dynamic.used = 0; + + #define f_new_dynamic_string(status, dynamic, new_length) \ + f_clear_dynamic_string(dynamic) \ + status = f_new_array((void **) & dynamic.string, sizeof(f_string), new_length); \ + if (status == f_none) { \ + dynamic.size = new_length; \ + dynamic.used = 0; \ + } + #define f_delete_dynamic_string(status, dynamic) \ status = f_delete((void **) & dynamic.string, sizeof(f_string), dynamic.size); \ if (status == f_none) { \ @@ -217,11 +239,26 @@ extern "C"{ #define f_dynamic_strings_initialize { 0, 0, 0 } + #define f_clear_dynamic_strings(dynamics) \ + dynamics.array = 0; \ + dynamics.size = 0; \ + dynamics.used = 0; + + #define f_new_dynamic_strings(status, dynamics, length) \ + dynamics.array = 0; \ + dynamics.size = 0; \ + dynamics.used = 0; \ + status = f_new_array((void **) & dynamics.array, sizeof(f_dynamic_string), length); \ + if (status == f_none) { \ + dynamics.size = length; \ + dynamics.used = 0; \ + } + #define f_delete_dynamic_strings(status, dynamics) \ status = f_none; \ while (dynamics.size > 0) { \ --dynamics.size; \ - f_delete_dynamic_string(status, dynamics.array[dynamics.size]); \ + f_destroy_dynamic_string(status, dynamics.array[dynamics.size]); \ if (status != f_none) break; \ } \ if (status == f_none) status = f_delete((void **) & dynamics.array, sizeof(f_dynamic_string), dynamics.size); \ @@ -242,7 +279,7 @@ extern "C"{ if (new_length < dynamics.size) { \ f_string_length i = dynamics.size - new_length; \ for (; i < dynamics.size; ++i) { \ - f_delete_dynamic_string(status, dynamics.array[i]); \ + f_destroy_dynamic_string(status, dynamics.array[i]); \ if (status != f_none) break; \ } \ } \ @@ -251,7 +288,7 @@ extern "C"{ if (new_length > dynamics.size) { \ f_string_length i = dynamics.size; \ for (; i < new_length; ++i) { \ - memset(&dynamics.array[i], 0, sizeof(f_string)); \ + memset(&dynamics.array[i], 0, sizeof(f_dynamic_string)); \ } \ } \ dynamics.size = new_length; \ @@ -263,7 +300,7 @@ extern "C"{ if (new_length < dynamics.size) { \ f_string_length i = dynamics.size - new_length; \ for (; i < dynamics.size; ++i) { \ - f_destroy_dynamic_string(status, dynamics.array[i]); \ + f_destroy_dynamic_string(status, dynamics.array[i], f_dynamic_string); \ if (status != f_none) break; \ } \ } \ diff --git a/level_0/f_types/c/types_array.h b/level_0/f_types/c/types_array.h index eff3aff..6aaa267 100644 --- a/level_0/f_types/c/types_array.h +++ b/level_0/f_types/c/types_array.h @@ -31,6 +31,9 @@ extern "C"{ #define f_array_lengths_initialize { 0, 0, 0 } + #define f_new_array_lengths(status, lengths, length) \ + f_new_structure(status, lengths, f_array_length, length) + #define f_delete_array_lengths(status, lengths) \ f_delete_structure(status, lengths, f_array_length)