Merge branch 'dev'

main
Brett 2024-08-19 20:02:47 -04:00
commit 58681803e9
4 changed files with 33 additions and 5 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.1.9) project(blt-gp VERSION 0.1.12)
include(CTest) include(CTest)

View File

@ -20,6 +20,7 @@
#define BLT_GP_STACK_H #define BLT_GP_STACK_H
#include <blt/std/types.h> #include <blt/std/types.h>
#include <blt/std/atomic_allocator.h>
#include <blt/std/assert.h> #include <blt/std/assert.h>
#include <blt/std/logging.h> #include <blt/std/logging.h>
#include <blt/std/allocator.h> #include <blt/std/allocator.h>
@ -42,13 +43,29 @@ namespace blt::gp
BLT_META_MAKE_FUNCTION_CHECK(drop); BLT_META_MAKE_FUNCTION_CHECK(drop);
} }
class aligned_allocator
{
public:
void* allocate(blt::size_t bytes) // NOLINT
{
return std::aligned_alloc(8, bytes);
}
void deallocate(void* ptr, blt::size_t) // NOLINT
{
std::free(ptr);
}
};
class stack_allocator class stack_allocator
{ {
constexpr static blt::size_t PAGE_SIZE = 0x1000; constexpr static blt::size_t PAGE_SIZE = 0x1000;
constexpr static blt::size_t MAX_ALIGNMENT = 8; constexpr static blt::size_t MAX_ALIGNMENT = 8;
template<typename T> template<typename T>
using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>; using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>;
using Allocator = aligned_allocator;
public: public:
static Allocator& get_allocator();
struct size_data_t struct size_data_t
{ {
blt::size_t total_size_bytes = 0; blt::size_t total_size_bytes = 0;
@ -104,7 +121,8 @@ namespace blt::gp
~stack_allocator() ~stack_allocator()
{ {
std::free(data_); //std::free(data_);
get_allocator().deallocate(data_, size_);
} }
void insert(const stack_allocator& stack) void insert(const stack_allocator& stack)
@ -252,10 +270,12 @@ namespace blt::gp
void expand(blt::size_t bytes) void expand(blt::size_t bytes)
{ {
bytes = to_nearest_page_size(bytes); bytes = to_nearest_page_size(bytes);
auto new_data = static_cast<blt::u8*>(std::malloc(bytes)); // auto new_data = static_cast<blt::u8*>(std::malloc(bytes));
auto new_data = static_cast<blt::u8*>(get_allocator().allocate(bytes));
if (bytes_stored > 0) if (bytes_stored > 0)
std::memcpy(new_data, data_, bytes_stored); std::memcpy(new_data, data_, bytes_stored);
std::free(data_); // std::free(data_);
get_allocator().deallocate(data_, size_);
data_ = new_data; data_ = new_data;
size_ = bytes; size_ = bytes;
} }
@ -310,6 +330,8 @@ namespace blt::gp
blt::size_t bytes_stored = 0; blt::size_t bytes_stored = 0;
blt::size_t size_ = 0; blt::size_t size_ = 0;
}; };
} }
#endif //BLT_GP_STACK_H #endif //BLT_GP_STACK_H

@ -1 +1 @@
Subproject commit 97990401e2332276b5397060a3ccaf19f07fb999 Subproject commit 72211e3d7b29cec887257d59578669f198c2d3da

View File

@ -50,6 +50,12 @@ namespace blt::gp
return random_engine; return random_engine;
} }
stack_allocator::Allocator& stack_allocator::get_allocator()
{
thread_local static Allocator allocator;
return allocator;
}
void gp_program::create_threads() void gp_program::create_threads()
{ {
if (config.threads == 0) if (config.threads == 0)