Fix XSPICE memory leaks found by valgrind.

This commit is contained in:
Giles Atkinson 2022-05-08 15:37:24 +01:00 committed by Holger Vogt
parent ac8f8ad65d
commit dbf4c16bc0
5 changed files with 42 additions and 30 deletions

View File

@ -30,6 +30,14 @@ EVTdest(Evt_Ckt_Data_t *evt)
return OK;
}
static void free_events(Evt_Inst_Event_t *event)
{
while (event) {
Evt_Inst_Event_t *next = event->next;
tfree(event);
event = next;
}
}
static void
Evt_Queue_destroy(Evt_Ckt_Data_t *evt, Evt_Queue_t *queue)
@ -40,15 +48,11 @@ Evt_Queue_destroy(Evt_Ckt_Data_t *evt, Evt_Queue_t *queue)
int i;
/* instance queue */
for (i = 0; i < evt->counts.num_insts; i++) {
Evt_Inst_Event_t *event = inst_queue->head[i];
while (event) {
Evt_Inst_Event_t *next = event->next;
tfree(event);
event = next;
}
free_events(inst_queue->head[i]);
free_events(inst_queue->free[i]);
}
tfree(inst_queue->head);
tfree(inst_queue->current);
tfree(inst_queue->last_step);
@ -114,6 +118,16 @@ Evt_Data_destroy(Evt_Ckt_Data_t *evt, Evt_Data_t *data)
}
*/
static void free_state(Evt_State_t *state)
{
while (state) {
Evt_State_t *next = state->next;
tfree(state->block);
tfree(state);
state = next;
}
}
static void
Evt_State_Data_destroy(Evt_Ckt_Data_t *evt, Evt_State_Data_t *state_data)
{
@ -123,13 +137,8 @@ Evt_State_Data_destroy(Evt_Ckt_Data_t *evt, Evt_State_Data_t *state_data)
return;
for (i = 0; i < evt->counts.num_insts; i++) {
Evt_State_t *state = state_data->head[i];
while (state) {
Evt_State_t *next = state->next;
tfree(state->block);
tfree(state);
state = next;
}
free_state(state_data->head[i]);
free_state(state_data->free[i]);
}
tfree(state_data->head);

View File

@ -185,7 +185,7 @@ void EVTqueue_inst(
Evt_Inst_Event_t *new_event;
Evt_Inst_Event_t *next;
Mif_Boolean_t splice, malloced = FALSE;
Mif_Boolean_t splice;
/* Get pointers for fast access */
@ -196,26 +196,12 @@ void EVTqueue_inst(
(event_time < inst_queue->next_time))
inst_queue->next_time = event_time;
/* Create a new event or get one from the free list and copy in data */
if(inst_queue->free[inst_index]) {
new_event = inst_queue->free[inst_index];
inst_queue->free[inst_index] = new_event->next;
}
else {
new_event = TMALLOC(Evt_Inst_Event_t, 1);
malloced = TRUE;
}
new_event->event_time = event_time;
new_event->posted_time = posted_time;
/* Find location at which to insert event */
splice = MIF_FALSE;
here = inst_queue->current[inst_index];
while(*here) {
/* If there's an event with the same time, don't duplicate it */
if(event_time == (*here)->event_time) {
if(malloced)
tfree(new_event);
return;
}
else if(event_time < (*here)->event_time) {
@ -225,6 +211,17 @@ void EVTqueue_inst(
here = &((*here)->next);
}
/* Create a new event or get one from the free list and copy in data */
if(inst_queue->free[inst_index]) {
new_event = inst_queue->free[inst_index];
inst_queue->free[inst_index] = new_event->next;
}
else {
new_event = TMALLOC(Evt_Inst_Event_t, 1);
}
new_event->event_time = event_time;
new_event->posted_time = posted_time;
/* If needs to be spliced into middle of existing list */
if(splice) {
/* splice it in */

View File

@ -1410,6 +1410,7 @@ static int cm_read_state_file(FILE *state_file,State_Table_t *states)
type = CNV_STRING_TOK;
while ( type != CNV_NO_TOK ) {
token = CNVget_token(&s, &type);
free(token);
j++;
}
num_tokens = (j-1);
@ -1815,7 +1816,10 @@ void cm_d_state(ARGS)
states->num_outputs = PORT_SIZE(out);
/* assign storage for arrays to pointers in states table */
/* Assign storage for arrays to pointers in states table.
* This needs to be fixed as the memory can not be recovered.
* The *states data is deleted before any deletion callback is made.
*/
states->state = (int *) calloc((size_t) (states->depth + 1), sizeof(int));
states->bits = (short *) calloc((size_t) (states->num_outputs * states->depth / 4 + 1), sizeof(short));
states->inputs = (short *) calloc((size_t) (states->num_inputs * states->depth / 8 + 1), sizeof(short));

View File

@ -524,6 +524,7 @@ static Table2_Data_t *init_local_data(const char *filename, int order)
while (token) {
if (i == ix) {
cm_message_printf("Too many numbers in x row.");
free(token);
xrc = -1;
goto EXITPOINT;
}

View File

@ -705,6 +705,7 @@ static Table3_Data_t *init_local_data(const char *filename, int interporder)
cm_message_printf("Too many numbers in y row "
"no. %d of table %d.",
lLineCount, lTableCount);
free(token);
xrc = -1;
goto EXITPOINT;
}