Physical memory allocator#

group Physical memory allocator

Functions

error_t pmallocator_get_header(memory_area_t area, get_next_reserved_region_t enumerator, void *user, memory_area_t *headerOUT)#

Looks for an area of suitable size and alignment to store all metadata about the area that the allocator needs.

Parameters:
  • area – The physical memory area to allocate from. Will be aligned to at least 4KiB boundary

  • iterator – A pointer to a function that returns reserved regions. Should be nullptr if there are no reserved regions.

Return values:
  • ERR_NONE – Success

  • ERR_OUT_OF_MEMORY – Failed to find enough space for the header reagion

error_t pmallocator_init_region(memory_area_t area, memory_region_t header_region, get_next_reserved_region_t enumerator, void *user)#

Note

The header region overlaps with area, it must be marked and not be allocated from. area.

Note

All pointers will break upon change of address space, because this is initialized before and will be used after the change, no pointers can be stored inside the header region.

Parameters:
  • area – The physical memory area to allocate from. Will be aligned to at least 4KiB boundary

  • header_region – A memory region of size at least pmallocator_get_header_size(@p area) aligned to 4KiB boundary

  • iterator – A pointer to a function that returns reserved regions. Should be nullptr if there are no reserved regions.

Return values:

ERR_NONE – Success

error_t pmallocator_allocate(u8 frame_order, memory_region_t header_region, memory_area_t *areaOUT)#

Note

FRAME_ORDER_4KiB (order 0) must be a valid frame order.

Parameters:
  • frame_order – The number of 4KiB frames to allocate is (1 << frame_order)

  • header_region

  • areaOUT – Pointer to the variable to which the return area will be written to. The return address will be aligned to the allocation size

Return values:
  • ERR_NONE – Success

  • ERR_NOT_VALID – The requested frame order is not supported by the allocator

  • ERR_PHYSICAL_MEMORY_FULL – Not enough memory in the area represented by header_region to allocate a frame of desired size

error_t pmallocator_free(memory_area_t area, memory_region_t header_region)#
Parameters:
  • area

  • header_region

Return values:
  • ERR_NONE – Success

  • ERR_NOT_VALID – The area represented by area is already free.

  • ERR_PHYSICAL_MEMORY_FULL – Not enough memory in the area represented by header_region to allocate a frame of desired size