From 6786ba4e1e8efe5f07a0bf1e6f1aa096aa7097b1 Mon Sep 17 00:00:00 2001 From: Marika Lehmann Date: Wed, 30 Nov 2022 15:35:58 +0100 Subject: [PATCH] iox-#1732 Remove alignment constant, fix allocate function Signed-off-by: Marika Lehmann --- .../internal/cxx/storable_function.inl | 14 +++++++++----- .../memory/include/iox/bump_allocator.hpp | 10 ++++------ iceoryx_hoofs/memory/source/bump_allocator.cpp | 5 ++--- .../test_posix_shared_memory_object.cpp | 6 ++++-- 4 files changed, 19 insertions(+), 16 deletions(-) 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 ba9ec4a230d..bbb898e181a 100644 --- a/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/storable_function.inl +++ b/iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/storable_function.inl @@ -167,10 +167,13 @@ inline void swap(storable_function& f, storable_function -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(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); } @@ -179,7 +182,7 @@ template inline void storable_function>::storeFunctor(const Functor& functor) noexcept { using StoredType = typename std::remove_reference::type; - m_callable = allocate(m_storage); + m_callable = allocate(m_storage, Capacity); // erase the functor type and store as reference to the call in storage new (m_callable) StoredType(functor); @@ -196,7 +199,7 @@ template inline void storable_function>::copy(const storable_function& src, storable_function& dest) noexcept { - dest.m_callable = allocate(dest.m_storage); + dest.m_callable = allocate(dest.m_storage, Capacity); // AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type const auto obj = static_cast(src.m_callable); @@ -214,7 +217,7 @@ inline void storable_function>::move(st storable_function& dest) noexcept { // m_callable = static_cast(align(reinterpret_cast(m_storage), alignof(CallableType))); - dest.m_callable = allocate(dest.m_storage); + dest.m_callable = allocate(dest.m_storage, Capacity); // AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type const auto obj = static_cast(src.m_callable); @@ -299,7 +302,8 @@ template template inline constexpr uint64_t storable_function>::required_storage_size() noexcept { - return StorageType::template allocation_size(); + // return StorageType::template allocation_size(); + return sizeof(T) + alignof(T); } template diff --git a/iceoryx_hoofs/memory/include/iox/bump_allocator.hpp b/iceoryx_hoofs/memory/include/iox/bump_allocator.hpp index 6ed809e7746..0fe5a59fd83 100644 --- a/iceoryx_hoofs/memory/include/iox/bump_allocator.hpp +++ b/iceoryx_hoofs/memory/include/iox/bump_allocator.hpp @@ -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 + namespace iox { namespace posix @@ -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 @@ -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; diff --git a/iceoryx_hoofs/memory/source/bump_allocator.cpp b/iceoryx_hoofs/memory/source/bump_allocator.cpp index 332631a4d15..4e34f675b46 100644 --- a/iceoryx_hoofs/memory/source/bump_allocator.cpp +++ b/iceoryx_hoofs/memory/source/bump_allocator.cpp @@ -23,9 +23,8 @@ namespace iox { -constexpr uint64_t BumpAllocator::MEMORY_ALIGNMENT; BumpAllocator::BumpAllocator(void* const startAddress, const uint64_t length) noexcept - : m_startAddress(static_cast(startAddress)) + : m_startAddress(static_cast(startAddress)) , m_length(length) { } @@ -47,7 +46,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(m_startAddress); - byte_t* l_returnValue = nullptr; + cxx::byte_t* l_returnValue = nullptr; if (m_length >= alignedPosition + size) { diff --git a/iceoryx_hoofs/test/moduletests/test_posix_shared_memory_object.cpp b/iceoryx_hoofs/test/moduletests/test_posix_shared_memory_object.cpp index 3a787665534..f8db16fb606 100644 --- a/iceoryx_hoofs/test/moduletests/test_posix_shared_memory_object.cpp +++ b/iceoryx_hoofs/test/moduletests/test_posix_shared_memory_object.cpp @@ -54,6 +54,8 @@ class SharedMemoryObject_Test : public Test // hiccpp-vararg) internal::CaptureStderr(); } + + static constexpr uint64_t MEMORY_ALIGNMENT{8}; }; TEST_F(SharedMemoryObject_Test, CTorWithValidArguments) @@ -151,7 +153,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) @@ -168,7 +170,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));