Files
mango/sandbox/memblock/include/socks/memblock.h
2022-12-29 20:53:39 +00:00

81 lines
2.4 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 {
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_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 int memblock_free(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