From f100e95a306e9a117d0a2f4d35e4e3e6208c70a0 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 24 Mar 2024 17:31:11 -0400 Subject: [PATCH] fix some issue with the allocator --- CMakeLists.txt | 2 +- include/blt/std/allocator.h | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8508b77..a323888 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.5) include(cmake/color.cmake) -set(BLT_VERSION 0.15.6) +set(BLT_VERSION 0.15.7) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/std/allocator.h b/include/blt/std/allocator.h index 2b31871..e5597aa 100644 --- a/include/blt/std/allocator.h +++ b/include/blt/std/allocator.h @@ -24,6 +24,8 @@ #include #include #include + // TODO: remove + //#include #include #include #include "logging.h" @@ -623,6 +625,7 @@ namespace blt private: stats_t stats; + //blt::hashset_t deletes; /** * Logging function used for handling mmap errors. call after a failed mmap call. @@ -695,7 +698,7 @@ namespace blt block* prev = nullptr; blt::u8* offset = nullptr; } metadata; - blt::u8 buffer[BLOCK_SIZE - sizeof(metadata)]{}; + blt::u8 buffer[BLOCK_SIZE - sizeof(block_metadata_t)]{}; block() { @@ -884,14 +887,30 @@ namespace blt #ifndef BLT_DISABLE_STATS stats.decrementBytes(sizeof(T) * count); #endif +// if (deletes.contains(p)) +// { +// BLT_FATAL("pointer %p has already been freed", p); +// throw std::bad_alloc(); +// }else +// deletes.insert(static_cast(p)); + auto blk = to_block(p); - if (--blk->metadata.allocated_objects == 0) + blk->metadata.allocated_objects--; + if (blk->metadata.allocated_objects == 0) { + //BLT_INFO("Deallocating block from %p in (1) %p current head %p, based: %p", p, blk, head, base); if (blk == base) - base = head = nullptr; - if (blk->metadata.prev != nullptr) + { + base = base->metadata.next; + // if they were equal (single allocated block) we also need to move the head forward + if (blk == head) + head = base; + } else if (blk == head) // else, need to make sure the head ptr gets moved back, otherwise we will use a head that has been freed + head = blk->metadata.prev; + else if (blk->metadata.prev != nullptr) // finally if it wasn't the head we need to bridge the gap in the list blk->metadata.prev->metadata.next = blk->metadata.next; + //BLT_INFO("Deallocating block from %p in (2) %p current head %p, based: %p", p, blk, head, base); delete_block(blk); } }