feat: libheap port for kmalloc

This commit is contained in:
Jordan ⌨️ 2023-12-15 22:31:51 +01:00
parent 5fc4c7b546
commit 0e61118fdf
5 changed files with 139 additions and 15 deletions

View file

@ -2,24 +2,27 @@
-Wall -Wall
-Wextra -Wextra
-Werror -Werror
-Isrc/libs -fcolor-diagnostics
-Isrc/kernel/klibs -Isrc/kernel/klibs
-Isrc/kernel/archs -Isrc/kernel/archs
-D__ck_toolchain_value=clang -I.cutekit/extern/cute-engineering
-D__ck_loader_value=limine -Isrc/kernel/klibs/stdc-shim
-D__ck_encoding_value=utf8 -Isrc/libs
-D__ck_bits_64__
-D__ck_toolchain_clang__
-D__ck_abi_value=sysv
-D__ck_sys_value=kernel
-D__ck_loader_limine__
-D__ck_sys_kernel__
-D__ck_encoding_utf8__
-D__ck_abi_sysv__
-D__ck_freestanding__ -D__ck_freestanding__
-D__ck_arch_value=x86_64 -D__ck_toolchain_value=clang
-D__ck_bits_value=64 -D__ck_bits_value=64
-D__ck_loader_value=limine
-D__ck_bits_64__
-D__ck_arch_value=x86_64
-D__ck_sys_kernel__
-D__ck_abi_sysv__
-D__ck_loader_limine__
-D__ck_arch_x86_64__ -D__ck_arch_x86_64__
-D__ck_sys_value=kernel
-D__ck_abi_value=sysv
-D__ck_toolchain_clang__
-D__ck_encoding_value=utf8
-D__ck_encoding_utf8__
-ffreestanding -ffreestanding
-fno-stack-protector -fno-stack-protector
-mno-80387 -mno-80387

View file

@ -1,5 +1,11 @@
{ {
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.project.v1", "$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.project.v1",
"type": "project", "type": "project",
"id": "kernel" "id": "kernel",
"extern": {
"cute-engineering/libheap": {
"git": "https://github.com/cute-engineering/libheap.git",
"tag": "v1.1.0"
}
}
} }

View file

@ -0,0 +1,74 @@
#include <hal.h>
#include <libheap/libheap.h>
#include "kmalloc.h"
#include "../../core/pmm.h"
static void *alloc_block([[gnu::unused]] void *ctx, size_t size)
{
PmmObj page = pmm_alloc(align_up$(size, PMM_PAGE_SIZE) / PMM_PAGE_SIZE);
if (page.base == 0)
{
return NULL;
}
return (void *)hal_mmap_l2h(page.base);
}
static void free_block([[gnu::unused]] void *ctx, void *block, size_t size)
{
PmmObj page = {
.base = hal_mmap_h2l((uintptr_t)block),
.len = align_up$(size, PMM_PAGE_SIZE),
};
pmm_free(page);
}
static void hook_log([[gnu::unused]] void *ctx, [[gnu::unused]] enum HeapLogType type,
[[gnu::unused]] const char *msg, [[gnu::unused]] va_list args)
{
return;
}
static struct Heap heap_impl = (struct Heap){
.alloc = alloc_block,
.free = free_block,
.log = hook_log,
};
static Res kmalloc_malloc(size_t size)
{
void *ptr = heap_alloc(&heap_impl, size);
return ptr == NULL ? err$(RES_NOMEM) : uok$((uintptr_t)ptr);
}
static Res kmalloc_free(void *ptr)
{
heap_free(&heap_impl, ptr);
return ok$();
}
static Res kmalloc_realloc(void *ptr, size_t size)
{
void *new_ptr = heap_realloc(&heap_impl, ptr, size);
return new_ptr == NULL ? err$(RES_NOMEM) : uok$((uintptr_t)new_ptr);
}
static Res kmalloc_calloc(size_t nmemb, size_t size)
{
void *ptr = heap_calloc(&heap_impl, nmemb, size);
return ptr == NULL ? err$(RES_NOMEM) : uok$((uintptr_t)ptr);
}
Alloc kmalloc_acquire(void)
{
return (Alloc){
.malloc = kmalloc_malloc,
.free = kmalloc_free,
.realloc = kmalloc_realloc,
.calloc = kmalloc_calloc,
};
}

View file

@ -0,0 +1,5 @@
#pragma once
#include <alloc.h>
Alloc kmalloc_acquire(void);

36
src/libs/alloc.h Normal file
View file

@ -0,0 +1,36 @@
#pragma once
#include <res.h>
#include <string.h>
typedef struct _Alloc
{
Res (*realloc)(void *ptr, size_t size);
Res (*malloc)(size_t size);
Res (*free)(void *ptr);
Res (*calloc)(size_t count, size_t size);
} Alloc;
typedef Alloc (*AllocAcquireFn)(void);
static inline Res alloc_default_malloc(Alloc *alloc, size_t size)
{
return alloc->realloc(NULL, size);
}
static inline Res alloc_default_free(Alloc *alloc, void *ptr)
{
return alloc->realloc(ptr, 0);
}
static inline Res alloc_default_calloc(Alloc *alloc, size_t count, size_t size)
{
Res res = alloc->realloc(NULL, count * size);
if (res.type == RES_OK)
{
memset((void *)res.uvalue, 0, count * size);
}
return res;
}