Skip to content

Commit

Permalink
Added destructor for pnet_t members
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
asade89 committed Mar 29, 2021
1 parent 9857aba commit a904eea
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/common/pf_lldp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
7 changes: 7 additions & 0 deletions src/common/pf_lldp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/common/pf_scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/common/pf_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/device/pf_cmrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
14 changes: 14 additions & 0 deletions src/device/pf_fspm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/device/pf_fspm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions src/device/pnet_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/pf_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/test_lldp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions test/utils_for_testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ******************************/
Expand Down

0 comments on commit a904eea

Please sign in to comment.