From 2d2db300b0e3ae7143a7c2a9bf40eccffff34417 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 13 Jan 2023 20:00:20 +0100 Subject: [PATCH] libc: Add malloc(), calloc(), realloc() and free() --- apps/app.c | 4 ++-- libc/include/stdlib.h | 13 +++++++++--- libc/src/stdio.cpp | 6 ++++++ libc/src/stdlib.cpp | 41 ++++++++++++++++++++++++++++++++++++ luna/CMakeLists.txt | 1 - luna/include/luna/Alloc.h | 31 ++++++--------------------- luna/include/luna/Heap.h | 18 ++++++++++++++++ luna/src/Alloc.cpp | 38 --------------------------------- luna/src/CString.cpp | 2 +- luna/src/Heap.cpp | 13 +----------- luna/src/OwnedStringView.cpp | 2 +- 11 files changed, 86 insertions(+), 83 deletions(-) delete mode 100644 luna/src/Alloc.cpp diff --git a/apps/app.c b/apps/app.c index cd7770c5..1a49bef1 100644 --- a/apps/app.c +++ b/apps/app.c @@ -22,11 +22,11 @@ int main() console_write("\n", 1); - char* address = (char*)allocate_memory(PAGE_SIZE * 2, PROT_READ | PROT_WRITE); + char* address = (char*)malloc(1); printf("address: %p\n", address); printf("memory at address: %c\n", *address); *address = 'e'; printf("memory at address: %c\n", *address); - deallocate_memory(address, PAGE_SIZE * 2); + free(address); } diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index fc63632d..9b38de3d 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -52,9 +52,16 @@ extern "C" /* Return the result of dividing two long long integers, including the remainder. */ lldiv_t lldiv(long long, long long); - void* malloc(size_t); - void* calloc(size_t, size_t); - void* realloc(void*, size_t); + /* Allocate heap memory. */ + void* malloc(size_t size); + + /* Allocate zero-initialized heap memory. */ + void* calloc(size_t nmemb, size_t size); + + /* Resize allocated heap memory. */ + void* realloc(void* ptr, size_t size); + + /* Free heap memory. */ void free(void*); /* Abort the program without performing any normal cleanup. */ diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index 0d4102f0..8881c675 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -71,3 +71,9 @@ extern "C" return 0; } } + +void debug_log_impl(const char* format, va_list ap) +{ + pure_cstyle_format( + format, [](char c, void*) { console_write(&c, 1); }, nullptr, ap); +} diff --git a/libc/src/stdlib.cpp b/libc/src/stdlib.cpp index d92a45bf..1eed9f9d 100644 --- a/libc/src/stdlib.cpp +++ b/libc/src/stdlib.cpp @@ -1,4 +1,7 @@ +#define _LUNA_SYSTEM_ERROR_EXTENSIONS +#include #include +#include #include #include #include @@ -118,4 +121,42 @@ extern "C" return code_points; } + + void* malloc(size_t size) + { + auto rc = malloc_impl(size); + if (rc.has_error()) + { + errno = rc.error(); + return nullptr; + } + return rc.value(); + } + + void* calloc(size_t nmemb, size_t size) + { + auto rc = calloc_impl(nmemb, size); + if (rc.has_error()) + { + errno = rc.error(); + return nullptr; + } + return rc.value(); + } + + void* realloc(void* ptr, size_t size) + { + auto rc = realloc_impl(ptr, size); + if (rc.has_error()) + { + errno = rc.error(); + return nullptr; + } + return rc.value(); + } + + void free(void* ptr) + { + free_impl(ptr); + } } diff --git a/luna/CMakeLists.txt b/luna/CMakeLists.txt index 0ce74c80..f9b6ec72 100644 --- a/luna/CMakeLists.txt +++ b/luna/CMakeLists.txt @@ -11,7 +11,6 @@ set(FREESTANDING_SOURCES src/SystemError.cpp src/Bitmap.cpp src/Stack.cpp - src/Alloc.cpp src/OwnedStringView.cpp src/Utf8.cpp src/TarStream.cpp diff --git a/luna/include/luna/Alloc.h b/luna/include/luna/Alloc.h index 96ef2b94..1e89feb6 100644 --- a/luna/include/luna/Alloc.h +++ b/luna/include/luna/Alloc.h @@ -1,38 +1,19 @@ #pragma once +#include +#include #include -namespace std -{ - struct nothrow_t - { - explicit nothrow_t() = default; - }; - - extern const nothrow_t nothrow; - - enum class align_val_t : usize - { - }; -}; - -[[nodiscard]] void* raw_malloc(usize); -void raw_free(void*); - -void* operator new(usize size, const std::nothrow_t&) noexcept; -void* operator new[](usize size, const std::nothrow_t&) noexcept; -void operator delete(void* ptr, usize size, std::align_val_t alignment) noexcept; - template [[nodiscard]] Result make(Args... args) { - T* const result = new (std::nothrow) T(args...); - if (!result) return err(ENOMEM); + T* const result = (T*)TRY(malloc_impl(sizeof(T))); + new (result) T(args...); return result; } template [[nodiscard]] Result make_array(usize count) { - T* const result = new (std::nothrow) T[count]; - if (!result) return err(ENOMEM); + T* const result = (T*)TRY(calloc_impl(count, sizeof(T))); + new (result) T[count]; return result; } diff --git a/luna/include/luna/Heap.h b/luna/include/luna/Heap.h index 5ae2f74a..2b864a07 100644 --- a/luna/include/luna/Heap.h +++ b/luna/include/luna/Heap.h @@ -1,6 +1,24 @@ #pragma once #include +namespace std +{ + struct nothrow_t + { + explicit nothrow_t() = default; + }; + + extern const nothrow_t nothrow; + + enum class align_val_t : usize + { + }; +}; + +void* operator new(usize size, const std::nothrow_t&) noexcept; +void* operator new[](usize size, const std::nothrow_t&) noexcept; +void operator delete(void* ptr, usize size, std::align_val_t alignment) noexcept; + extern Result allocate_pages_impl(usize count); extern Result release_pages_impl(void* address, usize count); diff --git a/luna/src/Alloc.cpp b/luna/src/Alloc.cpp deleted file mode 100644 index 65d8c609..00000000 --- a/luna/src/Alloc.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include - -#ifndef USE_FREESTANDING -#include -#endif - -[[nodiscard]] void* raw_malloc(usize size) -{ -#ifdef USE_FREESTANDING - char* const rc = new (std::nothrow) char[size]; - return (void*)rc; -#else - // return malloc(size); - (void)size; - return NULL; -#endif -} - -void raw_free(void* ptr) -{ -#ifdef USE_FREESTANDING - char* const arr = (char*)ptr; - delete[] arr; -#else - // return free(ptr); - (void)ptr; -#endif -} - -void operator delete(void* ptr, usize size, std::align_val_t) noexcept -{ -#ifdef USE_FREESTANDING - operator delete(ptr, size); -#else - (void)ptr; - (void)size; -#endif -} diff --git a/luna/src/CString.cpp b/luna/src/CString.cpp index 06f129f6..1fbb41d2 100644 --- a/luna/src/CString.cpp +++ b/luna/src/CString.cpp @@ -68,7 +68,7 @@ extern "C" { const usize len = strlen(str); - char* dest = (char*)raw_malloc(len + 1); + char* dest = (char*)malloc_impl(len + 1).value_or(nullptr); if (!dest) return nullptr; memcpy(dest, str, len + 1); diff --git a/luna/src/Heap.cpp b/luna/src/Heap.cpp index 43530e46..f7839c12 100644 --- a/luna/src/Heap.cpp +++ b/luna/src/Heap.cpp @@ -180,17 +180,6 @@ Result malloc_impl(usize size, bool should_scrub) size = align_up<16>(size); - if (heap.count() == 0) - { - const usize pages = get_pages_for_allocation(size + sizeof(HeapBlock)); - HeapBlock* const block = (HeapBlock*)TRY(allocate_pages_impl(pages)); - - block->full_size = (pages * PAGE_SIZE) - sizeof(HeapBlock); - block->magic = BLOCK_MAGIC; - block->status = BLOCK_START_MEM | BLOCK_END_MEM; - heap.append(block); - } - Option block = heap.first(); while (block.has_value()) { @@ -338,7 +327,7 @@ Result realloc_impl(void* ptr, usize size) return new_ptr; } -Result kcalloc(usize nmemb, usize size) +Result calloc_impl(usize nmemb, usize size) { const usize realsize = TRY(safe_mul(nmemb, size)); void* const ptr = TRY(malloc_impl(realsize, false)); diff --git a/luna/src/OwnedStringView.cpp b/luna/src/OwnedStringView.cpp index efeb5f78..c53fc446 100644 --- a/luna/src/OwnedStringView.cpp +++ b/luna/src/OwnedStringView.cpp @@ -23,7 +23,7 @@ OwnedStringView::OwnedStringView(char* c_str) OwnedStringView::~OwnedStringView() { - if (m_string) raw_free(m_string); + if (m_string) free_impl(m_string); } Result OwnedStringView::clone() const