luxos/Kernel/library/memory/memory_alloc.c
Tiberiu Chibici e3b3584734 [????] BUILD 0.1.1.??? DATE 9/0?/2011 AT ?:?? ??
====================================================
Mainly changed: Tasking
+ Implemented multitasking
2021-09-14 18:54:59 +03:00

93 lines
2.1 KiB
C

/*
* 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;
Log("Mem","%#Allocated %u bytes (%spage aligned) at end of kernel (0x%x).\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret);
return (ret);
}
uint32 _malloc_init2 (uint32 size, uint8 page_aligned, uint32* phys)
{
uint32 ret = MemHeapAlloc(size, page_aligned, KernelHeap, KernelDirectory);
if (phys)
{
*phys = PagingGetPhysical(ret, KernelDirectory) + (ret & 0xFFF);
Log("Mem","%#Allocated %u bytes (%spage aligned) at address 0x%x (phys=%x).\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret, *phys);
}
else {
Log("Mem","%#Allocated %u bytes (%spage aligned) at address 0x%x.\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret);
}
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;
}
void* kmrealloc (void* original, uint32 newsz)
{
void* re = kmalloc(newsz);
memcpy (re, original, newsz);
kfree(original);
return re;
}