Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1732 Move static_storage to iceoryx_dust
Browse files Browse the repository at this point in the history
Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Dec 6, 2022
1 parent 12f38bc commit c9bbc74
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_STATIC_STORAGE_HPP
#define IOX_HOOFS_STATIC_STORAGE_HPP
#ifndef IOX_DUST_STATIC_STORAGE_HPP
#define IOX_DUST_STATIC_STORAGE_HPP

#include <cstdint>
#include <cstring>
Expand Down Expand Up @@ -109,5 +109,5 @@ class static_storage final
} // namespace cxx
} // namespace iox

#include "iceoryx_hoofs/internal/cxx/static_storage.inl"
#endif // IOX_HOOFS_STATIC_STORAGE_HPP
#include "iceoryx_dust/internal/cxx/static_storage.inl"
#endif // IOX_DUST_STATIC_STORAGE_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_CXX_STATIC_STORAGE_INL
#define IOX_HOOFS_CXX_STATIC_STORAGE_INL
#ifndef IOX_DUST_CXX_STATIC_STORAGE_INL
#define IOX_DUST_CXX_STATIC_STORAGE_INL

#include "iceoryx_hoofs/internal/cxx/static_storage.hpp"
#include "iceoryx_dust/internal/cxx/static_storage.hpp"

namespace iox
{
Expand Down Expand Up @@ -113,4 +113,4 @@ constexpr uint64_t static_storage<Capacity, Align>::allocation_size() noexcept
} // namespace iox


#endif // IOX_HOOFS_STATIC_STORAGE_INL
#endif // IOX_DUST_STATIC_STORAGE_INL
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/internal/cxx/static_storage.hpp"
#include "iceoryx_dust/internal/cxx/static_storage.hpp"
#include "test.hpp"

#include <iostream>
Expand Down Expand Up @@ -246,5 +246,4 @@ TEST(static_storage_test, AllocationSizeReturnsMoreThanSizeIfTypeIsNotAlignedWit
EXPECT_EQ(size, sizeof(Data) + typeAlign - storageAlign);
}


} // namespace
1 change: 0 additions & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef IOX_HOOFS_FUNCTION_HPP
#define IOX_HOOFS_FUNCTION_HPP

#include "iceoryx_hoofs/internal/cxx/static_storage.hpp"
#include "iceoryx_hoofs/internal/cxx/storable_function.hpp"

namespace iox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "iceoryx_hoofs/cxx/type_traits.hpp"
#include "iceoryx_hoofs/iceoryx_hoofs_types.hpp"
#include "iceoryx_hoofs/internal/cxx/static_storage.hpp"

#include <iostream>
#include <utility>
Expand Down Expand Up @@ -49,7 +48,6 @@ template <uint64_t Capacity, typename ReturnType, typename... Args>
class storable_function<Capacity, signature<ReturnType, Args...>> final
{
public:
using StorageType = static_storage<Capacity>;
using signature_t = signature<ReturnType, Args...>;

/// @brief construct from functor (including lambdas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ inline void swap(storable_function<Capacity, T>& f, storable_function<Capacity,
template <typename T>
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");
Expand Down Expand Up @@ -301,15 +300,14 @@ 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 sizeof(T) + alignof(T);
}

template <uint64_t Capacity, typename ReturnType, typename... Args>
template <typename T>
inline constexpr bool storable_function<Capacity, signature<ReturnType, Args...>>::is_storable() noexcept
{
return (required_storage_size<T>() <= StorageType::capacity()) && is_invocable_r<ReturnType, T, Args...>::value;
return (required_storage_size<T>() <= Capacity) && is_invocable_r<ReturnType, T, Args...>::value;
}

template <uint64_t Capacity, typename ReturnType, typename... Args>
Expand Down
20 changes: 15 additions & 5 deletions iceoryx_hoofs/test/moduletests/test_cxx_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/cxx/function.hpp"
#include "iox/bump_allocator.hpp"
#include "test.hpp"

#include <functional>
Expand Down Expand Up @@ -254,9 +255,17 @@ TEST_F(function_test, ConstructionFromAnotherFunctionIsCallable)
TEST_F(function_test, FunctionStateIsIndependentOfSource)
{
::testing::Test::RecordProperty("TEST_ID", "8302046f-cd6a-4527-aca6-3e6408f87a6b");
constexpr uint32_t INITIAL_STATE = 73U;
static_storage<1024U> storage;
auto* p = storage.allocate<Functor>();
constexpr uint32_t INITIAL_STATE = 73;
constexpr uint32_t MEMORY_SIZE = 1024;

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

auto* p = static_cast<Functor*>(allocationResult.value());
p = new (p) Functor(INITIAL_STATE);

// call the dtor in any case (even if the test fails due to ASSERT)
Expand All @@ -272,7 +281,8 @@ TEST_F(function_test, FunctionStateIsIndependentOfSource)
EXPECT_EQ(sut(1U), functor(1U));

guard.reset(); // call the deleter
storage.clear();
// NOLINTNEXTLINE(hicpp-no-malloc, cppcoreguidelines-no-malloc) low-level memory management for testing purpose
free(memory);

EXPECT_EQ(sut(1U), INITIAL_STATE + 2U);
}
Expand Down Expand Up @@ -549,7 +559,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 c9bbc74

Please sign in to comment.