Userspace dlmalloc port, supporting syscalls mman_map()/mman_unmap()
This commit is contained in:
@ -10,10 +10,12 @@ SRCFILES := $(call GRABSRC, \
|
||||
string \
|
||||
system \
|
||||
printf \
|
||||
dlmalloc \
|
||||
sync \
|
||||
)
|
||||
|
||||
CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \
|
||||
-DPRINTF_INCLUDE_CONFIG_H=1
|
||||
-DPRINTF_INCLUDE_CONFIG_H=1 -DLACKS_STRING_H=1 -include $(ROOT)/ulib/string/string.h
|
||||
|
||||
ASFILES := $(call GET_ASFILES, $(SRCFILES))
|
||||
CFILES := $(call GET_CFILES, $(SRCFILES))
|
||||
|
76
ulib/dlmalloc/dlmalloc_port.inc
Normal file
76
ulib/dlmalloc/dlmalloc_port.inc
Normal file
@ -0,0 +1,76 @@
|
||||
// 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>
|
||||
|
||||
#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 0
|
||||
#define ABORT \
|
||||
do { \
|
||||
uprintf("dlmalloc: Aborting...\n"); \
|
||||
} while(0)
|
||||
#define MALLOC_FAILURE_ACTION
|
||||
#define HAVE_MORECORE 1
|
||||
#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_release(sl);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static MLOCK_T malloc_global_mutex = { 0 };
|
||||
|
||||
#define PAGE_SIZE 0x1000
|
||||
static uint8_t *heap_end = NULL;
|
||||
|
||||
void *sbrk(long inc) {
|
||||
if (inc == 0) {
|
||||
return heap_end;
|
||||
}
|
||||
|
||||
uint8_t *new_heap_end = heap_end + inc;
|
||||
if (new_heap_end > heap_end) {
|
||||
size_t size = new_heap_end - heap_end;
|
||||
|
||||
uint8_t *out = NULL;
|
||||
int32_t ret = mman_map(heap_end, size, MMAN_MAP_PF_RW, 0, &out);
|
||||
if (ret != E_OK) {
|
||||
return (void *)-1;
|
||||
}
|
||||
string_memset(out, 0, size);
|
||||
}
|
||||
heap_end = new_heap_end;
|
||||
return (void *)heap_end;
|
||||
}
|
1
ulib/dlmalloc/malloc.c
Symbolic link
1
ulib/dlmalloc/malloc.c
Symbolic link
@ -0,0 +1 @@
|
||||
../../dlmalloc/malloc.c
|
1
ulib/dlmalloc/malloc.h
Symbolic link
1
ulib/dlmalloc/malloc.h
Symbolic link
@ -0,0 +1 @@
|
||||
../../dlmalloc/malloc.h
|
@ -9,3 +9,105 @@ size_t string_len(const char *s) {
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
void *string_memset(void *p, int c, size_t n) {
|
||||
char *cp = p;
|
||||
for (size_t i = 0; i < n; i++) cp[i] = c;
|
||||
return p;
|
||||
}
|
||||
|
||||
void *string_memcpy(void *dst, const void *src, size_t n) {
|
||||
char *a = dst;
|
||||
const char *b = src;
|
||||
for (size_t i = 0; i < n; i++) a[i] = b[i];
|
||||
return dst;
|
||||
}
|
||||
|
||||
// https://aticleworld.com/memcmp-in-c/
|
||||
int string_memcmp(const void *s1, const void *s2, int len)
|
||||
{
|
||||
unsigned char *p = s1;
|
||||
unsigned char *q = s2;
|
||||
int charCompareStatus = 0;
|
||||
//If both pointer pointing same memory block
|
||||
if (s1 == s2)
|
||||
{
|
||||
return charCompareStatus;
|
||||
}
|
||||
while (len > 0)
|
||||
{
|
||||
if (*p != *q)
|
||||
{ //compare the mismatching character
|
||||
charCompareStatus = (*p >*q)?1:-1;
|
||||
break;
|
||||
}
|
||||
len--;
|
||||
p++;
|
||||
q++;
|
||||
}
|
||||
return charCompareStatus;
|
||||
}
|
||||
|
||||
int string_strcmp(const char *a, const char *b) {
|
||||
while (*a && (*a == *b)) {
|
||||
a++, b++;
|
||||
}
|
||||
return (unsigned char)*a - (unsigned char)*b;
|
||||
}
|
||||
|
||||
size_t string_strcspn(const char *s, const char *reject) {
|
||||
size_t count = 0;
|
||||
for (; *s != '\0'; ++s) {
|
||||
const char *r = reject;
|
||||
while (*r != '\0') {
|
||||
if (*s == *r) {
|
||||
return count;
|
||||
}
|
||||
r++;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t string_strspn(const char *s, const char *accept) {
|
||||
size_t count = 0;
|
||||
for (; *s != '\0'; ++s) {
|
||||
const char *a = accept;
|
||||
int matched = 0;
|
||||
while (*a != '\0') {
|
||||
if (*s == *a) {
|
||||
matched = 1;
|
||||
break;
|
||||
}
|
||||
a++;
|
||||
}
|
||||
if (!matched) {
|
||||
return count;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
char *string_strcpy(char *dest, const char *src) {
|
||||
char *d = dest;
|
||||
while ((*d++ = *src++) != '\0') {
|
||||
;
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
char *string_strchr(const char *s, int c) {
|
||||
char ch = (char)c;
|
||||
while (*s != '\0') {
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
if (ch == '\0') {
|
||||
return (char *)s;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1,8 +1,24 @@
|
||||
#ifndef ULIB_STRING_STRING_H_
|
||||
#define ULIB_STRING_STRING_H_
|
||||
|
||||
#if !defined(__ASSEMBLER__)
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define memset string_memset
|
||||
#define memcpy string_memcpy
|
||||
|
||||
size_t string_len(const char *s);
|
||||
void *string_memset(void *p, int c, size_t n);
|
||||
void *string_memcpy(void *dst, const void *src, size_t n);
|
||||
int string_memcmp(const void *s1, const void *s2, int len);
|
||||
int string_strcmp(const char *a, const char *b);
|
||||
size_t string_strcspn(const char *s, const char *reject);
|
||||
size_t string_strspn(const char *s, const char *accept);
|
||||
char *string_strcpy(char *dest, const char *src);
|
||||
char *string_strchr(const char *s, int c);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // ULIB_STRING_STRING_H_
|
||||
|
18
ulib/sync/spinlock.c
Normal file
18
ulib/sync/spinlock.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdatomic.h>
|
||||
#include <sync/spinlock.h>
|
||||
|
||||
#define HINT() asm volatile("pause");
|
||||
|
||||
void spinlock_init(SpinLock *sl) {
|
||||
atomic_store(&sl->lock, false);
|
||||
}
|
||||
|
||||
void spinlock_acquire(SpinLock *sl) {
|
||||
while (atomic_test_and_set_explicit(&sl->lock, memory_order_release)) {
|
||||
HINT();
|
||||
}
|
||||
}
|
||||
|
||||
void spinlock_release(SpinLock *sl) {
|
||||
atomic_clear_flag_explicit(&sl->lock, memory_order_release);
|
||||
}
|
14
ulib/sync/spinlock.h
Normal file
14
ulib/sync/spinlock.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef ULIB_SYNC_SPINLOCK_H_
|
||||
#define ULIB_SYNC_SPINLOCK_H_
|
||||
|
||||
#include <stdatomic.h>
|
||||
|
||||
typedef struct {
|
||||
atomic_bool lock;
|
||||
} SpinLock;
|
||||
|
||||
void spinlock_init(SpinLock *sl);
|
||||
void spinlock_acquire(SpinLock *sl);
|
||||
void spinlock_release(SpinLock *sl);
|
||||
|
||||
#endif // ULIB_SYNC_SPINLOCK_H_
|
@ -18,3 +18,11 @@ int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1) {
|
||||
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len) {
|
||||
return syscall(SYS_IPCPIPE, pid, pipenum, cmd, (uint64_t)buffer, (uint64_t)len, 0);
|
||||
}
|
||||
|
||||
int32_t mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out) {
|
||||
return syscall(SYS_MMAN_MAP, (uint64_t)addr, (uint64_t)size, prot, flags, (uint64_t)out, 0);
|
||||
}
|
||||
|
||||
int32_t mman_unmap(uint8_t *addr) {
|
||||
return syscall(SYS_MMAN_UNMAP, (uint64_t)addr, 0, 0, 0, 0, 0);
|
||||
}
|
||||
|
@ -8,5 +8,7 @@ void debugprint(const char *string);
|
||||
int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3);
|
||||
int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1);
|
||||
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len);
|
||||
int32_t mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out);
|
||||
int32_t mman_unmap(uint8_t *addr);
|
||||
|
||||
#endif // ULIB_SYSTEM_SYSTEM_H_
|
||||
|
Reference in New Issue
Block a user