2021-09-14 15:48:57 +00:00
|
|
|
/*
|
|
|
|
* memory_alloc.c
|
|
|
|
*
|
|
|
|
* Created on: Aug 27, 2011
|
|
|
|
* Author: Tiberiu
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <memory-add.h>
|
|
|
|
|
|
|
|
extern uint32 mem_kernel_end;
|
|
|
|
extern uint8 mem_initialized;
|
|
|
|
|
|
|
|
// Used prior to proper initialization
|
|
|
|
uint32 _malloc_init1 (uint32 size, uint8 page_aligned)
|
|
|
|
{
|
|
|
|
uint32 ret = mem_kernel_end;
|
|
|
|
|
|
|
|
if (page_aligned && (ret & 0xfff)) ret = (ret & 0xfffff000) + 0x1000;
|
|
|
|
mem_kernel_end = size + ret;
|
|
|
|
|
2021-09-14 15:51:43 +00:00
|
|
|
Log("Mem","%#Allocated %u bytes (%spage aligned) at end of kernel (0x%x).\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret);
|
2021-09-14 15:48:57 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 _malloc_init2 (uint32 size, uint8 page_aligned, uint32* phys)
|
|
|
|
{
|
|
|
|
uint32 ret = MemHeapAlloc(size, page_aligned, KernelHeap, KernelDirectory);
|
|
|
|
|
|
|
|
if (phys)
|
|
|
|
{
|
|
|
|
Page *pg = PagingGetPage(ret, 0, KernelDirectory);
|
|
|
|
*phys = (*pg & PageFrame) + (ret & 0xFFF);
|
|
|
|
|
2021-09-14 15:51:43 +00:00
|
|
|
Log("Mem","%#Allocated %u bytes (%spage aligned) at address 0x%x (phys=%x).\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret, *phys);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2021-09-14 15:51:43 +00:00
|
|
|
Log("Mem","%#Allocated %u bytes (%spage aligned) at address 0x%x.\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate 'size' bytes
|
|
|
|
void* kmalloc (uint32 size)
|
|
|
|
{
|
|
|
|
if (!mem_initialized) return 0;
|
|
|
|
if (mem_initialized == 1) return (void*)_malloc_init1(size, 0);
|
|
|
|
|
|
|
|
return (void*)_malloc_init2(size,0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate 'size' bytes, page aligned
|
|
|
|
void* kmalloc_a(uint32 size)
|
|
|
|
{
|
|
|
|
if (!mem_initialized) return 0;
|
|
|
|
if (mem_initialized == 1) return (void*)_malloc_init1(size, 1);
|
|
|
|
|
|
|
|
return (void*)_malloc_init2(size,1,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* kmalloc_p(uint32 size, uint32* phys)
|
|
|
|
{
|
|
|
|
if (!mem_initialized) return 0;
|
|
|
|
if (mem_initialized == 1) {
|
|
|
|
*phys = _malloc_init1(size,0);
|
|
|
|
return (void*)(*phys);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void*)_malloc_init2(size,0,phys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* kmalloc_ap(uint32 size, uint32* phys)
|
|
|
|
{
|
|
|
|
if (!mem_initialized) return 0;
|
|
|
|
|
|
|
|
uint32 ret;
|
|
|
|
if (mem_initialized == 1) {
|
|
|
|
*phys = ret = _malloc_init1(size,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
else ret = _malloc_init2(size,1,phys);
|
|
|
|
return (void*)ret;
|
|
|
|
}
|
2021-09-14 15:50:50 +00:00
|
|
|
|
|
|
|
void* kmrealloc (void* original, uint32 newsz)
|
|
|
|
{
|
|
|
|
void* re = kmalloc(newsz);
|
|
|
|
memcpy (re, original, newsz);
|
|
|
|
kfree(original);
|
|
|
|
|
|
|
|
return re;
|
|
|
|
}
|