94 lines
3.0 KiB
C
94 lines
3.0 KiB
C
#ifndef MEMBLOCK_H_
|
|
#define MEMBLOCK_H_
|
|
|
|
#include <stddef.h>
|
|
#include <limits.h>
|
|
#include <socks/types.h>
|
|
|
|
#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 {
|
|
/* bounds of the memory region that can be used by memblock_alloc()
|
|
both of these are virtual addresses */
|
|
uintptr_t m_alloc_start, m_alloc_end;
|
|
/* memblock assumes that all memory in the alloc zone is contiguously mapped
|
|
(if paging is enabled). m_voffset is the offset that needs to be added to
|
|
a given physical address to get the corresponding virtual address */
|
|
uintptr_t m_voffset;
|
|
|
|
struct memblock_type memory;
|
|
struct memblock_type reserved;
|
|
} memblock_t;
|
|
|
|
typedef struct memblock_iter {
|
|
memblock_index_t __idx;
|
|
phys_addr_t it_base;
|
|
phys_addr_t it_limit;
|
|
memblock_region_status_t it_status;
|
|
} memblock_iter_t;
|
|
|
|
extern memblock_t memblock;
|
|
|
|
extern int __next_mem_range(memblock_iter_t *it);
|
|
|
|
extern int memblock_init(uintptr_t alloc_start, uintptr_t alloc_end, uintptr_t voffset);
|
|
|
|
extern int memblock_add(phys_addr_t base, size_t size);
|
|
extern int memblock_reserve(phys_addr_t base, size_t size);
|
|
|
|
extern void *memblock_alloc(size_t size);
|
|
extern phys_addr_t memblock_alloc_phys(size_t size);
|
|
|
|
extern int memblock_free(void *addr, size_t size);
|
|
extern int memblock_free_phys(phys_addr_t addr, 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
|