Heap allocation is first fit. Getmem() begins searching the free memory from low addresses until it finds a chunk bit enough to satisfy the request. Note that getmem() must keep two pointers running through the memory list as the list is only singly-linked. If getmem() fails to find a chunk large enough, it returns SYSERR. Assuming it finds one, if the free block is larger than the requested size it breaks the block in two - the low part to allocate, and the remainder to link back into the free list as a new (though smaller) piece of free memory.
Freeing a block of memory requires that freemem() search through the free memory list looking for the appropriate place on the list for the returned chunk. In this case also the search must trace two pointers through the list. The search ends when the pointers point to the blocks which should neighbour the newly freed block (one will have a lower address, and the other will have a higher address). Once the correct location is found, a check is made to see whether either or both of the neighbors are contiguous with the newly freed block. If they are, they are joined into one, larger, free block. Notice that freemem() requires not only the location of the block to be freed, but also requires its size. Think about why other memory allocation routines (such as malloc() and free()) do not need the size on freeing.