22 Sep, 2010, Bojack wrote in the 1st comment:
Votes: 0
So I just got done creating a working version of mpsleep for rom but my question is this, what would be better in prog_update, using while or a for loop? So far ive been using a for loop but im a bit paranoid that itll crash the game at some point because of too many progs setting into memory faster then it being deleted.

for (tmp = first_pause; tmp != NULL; tmp = tmp->next)
{
…..

//Takes away 1 sec and checks it
if (–tmp->pause <= 0)
{
//Continue prog
program_flow( tmp->vnum, tmp->code, tmp->actor, tmp->obj,
tmp->room, tmp->player, tmp->arg1, tmp->arg2, tmp->lines );
//Delete pause data
if (!tmp->prev)
{
first_pause = tmp->next;
if (first_pause)
first_pause->prev = NULL;
}
else {
tmp->prev->next = tmp->next; }
if (!tmp->next)
{
last_pause = tmp->prev;
if (last_pause)
last_pause->next = NULL;
}
else {
tmp->next->prev = tmp->prev;
}
free_pause( tmp );
}
}
OR
tmp = first_pause;
while ( tmp )
{
…..

//Takes away 1 sec and checks it
if (–tmp->pause <= 0)
{
//Continue prog
program_flow( tmp->vnum, tmp->code, tmp->actor, tmp->obj,
tmp->room, tmp->player, tmp->arg1, tmp->arg2, tmp->lines );
//Delete pause data
if (!tmp->prev)
{
first_pause = tmp->next;
if (first_pause)
first_pause->prev = NULL;
}
else {
tmp->prev->next = tmp->next; }
if (!tmp->next)
{
last_pause = tmp->prev;
if (last_pause)
last_pause->next = NULL;
}
else {
tmp->next->prev = tmp->prev;
}
free_pause( tmp );
}
tmp = tmp->next;
}
22 Sep, 2010, Sharmair wrote in the 2nd comment:
Votes: 0
Both of these are functionally identical. In fact they may even compile to
the same op codes (though that is implementation dependent). What I am
more concerned with, is you accessing the next pointer out of the tmp
after freeing it. You probably want to save the next pointer to a local
variable at the start of the loop (like is used in a lot of code where the
current item might be deleted). Also, as a note, you probably should insert
pauses to the head of the list so they are not handled in the same update
if they are inserted in the call to program_flow() here (maybe you are, but
the code here does not show it either way). The issue of run away paused
programs really would have to be handled elsewhere though.
23 Sep, 2010, Bojack wrote in the 3rd comment:
Votes: 0
That was an accidental copy and paste of tmp = tmp->next and yes pauses are set in program flow along with moving the new pointers around. I'm just trying to figure out which is safer for the update call.
23 Sep, 2010, Rudha wrote in the 4th comment:
Votes: 0
Those are essentially functionally equivalent. I'm not sure how you think one would be safer than the other.

Maya/Rudha
23 Sep, 2010, David Haley wrote in the 5th comment:
Votes: 0
What they said. You basically wrote the for loop explicitly using a while loop. They're the same.
25 Sep, 2010, Bojack wrote in the 6th comment:
Votes: 0
Thanks guys, you can now find it in the code repository. Just look for recent uploads or in rom/snippets.
0.0/6