Skip to content

Freeing Memory Blocks

Gehrig Wilcox edited this page Oct 15, 2018 · 2 revisions

Freeing Memory Blocks

This is how the kernel goes about freeing memory blocks

Animation

Description

  1. The function void freeMemoryBlock(void* address, int size) is called. (code)
  2. We call void makeHeader(void* header, int size) (code)
  3. makeHeader(address) makes sure the address is block aligned, then fills out the magic number and points nextFreeMemory and previousFreeMemory to freeMemoryLinkedList
  4. We then find the memory block above (code)
  5. 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 headers adjacentFreeMemory is equal to the bottom headers adjacentFreeMemory (code)
  6. 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)
  7. If step 6 passes, call freeMemoryHeader_t* unsafeCombineWithBlockBelow(freeMemoryHeader_t* block) which will clear old headers and update adjacentFreeMemory (code)
  8. Repeat steps 5 through 7 for memory block below
  9. Call void unsafePlaceBlockInFreeLinkedList(freeMemoryHeader_t* address) which will search through the freeMemoryLinkedList and sort by size
Clone this wiki locally