diff --git a/CMakeLists.txt b/CMakeLists.txt index 5478de5..7a83c93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.1.10) +project(blt-gp VERSION 0.1.11) include(CTest) diff --git a/examples/symbolic_regression.cpp b/examples/symbolic_regression.cpp index d79e549..08364fc 100644 --- a/examples/symbolic_regression.cpp +++ b/examples/symbolic_regression.cpp @@ -86,10 +86,6 @@ float example_function(float x) int main() { - BLT_TRACE("Hello %ld", blt::align_size_to(532, 8)); - BLT_TRACE("Align %ld", blt::gp::stack_allocator::aligned_size(532)); - return 0; - BLT_INFO("Starting BLT-GP Symbolic Regression Example"); BLT_START_INTERVAL("Symbolic Regression", "Main"); BLT_DEBUG("Setup Fitness cases"); diff --git a/include/blt/gp/stack.h b/include/blt/gp/stack.h index be3a9fa..7d4e5df 100644 --- a/include/blt/gp/stack.h +++ b/include/blt/gp/stack.h @@ -43,13 +43,29 @@ namespace blt::gp 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 { constexpr static blt::size_t PAGE_SIZE = 0x1000; constexpr static blt::size_t MAX_ALIGNMENT = 8; template using NO_REF_T = std::remove_cv_t>; + using Allocator = aligned_allocator; public: + static Allocator& get_allocator(); struct size_data_t { blt::size_t total_size_bytes = 0; @@ -105,7 +121,8 @@ namespace blt::gp ~stack_allocator() { - std::free(data_); + //std::free(data_); + get_allocator().deallocate(data_, size_); } void insert(const stack_allocator& stack) @@ -253,10 +270,12 @@ namespace blt::gp void expand(blt::size_t bytes) { bytes = to_nearest_page_size(bytes); - auto new_data = static_cast(std::malloc(bytes)); +// auto new_data = static_cast(std::malloc(bytes)); + auto new_data = static_cast(get_allocator().allocate(bytes)); if (bytes_stored > 0) std::memcpy(new_data, data_, bytes_stored); - std::free(data_); +// std::free(data_); + get_allocator().deallocate(data_, size_); data_ = new_data; size_ = bytes; } @@ -311,6 +330,8 @@ namespace blt::gp blt::size_t bytes_stored = 0; blt::size_t size_ = 0; }; + + } #endif //BLT_GP_STACK_H diff --git a/src/program.cpp b/src/program.cpp index 9f4b023..b5e01be 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -50,6 +50,12 @@ namespace blt::gp return random_engine; } + stack_allocator::Allocator& stack_allocator::get_allocator() + { + thread_local static Allocator allocator; + return allocator; + } + void gp_program::create_threads() { if (config.threads == 0)