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 Dec 1, 2022
1 parent 6786ba4 commit 2bca7d5
Show file tree
Hide file tree
Showing 4 changed files with 18 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
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:
Expand Down
10 changes: 10 additions & 0 deletions iceoryx_hoofs/memory/source/bump_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(m_startAddress) + m_currentPosition;
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2bca7d5

Please sign in to comment.