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 Jan 18, 2023
1 parent efe93d4 commit ae2f65f
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 30 deletions.
13 changes: 13 additions & 0 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- Support user defined platforms with cmake switch `-DIOX_PLATFORM_PATH` [\#1619](https://github.com/eclipse-iceoryx/iceoryx/issues/1619)
- Add equality and inequality operators for `iox::variant` and `iox::expected` [\#1751](https://github.com/eclipse-iceoryx/iceoryx/issues/1751)
- Implement UninitializedArray [\#1614](https://github.com/eclipse-iceoryx/iceoryx/issues/1614)
- Implement BumpAllocator [\#1732](https://github.com/eclipse-iceoryx/iceoryx/issues/1732)

**Bugfixes:**

Expand Down Expand Up @@ -106,6 +107,7 @@
- Change return type of `cxx::variant::emplace_at_index` and `emplace` to void [\#1394](https://github.com/eclipse-iceoryx/iceoryx/issues/1394)
- Replace uses of `std::cout`, `std::cerr` with the iceoryx logger [\#1756](https://github.com/eclipse-iceoryx/iceoryx/issues/1756)
- Move `IOX_NO_DISCARD`, `IOX_FALLTHROUGH` and `IOX_MAYBE_UNUSED` to `iceoryx_platform` [\#1726](https://github.com/eclipse-iceoryx/iceoryx/issues/1726)
- Move `cxx::static_storage` from `iceoryx_hoofs` to `iceoryx_dust` [\#1732](https://github.com/eclipse-iceoryx/iceoryx/issues/1732)

**Workflow:**

Expand Down Expand Up @@ -808,3 +810,14 @@
iox::UninitializedArray<T, Capacity> myAlignedArray;

```

42. Move `static_storage` from `iceoryx_hoofs` to `iceoryx_dust`

```cpp
// before
#include "iceoryx_hoofs/internal/cxx/static_storage.hpp"

// after
#include "iceoryx_dust/internal/cxx/static_storage.hpp"
```

1 change: 1 addition & 0 deletions iceoryx_dust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ grouped together in categories or namespace, depending on where or how they are
|`SignalWatcher` | Batteries included signal handling with polling and optional blocking wait for `SIGINT` and `SIGTERM`. |
|`NamedPipe` | Shared memory based ipc channel. Mainly a `UnixDomainSocket` replacement on Windows. |
|`relocatable_ptr` | |
|`static_storage` | Untyped aligned static storage. |
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
2 changes: 1 addition & 1 deletion iceoryx_hoofs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ The module structure is a logical grouping. It is replicated for `concurrent` an
|`RelativePointer` | | Pointer which can be stored in shared memory |
|`ScopeGuard` | | This is an abstraction of the C++ RAII idiom. Sometimes you have constructs where you would like to perform a certain task on creation and then again when they are getting out of scope, this is where `ScopeGuard` comes in. It is like a `std::lock_guard` or a `std::shared_ptr` but more generic. |
|`scoped_static` | | Helper function to limit lifetime of static or global variables to a scope |
|`static_storage` | i | Untyped aligned static storage. |
|`shared_memory_object/Allocator` | i | Helper class for the `SharedMemoryObject`. |
|`BumpAllocator` | i | Implementation of a bump allocator |

### Container (container)

Expand Down
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 @@ -26,6 +26,7 @@ namespace iox
namespace cxx
{
// AXIVION Next Construct AutosarC++19_03-A12.6.1: members are initialized in body before read access
// @NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
template <uint64_t Capacity, typename ReturnType, typename... Args>
template <typename Functor, typename>
inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_function(const Functor& functor) noexcept
Expand All @@ -35,9 +36,10 @@ inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_fun

// AXIVION Next Construct AutosarC++19_03-A12.1.5: constructor delegation is not feasible here due
// to lack of sufficient common initialization
// AXIVION Next Construct AutosarC++19_03-A12.6.1: members are default initialized
// AXIVION Next Construct AutosarC++19_03-M5.2.6: the converted pointer is only used
// as its original function pointer type after reconversion (type erasure)
// AXIVION Next Construct AutosarC++19_03-A12.6.1: members are default initialized
// @NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
template <uint64_t Capacity, typename ReturnType, typename... Args>
inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_function(
ReturnType (*function)(Args...)) noexcept
Expand All @@ -56,6 +58,7 @@ inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_fun
}

// AXIVION Next Construct AutosarC++19_03-A12.6.1: members are default initialized
// @NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
template <uint64_t Capacity, typename ReturnType, typename... Args>
template <typename T, typename>
inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_function(
Expand All @@ -69,6 +72,7 @@ inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_fun
}

// AXIVION Next Construct AutosarC++19_03-A12.6.1: members are default initialized
// @NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
template <uint64_t Capacity, typename ReturnType, typename... Args>
template <typename T, typename>
inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_function(const T& object,
Expand All @@ -83,6 +87,7 @@ inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_fun
}

// AXIVION Next Construct AutosarC++19_03-A12.6.1: m_storage is default initialized
// @NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
template <uint64_t Capacity, typename ReturnType, typename... Args>
inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_function(
const storable_function& other) noexcept
Expand All @@ -92,9 +97,10 @@ inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_fun
m_operations.copy(other, *this);
}

// AXIVION Next Construct AutosarC++19_03-A12.6.1: m_storage is default initialized
// AXIVION Next Construct AutosarC++19_03-A12.8.4: we copy only the operation pointer table
// (required) and will perform a move with its type erased move function
// AXIVION Next Construct AutosarC++19_03-A12.6.1: m_storage is default initialized
// @NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
template <uint64_t Capacity, typename ReturnType, typename... Args>
inline storable_function<Capacity, signature<ReturnType, Args...>>::storable_function(
storable_function&& other) noexcept
Expand Down Expand Up @@ -169,11 +175,13 @@ 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));
// 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
}

template <uint64_t Capacity, typename ReturnType, typename... Args>
Expand Down Expand Up @@ -204,6 +212,7 @@ inline void storable_function<Capacity, signature<ReturnType, Args...>>::copy(co
const auto obj = static_cast<CallableType*>(src.m_callable);
cxx::Expects(obj != nullptr); // should not happen unless src is incorrectly used after move

// NOLINTNEXTLINE(clang-analyzer-core.NonNullParamChecker) checked two lines above
new (dest.m_callable) CallableType(*obj);
dest.m_invoker = src.m_invoker;
}
Expand All @@ -215,13 +224,13 @@ template <typename CallableType>
inline void storable_function<Capacity, signature<ReturnType, Args...>>::move(storable_function& src,
storable_function& dest) noexcept
{
// m_callable = static_cast<void*>(align(reinterpret_cast<uint64_t>(m_storage), alignof(CallableType)));
dest.m_callable = allocate<CallableType>(dest.m_storage, Capacity);

// AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type
const auto obj = static_cast<CallableType*>(src.m_callable);
cxx::Expects(obj != nullptr); // should not happen unless src is incorrectly used after move

// NOLINTNEXTLINE(clang-analyzer-core.NonNullParamChecker) checked two lines above
new (dest.m_callable) CallableType(std::move(*obj));
dest.m_invoker = src.m_invoker;
src.m_operations.destroy(src);
Expand Down Expand Up @@ -272,9 +281,10 @@ inline ReturnType storable_function<Capacity, signature<ReturnType, Args...>>::i
Args&&... args) noexcept
{
// AXIVION Next Construct AutosarC++19_03-A18.9.2: we use idiomatic perfect forwarding
// AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type
// AXIVION Next Construct AutosarC++19_03-A5.3.2: callable is guaranteed not to be nullptr
// when invoke is called (it is private and only used for type erasure)
// AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type
// NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage) see justification above
return (*static_cast<CallableType*>(callable))(std::forward<Args>(args)...);
}

Expand All @@ -301,15 +311,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
2 changes: 1 addition & 1 deletion iceoryx_hoofs/memory/include/iox/bump_allocator.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. 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.
Expand Down
17 changes: 13 additions & 4 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 @@ -252,9 +253,16 @@ 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;

// 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));
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 @@ -270,7 +278,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
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ TEST_F(BumpAllocator_Test, allocateElementOfSizeZero)
// @todo iox-#1613 remove EXPECT_DEATH
// NOLINTBEGIN(hicpp-avoid-goto, cppcoreguidelines-avoid-goto, cert-err33-c, cppcoreguidelines-pro-type-vararg,
// hiccpp-vararg)
EXPECT_DEATH(sut.allocate(0, MEMORY_ALIGNMENT), ".*");
EXPECT_DEATH(IOX_DISCARD_RESULT(sut.allocate(0, MEMORY_ALIGNMENT)), ".*");
// NOLINTEND(hicpp-avoid-goto, cppcoreguidelines-avoid-goto, cert-err33-c, cppcoreguidelines-pro-type-vararg,
// hiccpp-vararg)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. 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.
Expand Down

0 comments on commit ae2f65f

Please sign in to comment.