Created separate memblock functions for virtual and physical memory allocation

This commit is contained in:
2023-01-08 12:21:13 +00:00
parent 0d77d97561
commit bbb09b2549
3 changed files with 22 additions and 5 deletions

View File

@@ -50,7 +50,7 @@ int main(int argc, const char **argv)
memblock_reserve(0x30000, 0x40000); memblock_reserve(0x30000, 0x40000);
memblock_reserve(0x100000, 0x10000); memblock_reserve(0x100000, 0x10000);
phys_addr_t alloc = memblock_alloc(512); phys_addr_t alloc = memblock_alloc_phys(512);
printf("allocated 512 bytes at 0x%" PRIxPTR "\n", alloc); printf("allocated 512 bytes at 0x%" PRIxPTR "\n", alloc);
printf("memory regions:\n"); printf("memory regions:\n");

View File

@@ -80,8 +80,11 @@ extern int memblock_init(uintptr_t alloc_start, uintptr_t alloc_end, uintptr_t v
extern int memblock_add(phys_addr_t base, size_t size); extern int memblock_add(phys_addr_t base, size_t size);
extern int memblock_reserve(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 *memblock_alloc(size_t size);
extern int memblock_free(phys_addr_t addr, 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, \ extern void __next_memory_region(memblock_iter_t *it, \
memblock_type_t *type_a, memblock_type_t *type_b, memblock_type_t *type_a, memblock_type_t *type_b,

View File

@@ -236,7 +236,16 @@ static phys_addr_t do_alloc(size_t size)
return allocated_base; return allocated_base;
} }
phys_addr_t memblock_alloc(size_t size) void *memblock_alloc(size_t size)
{
if (memblock.reserved.count >= memblock.reserved.max - 2) {
memblock_double_capacity(&memblock.reserved);
}
return (void *)(do_alloc(size) + memblock.m_voffset);
}
phys_addr_t memblock_alloc_phys(size_t size)
{ {
if (memblock.reserved.count >= memblock.reserved.max - 2) { if (memblock.reserved.count >= memblock.reserved.max - 2) {
memblock_double_capacity(&memblock.reserved); memblock_double_capacity(&memblock.reserved);
@@ -245,7 +254,12 @@ phys_addr_t memblock_alloc(size_t size)
return do_alloc(size); return do_alloc(size);
} }
int memblock_free(phys_addr_t addr, size_t size) int memblock_free(void *p, size_t size)
{
return 0;
}
int memblock_free_phys(phys_addr_t addr, size_t size)
{ {
return 0; return 0;
} }