From a904eea16cbc5836405e5baebc736c86684ddd92 Mon Sep 17 00:00:00 2001 From: Asad <20233123+asade89@users.noreply.github.com> Date: Sun, 28 Mar 2021 16:36:58 +0200 Subject: [PATCH] Added destructor for pnet_t members This is a destructor for pnet_init_only(), meaning it does not free the pnet_t structure itself. It will only free the dynamically allocated members of pnet_t. Implementing a pnet_init() destructor in addition to this would be trivial. Seems like this has a use in tests' TearDown() function to release dynamically allocated objects. --- src/common/pf_lldp.c | 8 ++++++++ src/common/pf_lldp.h | 7 +++++++ src/common/pf_scheduler.c | 8 ++++++++ src/common/pf_scheduler.h | 7 +++++++ src/device/pf_cmrpc.c | 2 +- src/device/pf_fspm.c | 14 ++++++++++++++ src/device/pf_fspm.h | 7 +++++++ src/device/pnet_api.c | 9 +++++++++ src/pf_types.h | 10 ++++++++++ test/test_lldp.cpp | 4 ++++ test/utils_for_testing.h | 4 ++++ 11 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/common/pf_lldp.c b/src/common/pf_lldp.c index db903527..b65d649a 100644 --- a/src/common/pf_lldp.c +++ b/src/common/pf_lldp.c @@ -1193,6 +1193,14 @@ void pf_lldp_init (pnet_t * net) CC_ASSERT (net->lldp_mutex != NULL); } +void pf_lldp_exit (pnet_t * net){ + if (net->lldp_mutex != NULL) + { + os_mutex_destroy (net->lldp_mutex); + memset (&net->lldp_mutex, 0, sizeof (net->lldp_mutex)); + } +} + void pf_lldp_send_enable (pnet_t * net, int loc_port_num) { LOG_DEBUG ( diff --git a/src/common/pf_lldp.h b/src/common/pf_lldp.h index cc1ce2a8..6f9eacd8 100644 --- a/src/common/pf_lldp.h +++ b/src/common/pf_lldp.h @@ -361,6 +361,13 @@ void pf_lldp_mac_address_to_string ( */ void pf_lldp_init (pnet_t * net); +/** + * Free and clear dynamic objects created by the LLDP component. + * + * @param net InOut: The p-net stack instance + */ +void pf_lldp_exit (pnet_t * net); + /** * Start or restart a timer that monitors reception of LLDP frames from peer. * diff --git a/src/common/pf_scheduler.c b/src/common/pf_scheduler.c index d56b8545..ef113491 100644 --- a/src/common/pf_scheduler.c +++ b/src/common/pf_scheduler.c @@ -240,6 +240,14 @@ void pf_scheduler_init (pnet_t * net, uint32_t tick_interval) } } +void pf_scheduler_exit (pnet_t * net){ + if (net->scheduler_timeout_mutex != NULL) + { + os_mutex_destroy (net->scheduler_timeout_mutex); + memset (&net->scheduler_timeout_mutex, 0, sizeof (net->scheduler_timeout_mutex)); + } +} + int pf_scheduler_add ( pnet_t * net, uint32_t delay, diff --git a/src/common/pf_scheduler.h b/src/common/pf_scheduler.h index cc98f299..6523be60 100644 --- a/src/common/pf_scheduler.h +++ b/src/common/pf_scheduler.h @@ -30,6 +30,13 @@ extern "C" { */ void pf_scheduler_init (pnet_t * net, uint32_t tick_interval); +/** + * Free and clear dynamic objects created by the scheduler. + * + * @param net InOut: The p-net stack instance + */ +void pf_scheduler_exit (pnet_t * net); + /** * Schedule a call-back at a specific time. * diff --git a/src/device/pf_cmrpc.c b/src/device/pf_cmrpc.c index b7a27c54..b7394ecd 100644 --- a/src/device/pf_cmrpc.c +++ b/src/device/pf_cmrpc.c @@ -4829,12 +4829,12 @@ void pf_cmrpc_init (pnet_t * net) net->cmrpc_session_number = 0x12345678; /* Starting number */ } -/* Not used */ void pf_cmrpc_exit (pnet_t * net) { if (net->p_cmrpc_rpc_mutex != NULL) { os_mutex_destroy (net->p_cmrpc_rpc_mutex); + memset (&net->p_cmrpc_rpc_mutex, 0, sizeof (net->p_cmrpc_rpc_mutex)); memset (net->cmrpc_ar, 0, sizeof (net->cmrpc_ar)); memset (net->cmrpc_session_info, 0, sizeof (net->cmrpc_session_info)); } diff --git a/src/device/pf_fspm.c b/src/device/pf_fspm.c index 9864a771..e33b9a21 100644 --- a/src/device/pf_fspm.c +++ b/src/device/pf_fspm.c @@ -459,6 +459,20 @@ int pf_fspm_init (pnet_t * net, const pnet_cfg_t * p_cfg) return 0; } +void pf_fspm_exit (pnet_t * net) +{ + if (net->fspm_im_mutex != NULL) + { + os_mutex_destroy (net->fspm_im_mutex); + memset (&net->fspm_im_mutex, 0, sizeof (net->fspm_im_mutex)); + } + if (net->fspm_log_book_mutex != NULL) + { + os_mutex_destroy (net->fspm_log_book_mutex); + memset (&net->fspm_log_book_mutex, 0, sizeof (net->fspm_log_book_mutex)); + } +} + void pf_fspm_create_log_book_entry ( pnet_t * net, uint32_t arep, diff --git a/src/device/pf_fspm.h b/src/device/pf_fspm.h index 72d7aa13..20588a49 100644 --- a/src/device/pf_fspm.h +++ b/src/device/pf_fspm.h @@ -40,6 +40,13 @@ extern "C" { */ int pf_fspm_init (pnet_t * net, const pnet_cfg_t * p_cfg); +/** + * Free and clear dynamic objects created by the FSPM component. + * + * @param net InOut: The p-net stack instance + */ +void pf_fspm_exit (pnet_t * net); + /** * Retrieve the minimum device interval, from the configuration. * diff --git a/src/device/pnet_api.c b/src/device/pnet_api.c index 2805ad58..b83f0ec9 100644 --- a/src/device/pnet_api.c +++ b/src/device/pnet_api.c @@ -73,6 +73,15 @@ int pnet_init_only (pnet_t * net, const pnet_cfg_t * p_cfg) return 0; } +void pnet_free_members (pnet_t * net) +{ + pf_fspm_exit (net); + pf_scheduler_exit (net); + pf_lldp_exit (net); + pf_cmdev_exit (net); + pf_cmrpc_exit (net); +} + pnet_t * pnet_init (const pnet_cfg_t * p_cfg) { pnet_t * net = NULL; diff --git a/src/pf_types.h b/src/pf_types.h index 6f2345c0..def66a69 100644 --- a/src/pf_types.h +++ b/src/pf_types.h @@ -2860,6 +2860,16 @@ struct pnet */ int pnet_init_only (pnet_t * net, const pnet_cfg_t * p_cfg); +/** + * @internal + * Free and clear members within the pnet_t structure. + * + * This does free the pnet_t structure itself. + * + * @param net InOut: The p-net stack instance to destroy. + */ +void pnet_free_members (pnet_t * net); + #ifdef __cplusplus } #endif diff --git a/test/test_lldp.cpp b/test/test_lldp.cpp index e8876073..f4747e77 100644 --- a/test/test_lldp.cpp +++ b/test/test_lldp.cpp @@ -448,6 +448,10 @@ class LldpTest : public PnetIntegrationTestBase /* Do not clear mock or callcounters here - we need to verify send at init * from LLDP */ }; + virtual void TearDown() override + { + pnet_free_members (net); + }; }; TEST_F (LldpTest, LldpInitTest) diff --git a/test/utils_for_testing.h b/test/utils_for_testing.h index 09402939..34544bb8 100644 --- a/test/utils_for_testing.h +++ b/test/utils_for_testing.h @@ -283,6 +283,10 @@ class PnetIntegrationTest : public PnetIntegrationTestBase mock_clear(); /* lldp sends a frame at init */ }; + virtual void TearDown() override + { + pnet_free_members (net); + }; }; /*************************** Assertion helpers ******************************/