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)
project(blt-gp VERSION 0.1.46)
project(blt-gp VERSION 0.1.47)
include(CTest)

View File

@ -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};

View File

@ -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<block_t*> deallocated_blocks;
Alloc alloc;
blt::size_t default_block_size;
Alloc alloc;
};
template<typename T>

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