Brett 2024-09-04 02:42:45 -04:00
parent a7645d9dde
commit 82cc1aff96
5 changed files with 111 additions and 78 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake)
set(BLT_VERSION 0.19.5)
set(BLT_VERSION 0.19.6)
set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT)

View File

@ -31,6 +31,12 @@
#include "logging.h"
#include <cstdlib>
#ifdef __unix__
#include <sys/mman.h>
#endif
namespace blt
{
@ -548,7 +554,7 @@ namespace blt
if constexpr (WARN_ON_FAIL)
{
BLT_WARN_STREAM << "We failed to allocate huge pages\n";
handle_mmap_error(BLT_WARN_STREAM);
BLT_WARN_STREAM << handle_mmap_error();
BLT_WARN_STREAM << "\033[1;31mYou should attempt to enable "
"huge pages as this will allocate normal pages and double the memory usage!\033[22m\n";
}
@ -557,8 +563,7 @@ namespace blt
if (buffer == MAP_FAILED)
{
BLT_ERROR_STREAM << "Failed to allocate normal pages\n";
handle_mmap_error(BLT_ERROR_STREAM);
throw std::bad_alloc();
throw bad_alloc_t(handle_mmap_error());
}
if constexpr (WARN_ON_FAIL)
{
@ -768,7 +773,7 @@ namespace blt
if (munmap(p, BLOCK_SIZE))
{
BLT_ERROR_STREAM << "FAILED TO DEALLOCATE BLOCK\n";
handle_mmap_error(BLT_ERROR_STREAM);
throw bad_alloc_t(handle_mmap_error());
}
} else
free(p);

View File

@ -23,22 +23,43 @@
#include <blt/std/types.h>
#include <cstdlib>
#ifdef __unix__
#include <sys/mman.h>
#endif
// size of 2mb in bytes
inline constexpr blt::size_t BLT_2MB_SIZE = 4096 * 512;
inline constexpr blt::size_t BLT_2MB_SIZE = 2048 * 1024;
inline constexpr blt::size_t BLT_1GB_SIZE = 1048576 * 1024;
namespace blt
{
enum class huge_page_t : blt::u64
{
BLT_2MB_PAGE,
BLT_1GB_PAGE
};
class bad_alloc_t : public std::exception
{
public:
bad_alloc_t() = default;
explicit bad_alloc_t(std::string_view str): str(str)
{}
explicit bad_alloc_t(std::string str): str(std::move(str))
{}
[[nodiscard]] const char* what() const noexcept override
{
return str.c_str();
}
private:
std::string str;
};
/**
* Logging function used for handling mmap errors. call after a failed mmap call.
*/
void handle_mmap_error(blt::logging::logger func);
std::string handle_mmap_error();
inline static constexpr blt::size_t align_size_to(blt::size_t size, blt::size_t align)
{
@ -46,16 +67,16 @@ namespace blt
return (size & MASK) + align;
}
void* allocate_2mb_huge_pages(blt::size_t bytes);
void* allocate_huge_pages(huge_page_t page_type, blt::size_t bytes);
void mmap_free(void* ptr, blt::size_t bytes);
class mmap_huge_allocator
{
public:
void* allocate(blt::size_t bytes) // NOLINT
void* allocate(blt::size_t bytes, huge_page_t page_type) // NOLINT
{
return allocate_2mb_huge_pages(bytes);
return allocate_huge_pages(page_type, bytes);
}
void deallocate(void* ptr, blt::size_t bytes) // NOLINT

View File

@ -17,13 +17,19 @@
*/
#include <blt/std/mmap.h>
#ifdef __unix__
#include <sys/mman.h>
#endif
namespace blt
{
void handle_mmap_error(blt::logging::logger func)
std::string handle_mmap_error()
{
{
#define BLT_WRITE(arg) func << arg << '\n';
std::string str;
#define BLT_WRITE(arg) str += arg; str += '\n';
switch (errno)
{
case EACCES:
@ -76,18 +82,20 @@ namespace blt
BLT_WRITE("MAP_DENYWRITE was set but the object specified by fd is open for writing.");
break;
}
}
return str;
}
void* allocate_2mb_huge_pages(blt::size_t bytes)
void* allocate_huge_pages(huge_page_t page_type, blt::size_t bytes)
{
#ifdef __unix__
auto buffer = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, -1, 0);
auto type = (21 << MAP_HUGE_SHIFT);
if (page_type == huge_page_t::BLT_1GB_PAGE)
type = (30 << MAP_HUGE_SHIFT);
auto buffer = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | type | MAP_POPULATE, -1, 0);
if (buffer == MAP_FAILED)
{
BLT_WARN_STREAM << "Failed to allocate huge pages, reason:\n";
handle_mmap_error(BLT_WARN_STREAM);
throw std::bad_alloc();
throw bad_alloc_t(handle_mmap_error());
}
return buffer;
#else
@ -100,8 +108,7 @@ namespace blt
if (munmap(ptr, bytes))
{
BLT_ERROR_STREAM << "Failed to deallocate\n";
handle_mmap_error(BLT_ERROR_STREAM);
throw std::bad_alloc();
throw bad_alloc_t(handle_mmap_error());
}
}
}