Added very simple kernel memory allocator
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
noinst_PROGRAMS = quark-kernel
|
||||
quark_kernel_SOURCES = kernel.c memorymap.c pageallocator.c priorityqueue.c stdio.c string.c elf.c resource.c
|
||||
quark_kernel_SOURCES = kernel.c memorymap.c pageallocator.c priorityqueue.c stdio.c string.c elf.c resource.c allocator.c
|
||||
quark_kernel_LDADD = -lgcc
|
||||
quark_kernel_CFLAGS = -I$(top_srcdir)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb
|
||||
quark_kernel_LDFLAGS = -nostdlib
|
||||
|
||||
34
src/allocator.c
Normal file
34
src/allocator.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "allocator.h"
|
||||
|
||||
struct linear_allocator_t
|
||||
{
|
||||
void *bottom;
|
||||
void *top;
|
||||
} allocator;
|
||||
|
||||
void initialize_allocator(void *bottom, void *top)
|
||||
{
|
||||
allocator.bottom = bottom;
|
||||
allocator.top = top;
|
||||
}
|
||||
|
||||
void *allocate_from_bottom(size_t size)
|
||||
{
|
||||
if((size_t)allocator.bottom + size <= (size_t)allocator.top)
|
||||
{
|
||||
void *ptr = allocator.bottom;
|
||||
allocator.bottom += size;
|
||||
return ptr;
|
||||
}
|
||||
return (void*)NULL;
|
||||
}
|
||||
|
||||
void *allocate_from_top(size_t size)
|
||||
{
|
||||
if((size_t)allocator.top - size >= (size_t)allocator.bottom)
|
||||
{
|
||||
allocator.top -= size;
|
||||
return allocator.top;
|
||||
}
|
||||
return (void*)NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user