Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1560 Renam 'algorithm' namespace and move memory-…
Browse files Browse the repository at this point in the history
…related functions to 'memory.hpp'

Signed-off-by: Simon Hoinkis <[email protected]>
  • Loading branch information
mossmaurice committed Feb 1, 2023
1 parent 7f487ed commit 42d8cb3
Show file tree
Hide file tree
Showing 30 changed files with 221 additions and 169 deletions.
2 changes: 1 addition & 1 deletion iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ iox_add_library(
source/cxx/deadline_timer.cpp
source/cxx/filesystem.cpp
source/cxx/functional_interface.cpp
source/cxx/helplets.cpp
source/cxx/requires.cpp
source/cxx/type_traits.cpp
source/cxx/unique_id.cpp
Expand All @@ -91,6 +90,7 @@ iox_add_library(
source/memory/relative_pointer_data.cpp
source/units/duration.cpp
memory/source/bump_allocator.cpp
memory/source/memory.cpp
)

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/iceoryx_hoofs_deployment.hpp.in"
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ inline constexpr bool doesContainValue(const T) noexcept;
template <typename T, typename... ValueList>
inline constexpr bool
doesContainValue(const T value, const T firstValueListEntry, const ValueList... remainingValueListEntries) noexcept;
} // namespace algorithm

namespace internal
{
Expand Down Expand Up @@ -219,7 +220,6 @@ constexpr bool isPowerOfTwo(const T n) noexcept
static_assert(std::is_unsigned<T>::value && !std::is_same<T, bool>::value, "Only unsigned integer are allowed!");
return n && ((n & (n - 1U)) == 0U);
}
} // namespace algorithm
} // namespace iox

#include "iceoryx_hoofs/internal/cxx/algorithm.inl"
Expand Down
49 changes: 0 additions & 49 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/helplets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include "iceoryx_hoofs/cxx/type_traits.hpp"
#include "iox/string.hpp"

#include <cassert>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <type_traits>
Expand Down Expand Up @@ -77,53 +75,6 @@ struct not_null
T m_value;
};

/// @note value + alignment - 1 must not exceed the maximum value for type T
/// @note alignment must be a power of two
template <typename T>
// AXIVION Next Construct AutosarC++19_03-A2.10.5, AutosarC++19_03-M17.0.3: The function is in the iox::cxx namespace which prevents easy misuse
T align(const T value, const T alignment) noexcept
{
return (value + (alignment - 1)) & (-alignment);
}

/// @brief allocates aligned memory which can only be free'd by alignedFree
/// @param[in] alignment, alignment of the memory
/// @param[in] size, memory size
/// @return void pointer to the aligned memory
void* alignedAlloc(const uint64_t alignment, const uint64_t size) noexcept;

/// @brief frees aligned memory allocated with alignedAlloc
/// @param[in] memory, pointer to the aligned memory
void alignedFree(void* const memory) noexcept;

/// template recursion stopper for maximum alignment calculation
template <size_t S = 0>
constexpr size_t maxAlignment() noexcept
{
return S;
}

/// calculate maximum alignment of supplied types
template <typename T, typename... Args>
constexpr size_t maxAlignment() noexcept
{
return (alignof(T) > maxAlignment<Args...>()) ? alignof(T) : maxAlignment<Args...>();
}

/// template recursion stopper for maximum size calculation
template <size_t S = 0>
constexpr size_t maxSize() noexcept
{
return S;
}

/// calculate maximum size of supplied types
template <typename T, typename... Args>
constexpr size_t maxSize() noexcept
{
return (sizeof(T) > maxSize<Args...>()) ? sizeof(T) : maxSize<Args...>();
}

/// @brief Get the capacity of a C array at compile time
/// @code
/// constexpr uint32_t FOO[42]{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#ifndef IOX_HOOFS_STORABLE_FUNCTION_INL
#define IOX_HOOFS_STORABLE_FUNCTION_INL

#include "iceoryx_hoofs/cxx/helplets.hpp" //align
#include "iceoryx_hoofs/cxx/requires.hpp"
#include "iceoryx_hoofs/internal/cxx/storable_function.hpp"
#include "iox/memory.hpp"

namespace iox
{
Expand Down Expand Up @@ -179,7 +179,7 @@ inline constexpr void* storable_function<Capacity, signature<ReturnType, Args...
static_assert(is_storable<T>(), "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);
uint64_t alignedPosition = align(reinterpret_cast<uint64_t>(startAddress), alignment);
return reinterpret_cast<void*>(alignedPosition);
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast, performance-no-int-to-ptr) required for low level pointer alignment
}
Expand Down
73 changes: 73 additions & 0 deletions iceoryx_hoofs/memory/include/iox/memory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_MEMORY_MEMORY_HPP
#define IOX_HOOFS_MEMORY_MEMORY_HPP

#include <cassert>
#include <cstdint>

namespace iox
{

/// @note value + alignment - 1 must not exceed the maximum value for type T
/// @note alignment must be a power of two
template <typename T>
// AXIVION Next Construct AutosarC++19_03-A2.10.5, AutosarC++19_03-M17.0.3: The function is in the iox::cxx namespace which prevents easy misuse
T align(const T value, const T alignment) noexcept
{
return (value + (alignment - 1)) & (-alignment);
}

/// @brief allocates aligned memory which can only be free'd by alignedFree
/// @param[in] alignment, alignment of the memory
/// @param[in] size, memory size
/// @return void pointer to the aligned memory
void* alignedAlloc(const uint64_t alignment, const uint64_t size) noexcept;

/// @brief frees aligned memory allocated with alignedAlloc
/// @param[in] memory, pointer to the aligned memory
void alignedFree(void* const memory) noexcept;

/// template recursion stopper for maximum alignment calculation
template <std::size_t S = 0>
constexpr std::size_t maxAlignment() noexcept
{
return S;
}

/// calculate maximum alignment of supplied types
template <typename T, typename... Args>
constexpr std::size_t maxAlignment() noexcept
{
return (alignof(T) > maxAlignment<Args...>()) ? alignof(T) : maxAlignment<Args...>();
}

/// template recursion stopper for maximum size calculation
template <std::size_t S = 0>
constexpr std::size_t maxSize() noexcept
{
return S;
}

/// calculate maximum size of supplied types
template <typename T, typename... Args>
constexpr std::size_t maxSize() noexcept
{
return (sizeof(T) > maxSize<Args...>()) ? sizeof(T) : maxSize<Args...>();
}
} // namespace iox
#endif // IOX_HOOFS_MEMORY_MEMORY_HPP
4 changes: 2 additions & 2 deletions iceoryx_hoofs/memory/source/bump_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// SPDX-License-Identifier: Apache-2.0

#include "iox/bump_allocator.hpp"
#include "iceoryx_hoofs/cxx/helplets.hpp" //align
#include "iceoryx_hoofs/log/logging.hpp"
#include "iceoryx_platform/platform_correction.hpp"
#include "iox/memory.hpp"

#include <iostream>

Expand All @@ -42,7 +42,7 @@ cxx::expected<void*, BumpAllocatorError> BumpAllocator::allocate(const uint64_t

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for low level pointer alignment
uint64_t currentAddress = reinterpret_cast<uint64_t>(m_startAddress) + m_currentPosition;
uint64_t alignedPosition = cxx::align(currentAddress, static_cast<uint64_t>(alignment));
uint64_t alignedPosition = align(currentAddress, static_cast<uint64_t>(alignment));

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for low level pointer alignment
alignedPosition -= reinterpret_cast<uint64_t>(m_startAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/cxx/helplets.hpp"
#include "iox/memory.hpp"

#include <cstdlib>

namespace iox
{
namespace cxx
{
void* alignedAlloc(const uint64_t alignment, const uint64_t size) noexcept
{
// -1 == since the max alignment addition is alignment - 1 otherwise the
// memory is already aligned and we have to do nothing
// low-level memory management, no other approach then to use malloc to acquire heap memory
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,cppcoreguidelines-pro-type-reinterpret-cast,hicpp-no-malloc,cppcoreguidelines-no-malloc)
auto memory = reinterpret_cast<uint64_t>(malloc(size + alignment + sizeof(void*) - 1));
auto memory = reinterpret_cast<uint64_t>(std::malloc(size + alignment + sizeof(void*) - 1));
if (memory == 0)
{
return nullptr;
Expand All @@ -53,8 +53,7 @@ void alignedFree(void* const memory) noexcept
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc,
// cppcoreguidelines-pro-bounds-pointer-arithmetic)
// NOLINTNEXTLINE
free(reinterpret_cast<void**>(memory)[-1]);
std::free(reinterpret_cast<void**>(memory)[-1]);
}
}
} // namespace cxx
} // namespace iox
1 change: 1 addition & 0 deletions iceoryx_hoofs/test/moduletests/test_cxx_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace
using namespace ::testing;
using namespace iox::algorithm;
using namespace iox::cxx;
using namespace iox;

class algorithm_test : public Test
{
Expand Down
60 changes: 0 additions & 60 deletions iceoryx_hoofs/test/moduletests/test_cxx_helplets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,6 @@ using namespace iox::cxx;
using namespace iox;
using namespace iox::cxx::internal;

namespace
{
struct Bar
{
// required for testing, a struct with a defined size
// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
alignas(8) uint8_t m_dummy[73];
};
struct Foo
{
// required for testing, a struct with a defined size
// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
uint8_t m_dummy[73];
};
struct FooBar
{
// required for testing, a struct with a defined size
// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
alignas(32) uint8_t m_dummy[73];
};
struct FuBar
{
// required for testing, a struct with a defined size
// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
alignas(32) uint8_t m_dummy[73];
};
} // namespace

class Helplets_test : public Test
{
public:
Expand All @@ -112,38 +84,6 @@ bool isValidFileCharacter(const int32_t i) noexcept

constexpr uint64_t FILE_PATH_LENGTH = 128U;

TEST_F(Helplets_test, MaxSizeWorksAsExpected)
{
::testing::Test::RecordProperty("TEST_ID", "5b3e938d-aec5-478d-b1c1-49ff2cc4e3ef");
EXPECT_THAT(iox::cxx::maxSize<Foo>(), Eq(sizeof(Foo)));

EXPECT_THAT(sizeof(Bar), Ne(sizeof(Foo)));
EXPECT_THAT((iox::cxx::maxSize<Bar, Foo>()), Eq(sizeof(Bar)));

EXPECT_THAT(sizeof(Bar), Ne(sizeof(FooBar)));
EXPECT_THAT(sizeof(Foo), Ne(sizeof(FooBar)));
EXPECT_THAT((iox::cxx::maxSize<Bar, Foo, FooBar>()), Eq(sizeof(FooBar)));

EXPECT_THAT(sizeof(FooBar), Eq(sizeof(FuBar)));
EXPECT_THAT((iox::cxx::maxSize<FooBar, FuBar>()), Eq(sizeof(FooBar)));
}

TEST_F(Helplets_test, MaxAlignmentWorksAsExpected)
{
::testing::Test::RecordProperty("TEST_ID", "7d5d3de1-f22c-47c1-b7fd-cacc35eef13c");
EXPECT_THAT(iox::cxx::maxAlignment<Foo>(), Eq(alignof(Foo)));

EXPECT_THAT(alignof(Bar), Ne(alignof(Foo)));
EXPECT_THAT((iox::cxx::maxAlignment<Bar, Foo>()), Eq(alignof(Bar)));

EXPECT_THAT(alignof(Bar), Ne(alignof(FooBar)));
EXPECT_THAT(alignof(Foo), Ne(alignof(FooBar)));
EXPECT_THAT((iox::cxx::maxAlignment<Bar, Foo, FooBar>()), Eq(alignof(FooBar)));

EXPECT_THAT(alignof(FooBar), Eq(alignof(FuBar)));
EXPECT_THAT((iox::cxx::maxAlignment<FooBar, FuBar>()), Eq(alignof(FooBar)));
}

TEST_F(Helplets_test, ArrayCapacityReturnsCorrectValues)
{
::testing::Test::RecordProperty("TEST_ID", "8392b2ba-04ef-45e6-8b47-4c0c90d98f61");
Expand Down
Loading

0 comments on commit 42d8cb3

Please sign in to comment.