libc: Add malloc(), calloc(), realloc() and free()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-01-13 20:00:20 +01:00
parent 5fb2ff09c7
commit 2d2db300b0
Signed by: apio
GPG Key ID: B8A7D06E42258954
11 changed files with 86 additions and 83 deletions

View File

@ -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);
}

View File

@ -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. */

View File

@ -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);
}

View File

@ -1,4 +1,7 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <errno.h>
#include <limits.h>
#include <luna/Heap.h>
#include <luna/NumberParsing.h>
#include <luna/Utf8.h>
#include <stdlib.h>
@ -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);
}
}

View File

@ -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

View File

@ -1,38 +1,19 @@
#pragma once
#include <luna/Heap.h>
#include <luna/PlacementNew.h>
#include <luna/Result.h>
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 <typename T, class... Args> [[nodiscard]] Result<T*> 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 <typename T> [[nodiscard]] Result<T*> 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;
}

View File

@ -1,6 +1,24 @@
#pragma once
#include <luna/Result.h>
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<void*> allocate_pages_impl(usize count);
extern Result<void> release_pages_impl(void* address, usize count);

View File

@ -1,38 +0,0 @@
#include <luna/Alloc.h>
#ifndef USE_FREESTANDING
#include <stdlib.h>
#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
}

View File

@ -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);

View File

@ -180,17 +180,6 @@ Result<void*> 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<HeapBlock*> block = heap.first();
while (block.has_value())
{
@ -338,7 +327,7 @@ Result<void*> realloc_impl(void* ptr, usize size)
return new_ptr;
}
Result<void*> kcalloc(usize nmemb, usize size)
Result<void*> calloc_impl(usize nmemb, usize size)
{
const usize realsize = TRY(safe_mul(nmemb, size));
void* const ptr = TRY(malloc_impl(realsize, false));

View File

@ -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> OwnedStringView::clone() const