- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
Freeing Memory Blocks
        Gehrig Wilcox edited this page Oct 15, 2018 
        ·
        2 revisions
      
    This is how the kernel goes about freeing memory blocks

- The function void freeMemoryBlock(void* address, int size)is called. (code)
- We call void makeHeader(void* header, int size)(code)
- 
makeHeader(address)makes sure the address is block aligned, then fills out the magic number and pointsnextFreeMemoryandpreviousFreeMemorytofreeMemoryLinkedList
- We then find the memory block above (code)
- We call uint8_t isValidMemoryBlock(freeMemoryHeader_t* block)and test to see if the block is block aligned, the magic number is correct, and the top headersadjacentFreeMemoryis equal to the bottom headersadjacentFreeMemory(code)
- If step 5 passes, we call uint8_t unsafeRemoveFromFreeMemoryLinkedList(freeMemoryHeader_t* block)which makes sure that the header was in the linked list by testing if the headers neighbors point to it. If it was in the linked list, it makes the neighbors point to each other. Note: we assume that the headers block points to are valid (code)
- If step 6 passes, call freeMemoryHeader_t* unsafeCombineWithBlockBelow(freeMemoryHeader_t* block)which will clear old headers and updateadjacentFreeMemory(code)
- Repeat steps 5 through 7 for memory block below
- Call void unsafePlaceBlockInFreeLinkedList(freeMemoryHeader_t* address)which will search through thefreeMemoryLinkedListand sort by size