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..cbb8266821b 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 + 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 cee687a9d95..8aaa3387fb9 100644 --- a/iceoryx_hoofs/memory/source/bump_allocator.cpp +++ b/iceoryx_hoofs/memory/source/bump_allocator.cpp @@ -39,6 +39,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; @@ -72,4 +74,10 @@ void BumpAllocator::finalizeAllocation() noexcept m_allocationFinalized = true; } +void BumpAllocator::deallocate() noexcept +{ + 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 8834870ce72..ee96081a816 100644 --- a/iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp +++ b/iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp @@ -149,4 +149,42 @@ TEST_F(BumpAllocator_Test, allocateAfterFinalizeAllocation) // NOLINTEND(hicpp-avoid-goto, cppcoreguidelines-avoid-goto, cert-err33-c, cppcoreguidelines-pro-type-vararg, // hiccpp-vararg) } + +TEST_F(BumpAllocator_Test, allocateAfterDeallocateWorks) +{ + ::testing::Test::RecordProperty("TEST_ID", ""); + iox::BumpAllocator sut(memory, memorySize); + sut.allocate(memorySize, 1); + + sut.deallocate(); + + int* bla = static_cast(sut.allocate(memorySize, 1)); + *bla = 1990; + EXPECT_THAT(*bla, Eq(1990)); +} + +TEST_F(BumpAllocator_Test, allocateAfterFinalizeAllocationAndDeallocateWorks) +{ + ::testing::Test::RecordProperty("TEST_ID", ""); + class AllocatorAccess : iox::BumpAllocator + { + public: + AllocatorAccess(void* const startAddress, const uint64_t length) + : iox::BumpAllocator(startAddress, length) + { + } + using iox::BumpAllocator::allocate; + using iox::BumpAllocator::deallocate; + using iox::BumpAllocator::finalizeAllocation; + }; + AllocatorAccess sut(memory, memorySize); + sut.allocate(memorySize, 1); + sut.finalizeAllocation(); + + sut.deallocate(); + + int* bla = static_cast(sut.allocate(memorySize, 1)); + *bla = 32; + EXPECT_THAT(*bla, Eq(32)); +} } // namespace