1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# Internals - Memory Map
## `mmap_entry` fields
* 32-bit base address
* 32-bit length
* 16-bit value with 0 representing a free region, 1 representing a hardware-reserved or damaged region, 2 representing kernel memory and high numbers representing memory belonging to the process with that id
* pointer to `mmap_entry` with base equal to this one's base plus this one's length, or a null pointer if that is the end of memory
* pointer to `mmap_entry` with base plus length equal to this one's base, or a null pointer if that is the start of memory
## Symbols
* `mmap_bss` - array of `mmap_entry` structures statically allocated in bss section
* `MMAP_SIZE` - compile-time constant integer (65536 in the current source) for number of slots in `mmap_bss`
* `mmap_start` - pointer to slot in `mmap_bss` containing entry with base 0x0
* `mmap_bitmap` - bitmap statically allocated in bss section with byte `x / 8`, bit `x % 8` representing whether or not the `x`th slot in `mmap_bss` has an entry in it
## Functions
* `allocate_block` - takes a 32-bit length and a process number, and returns a pointer to a region with that length now owned by that process, or a null pointer if a region of that size cannot be allocated. specifically, goes through each memory region in order, and if it finds one of the same length, changes the owner and returns that. if it finds one of the longer length while going through, splits that one in two. the original has its owner changed and its length changed to the length requested. the new one made is marked free, and starts where the region requested ends, ending where the region originally ended. if there is not space in `mmap_bss` to split this entry, it looks through the rest of the memory map, now ignoring those with a longer length. if this function is unable to find a region with the exact size, and unable to split a region of larger size, a null pointer is returned.
* `deallocate_block` - takes a pointer to a region of memory, and deallocates it, unless that region is not allocated, in which case nothing is done. specifically, looks through the memory map in order for an entry with the base address matching the region passed. if this entry is not found, nothing is done. if this entry is found, it is marked free. if the region immediately preceding is also free, they are combined. this step is repeated until the region preceding either is not free or does not exist, and then the process is done again with the region following instead of the one preceding.
|