From: Kevin Day Date: Fri, 9 Feb 2024 05:10:34 +0000 (-0600) Subject: Update: Use typedef on a structure tag to avoid the need for void pointer on the... X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=d5b89e1e3508e19b293a55fd825c25b5dce30ede;p=fll Update: Use typedef on a structure tag to avoid the need for void pointer on the callback for f_state_t. Using a typedef on the structure tag allows for having the structure reference itself. In this case, the callback that is part of the structure accepts its own structure as one of the callback parameters. I previously did not know that I could do this and opted to use a void pointer to provide this. With this approach I get more safety, integrity, and less memory usage (for not having to add a variable that is a cast of the void pointer to the f_state_t). --- diff --git a/level_0/f_type/c/type/state.h b/level_0/f_type/c/type/state.h index 311516e..5c5b236 100644 --- a/level_0/f_type/c/type/state.h +++ b/level_0/f_type/c/type/state.h @@ -37,7 +37,7 @@ extern "C" { * This allows for the error to be processed with all relevant data before the function returns. * * These two callbacks (handle() and interrupt()) accept the following parameters: - * - state: The state data. Must be of type f_state_t. Must not be NULL. + * - state: The state data. Must not be NULL. * - internal: Additional data passed by the function being called, often containing internal data to the called function. May be NULL. * * The "custom" property on f_state_t is intended to be used so that the callback, such as the interrupt(), can make changes to something within the scope of the parent. @@ -65,20 +65,21 @@ extern "C" { * - data: A structure (defined by function) for holding data relevant to the function. May be NULL. May be required. */ #ifndef _di_f_state_t_ - typedef struct { + typedef struct f_state_t_ f_state_t; + struct f_state_t_ { uint16_t step_large; uint16_t step_small; f_status_t status; uint64_t flag; uint64_t code; - void (*handle)(void * const state, void * const internal); - void (*interrupt)(void * const state, void * const internal); + void (*handle)(f_state_t * const state, void * const internal); + void (*interrupt)(f_state_t * const state, void * const internal); void *callbacks; void *custom; void *data; - } f_state_t; + }; #define f_state_t_initialize { \ F_memory_default_allocation_large_d, \ diff --git a/level_2/fll_program/c/program.c b/level_2/fll_program/c/program.c index 6d15b31..ee6cb75 100644 --- a/level_2/fll_program/c/program.c +++ b/level_2/fll_program/c/program.c @@ -532,13 +532,11 @@ extern "C" { #endif // _di_fll_program_standard_signal_received_ #ifndef _di_fll_program_standard_signal_handle_ - void fll_program_standard_signal_handle(void * const void_state, void * const internal) { + void fll_program_standard_signal_handle(f_state_t * const state, void * const internal) { #ifndef _di_level_2_parameter_checking_ - if (!void_state) return; + if (!state) return; #endif // _di_level_2_parameter_checking_ - f_state_t * const state = (f_state_t *) void_state; - if (!state->custom) { state->status = F_interrupt_not; diff --git a/level_2/fll_program/c/program.h b/level_2/fll_program/c/program.h index f2aea88..94c6579 100644 --- a/level_2/fll_program/c/program.h +++ b/level_2/fll_program/c/program.h @@ -419,7 +419,7 @@ extern "C" { * @see fll_program_standard_signal_received() */ #ifndef _di_fll_program_standard_signal_handle_ - extern void fll_program_standard_signal_handle(void * const state, void * const internal); + extern void fll_program_standard_signal_handle(f_state_t * const state, void * const internal); #endif // _di_fll_program_standard_signal_handle_ #ifdef __cplusplus diff --git a/level_3/controller/c/thread/private-thread_signal.c b/level_3/controller/c/thread/private-thread_signal.c index 398b41a..a46e145 100644 --- a/level_3/controller/c/thread/private-thread_signal.c +++ b/level_3/controller/c/thread/private-thread_signal.c @@ -43,19 +43,13 @@ extern "C" { #endif // _di_controller_thread_signal_ #ifndef _di_controller_thread_signal_state_fss_ - f_status_t controller_thread_signal_state_fss(void * const state, void * const internal) { + f_status_t controller_thread_signal_state_fss(f_state_t * const state, void * const internal) { - if (!state) { + if (!state || !state->custom) { return F_interrupt_not; } - f_state_t *state_ptr = (f_state_t *) state; - - if (!state_ptr->custom) { - return F_interrupt_not; - } - - controller_state_interrupt_t *custom = (controller_state_interrupt_t *) state_ptr->custom; + controller_state_interrupt_t *custom = (controller_state_interrupt_t *) state->custom; controller_thread_t *thread = custom->thread; if (!controller_thread_is_enabled(custom->is_normal, thread)) { @@ -73,17 +67,11 @@ extern "C" { #ifndef _di_controller_thread_signal_state_iki_ f_status_t controller_thread_signal_state_iki(void * const state, void * const internal) { - if (!state) { - return F_interrupt_not; - } - - f_state_t *state_ptr = (f_state_t *) state; - - if (!state_ptr->custom) { + if (!state || !state->custom) { return F_interrupt_not; } - controller_state_interrupt_t *custom = (controller_state_interrupt_t *) state_ptr->custom; + controller_state_interrupt_t *custom = (controller_state_interrupt_t *) state->custom; controller_thread_t *thread = custom->thread; if (!controller_thread_is_enabled(custom->is_normal, thread)) { diff --git a/level_3/fake/c/main/signal.c b/level_3/fake/c/main/signal.c index 80bc085..f2188d6 100644 --- a/level_3/fake/c/main/signal.c +++ b/level_3/fake/c/main/signal.c @@ -107,11 +107,9 @@ extern "C" { #endif // !defined(_di_fake_signal_handler_) && !defined(_di_thread_support_) #ifndef _di_fake_signal_handler_callback_ - void fake_signal_handler_callback(void * const void_state, void * const void_internal) { + void fake_signal_handler_callback(f_state_t * const state, void * const void_internal) { - if (!void_state) return; - - f_state_t * const state = (f_state_t *) void_state; + if (!state) return; if (!state->custom) { state->status = F_interrupt_not; diff --git a/level_3/fake/c/main/signal.h b/level_3/fake/c/main/signal.h index 87ac4df..16ec740 100644 --- a/level_3/fake/c/main/signal.h +++ b/level_3/fake/c/main/signal.h @@ -96,7 +96,7 @@ extern "C" { * Not used. */ #ifndef _di_fake_signal_handler_callback_ - extern void fake_signal_handler_callback(void * const state, void * const internal); + extern void fake_signal_handler_callback(f_state_t * const state, void * const internal); #endif // _di_fake_signal_handler_callback_ #ifdef __cplusplus