Handmade Hero»Forums»Code
17 posts
the linkedlist impl. (its memory)
Hi I am occasionaly following the stream and am building something with the knowledge.

A few episodes ago (in one of the first sound episodes ones) Casey builds a linked list, I am wondering where the memory comes from, I am guessing from the transient state, but how is a new node inserted and how does the correct memory stay with it? (and how does the freeList work ?)

I have missed a good few episodes inbetween I think ;), I imagine something like preallocating more then enough nodes? (using pushArray on the temp_arena with more then enough?)

sorry for being a bit unclear
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
the linkedlist impl. (its memory)
The arenas are basically just giant stacks. When the linked list needs a new link, it just takes some memory from the end of the stack. It never frees it, because it is assumed that the linked list generally wants to keep around enough memory to hold whatever it's maximum size might be. So when a link gets "freed", it just puts it onto a free list from which it will get links later when it needs them again.

- Casey
17 posts
the linkedlist impl. (its memory)
Ah right-o I think i've got it now,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
TempMemory begin_temporary_memory(Memory_Arena* arena)
{
    TempMemory result = {.used= arena->used, .arena=arena};
    return result;
}

void end_temporary_memory(TempMemory temp, Memory_Arena* arena)
{
    arena->used = temp.used;
}


and then I can just use the PUSH_STRUCT way of getting some meory from the arena and eventually calling the end_temporary_memory() to reset the used. I've seen those functions being called but missed their implementation, I just have to buy the source really, thanks a bunch already, or are there any glaring bugs in the little bit of code I pasted ?

thanks, Nikki