mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
[libc] Implement sbrk to allow malloc() to work
Userspace can now allocte via malloc. This is slightly janky because it relies on a single static handle in the library code.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <j6/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
@@ -1,4 +1,39 @@
|
||||
#include <stdint.h>
|
||||
void *sbrk(intptr_t) __attribute__ ((weak));
|
||||
#include <j6/errors.h>
|
||||
#include <j6libc/syscalls.h>
|
||||
//void *sbrk(intptr_t) __attribute__ ((weak));
|
||||
|
||||
void *sbrk(intptr_t i) { return 0; }
|
||||
static j6_handle_t __core_handle = 0;
|
||||
static intptr_t __core_size = 0;
|
||||
|
||||
static const uintptr_t __core_base = 0x1c0000000;
|
||||
|
||||
static const void *error_val = (void*)-1;
|
||||
|
||||
void *sbrk(intptr_t i)
|
||||
{
|
||||
if (i == 0)
|
||||
return (void*)__core_base;
|
||||
|
||||
if (__core_size == 0) {
|
||||
if (i < 0)
|
||||
return (void*)-1;
|
||||
|
||||
j6_status_t result = _syscall_vma_create_map(&__core_handle, i, __core_base, 1);
|
||||
if (result != j6_status_ok)
|
||||
return (void*)-1;
|
||||
|
||||
__core_size = i;
|
||||
return (void*)__core_base;
|
||||
}
|
||||
|
||||
size_t new_size = __core_size + i;
|
||||
j6_status_t result = _syscall_vma_resize(__core_handle, &new_size);
|
||||
if (result != j6_status_ok)
|
||||
return (void*)-1;
|
||||
|
||||
uintptr_t prev = __core_base + __core_size;
|
||||
__core_size += i;
|
||||
|
||||
return (void*)prev;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user