Ditch dlmalloc in favour of custom umalloc
This commit is contained in:
89
ulib/umalloc/umalloc.c
Normal file
89
ulib/umalloc/umalloc.c
Normal file
@ -0,0 +1,89 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <umalloc/umalloc.h>
|
||||
#include <sysdefs/mman.h>
|
||||
#include <system/system.h>
|
||||
#include <errors.h>
|
||||
#include <linklist.h>
|
||||
#include <string/string.h>
|
||||
#include <write/write.h>
|
||||
|
||||
#define ARENA_SIZE (1<<20)
|
||||
|
||||
typedef struct UmArena {
|
||||
struct UmArena *next;
|
||||
uintptr_t mem;
|
||||
size_t cursor;
|
||||
int64_t balance;
|
||||
} UmArena;
|
||||
|
||||
static UmArena *ARENAS = NULL;
|
||||
|
||||
UmArena *um_newarena(void) {
|
||||
size_t arenasize = ARENA_SIZE;
|
||||
|
||||
uint8_t *mem = NULL;
|
||||
int32_t err = mman_map(NULL, arenasize, MMAN_MAP_PF_RW, 0, &mem);
|
||||
if (mem == NULL || err != E_OK) {
|
||||
return NULL;
|
||||
}
|
||||
string_memset(mem, 0, arenasize);
|
||||
|
||||
UmArena *arena = (UmArena *)mem;
|
||||
arena->mem = (uintptr_t)mem + sizeof(arena);
|
||||
arena->cursor = 0;
|
||||
LL_APPEND(ARENAS, arena);
|
||||
|
||||
return arena;
|
||||
}
|
||||
|
||||
void *umalloc(size_t size) {
|
||||
UmArena *usable = NULL;
|
||||
|
||||
UmArena *arena, *arenatmp;
|
||||
LL_FOREACH_SAFE(ARENAS, arena, arenatmp) {
|
||||
if (arena->cursor + size < ARENA_SIZE) {
|
||||
usable = arena;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (usable == NULL) {
|
||||
usable = um_newarena();
|
||||
}
|
||||
|
||||
if (usable == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uintptr_t current = usable->mem + usable->cursor;
|
||||
usable->cursor += size;
|
||||
usable->balance += 1;
|
||||
return (void *)current;
|
||||
}
|
||||
|
||||
void ufree(void *ptr_) {
|
||||
uintptr_t ptr = (uintptr_t)ptr_;
|
||||
|
||||
UmArena *freeable = NULL;
|
||||
|
||||
UmArena *arena, *arenatmp;
|
||||
LL_FOREACH_SAFE(ARENAS, arena, arenatmp) {
|
||||
if (ptr >= arena->mem && ptr < arena->mem + ARENA_SIZE) {
|
||||
freeable = arena;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (freeable == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
freeable->balance -= 1;
|
||||
|
||||
if (freeable->balance <= 0) {
|
||||
uintptr_t origin = (uintptr_t)freeable - sizeof(UmArena);
|
||||
mman_unmap((void *)origin);
|
||||
}
|
||||
}
|
||||
|
10
ulib/umalloc/umalloc.h
Normal file
10
ulib/umalloc/umalloc.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef ULIB_UMALLOC_UMALLOC_H_
|
||||
#define ULIB_UMALLOC_UMALLOC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void *umalloc(size_t size);
|
||||
void ufree(void *ptr_);
|
||||
|
||||
#endif // ULIB_UMALLOC_UMALLOC_H_
|
Reference in New Issue
Block a user