Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1732 Fix BumpAllocator tests, AUTOSAR violations …
Browse files Browse the repository at this point in the history
…in static_storage

Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Jan 18, 2023
1 parent aa1bb90 commit c00a5cf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class static_storage final
static constexpr uint64_t allocation_size() noexcept;

private:
/// @NOLINTJUSTIFICATION @todo iox-#1732 will be refactored with BumpAllocator
/// AXIVION Next Construct AutosarC++19_03-A18.1.1 : safe access is guaranteed since the c-array is wrapped inside the static_storage
/// @NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
alignas(Align) uint8_t m_bytes[Capacity];
void* m_ptr{nullptr};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace cxx
template <uint64_t Capacity, uint64_t Align>
constexpr uint64_t static_storage<Capacity, Align>::align_mismatch(uint64_t align, uint64_t requiredAlign) noexcept
{
const auto r = align % requiredAlign;
const uint64_t r = align % requiredAlign;

// If r != 0 we are not aligned with requiredAlign and need to add r to an align
// aligned address to be aligned with requiredAlign.
Expand Down
12 changes: 5 additions & 7 deletions iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,16 @@ TEST_F(BumpAllocator_Test, AllocateCompleteMemoryWithEquallySizedChunksWorks)
{
::testing::Test::RecordProperty("TEST_ID", "21d0fa61-54f9-41a0-8e53-e3448784497b");
constexpr uint64_t MEMORY_CHUNK_SIZE{32};
constexpr uint64_t MEMORY_CHUNK_ALIGNMENT{32};
iox::BumpAllocator sut(memory, MEMORY_SIZE);

auto allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
auto allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p0 = reinterpret_cast<uintptr_t>(allocationResult.value());

for (uint64_t i{MEMORY_CHUNK_SIZE}; i < MEMORY_SIZE; i += MEMORY_CHUNK_SIZE)
{
allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p1 = reinterpret_cast<uintptr_t>(allocationResult.value());
Expand All @@ -152,25 +151,24 @@ TEST_F(BumpAllocator_Test, AllocateCompleteMemoryWithDifferentSizedChunksWorks)
{
::testing::Test::RecordProperty("TEST_ID", "c8079bb7-1de6-45a6-92af-a50aa59d7481");
constexpr uint64_t MEMORY_CHUNK_SIZE{64};
constexpr uint64_t MEMORY_CHUNK_ALIGNMENT{64};
iox::BumpAllocator sut(memory, MEMORY_SIZE);

auto allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
auto allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p0 = reinterpret_cast<uintptr_t>(allocationResult.value());

for (uint64_t i{MEMORY_CHUNK_SIZE}; i < MEMORY_SIZE - MEMORY_CHUNK_SIZE; i += MEMORY_CHUNK_SIZE)
{
allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p1 = reinterpret_cast<uintptr_t>(allocationResult.value());
EXPECT_THAT(p1 - p0, Eq(MEMORY_CHUNK_SIZE));
p0 = p1;
}

allocationResult = sut.allocate(MEMORY_CHUNK_SIZE / 2, MEMORY_CHUNK_ALIGNMENT / 2);
allocationResult = sut.allocate(MEMORY_CHUNK_SIZE / 2, MEMORY_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p1 = reinterpret_cast<uintptr_t>(allocationResult.value());
Expand Down

0 comments on commit c00a5cf

Please sign in to comment.