From 3c3c7826ef0284d64efef4b10383d657ae9a375b Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 2 Jun 2023 17:56:13 +0300 Subject: [PATCH] LibC: Add simple definition for realloc The syscall just crashes the kernel currently. I will implement this when needed --- libc/include/sys/syscall.h | 1 + libc/stdlib.cpp | 10 ++++++++++ libc/unistd.cpp | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/libc/include/sys/syscall.h b/libc/include/sys/syscall.h index a253b2141..3cb1af423 100644 --- a/libc/include/sys/syscall.h +++ b/libc/include/sys/syscall.h @@ -20,6 +20,7 @@ __BEGIN_DECLS #define SYS_FORK 13 #define SYS_SLEEP 14 #define SYS_EXEC 15 +#define SYS_REALLOC 16 __END_DECLS diff --git a/libc/stdlib.cpp b/libc/stdlib.cpp index 8209b12dd..6de113a35 100644 --- a/libc/stdlib.cpp +++ b/libc/stdlib.cpp @@ -76,6 +76,16 @@ void* calloc(size_t nmemb, size_t size) return ptr; } +void* realloc(void* ptr, size_t size) +{ + if (ptr == nullptr) + return malloc(size); + long ret = syscall(SYS_REALLOC, ptr, size); + if (ret == -1) + return nullptr; + return (void*)ret; +} + void free(void* ptr) { if (ptr == nullptr) diff --git a/libc/unistd.cpp b/libc/unistd.cpp index 970a1fc0c..4818b6d33 100644 --- a/libc/unistd.cpp +++ b/libc/unistd.cpp @@ -130,6 +130,13 @@ long syscall(long syscall, ...) ret = Kernel::syscall(SYS_EXEC, (uintptr_t)pathname, (uintptr_t)argv, (uintptr_t)envp); break; } + case SYS_REALLOC: + { + void* ptr = va_arg(args, void*); + size_t size = va_arg(args, size_t); + ret = Kernel::syscall(SYS_REALLOC, (uintptr_t)ptr, size); + break; + } default: puts("LibC: Unhandeled syscall"); ret = -ENOSYS;