Memory management - malloc & free (mm_kernel.c)
General features
Malloc and free functions operate on 4MB frames of memory. If malloc does not
have enough memory, physical memory manager's function pmm_allocate_kernel_frames_unsafe
is called to receive more frames.
Each received frame has this structure:

In the figure above H is 16B long free block with is_first flag
set to 1 indicating that this free block cannot be assigned as full block
(flags are described later). This is here to prevent disintegration and make
4MB frames connections easier. "data" means free and full blocks allocated by
user. Finally, F is at least 16B long free block which is here to make
4MB frames connections easier.
However, contiguous 4MB frames of memory are merged as below (i is
some natural number).

Free block structure
Free block has 16B long header that has the following structure:
-
size
- size of free block in bytes
-
"F" bit is set to 1, if it is first block in non-contiguous block of memory, or
a head of bidirectional linked list of free blocks; else it is set to 0
-
"L" bit is set to 1, if it is last block in non-contiguous block of memory,
otherwise it is set to 0
-
the second bit of the first byte is always set to 1, which means
that this block is free
-
pointer to previous block, does not matter whether full or free
-
prev block
- pointer to previous free block
-
prev free - pointer to previous free block
-
next free - pointer to next free block
Full block structure
Full block has 8B long header consisting of these items:
-
size
- size of full block in bytes
-
the first bit of the first byte is always set to 0, full block cannot be first
block in non-contiguous block of memory
-
the second bit of the first byte is always set to 0, last block in
non-contiguous block of memory cannot be full
-
the last bit of the first byte is always set to 0, which means that this
block is full
-
prev block - pointer to previous block, does not matter whether full or free