From 9308c6b077b9d39960bc00df6fb5eb0e15d831ca Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Mon, 3 Jan 2022 21:28:47 +0100 Subject: [PATCH] Reuse memory for queued events. Patch #109 - 2 provided by Giles Atkinson --- src/xspice/evt/evtaccept.c | 42 ++++++++++++++++++++++++++++++++++---- src/xspice/evt/evtdeque.c | 8 ++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/xspice/evt/evtaccept.c b/src/xspice/evt/evtaccept.c index 251029e88..497a83c7c 100644 --- a/src/xspice/evt/evtaccept.c +++ b/src/xspice/evt/evtaccept.c @@ -93,12 +93,29 @@ void EVTaccept( num_modified = inst_queue->num_modified; /* Loop through list of items modified since last time */ for(i = 0; i < num_modified; i++) { + Evt_Inst_Event_t *stale, *next; + /* Get the index of the inst modified */ index = inst_queue->modified_index[i]; - /* Update last_step for this index */ - inst_queue->last_step[index] = inst_queue->current[index]; /* Reset the modified flag */ inst_queue->modified[index] = MIF_FALSE; + /* Move stale entries to the free list. */ + next = inst_queue->head[index]; + while (next) { + if (next->event_time >= time || + &next->next == inst_queue->current[index]) { + break; + } + stale = next; + next = next->next; + stale->next = inst_queue->free[index]; + inst_queue->free[index] = stale; + } + inst_queue->head[index] = next; + if (!next) + inst_queue->current[index] = &inst_queue->head[index]; + /* Update last_step for this index */ + inst_queue->last_step[index] = inst_queue->current[index]; } /* Record the new last_time and reset number modified to zero */ inst_queue->last_time = time; @@ -109,12 +126,29 @@ void EVTaccept( num_modified = output_queue->num_modified; /* Loop through list of items modified since last time */ for(i = 0; i < num_modified; i++) { + Evt_Output_Event_t *stale, *next; + /* Get the index of the output modified */ index = output_queue->modified_index[i]; - /* Update last_step for this index */ - output_queue->last_step[index] = output_queue->current[index]; /* Reset the modified flag */ output_queue->modified[index] = MIF_FALSE; + /* Move stale entries to the free list. */ + next = output_queue->head[index]; + while (next) { + if (next->event_time >= time || + &next->next == output_queue->current[index]) { + break; + } + stale = next; + next = next->next; + stale->next = output_queue->free[index]; + output_queue->free[index] = stale; + } + output_queue->head[index] = next; + if (!next) + output_queue->current[index] = &output_queue->head[index]; + /* Update last_step for this index */ + output_queue->last_step[index] = output_queue->current[index]; } /* Record the new last_time and reset number modified to zero */ output_queue->last_time = time; diff --git a/src/xspice/evt/evtdeque.c b/src/xspice/evt/evtdeque.c index 60875327b..046dd3052 100644 --- a/src/xspice/evt/evtdeque.c +++ b/src/xspice/evt/evtdeque.c @@ -132,8 +132,8 @@ static void EVTdequeue_output( /* Get pointer to next event in queue at this index */ output = *(output_queue->current[index]); - /* If event time does not match current time, skip */ - if(output->event_time != time) + /* If cleaned or event time does not match current time, skip */ + if(!output || output->event_time != time) continue; /* It must match, so pull the event from the queue and process it */ @@ -232,8 +232,8 @@ void EVTdequeue_inst( /* Get pointer to next event in queue at this index */ inst = *(inst_queue->current[index]); - /* If event time does not match current time, skip */ - if(inst->event_time != time) + /* If cleaned or event time does not match current time, skip */ + if(!inst || inst->event_time != time) continue; /* It must match, so pull the event from the queue and process it */