libc: Add malloc(), calloc(), realloc() and free()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
5fb2ff09c7
commit
2d2db300b0
@ -22,11 +22,11 @@ int main()
|
|||||||
|
|
||||||
console_write("\n", 1);
|
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("address: %p\n", address);
|
||||||
printf("memory at address: %c\n", *address);
|
printf("memory at address: %c\n", *address);
|
||||||
*address = 'e';
|
*address = 'e';
|
||||||
printf("memory at address: %c\n", *address);
|
printf("memory at address: %c\n", *address);
|
||||||
|
|
||||||
deallocate_memory(address, PAGE_SIZE * 2);
|
free(address);
|
||||||
}
|
}
|
||||||
|
@ -52,9 +52,16 @@ extern "C"
|
|||||||
/* Return the result of dividing two long long integers, including the remainder. */
|
/* Return the result of dividing two long long integers, including the remainder. */
|
||||||
lldiv_t lldiv(long long, long long);
|
lldiv_t lldiv(long long, long long);
|
||||||
|
|
||||||
void* malloc(size_t);
|
/* Allocate heap memory. */
|
||||||
void* calloc(size_t, size_t);
|
void* malloc(size_t size);
|
||||||
void* realloc(void*, size_t);
|
|
||||||
|
/* 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*);
|
void free(void*);
|
||||||
|
|
||||||
/* Abort the program without performing any normal cleanup. */
|
/* Abort the program without performing any normal cleanup. */
|
||||||
|
@ -71,3 +71,9 @@ extern "C"
|
|||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
|
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
|
||||||
|
#include <errno.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <luna/Heap.h>
|
||||||
#include <luna/NumberParsing.h>
|
#include <luna/NumberParsing.h>
|
||||||
#include <luna/Utf8.h>
|
#include <luna/Utf8.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -118,4 +121,42 @@ extern "C"
|
|||||||
|
|
||||||
return code_points;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ set(FREESTANDING_SOURCES
|
|||||||
src/SystemError.cpp
|
src/SystemError.cpp
|
||||||
src/Bitmap.cpp
|
src/Bitmap.cpp
|
||||||
src/Stack.cpp
|
src/Stack.cpp
|
||||||
src/Alloc.cpp
|
|
||||||
src/OwnedStringView.cpp
|
src/OwnedStringView.cpp
|
||||||
src/Utf8.cpp
|
src/Utf8.cpp
|
||||||
src/TarStream.cpp
|
src/TarStream.cpp
|
||||||
|
@ -1,38 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <luna/Heap.h>
|
||||||
|
#include <luna/PlacementNew.h>
|
||||||
#include <luna/Result.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)
|
template <typename T, class... Args> [[nodiscard]] Result<T*> make(Args... args)
|
||||||
{
|
{
|
||||||
T* const result = new (std::nothrow) T(args...);
|
T* const result = (T*)TRY(malloc_impl(sizeof(T)));
|
||||||
if (!result) return err(ENOMEM);
|
new (result) T(args...);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> [[nodiscard]] Result<T*> make_array(usize count)
|
template <typename T> [[nodiscard]] Result<T*> make_array(usize count)
|
||||||
{
|
{
|
||||||
T* const result = new (std::nothrow) T[count];
|
T* const result = (T*)TRY(calloc_impl(count, sizeof(T)));
|
||||||
if (!result) return err(ENOMEM);
|
new (result) T[count];
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,24 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <luna/Result.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
|
||||||
|
{
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
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*> allocate_pages_impl(usize count);
|
||||||
extern Result<void> release_pages_impl(void* address, usize count);
|
extern Result<void> release_pages_impl(void* address, usize count);
|
||||||
|
|
||||||
|
@ -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
|
|
||||||
}
|
|
@ -68,7 +68,7 @@ extern "C"
|
|||||||
{
|
{
|
||||||
const usize len = strlen(str);
|
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;
|
if (!dest) return nullptr;
|
||||||
|
|
||||||
memcpy(dest, str, len + 1);
|
memcpy(dest, str, len + 1);
|
||||||
|
@ -180,17 +180,6 @@ Result<void*> malloc_impl(usize size, bool should_scrub)
|
|||||||
|
|
||||||
size = align_up<16>(size);
|
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();
|
Option<HeapBlock*> block = heap.first();
|
||||||
while (block.has_value())
|
while (block.has_value())
|
||||||
{
|
{
|
||||||
@ -338,7 +327,7 @@ Result<void*> realloc_impl(void* ptr, usize size)
|
|||||||
return new_ptr;
|
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));
|
const usize realsize = TRY(safe_mul(nmemb, size));
|
||||||
void* const ptr = TRY(malloc_impl(realsize, false));
|
void* const ptr = TRY(malloc_impl(realsize, false));
|
||||||
|
@ -23,7 +23,7 @@ OwnedStringView::OwnedStringView(char* c_str)
|
|||||||
|
|
||||||
OwnedStringView::~OwnedStringView()
|
OwnedStringView::~OwnedStringView()
|
||||||
{
|
{
|
||||||
if (m_string) raw_free(m_string);
|
if (m_string) free_impl(m_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<OwnedStringView> OwnedStringView::clone() const
|
Result<OwnedStringView> OwnedStringView::clone() const
|
||||||
|
Loading…
Reference in New Issue
Block a user