Skip to content

Commit a904eea

Browse files
committed
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.
1 parent 9857aba commit a904eea

11 files changed

+79
-1
lines changed

src/common/pf_lldp.c

+8
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,14 @@ void pf_lldp_init (pnet_t * net)
11931193
CC_ASSERT (net->lldp_mutex != NULL);
11941194
}
11951195

1196+
void pf_lldp_exit (pnet_t * net){
1197+
if (net->lldp_mutex != NULL)
1198+
{
1199+
os_mutex_destroy (net->lldp_mutex);
1200+
memset (&net->lldp_mutex, 0, sizeof (net->lldp_mutex));
1201+
}
1202+
}
1203+
11961204
void pf_lldp_send_enable (pnet_t * net, int loc_port_num)
11971205
{
11981206
LOG_DEBUG (

src/common/pf_lldp.h

+7
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ void pf_lldp_mac_address_to_string (
361361
*/
362362
void pf_lldp_init (pnet_t * net);
363363

364+
/**
365+
* Free and clear dynamic objects created by the LLDP component.
366+
*
367+
* @param net InOut: The p-net stack instance
368+
*/
369+
void pf_lldp_exit (pnet_t * net);
370+
364371
/**
365372
* Start or restart a timer that monitors reception of LLDP frames from peer.
366373
*

src/common/pf_scheduler.c

+8
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ void pf_scheduler_init (pnet_t * net, uint32_t tick_interval)
240240
}
241241
}
242242

243+
void pf_scheduler_exit (pnet_t * net){
244+
if (net->scheduler_timeout_mutex != NULL)
245+
{
246+
os_mutex_destroy (net->scheduler_timeout_mutex);
247+
memset (&net->scheduler_timeout_mutex, 0, sizeof (net->scheduler_timeout_mutex));
248+
}
249+
}
250+
243251
int pf_scheduler_add (
244252
pnet_t * net,
245253
uint32_t delay,

src/common/pf_scheduler.h

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ extern "C" {
3030
*/
3131
void pf_scheduler_init (pnet_t * net, uint32_t tick_interval);
3232

33+
/**
34+
* Free and clear dynamic objects created by the scheduler.
35+
*
36+
* @param net InOut: The p-net stack instance
37+
*/
38+
void pf_scheduler_exit (pnet_t * net);
39+
3340
/**
3441
* Schedule a call-back at a specific time.
3542
*

src/device/pf_cmrpc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4829,12 +4829,12 @@ void pf_cmrpc_init (pnet_t * net)
48294829
net->cmrpc_session_number = 0x12345678; /* Starting number */
48304830
}
48314831

4832-
/* Not used */
48334832
void pf_cmrpc_exit (pnet_t * net)
48344833
{
48354834
if (net->p_cmrpc_rpc_mutex != NULL)
48364835
{
48374836
os_mutex_destroy (net->p_cmrpc_rpc_mutex);
4837+
memset (&net->p_cmrpc_rpc_mutex, 0, sizeof (net->p_cmrpc_rpc_mutex));
48384838
memset (net->cmrpc_ar, 0, sizeof (net->cmrpc_ar));
48394839
memset (net->cmrpc_session_info, 0, sizeof (net->cmrpc_session_info));
48404840
}

src/device/pf_fspm.c

+14
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,20 @@ int pf_fspm_init (pnet_t * net, const pnet_cfg_t * p_cfg)
459459
return 0;
460460
}
461461

462+
void pf_fspm_exit (pnet_t * net)
463+
{
464+
if (net->fspm_im_mutex != NULL)
465+
{
466+
os_mutex_destroy (net->fspm_im_mutex);
467+
memset (&net->fspm_im_mutex, 0, sizeof (net->fspm_im_mutex));
468+
}
469+
if (net->fspm_log_book_mutex != NULL)
470+
{
471+
os_mutex_destroy (net->fspm_log_book_mutex);
472+
memset (&net->fspm_log_book_mutex, 0, sizeof (net->fspm_log_book_mutex));
473+
}
474+
}
475+
462476
void pf_fspm_create_log_book_entry (
463477
pnet_t * net,
464478
uint32_t arep,

src/device/pf_fspm.h

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ extern "C" {
4040
*/
4141
int pf_fspm_init (pnet_t * net, const pnet_cfg_t * p_cfg);
4242

43+
/**
44+
* Free and clear dynamic objects created by the FSPM component.
45+
*
46+
* @param net InOut: The p-net stack instance
47+
*/
48+
void pf_fspm_exit (pnet_t * net);
49+
4350
/**
4451
* Retrieve the minimum device interval, from the configuration.
4552
*

src/device/pnet_api.c

+9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ int pnet_init_only (pnet_t * net, const pnet_cfg_t * p_cfg)
7373
return 0;
7474
}
7575

76+
void pnet_free_members (pnet_t * net)
77+
{
78+
pf_fspm_exit (net);
79+
pf_scheduler_exit (net);
80+
pf_lldp_exit (net);
81+
pf_cmdev_exit (net);
82+
pf_cmrpc_exit (net);
83+
}
84+
7685
pnet_t * pnet_init (const pnet_cfg_t * p_cfg)
7786
{
7887
pnet_t * net = NULL;

src/pf_types.h

+10
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,16 @@ struct pnet
28602860
*/
28612861
int pnet_init_only (pnet_t * net, const pnet_cfg_t * p_cfg);
28622862

2863+
/**
2864+
* @internal
2865+
* Free and clear members within the pnet_t structure.
2866+
*
2867+
* This does free the pnet_t structure itself.
2868+
*
2869+
* @param net InOut: The p-net stack instance to destroy.
2870+
*/
2871+
void pnet_free_members (pnet_t * net);
2872+
28632873
#ifdef __cplusplus
28642874
}
28652875
#endif

test/test_lldp.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ class LldpTest : public PnetIntegrationTestBase
448448
/* Do not clear mock or callcounters here - we need to verify send at init
449449
* from LLDP */
450450
};
451+
virtual void TearDown() override
452+
{
453+
pnet_free_members (net);
454+
};
451455
};
452456

453457
TEST_F (LldpTest, LldpInitTest)

test/utils_for_testing.h

+4
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ class PnetIntegrationTest : public PnetIntegrationTestBase
283283

284284
mock_clear(); /* lldp sends a frame at init */
285285
};
286+
virtual void TearDown() override
287+
{
288+
pnet_free_members (net);
289+
};
286290
};
287291

288292
/*************************** Assertion helpers ******************************/

0 commit comments

Comments
 (0)