From 45db2acb11777178a01d85d9678edd8070228bb4 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 16 Jul 2024 21:56:21 -0400 Subject: [PATCH] fix leaks --- CMakeLists.txt | 2 +- include/blt/gp/program.h | 1 + include/blt/gp/stack.h | 44 ++++++++++++++++++++++++++++------------ src/program.cpp | 7 ++++++- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 54c0bbb..a53aaa3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.86) +project(blt-gp VERSION 0.0.87) include(CTest) diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index f6bdb7e..738471a 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -491,6 +491,7 @@ namespace blt::gp { thread_helper.lifetime_over = true; thread_helper.barrier.notify_all(); + thread_helper.thread_function_condition.notify_all(); for (auto& thread : thread_helper.threads) { if (thread->joinable()) diff --git a/include/blt/gp/stack.h b/include/blt/gp/stack.h index 61fd2f2..88acd32 100644 --- a/include/blt/gp/stack.h +++ b/include/blt/gp/stack.h @@ -206,10 +206,11 @@ namespace blt::gp using NO_REF_T = std::remove_cv_t>; static_assert(std::is_trivially_copyable_v && "Type must be bitwise copyable!"); constexpr static auto TYPE_SIZE = aligned_size(); - if (head == nullptr) + + while (head->used_bytes_in_block() == 0 && move_back()); + if (empty()) throw std::runtime_error("Silly boi the stack is empty!"); - if (head->used_bytes_in_block() == 0) - move_back(); + if (head->used_bytes_in_block() < static_cast(aligned_size())) throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string( head->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(NO_REF_T))).c_str()); @@ -298,10 +299,10 @@ namespace blt::gp */ void transfer_bytes(stack_allocator& to, blt::size_t bytes) { - while (!empty() && head->used_bytes_in_block() == 0) - move_back(); + while (head->used_bytes_in_block() == 0 && move_back()); if (empty()) throw std::runtime_error("This stack is empty!"); + if (head->used_bytes_in_block() < static_cast(bytes)) BLT_ABORT(("This stack doesn't contain enough data for this type! " + std::to_string(head->used_bytes_in_block()) + " / " + std::to_string(bytes) + " This is an invalid runtime state!").c_str()); @@ -318,8 +319,8 @@ namespace blt::gp { blt::size_t offset = 0; - ((from < NO_REF_T < Args >> (offset).~NO_REF_T(), offset += stack_allocator::aligned_size> - ()), ...); + ((from>(offset).~NO_REF_T(), offset += stack_allocator::aligned_size> + ()), ...); } [[nodiscard]] bool empty() const @@ -418,6 +419,16 @@ namespace blt::gp ~stack_allocator() { free_chain(head); + if (head != nullptr) + { + auto blk = head->metadata.next; + while (blk != nullptr) + { + auto ptr = blk; + blk = blk->metadata.next; + std::free(ptr); + } + } } template @@ -480,7 +491,7 @@ namespace blt::gp template void* allocate_bytes() { - return allocate_bytes(sizeof(NO_REF_T < T > )); + return allocate_bytes(sizeof(NO_REF_T)); } void* allocate_bytes(blt::size_t size) @@ -488,13 +499,16 @@ namespace blt::gp auto ptr = get_aligned_pointer(size); if (ptr == nullptr) { - if (head != nullptr && head->metadata.next != nullptr) + while (head != nullptr && head->metadata.next != nullptr) { head = head->metadata.next; if (head != nullptr) head->reset(); - } else - push_block(aligned_size(size)); + if (head->remaining_bytes_in_block() >= static_cast(size)) + break; + } + if (head == nullptr || head->remaining_bytes_in_block() < static_cast(size)) + push_block(aligned_size(size) + sizeof(typename block::block_metadata_t)); } ptr = get_aligned_pointer(size); if (ptr == nullptr) @@ -532,7 +546,7 @@ namespace blt::gp static block* allocate_block(blt::size_t bytes) { - auto size = to_nearest_page_size(bytes + sizeof(typename block::block_metadata_t)); + auto size = to_nearest_page_size(bytes); auto* data = std::aligned_alloc(PAGE_SIZE, size); //auto* data = get_allocator().allocate(size); new(data) block{size}; @@ -550,12 +564,16 @@ namespace blt::gp } } - inline void move_back() + inline bool move_back() { auto old = head; head = head->metadata.prev; if (head == nullptr) + { head = old; + return false; + } + return true; //free_chain(old); // required to prevent silly memory :3 // if (head != nullptr) diff --git a/src/program.cpp b/src/program.cpp index fda45e6..3e679b7 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -64,7 +64,12 @@ namespace blt::gp if (execution_function == nullptr) { std::unique_lock lock(thread_helper.thread_function_control); - thread_helper.thread_function_condition.wait(lock, [this]() { return thread_execution_service != nullptr; }); + while (thread_execution_service == nullptr) + { + thread_helper.thread_function_condition.wait(lock); + if (should_thread_terminate()) + return; + } execution_function = thread_execution_service.load(std::memory_order_acquire); } if (execution_function != nullptr)