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;