Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1732 Remove alignment constant, fix allocate func…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Jan 16, 2023
1 parent e50d063 commit cd97eb4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,13 @@ inline void swap(storable_function<Capacity, T>& f, storable_function<Capacity,
}

template <typename T>
void* allocate(byte_t* startAddress)
void* allocate(byte_t* startAddress, uint64_t Capacity)
{
// static_assert: startAddress + size < 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 All @@ -179,7 +182,7 @@ template <typename Functor, typename>
inline void storable_function<Capacity, signature<ReturnType, Args...>>::storeFunctor(const Functor& functor) noexcept
{
using StoredType = typename std::remove_reference<Functor>::type;
m_callable = allocate<StoredType>(m_storage);
m_callable = allocate<StoredType>(m_storage, Capacity);

// erase the functor type and store as reference to the call in storage
new (m_callable) StoredType(functor);
Expand All @@ -196,7 +199,7 @@ template <typename CallableType>
inline void storable_function<Capacity, signature<ReturnType, Args...>>::copy(const storable_function& src,
storable_function& dest) noexcept
{
dest.m_callable = allocate<CallableType>(dest.m_storage);
dest.m_callable = allocate<CallableType>(dest.m_storage, Capacity);

// AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type
const auto obj = static_cast<CallableType*>(src.m_callable);
Expand All @@ -214,7 +217,7 @@ inline void storable_function<Capacity, signature<ReturnType, Args...>>::move(st
storable_function& dest) noexcept
{
// m_callable = static_cast<void*>(align(reinterpret_cast<uint64_t>(m_storage), alignof(CallableType)));
dest.m_callable = allocate<CallableType>(dest.m_storage);
dest.m_callable = allocate<CallableType>(dest.m_storage, Capacity);

// AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type
const auto obj = static_cast<CallableType*>(src.m_callable);
Expand Down Expand Up @@ -299,7 +302,8 @@ template <uint64_t Capacity, typename ReturnType, typename... Args>
template <typename T>
inline constexpr uint64_t storable_function<Capacity, signature<ReturnType, Args...>>::required_storage_size() noexcept
{
return StorageType::template allocation_size<T>();
// return StorageType::template allocation_size<T>();
return sizeof(T) + alignof(T);
}

template <uint64_t Capacity, typename ReturnType, typename... Args>
Expand Down
10 changes: 4 additions & 6 deletions iceoryx_hoofs/memory/include/iox/bump_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
#ifndef IOX_HOOFS_MEMORY_BUMP_ALLOCATOR_HPP
#define IOX_HOOFS_MEMORY_BUMP_ALLOCATOR_HPP

#include "iceoryx_hoofs/iceoryx_hoofs_types.hpp"

#include <cstdint>

namespace iox
{
namespace posix
Expand All @@ -27,12 +30,7 @@ class SharedMemoryObject;

class BumpAllocator
{
using byte_t = uint8_t;


public:
// remove:
static constexpr uint64_t MEMORY_ALIGNMENT = 8U;
/// @brief A bump allocator for the memory provided in the ctor arguments
/// @param[in] startAddress of the memory this allocator manages
/// @param[in] length of the memory this allocator manages
Expand All @@ -56,7 +54,7 @@ class BumpAllocator
void finalizeAllocation() noexcept;

private:
byte_t* m_startAddress{nullptr};
cxx::byte_t* m_startAddress{nullptr};
uint64_t m_length{0U};
uint64_t m_currentPosition = 0U;
bool m_allocationFinalized = false;
Expand Down
5 changes: 2 additions & 3 deletions iceoryx_hoofs/memory/source/bump_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@

namespace iox
{
constexpr uint64_t BumpAllocator::MEMORY_ALIGNMENT;
BumpAllocator::BumpAllocator(void* const startAddress, const uint64_t length) noexcept
: m_startAddress(static_cast<byte_t*>(startAddress))
: m_startAddress(static_cast<cxx::byte_t*>(startAddress))
, m_length(length)
{
}
Expand All @@ -48,7 +47,7 @@ void* BumpAllocator::allocate(const uint64_t size, const uint64_t alignment) noe
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for low level pointer alignment
alignedPosition -= reinterpret_cast<uint64_t>(m_startAddress);

byte_t* l_returnValue = nullptr;
cxx::byte_t* l_returnValue = nullptr;

if (m_length >= alignedPosition + size)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class SharedMemoryObject_Test : public Test
// NOLINTEND(hicpp-avoid-goto, cppcoreguidelines-avoid-goto, cert-err33-c, cppcoreguidelines-pro-type-vararg,
// hiccpp-vararg)
}

static constexpr uint64_t MEMORY_ALIGNMENT{8};
};

TEST_F(SharedMemoryObject_Test, CTorWithValidArguments)
Expand Down Expand Up @@ -141,7 +143,7 @@ TEST_F(SharedMemoryObject_Test, AllocateTooMuchMemoryInSharedMemoryWithOneChunk)

ASSERT_THAT(sut.has_error(), Eq(false));

PerformDeathTest([&] { sut->allocate(cxx::align(memorySize, BumpAllocator::MEMORY_ALIGNMENT) + 1, 1); });
PerformDeathTest([&] { sut->allocate(cxx::align(memorySize, MEMORY_ALIGNMENT) + 1, 1); });
}

TEST_F(SharedMemoryObject_Test, AllocateTooMuchSharedMemoryWithMultipleChunks)
Expand All @@ -158,7 +160,7 @@ TEST_F(SharedMemoryObject_Test, AllocateTooMuchSharedMemoryWithMultipleChunks)

ASSERT_THAT(sut.has_error(), Eq(false));

for (uint64_t i = 0; i < cxx::align(memorySize, BumpAllocator::MEMORY_ALIGNMENT); ++i)
for (uint64_t i = 0; i < cxx::align(memorySize, MEMORY_ALIGNMENT); ++i)
{
void* test = sut->allocate(1, 1);
ASSERT_THAT(test, Ne(nullptr));
Expand Down

0 comments on commit cd97eb4

Please sign in to comment.