From d40f741431af52c2ce23c4e7c42b24fe63c8df2a Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 4 Sep 2024 13:30:05 -0400 Subject: [PATCH] fix the stupid segfault using an ugly hack. TODO: make allocator owned by classes (silly) --- CMakeLists.txt | 2 +- examples/symbolic_regression.cpp | 10 +++++++ include/blt/gp/fwdecl.h | 50 +++++++++++++++++++------------- lib/blt | 2 +- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e3e9fe7..2a27092 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.1.46) +project(blt-gp VERSION 0.1.47) include(CTest) diff --git a/examples/symbolic_regression.cpp b/examples/symbolic_regression.cpp index 45b44a3..b51b6e4 100644 --- a/examples/symbolic_regression.cpp +++ b/examples/symbolic_regression.cpp @@ -44,6 +44,16 @@ blt::gp::prog_config_t config = blt::gp::prog_config_t() .set_max_generations(50) .set_pop_size(20000) .set_thread_count(0); +//blt::gp::prog_config_t config = blt::gp::prog_config_t() +// .set_initial_min_tree_size(2) +// .set_initial_max_tree_size(6) +// .set_elite_count(2) +// .set_crossover_chance(0.9) +// .set_mutation_chance(0.1) +// .set_reproduction_chance(0) +// .set_max_generations(50) +// .set_pop_size(500) +// .set_thread_count(0); blt::gp::gp_program program{SEED, config}; diff --git a/include/blt/gp/fwdecl.h b/include/blt/gp/fwdecl.h index 4d7afa3..c824622 100644 --- a/include/blt/gp/fwdecl.h +++ b/include/blt/gp/fwdecl.h @@ -149,24 +149,29 @@ namespace blt::gp (void) bytes; #endif std::scoped_lock lock(mutex); - auto blk = to_block(ptr); + block_t* blk = to_block(ptr); --blk->metadata.allocated_objects; if (blk->metadata.allocated_objects == 0) { - if (head == blk) - head = head->metadata.next; + if (blk->metadata.has_deallocated) + alloc.deallocate(blk, blk->metadata.size); else { - auto prev = head; - auto next = head->metadata.next; - while (next != blk) + if (head == blk) + head = head->metadata.next; + else { - prev = next; - next = next->metadata.next; + auto prev = head; + auto next = head->metadata.next; + while (next != blk) + { + prev = next; + next = next->metadata.next; + } + prev->metadata.next = next->metadata.next; } - prev->metadata.next = next->metadata.next; + deallocated_blocks.push_back(blk); } - deallocated_blocks.push_back(blk); } } @@ -174,13 +179,15 @@ namespace blt::gp { std::scoped_lock lock(mutex); for (auto* blk : deallocated_blocks) + { alloc.deallocate(blk, blk->metadata.size); + } auto cur = head; while (cur != nullptr) { auto* ptr = cur; + ptr->metadata.has_deallocated = true; cur = cur->metadata.next; - alloc.deallocate(ptr, ptr->metadata.size); } head = nullptr; } @@ -190,22 +197,24 @@ namespace blt::gp { struct block_metadata_t { - blt::size_t size = 0; - blt::size_t allocated_objects = 0; - block_t* next = nullptr; - blt::u8* offset = nullptr; + blt::size_t size; + blt::size_t allocated_objects : 63; + bool has_deallocated : 1; + block_t* next; + blt::u8* offset; } metadata; blt::u8 buffer[8]{}; - explicit block_t(blt::size_t size) + explicit block_t(blt::size_t size): metadata{size, 0, false, nullptr, nullptr} { - metadata.size = size; reset(); } void reset() { metadata.offset = buffer; + metadata.allocated_objects = 0; + metadata.next = nullptr; } [[nodiscard]] blt::ptrdiff_t storage_size() const noexcept @@ -232,6 +241,7 @@ namespace blt::gp void push_block(blt::size_t bytes) { auto blk = allocate_block(bytes); + BLT_TRACE("Allocated block %p", blk); blk->metadata.next = head; head = blk; } @@ -240,9 +250,9 @@ namespace blt::gp { if (!deallocated_blocks.empty()) { - auto blk = deallocated_blocks.back(); - blk->reset(); + block_t* blk = deallocated_blocks.back(); deallocated_blocks.pop_back(); + blk->reset(); return blk; } auto size = align_size_to(bytes + sizeof(typename block_t::block_metadata_t), default_block_size); @@ -255,8 +265,8 @@ namespace blt::gp block_t* head = nullptr; std::mutex mutex; std::vector deallocated_blocks; - Alloc alloc; blt::size_t default_block_size; + Alloc alloc; }; template diff --git a/lib/blt b/lib/blt index a7645d9..82cc1af 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit a7645d9ddec57ecaad525b48a30f8001adcf75e8 +Subproject commit 82cc1aff9688e1917e261bd341178562f37e190a