#ifndef MEMBLOCK_H_ #define MEMBLOCK_H_ #include #include #include #define MEMBLOCK_INIT_MEMORY_REGION_COUNT 128 #define MEMBLOCK_INIT_RESERVED_REGION_COUNT 128 #define __for_each_mem_range(i, type_a, type_b, p_start, p_end) \ for ((i)->idx = 0, __next_memory_region(i, type_a, type_b, p_start, p_end); \ (i)->idx != ULLONG_MAX; \ __next_memory_region(i, type_a, type_b, p_start, p_end)) #define for_each_mem_range(i, p_start, p_end) \ __for_each_mem_range(i, &memblock.memory, NULL, p_start, p_end) #define for_each_reserved_mem_range(i, p_start, p_end) \ __for_each_mem_range(i, &memblock.reserved, NULL, p_start, p_end) #define for_each_free_mem_range(i, p_start, p_end) \ __for_each_mem_range(i, &memblock.memory, &memblock.reserved, p_start, p_end) typedef uint64_t memblock_index_t; typedef enum memblock_region_status { /* Used in memblock.memory regions, indicates that the memory region exists */ MEMBLOCK_MEMORY = 0, /* Used in memblock.reserved regions, indicates that the memory region was reserved * by a call to memblock_alloc() */ MEMBLOCK_ALLOC, /* Used in memblock.reserved regions, indicates that the memory region was reserved * by a call to memblock_reserve() */ MEMBLOCK_RESERVED, } memblock_region_status_t; typedef struct memblock_region { /* the status of the memory region (free, reserved, allocated, etc) */ memblock_region_status_t status; /* the address of the first byte that makes up the region */ phys_addr_t base; /* the address of the last byte that makes up the region */ phys_addr_t limit; } memblock_region_t; typedef struct memblock_type { struct memblock_region *regions; unsigned int count; unsigned int max; const char *name; } memblock_type_t; typedef struct memblock { struct memblock_type memory; struct memblock_type reserved; } memblock_t; typedef struct memblock_iter { memblock_index_t idx; phys_addr_t base; phys_addr_t limit; } memblock_iter_t; extern memblock_t memblock; extern int __next_mem_range(memblock_iter_t *it); extern int memblock_add(phys_addr_t base, size_t size); extern int memblock_reserve(phys_addr_t base, size_t size); extern phys_addr_t memblock_alloc(size_t size); extern void __next_memory_region(memblock_iter_t *it, \ memblock_type_t *type_a, memblock_type_t *type_b, phys_addr_t start, phys_addr_t end); #endif