Implemented more STL and page allocation for malloc.

This commit is contained in:
2024-01-27 15:24:50 +01:00
parent edd9ee85c7
commit 193e3a19dc
13 changed files with 701 additions and 28 deletions

View File

@@ -0,0 +1,43 @@
#pragma once
#if !defined(BAD_APPLE_OS_MATH_H_INCLUDED)
#define BAD_APPLE_OS_MATH_H_INCLUDED
#if !defined(HUGE_VALF)
#define HUGE_VALF (__builtin_inff())
#endif
#if !defined(FLT_QNAN)
#define FLT_QNAN (__builtin_nanf(""))
#endif
#if !defined(FLT_SNAN)
#define FLT_SNAN (__builtin_nansf(""))
#endif
#if !defined(HUGE_VAL)
#define HUGE_VAL (__builtin_inf())
#endif
#if !defined(DBL_QNAN)
#define DBL_QNAN (__builtin_nan(""))
#endif
#if !defined(DBL_SNAN)
#define DBL_SNAN (__builtin_nans(""))
#endif
#if !defined(HUGE_VALL)
#define HUGE_VALL (__builtin_infl())
#endif
#if !defined(LDBL_QNAN)
#define LDBL_QNAN (__builtin_nanl(""))
#endif
#if !defined(LDBL_SNAN)
#define LDBL_SNAN (__builtin_nansl(""))
#endif
#endif // !defined(BAD_APPLE_OS_MATH_H_INCLUDED)

View File

@@ -40,8 +40,8 @@ enum class MemoryType
struct MemoryRange
{
std::uint64_t pageBase;
std::uint64_t numPages;
std::uint64_t pageBase = 0;
std::uint64_t numPages = 0;
MemoryType type = MemoryType::USABLE;
MemoryRange& operator=(const MemoryRange&) noexcept = default;
@@ -51,11 +51,31 @@ struct MemoryRange
}
};
struct AllocatePagesOptions
{
std::size_t numPages = 1;
bool bigPages = false;
};
struct PageRange
{
std::uint64_t mBase = 0;
std::uint64_t numPages = 0;
explicit operator bool() const noexcept { return numPages > 0; }
bool operator!() const noexcept { return numPages == 0; }
[[nodiscard]] std::span<std::uint8_t> getMemory() const noexcept
{
std::uint8_t* basePtr = std::bit_cast<std::uint8_t*>(mBase << 12);
return {basePtr, basePtr + (numPages * 512)};
}
};
[[nodiscard]] bool setupPaging(std::span<const MemoryRange> memoryRanges) noexcept;
[[nodiscard]] const char* memoryTypeName(MemoryType type) noexcept;
[[nodiscard]] void* allocatePages(unsigned numConsecutive) noexcept;
[[nodiscard]] inline void* allocatePage() noexcept { return allocatePages(1); }
[[nodiscard]] PageRange allocatePages(const AllocatePagesOptions& options = {}) noexcept;
void setupIdentityPaging(std::uint64_t page) noexcept;
void setupIdentityPaging2M(std::uint64_t page) noexcept;
@@ -69,10 +89,16 @@ inline void identityMapRegion(void* start, std::size_t bytes) noexcept
}
template<typename T = void>
inline T* pageToPointer(std::uint64_t page) noexcept
[[nodiscard]] inline T* pageToPointer(std::uint64_t page) noexcept
{
return std::bit_cast<T*>(page << 12);
}
[[nodiscard]] inline std::size_t bytesToPages(std::size_t bytes, bool bigPages = false) noexcept
{
const std::size_t pageBytes = bigPages ? (2 << 20) : 4096;
return (bytes + pageBytes - 1) / pageBytes;
}
}
void __ba_initInitialHeap() noexcept;

View File

@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <stddef.h>
#include "os/memory.hpp"
#include "os/tty.hpp"
namespace
@@ -33,18 +34,6 @@ void __ba_initInitialHeap() noexcept
gNextBlock->nextBlock = nullptr;
}
void __ba_registerAllocatableMemory(void* memory, size_t size) noexcept
{
if (size < sizeof(max_align_t))
{
return;
}
MallocBlock* newBlock = static_cast<MallocBlock*>(memory);
newBlock->nextBlock = gNextBlock;
newBlock->elements = size / sizeof(max_align_t);
gNextBlock = newBlock;
}
extern "C"
{
void abort()
@@ -94,7 +83,21 @@ void* malloc(size_t size) noexcept
prevBlock = block;
}
return nullptr;
baos::PageRange pages = baos::allocatePages({
.numPages = std::max(1ul, baos::bytesToPages(size, /* bigPages = */ true)),
.bigPages = true
});
if (!pages) {
return nullptr;
}
std::span<std::uint8_t> memory = pages.getMemory();
MallocBlock* newBlock = std::bit_cast<MallocBlock*>(memory.begin());
newBlock->nextBlock = gNextBlock;
newBlock->elements = memory.size();
gNextBlock = newBlock;
return malloc(size);
}
void free(void* memory) noexcept

View File

@@ -188,7 +188,7 @@ bool allocateIdentityMappedPages(std::size_t numPages, std::size_t pageAlignment
if (!allocateMemoryRange(numPages, pageAlignment, page)) {
return false;
}
setupIdentityPaging2M(page);
setupIdentityPaging(page, page + numPages);
outPage = page;
return true;
}
@@ -272,6 +272,27 @@ const char* memoryTypeName(MemoryType type) noexcept
return "<INVALID>";
}
PageRange allocatePages(const AllocatePagesOptions& options) noexcept
{
if (options.numPages == 0) {
return {};
}
std::uint64_t page = 0;
const std::uint64_t numPages = options.numPages * (options.bigPages ? BIG_PAGE_SIZE : 1);
if (!allocateIdentityMappedPages(
/* numPages = */ numPages,
/* pageAlignment = */ options.bigPages ? BIG_PAGE_ALIGNMENT : 1,
/* outPage = */ page
))
{
return {};
}
return {
.mBase = page,
.numPages = numPages
};
}
void setupIdentityPaging(std::uint64_t page) noexcept
{
PageTableEntry& pageTableEntry = getOrCreatePage(page);