Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1732 Make Capacity second template parameter of a…
Browse files Browse the repository at this point in the history
…llocate

Correct required storage size, rename some variables

Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Jan 16, 2023
1 parent bb22222 commit f00972a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,13 @@ inline void swap(storable_function<Capacity, T>& f, storable_function<Capacity,
f.swap(g);
}

template <typename T>
void* allocate(byte_t* startAddress, uint64_t Capacity)
template <typename T, uint64_t Capacity>
void* allocate(byte_t* startAddress)
{
static_assert(sizeof(T) + alignof(T) - 1 <= Capacity, "type does not fit into storage");
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast, performance-no-int-to-ptr) required for low level pointer alignment
uint64_t alignment = alignof(T);
uint64_t alignedPosition = cxx::align(reinterpret_cast<uint64_t>(startAddress), alignment);
cxx::Expects(alignedPosition + sizeof(T) - reinterpret_cast<uint64_t>(startAddress) < Capacity
&& "type does not fit into storage");
return reinterpret_cast<void*>(alignedPosition);
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast, performance-no-int-to-ptr) required for low level pointer alignment
}
Expand All @@ -189,7 +188,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, Capacity);
m_callable = allocate<StoredType, Capacity>(m_storage);

// erase the functor type and store as reference to the call in storage
new (m_callable) StoredType(functor);
Expand All @@ -206,7 +205,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, Capacity);
dest.m_callable = allocate<CallableType, Capacity>(dest.m_storage);

// 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 @@ -224,7 +223,7 @@ template <typename CallableType>
inline void storable_function<Capacity, signature<ReturnType, Args...>>::move(storable_function& src,
storable_function& dest) noexcept
{
dest.m_callable = allocate<CallableType>(dest.m_storage, Capacity);
dest.m_callable = allocate<CallableType, Capacity>(dest.m_storage);

// 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 @@ -311,7 +310,7 @@ 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 sizeof(T) + alignof(T);
return sizeof(T) + alignof(T) - 1;
}

template <uint64_t Capacity, typename ReturnType, typename... Args>
Expand Down
3 changes: 2 additions & 1 deletion iceoryx_hoofs/memory/include/iox/bump_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ enum class BumpAllocatorError
REQUESTED_ZERO_SIZED_MEMORY
};

/// @brief A bump allocator for the memory provided in the ctor arguments
class BumpAllocator
{
public:
/// @brief A bump allocator for the memory provided in the ctor arguments
/// @brief c'tor
/// @param[in] startAddress of the memory this allocator manages
/// @param[in] length of the memory this allocator manages
BumpAllocator(void* const startAddress, const uint64_t length) noexcept;
Expand Down
6 changes: 3 additions & 3 deletions iceoryx_hoofs/memory/source/bump_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ cxx::expected<void*, BumpAllocatorError> BumpAllocator::allocate(const uint64_t
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for low level pointer alignment
alignedPosition -= reinterpret_cast<uint64_t>(m_startAddress);

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

if (m_length >= alignedPosition + size)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) low-level memory management
l_returnValue = m_startAddress + alignedPosition;
allocation = m_startAddress + alignedPosition;
m_currentPosition = alignedPosition + size;
}
else
Expand All @@ -63,7 +63,7 @@ cxx::expected<void*, BumpAllocatorError> BumpAllocator::allocate(const uint64_t
return cxx::error<BumpAllocatorError>(BumpAllocatorError::OUT_OF_MEMORY);
}

return cxx::success<void*>(l_returnValue);
return cxx::success<void*>(allocation);
}

void BumpAllocator::deallocate() noexcept
Expand Down
3 changes: 1 addition & 2 deletions iceoryx_hoofs/source/posix_wrapper/shared_memory_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ cxx::expected<void*, SharedMemoryAllocationError> SharedMemoryObject::allocate(c
}
if (m_allocationFinalized)
{
IOX_LOG(WARN) << "allocate() call after finalizeAllocation()! You are not allowed to acquire shared memory "
"chunks anymore.";
IOX_LOG(WARN) << "allocate() call after finalizeAllocation()! Could not acquire shared memory chunk.";
return cxx::error<SharedMemoryAllocationError>(
SharedMemoryAllocationError::REQUESTED_MEMORY_AFTER_FINALIZED_ALLOCATION);
}
Expand Down
6 changes: 3 additions & 3 deletions iceoryx_hoofs/test/moduletests/test_cxx_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ TEST_F(function_test, FunctionStateIsIndependentOfSource)

// NOLINTNEXTLINE(hicpp-no-malloc, cppcoreguidelines-no-malloc) low-level memory management for testing purpose
void* memory = malloc(MEMORY_SIZE);
iox::BumpAllocator storage(memory, MEMORY_SIZE);
auto allocationResult = storage.allocate(sizeof(Functor), alignof(Functor));
iox::BumpAllocator allocator(memory, MEMORY_SIZE);
auto allocationResult = allocator.allocate(sizeof(Functor), alignof(Functor));
ASSERT_FALSE(allocationResult.has_error());

auto* p = static_cast<Functor*>(allocationResult.value());
Expand Down Expand Up @@ -556,7 +556,7 @@ TEST_F(function_test, CallWithRValueReferenceArgumentsWorks)
Arg arg(initial);

auto lambda = [](Arg&& a) { return a.value + 1; };
iox::cxx::function<int32_t(Arg &&), 128> sut(lambda);
iox::cxx::function<int32_t(Arg&&), 128> sut(lambda);

auto result = sut(std::move(arg));

Expand Down

0 comments on commit f00972a

Please sign in to comment.