Added very simple kernel memory allocator

This commit is contained in:
2021-04-17 01:18:14 -05:00
parent b582c39ff8
commit b141582f40
3 changed files with 44 additions and 1 deletions

9
include/allocator.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <stddef.h>
void initialize_allocator(void *bottom, void *top);
void *allocate_from_bottom(size_t size);
void *allocate_from_top(size_t size);

View File

@@ -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
View 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;
}