Ditch dlmalloc in favour of custom umalloc

This commit is contained in:
2025-09-27 22:32:38 +02:00
parent 3b1bb9d531
commit 8d081bedb0
13 changed files with 114 additions and 110 deletions

View File

@ -10,12 +10,12 @@ SRCFILES := $(call GRABSRC, \
string \
system \
printf \
dlmalloc \
sync \
args \
util \
ubsan \
write \
umalloc \
)
CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \

View File

@ -4,10 +4,10 @@
#include <sysdefs/processctl.h>
#include <sysdefs/devctl.h>
#include <errors.h>
#include <dlmalloc/malloc.h>
#include <uprintf.h>
#include <log.h>
#include <util/util.h>
#include <umalloc/umalloc.h>
#include <devids.h>
extern void main(void);
@ -43,7 +43,7 @@ void _premain(void) {
devctl(&termdev, DEVCTL_GET_HANDLE, (uint8_t *)DEV_TERMDEV, 0, 0);
for (size_t i = 0; i < ARRLEN(_args); i++) {
_args[i] = dlmalloc(PROC_ARG_MAX);
_args[i] = umalloc(PROC_ARG_MAX);
}
processctl(-1, PCTL_ARGV, (uint64_t)&_argslen, (uint64_t)_args, MAX_ARGS);

View File

@ -1,93 +0,0 @@
// Config
#include <stddef.h>
#include <dlmalloc/malloc.h>
#include <uprintf.h>
#include <errors.h>
#include <sync/spinlock.h>
#include <string/string.h>
#include <sysdefs/mman.h>
#include <system/system.h>
#include <write/write.h>
#define USE_DL_PREFIX 1
#define LACKS_SYS_TYPES_H 1
#define NO_MALLOC_STATS 1
#define LACKS_ERRNO_H 1
#define LACKS_TIME_H 1
#define LACKS_STDLIB_H 1
#define LACKS_SYS_MMAN_H 1
#define LACKS_FCNTL_H 1
#define LACKS_UNISTD_H 1
#define LACKS_SYS_PARAM_H 1
#define LACKS_STRINGS_H 1
#define LACKS_SCHED_H 1
#define HAVE_MMAP 1
#define HAVE_MORECORE 0
#define ABORT writefmt("dlmalloc: Aborting...\n")
#define MALLOC_FAILURE_ACTION
#define USE_LOCKS 2
#define malloc_getpagesize 0x1000
#define EINVAL E_INVALIDARGUMENT
#define ENOMEM E_NOMEMORY
#define MLOCK_T SpinLock
int ACQUIRE_LOCK(SpinLock *sl) {
spinlock_acquire(sl);
return 0;
}
int RELEASE_LOCK(SpinLock *sl) {
spinlock_release(sl);
return 0;
}
int INITIAL_LOCK(SpinLock *sl) {
spinlock_init(sl);
return 0;
}
static MLOCK_T malloc_global_mutex = { 0 };
#define PAGE_SIZE 0x1000
static size_t _roundpage(size_t sz) {
return (sz + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
}
#define MAP_PRIVATE 0
#define PROT_READ 0
#define PROT_WRITE 0
#define O_RDWR 0
#define EMUL_DEV_ZERO_FD 123
#define EMUL_MAP_FAILED ((void *)-1)
int open(const char *path, int flags, ...) {
return EMUL_DEV_ZERO_FD;
}
void *mmap(void *addr, size_t len, int prot, int flags, int fd, int off) {
(void)off;
uint8_t *outaddr = NULL;
size_t need = _roundpage(len);
int32_t err = mman_map(NULL, need, MMAN_MAP_PF_RW, 0, &outaddr);
if (err != E_OK || outaddr == NULL) {
return EMUL_MAP_FAILED;
}
if (fd == EMUL_DEV_ZERO_FD) {
string_memset(outaddr, 0, need);
}
return outaddr;
}
int munmap(void *addr, size_t len) {
(void)len;
mman_unmap((uint8_t *)addr);
return 0;
}

View File

@ -1 +0,0 @@
../../dlmalloc/malloc.c

View File

@ -1 +0,0 @@
../../dlmalloc/malloc.h

View File

@ -1,6 +1,6 @@
#include <stddef.h>
#include <string/string.h>
#include <dlmalloc/malloc.h>
#include <umalloc/umalloc.h>
size_t string_len(const char *s) {
size_t l = 0;
@ -140,7 +140,7 @@ char *string_tokenizealloc(char *s, char *delim) {
return NULL;
}
char *w = (char *)dlmalloc(sizeof(char) * STRING_TOKENIZEALLOC_TOK_SIZE);
char *w = (char *)umalloc(sizeof(char) * STRING_TOKENIZEALLOC_TOK_SIZE);
string_memset(w, 0, sizeof(char) * STRING_TOKENIZEALLOC_TOK_SIZE);
int i = curridx, k = 0, j = 0;

View File

@ -3,7 +3,6 @@
#include <ansiq/all.h>
#include <args/args.h>
#include <dlmalloc/malloc.h>
#include <string/string.h>
#include <string/char.h>
#include <sync/spinlock.h>
@ -15,6 +14,7 @@
#include <log.h>
#include <assert.h>
#include <write/write.h>
#include <umalloc/umalloc.h>
#include <errors.h>
#include <sysdefs/ioctl.h>

89
ulib/umalloc/umalloc.c Normal file
View 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
View 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_

View File

@ -61,8 +61,8 @@ void pctl_ls(void) {
uint64_t procslen = processctl(-1, PCTL_PLS_SZ, 0, 0, 0);
char *namebuf = dlmalloc(34);
char *membuf = dlmalloc(20);
char *namebuf = umalloc(34);
char *membuf = umalloc(20);
uprintf("%-30s %s %-6s %-15s %-8s\n", "NAME", "PID", "TYPE", "MEMORY", "STATE");
for (size_t i = 0; i < procslen; i++) {

View File

@ -28,25 +28,25 @@ void tz_tokenize(Tokenizer *tz) {
size_t len = string_len(tz->str);
for (size_t i = 0; i < len; i++) {
if (tz->str[i] == '\'') {
char *str = dlmalloc(TZ_MAX_TK);
char *str = umalloc(TZ_MAX_TK);
string_memset(str, 0, TZ_MAX_TK);
i++;
size_t j = 0;
while (i < len && tz->str[i] != '\'') {
str[j++] = tz->str[i++];
}
Token *tk = dlmalloc(sizeof(*tk));
Token *tk = umalloc(sizeof(*tk));
tk->str = str;
tk->next = NULL;
LL_APPEND(tz->tokens, tk);
} else {
char *tkstr = dlmalloc(TZ_MAX_TK);
char *tkstr = umalloc(TZ_MAX_TK);
string_memset(tkstr, 0, TZ_MAX_TK);
size_t j = 0;
while (i < len && !string_chr_isspace(tz->str[i])) {
tkstr[j++] = tz->str[i++];
}
Token *tk = dlmalloc(sizeof(*tk));
Token *tk = umalloc(sizeof(*tk));
tk->str = tkstr;
tk->next = NULL;
LL_APPEND(tz->tokens, tk);

View File

@ -64,7 +64,7 @@ void do_file(char *filepath) {
LOG(LOG_ERR, "{s} is not a file ({d})\n", filepath, statbuf.type);
return;
}
uint8_t *buf = dlmalloc(statbuf.size+1);
uint8_t *buf = umalloc(statbuf.size+1);
string_memset(buf, 0, statbuf.size+1);
if ((ret = ioctl(ioh, IOCTL_READ, (uint64_t)buf, statbuf.size, 0)) < 0) {

View File

@ -9,7 +9,7 @@ RtAlias *RTALIASES = NULL;
#define RTCMD(name, _fn) \
do { \
RtCmd *_cmd = dlmalloc(sizeof(*_cmd)); \
RtCmd *_cmd = umalloc(sizeof(*_cmd)); \
_cmd->cmdname = (name); \
_cmd->fn = (_fn); \
LL_APPEND(RTCMDS, _cmd); \
@ -28,7 +28,7 @@ bool rt_print(Token *tks) {
}
bool rt_mkalias(Token *tks) {
RtAlias *alias = dlmalloc(sizeof(*alias));
RtAlias *alias = umalloc(sizeof(*alias));
string_memset(alias, 0, sizeof(*alias));
size_t i;