fix the stupid segfault using an ugly hack. TODO: make allocator owned by classes (silly)

pages
Brett 2024-09-04 13:30:05 -04:00
parent 20e41f34cc
commit d40f741431
4 changed files with 42 additions and 22 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.46) project(blt-gp VERSION 0.1.47)
include(CTest) include(CTest)

View File

@ -44,6 +44,16 @@ blt::gp::prog_config_t config = blt::gp::prog_config_t()
.set_max_generations(50) .set_max_generations(50)
.set_pop_size(20000) .set_pop_size(20000)
.set_thread_count(0); .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}; blt::gp::gp_program program{SEED, config};

View File

@ -149,9 +149,13 @@ namespace blt::gp
(void) bytes; (void) bytes;
#endif #endif
std::scoped_lock lock(mutex); std::scoped_lock lock(mutex);
auto blk = to_block(ptr); block_t* blk = to_block(ptr);
--blk->metadata.allocated_objects; --blk->metadata.allocated_objects;
if (blk->metadata.allocated_objects == 0) if (blk->metadata.allocated_objects == 0)
{
if (blk->metadata.has_deallocated)
alloc.deallocate(blk, blk->metadata.size);
else
{ {
if (head == blk) if (head == blk)
head = head->metadata.next; head = head->metadata.next;
@ -169,18 +173,21 @@ namespace blt::gp
deallocated_blocks.push_back(blk); deallocated_blocks.push_back(blk);
} }
} }
}
~variable_bump_allocator() ~variable_bump_allocator()
{ {
std::scoped_lock lock(mutex); std::scoped_lock lock(mutex);
for (auto* blk : deallocated_blocks) for (auto* blk : deallocated_blocks)
{
alloc.deallocate(blk, blk->metadata.size); alloc.deallocate(blk, blk->metadata.size);
}
auto cur = head; auto cur = head;
while (cur != nullptr) while (cur != nullptr)
{ {
auto* ptr = cur; auto* ptr = cur;
ptr->metadata.has_deallocated = true;
cur = cur->metadata.next; cur = cur->metadata.next;
alloc.deallocate(ptr, ptr->metadata.size);
} }
head = nullptr; head = nullptr;
} }
@ -190,22 +197,24 @@ namespace blt::gp
{ {
struct block_metadata_t struct block_metadata_t
{ {
blt::size_t size = 0; blt::size_t size;
blt::size_t allocated_objects = 0; blt::size_t allocated_objects : 63;
block_t* next = nullptr; bool has_deallocated : 1;
blt::u8* offset = nullptr; block_t* next;
blt::u8* offset;
} metadata; } metadata;
blt::u8 buffer[8]{}; 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(); reset();
} }
void reset() void reset()
{ {
metadata.offset = buffer; metadata.offset = buffer;
metadata.allocated_objects = 0;
metadata.next = nullptr;
} }
[[nodiscard]] blt::ptrdiff_t storage_size() const noexcept [[nodiscard]] blt::ptrdiff_t storage_size() const noexcept
@ -232,6 +241,7 @@ namespace blt::gp
void push_block(blt::size_t bytes) void push_block(blt::size_t bytes)
{ {
auto blk = allocate_block(bytes); auto blk = allocate_block(bytes);
BLT_TRACE("Allocated block %p", blk);
blk->metadata.next = head; blk->metadata.next = head;
head = blk; head = blk;
} }
@ -240,9 +250,9 @@ namespace blt::gp
{ {
if (!deallocated_blocks.empty()) if (!deallocated_blocks.empty())
{ {
auto blk = deallocated_blocks.back(); block_t* blk = deallocated_blocks.back();
blk->reset();
deallocated_blocks.pop_back(); deallocated_blocks.pop_back();
blk->reset();
return blk; return blk;
} }
auto size = align_size_to(bytes + sizeof(typename block_t::block_metadata_t), default_block_size); 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; block_t* head = nullptr;
std::mutex mutex; std::mutex mutex;
std::vector<block_t*> deallocated_blocks; std::vector<block_t*> deallocated_blocks;
Alloc alloc;
blt::size_t default_block_size; blt::size_t default_block_size;
Alloc alloc;
}; };
template<typename T> template<typename T>

@ -1 +1 @@
Subproject commit a7645d9ddec57ecaad525b48a30f8001adcf75e8 Subproject commit 82cc1aff9688e1917e261bd341178562f37e190a