From 2bca7d5948e44df4bfbc469699e2d53fc7577492 Mon Sep 17 00:00:00 2001 From: Marika Lehmann Date: Thu, 1 Dec 2022 19:10:10 +0100 Subject: [PATCH] iox-#1732 Implement deallocate method Signed-off-by: Marika Lehmann --- .../iceoryx_hoofs/internal/cxx/storable_function.inl | 1 - iceoryx_hoofs/memory/include/iox/bump_allocator.hpp | 5 ++++- iceoryx_hoofs/memory/source/bump_allocator.cpp | 10 ++++++++++ .../test/moduletests/test_memory_bump_allocator.cpp | 4 ++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/storable_function.inl b/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/storable_function.inl index bbb898e181a..d944c384fe5 100644 --- a/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/storable_function.inl +++ b/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/storable_function.inl @@ -173,7 +173,6 @@ void* allocate(byte_t* startAddress, uint64_t Capacity) uint64_t alignedPosition = cxx::align(reinterpret_cast(startAddress), alignof(T)); cxx::Expects(alignedPosition + sizeof(T) - reinterpret_cast(startAddress) < Capacity && "type does not fit into storage"); - // cxx::Expects(alignedPosition + sizeof(T) < Capacity && "type does not fit into storage"); return reinterpret_cast(alignedPosition); } diff --git a/iceoryx_hoofs/memory/include/iox/bump_allocator.hpp b/iceoryx_hoofs/memory/include/iox/bump_allocator.hpp index 0fe5a59fd83..dde3d30f95b 100644 --- a/iceoryx_hoofs/memory/include/iox/bump_allocator.hpp +++ b/iceoryx_hoofs/memory/include/iox/bump_allocator.hpp @@ -48,9 +48,12 @@ class BumpAllocator /// @note May terminate if out of memory or finalizeAllocation() was called before void* allocate(const uint64_t size, const uint64_t alignment) noexcept; + /// @brief mark the memory as unused + constexpr void deallocate() noexcept; + protected: friend class posix::SharedMemoryObject; - // make free function; destructive move? + // make free function; destructive move? - then we would not be able to deallocate and allocate again afterwards void finalizeAllocation() noexcept; private: diff --git a/iceoryx_hoofs/memory/source/bump_allocator.cpp b/iceoryx_hoofs/memory/source/bump_allocator.cpp index 4e34f675b46..67608a35237 100644 --- a/iceoryx_hoofs/memory/source/bump_allocator.cpp +++ b/iceoryx_hoofs/memory/source/bump_allocator.cpp @@ -38,6 +38,8 @@ void* BumpAllocator::allocate(const uint64_t size, const uint64_t alignment) noe cxx::Expects( !m_allocationFinalized && "allocate() call after finalizeAllocation()! You are not allowed to acquire shared memory chunks anymore"); + // return a nullptr instead of terminate? Then this could be checked in SharedMemoryObject and the log messages + // could be printed there... // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for low level pointer alignment uint64_t currentAddress = reinterpret_cast(m_startAddress) + m_currentPosition; @@ -71,4 +73,12 @@ void BumpAllocator::finalizeAllocation() noexcept m_allocationFinalized = true; } +constexpr void BumpAllocator::deallocate() noexcept +{ + m_startAddress = nullptr; + m_length = 0; + m_currentPosition = 0; + m_allocationFinalized = false; +} + } // namespace iox diff --git a/iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp b/iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp index bafa1c378de..e4f0dc640c6 100644 --- a/iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp +++ b/iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp @@ -153,4 +153,8 @@ TEST_F(BumpAllocator_Test, allocateAfterFinalizeAllocation) // NOLINTEND(hicpp-avoid-goto, cppcoreguidelines-avoid-goto, cert-err33-c, cppcoreguidelines-pro-type-vararg, // hiccpp-vararg) } + +// missing tests: +// deallocate sets values to null +// deallocate after finalizeAllocation works + allocation is possible again } // namespace