From ad00e53c0b60726b1bb4d7d7bf2e7c39c3c3d4cb Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Jul 2024 16:18:07 +0200 Subject: [PATCH] Fixed crashes when freeing memory from fixed size allocator. --- src/mx/memory/fixed_size_allocator.h | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/mx/memory/fixed_size_allocator.h b/src/mx/memory/fixed_size_allocator.h index b412bee..412e6bb 100644 --- a/src/mx/memory/fixed_size_allocator.h +++ b/src/mx/memory/fixed_size_allocator.h @@ -16,6 +16,7 @@ #include #include #include +#include namespace mx::memory::fixed { /** @@ -45,17 +46,18 @@ class Chunk { public: Chunk() noexcept = default; - explicit Chunk(void *memory) noexcept : _memory(memory) {} + explicit Chunk(void *memory, bool is_allocated) noexcept : _memory(memory), _is_allocated(is_allocated) {} ~Chunk() noexcept = default; static constexpr auto size() { return 4096 * 4096; /* 16mb */ } explicit operator void *() const noexcept { return _memory; } explicit operator std::uintptr_t() const noexcept { return reinterpret_cast(_memory); } - explicit operator bool() const noexcept { return _memory != nullptr; } + explicit operator bool() const noexcept { return _memory != nullptr && _is_allocated; } private: void *_memory{nullptr}; + bool _is_allocated{true}; }; /** @@ -116,7 +118,7 @@ public: */ Chunk allocate() noexcept { - const auto next_free_chunk = _next_free_chunk.fetch_add(1, std::memory_order_relaxed); + const auto next_free_chunk = _next_free_chunk.fetch_add(1, std::memory_order_relaxed); //todo: relax if (next_free_chunk < _free_chunk_buffer.size()) { return _free_chunk_buffer[next_free_chunk]; @@ -175,10 +177,11 @@ private: } auto *heap_memory = GlobalHeap::allocate(_numa_node_id, Chunk::size() * _free_chunk_buffer.size()); + assert(heap_memory != nullptr && "heap_memory is null"); auto heap_memory_address = reinterpret_cast(heap_memory); for (auto i = 0U; i < _free_chunk_buffer.size(); ++i) { - _free_chunk_buffer[i] = Chunk(reinterpret_cast(heap_memory_address + (i * Chunk::size()))); + _free_chunk_buffer[i] = Chunk(reinterpret_cast(heap_memory_address + (i * Chunk::size())), i==0); } _next_free_chunk.store(0U); @@ -281,13 +284,13 @@ public: { for (auto node_id = std::uint8_t(0U); node_id < config::max_numa_nodes(); ++node_id) { - if (core_set.has_core_of_numa_node(node_id)) - { + //if (core_set.has_core_of_numa_node(node_id)) + //{ _processor_heaps[node_id] = ProcessorHeap{node_id}; - } + //} } - for (const auto core_id : core_set) + for (unsigned core_id = 0; core_id < system::topology::count_cores(); core_id++) { const auto node_id = system::topology::node_id(core_id); _core_heaps[core_id] = CoreHeap{&_processor_heaps[node_id]}; @@ -302,7 +305,10 @@ public: * @param core_id ID of the core. * @return Allocated memory object. */ - [[nodiscard]] void *allocate(const std::uint16_t core_id) override { return _core_heaps[core_id].allocate(); } + [[nodiscard]] void *allocate(const std::uint16_t core_id) override { void *ptr = _core_heaps[core_id].allocate(); + assert(ptr != nullptr && "allocate returned null"); + return ptr; + } /** * Frees memory. @@ -319,4 +325,4 @@ private: // Map from core_id to core-local allocator. std::array, tasking::config::max_cores()> _core_heaps; }; -} // namespace mx::memory::fixed \ No newline at end of file +} // namespace mx::memory::fixed