Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1732 Implement deallocate method
Browse files Browse the repository at this point in the history
Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Jan 16, 2023
1 parent cd97eb4 commit 5ed71ff
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ void* allocate(byte_t* startAddress, uint64_t Capacity)
uint64_t alignedPosition = cxx::align(reinterpret_cast<uint64_t>(startAddress), alignof(T));
cxx::Expects(alignedPosition + sizeof(T) - reinterpret_cast<uint64_t>(startAddress) < Capacity
&& "type does not fit into storage");
// cxx::Expects(alignedPosition + sizeof(T) < Capacity && "type does not fit into storage");
return reinterpret_cast<void*>(alignedPosition);
}

Expand Down
5 changes: 4 additions & 1 deletion iceoryx_hoofs/memory/include/iox/bump_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions iceoryx_hoofs/memory/source/bump_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(m_startAddress) + m_currentPosition;
Expand Down Expand Up @@ -72,4 +74,10 @@ void BumpAllocator::finalizeAllocation() noexcept
m_allocationFinalized = true;
}

void BumpAllocator::deallocate() noexcept
{
m_currentPosition = 0;
m_allocationFinalized = false;
}

} // namespace iox
38 changes: 38 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int*>(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<int*>(sut.allocate(memorySize, 1));
*bla = 32;
EXPECT_THAT(*bla, Eq(32));
}
} // namespace

0 comments on commit 5ed71ff

Please sign in to comment.