From 5f73c32d0b47b15639fc8bc87ce368c677e4bf30 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 21 Jul 2019 15:58:23 -0500 Subject: [PATCH] Update: make sure f_new_* calls exist and add f_clear_* calls --- level_0/f_memory/c/memory.h | 50 ++++++++++++++++++++++++++++++++++++++-- level_0/f_strings/c/strings.h | 51 +++++++++++++++++++++++++++++++++++------ level_0/f_types/c/types_array.h | 3 +++ 3 files changed, 95 insertions(+), 9 deletions(-) diff --git a/level_0/f_memory/c/memory.h b/level_0/f_memory/c/memory.h index 9223620..28dc39b 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 8a7d618..f4df772 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((f_void_p *) & string, sizeof(f_string_length), length) - #define f_delete_string_length(status, string) status = f_delete((f_void_p *) & string) - #define f_destroy_string_length(status, string, size) status = f_destroy((f_void_p *) & string, sizeof(f_string_length), size) + #define f_new_string_length(status, string, length) status = f_new_array((f_void_p *) & string, sizeof(f_string_length), length) + #define f_delete_string_length(status, string, length) status = f_delete((f_void_p *) & string, sizeof(f_string_length), length) + #define f_destroy_string_length(status, string, size) status = f_destroy((f_void_P *) & string, sizeof(f_string_length), size) #define f_resize_string_length(status, length, old_length, new_length) \ status = f_resize((f_void_p *) & 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((f_void_p *) & 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((f_void_p *) & 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 9b150ff..f068af9 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) -- 1.8.3.1