Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iox-#1614 Move uninitialized array #1774

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .clang-tidy-diff-scans.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
./iceoryx_hoofs/test/moduletests/test_posix*
./iceoryx_hoofs/include/iceoryx_hoofs/design_pattern/builder.hpp

./iceoryx_hoofs/include/iceoryx_hoofs/internal/containers/*
./iceoryx_hoofs/test/moduletests/test_containers_*
./iceoryx_hoofs/container/include/iox/**/*
./iceoryx_hoofs/test/moduletests/test_container_*

# IMPORTANT:
# after the first # everything is considered a comment, add new files and
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- uses: actions/checkout@v3
- uses: egor-tensin/setup-gcc@v1
with:
version: 9
version: 8
platform: x64
- uses: jwlawson/actions-setup-cmake@v1.12
with:
Expand Down
8 changes: 4 additions & 4 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@
* `iox::bar::foo` to `iox::foo`
* `iceoryx_hoofs/bar/foo.hpp` to `iox/foo.hpp`

41. Use proper aligned `containers::UninitializedArray` instead of C-style array
41. Use proper aligned `iox::UninitializedArray` instead of C-style array

```cpp
// before
Expand All @@ -795,10 +795,10 @@
alignas(T) element_t myAlignedArray[Capacity];

// after
#include "iceoryx_hoofs/containers/uninitialized_array.hpp"
#include "iox/uninitialized_array.hpp"

containers::UninitializedArray<char, Capacity, containers::ZeroedBuffer> myCharArray;
iox::UninitializedArray<char, Capacity, iox::ZeroedBuffer> myCharArray;

containers::UninitializedArray<T, Capacity> myAlignedArray;
iox::UninitializedArray<T, Capacity> myAlignedArray;

```
6 changes: 3 additions & 3 deletions iceoryx_dust/include/iceoryx_dust/cxx/forward_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define IOX_DUST_CXX_FORWARD_LIST_HPP

#include "iceoryx_hoofs/cxx/helplets.hpp"
#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iox/uninitialized_array.hpp"

#include <cstdint>
#include <iostream>
Expand Down Expand Up @@ -356,8 +356,8 @@ class forward_list
// are inserted by the user (starting from BEFORE_BEGIN_INDEX)
size_type m_freeListHeadIdx{0U};

containers::UninitializedArray<NodeLink, NODE_LINK_COUNT> m_links;
containers::UninitializedArray<T, Capacity> m_data;
UninitializedArray<NodeLink, NODE_LINK_COUNT> m_links;
UninitializedArray<T, Capacity> m_data;

size_type m_size{0U};
}; // forward_list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
#include "iceoryx_hoofs/concurrent/lockfree_queue.hpp"
#include "iceoryx_hoofs/cxx/string.hpp"
#include "iceoryx_hoofs/design_pattern/creation.hpp"
#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iceoryx_hoofs/internal/posix_wrapper/ipc_channel.hpp"
#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object.hpp"
#include "iceoryx_hoofs/internal/units/duration.hpp"
#include "iceoryx_hoofs/posix_wrapper/unnamed_semaphore.hpp"
#include "iceoryx_platform/semaphore.hpp"
#include "iox/uninitialized_array.hpp"

#include <cstdint>

Expand Down
3 changes: 2 additions & 1 deletion iceoryx_hoofs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ cc_library(
"source/**/*.cpp",
"source/**/*.hpp",
]),
hdrs = glob(["include/**"]) + glob(["legacy/**"]) + glob(["memory/**"]) + [
hdrs = glob(["include/**"]) + glob(["legacy/**"]) + glob(["memory/**"]) + glob(["container/**"]) + [
":iceoryx_hoofs_deployment_hpp",
],
includes = [
"container/include/",
"include/",
"legacy/include/",
"memory/include/",
Expand Down
2 changes: 2 additions & 0 deletions iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ iox_add_library(
BUILD_INTERFACE ${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/legacy/include
${PROJECT_SOURCE_DIR}/memory/include
${PROJECT_SOURCE_DIR}/container/include
${CMAKE_BINARY_DIR}/generated/iceoryx_hoofs/include
INSTALL_INTERFACE include/${PREFIX}
EXPORT_INCLUDE_DIRS include/
legacy/include/
memory/include/
container/include/
FILES
source/concurrent/loffli.cpp
source/cxx/adaptive_wait.cpp
Expand Down
6 changes: 3 additions & 3 deletions iceoryx_hoofs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ The module structure is a logical grouping. It is replicated for `concurrent` an
|`static_storage` | i | Untyped aligned static storage. |
|`shared_memory_object/Allocator` | i | Helper class for the `SharedMemoryObject`. |

### Containers (containers)
### Container (container)

| class | internal | description |
|:---------------------:|:--------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|`vector` | | Heap and exception free implementation of `std::vector` |
|`list` | | Heap and exception free, relocatable implementation of `std::list` |
|`UninitializedArray` | i | Wrapper class for an uninitialized C-style array which can be zeroed via a template parameter |
|`UninitializedArray` | | Wrapper class for an uninitialized C-style array which can be zeroed via a template parameter |

### Vocabulary types (vocabulary)

Expand Down Expand Up @@ -88,7 +88,7 @@ The module structure is a logical grouping. It is replicated for `concurrent` an
|`attributes` | | C++17 and C++20 attributes are sometimes available through compiler extensions. The attribute macros defined in here (like `IOX_FALLTHROUGH`, `IOX_MAYBE_UNUSED` ... ) make sure that we are able to use them if the compiler supports it. |
|`algorithm` | | Implements `min` and `max` for an arbitrary number of values of the same type. For instance `min(1,2,3,4,5);` |

### Buffers (buffers)
### Buffer (buffer)

| class | internal | description |
|:----------------------------------:|:--------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_CONTAINERS_UNINITIALIZED_ARRAY_INL
#define IOX_HOOFS_CONTAINERS_UNINITIALIZED_ARRAY_INL
#ifndef IOX_HOOFS_CONTAINER_UNINITIALIZED_ARRAY_INL
#define IOX_HOOFS_CONTAINER_UNINITIALIZED_ARRAY_INL

#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iox/uninitialized_array.hpp"

namespace iox
{
namespace containers
{
template <typename ElementType, uint64_t Capacity, template <typename, uint64_t> class Buffer>
inline constexpr ElementType&
UninitializedArray<ElementType, Capacity, Buffer>::operator[](const uint64_t index) noexcept
Expand Down Expand Up @@ -79,7 +77,6 @@ UninitializedArray<ElementType, Capacity, Buffer>::end() const noexcept
{
return &operator[](0) + Capacity;
}
} // namespace containers
} // namespace iox

#endif // IOX_HOOFS_CONTAINERS_UNINITIALIZED_ARRAY_INL
#endif // IOX_HOOFS_CONTAINER_UNINITIALIZED_ARRAY_INL
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,30 @@
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_CONTAINERS_UNINITIALIZED_ARRAY_HPP
#define IOX_HOOFS_CONTAINERS_UNINITIALIZED_ARRAY_HPP
#ifndef IOX_HOOFS_CONTAINER_UNINITIALIZED_ARRAY_HPP
#define IOX_HOOFS_CONTAINER_UNINITIALIZED_ARRAY_HPP

#include "iceoryx_hoofs/iceoryx_hoofs_types.hpp"

#include <cstdint>

namespace iox
{
namespace containers
{
/// @brief struct used as policy parameter in UninitializedArray to wrap an array with all elements zeroed
/// @tparam ElementType is the array type
/// @tparam Capacity is the array size
template <typename ElementType, uint64_t Capacity>
struct ZeroedBuffer
{
struct alignas(ElementType) element_t
FerdinandSpitzschnueffler marked this conversation as resolved.
Show resolved Hide resolved
{
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : required by low level UninitializedArray building block and encapsulated in abstraction
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
cxx::byte_t data[sizeof(ElementType)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding any other fields to the struct will break things. Maybe worth a comment.

I am also wondering whether the zero-initialization braces { } are required here (instead of array of element_t).

};
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : required by low level UninitializedArray building block and encapsulated in abstraction
// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
using element_t = cxx::byte_t[sizeof(ElementType)];
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : required by low level UninitializedArray building block and encapsulated in abstraction
alignas(ElementType) element_t value[Capacity]{};
// NOLINTEND(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
element_t value[Capacity]{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move the braces to element_t, they should not be required here. I am generally not sure how it works with padding without trying it out.

There can be trailing padding in element_t (to make size a multiple of alignment). Depends on our requirements of the padding initialization. Padding initialization which would not happen if we move it I think, but would it as it is now?

@elBoberido FYI

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the UninitialiizedArray is typed and element_t uses the size and alignment of that type, there are no issues with additional padding bytes

};

/// @brief struct used as policy parameter in UninitializedArray to wrap an uninitialized array
Expand All @@ -46,12 +47,15 @@ struct ZeroedBuffer
template <typename ElementType, uint64_t Capacity>
struct NonZeroedBuffer
{
struct alignas(ElementType) element_t
{
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : required by low level UninitializedArray building block and encapsulated in abstraction
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
cxx::byte_t data[sizeof(ElementType)];
};
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : required by low level UninitializedArray building block and encapsulated in abstraction
// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
using element_t = cxx::byte_t[sizeof(ElementType)];
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : required by low level UninitializedArray building block and encapsulated in abstraction
alignas(ElementType) element_t value[Capacity];
// NOLINTEND(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
element_t value[Capacity];
};

/// @brief Wrapper class for a C-style array of type ElementType and size Capacity. Per default it is uninitialized but
Expand Down Expand Up @@ -109,9 +113,8 @@ class UninitializedArray
Buffer<ElementType, Capacity> m_buffer;
};

} // namespace containers
} // namespace iox

#include "iceoryx_hoofs/internal/containers/uninitialized_array.inl"
#include "iox/detail/uninitialized_array.inl"

#endif // IOX_HOOFS_CONTAINERS_UNINITIALIZED_ARRAY_HPP
#endif // IOX_HOOFS_CONTAINER_UNINITIALIZED_ARRAY_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/internal/concurrent/lockfree_queue/index_queue.hpp"
#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iox/uninitialized_array.hpp"

#include <atomic>

Expand Down Expand Up @@ -110,7 +110,7 @@ class LockFreeQueue
// required to be a queue for LockFreeQueue to exhibit FIFO behaviour
Queue m_usedIndices;

containers::UninitializedArray<ElementType, Capacity> m_buffer;
UninitializedArray<ElementType, Capacity> m_buffer;

std::atomic<uint64_t> m_size{0U};

Expand Down
6 changes: 3 additions & 3 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define IOX_HOOFS_CXX_LIST_HPP

#include "iceoryx_hoofs/cxx/helplets.hpp"
#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iox/uninitialized_array.hpp"

#include <cstdint>
#include <iostream>
Expand Down Expand Up @@ -383,8 +383,8 @@ class list
// to the beginning and end of the list. This additional element (index position 'capacity' aka
// BEGIN_END_LINK_INDEX) 'previous' will point to the last valid element (end()) and 'next' will point to the
// first used list element (begin())
containers::UninitializedArray<NodeLink, NODE_LINK_COUNT> m_links;
containers::UninitializedArray<T, Capacity> m_data;
UninitializedArray<NodeLink, NODE_LINK_COUNT> m_links;
UninitializedArray<T, Capacity> m_data;
size_type m_size{0U};
}; // list

Expand Down
10 changes: 9 additions & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "iceoryx_hoofs/cxx/functional_interface.hpp"
#include "iceoryx_hoofs/cxx/requires.hpp"
#include "iceoryx_hoofs/iceoryx_hoofs_types.hpp"

#include <new> // needed for placement new in the construct_value member function
#include <utility>
Expand Down Expand Up @@ -229,7 +230,14 @@ class optional final : public FunctionalInterface<optional<T>, T, void>
// initHandle(&handle);
// }
bool m_hasValue{false};
typename std::aligned_storage<sizeof(T), alignof(T)>::type m_data;
struct alignas(T) element_t
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be necessary to use our own struct. Do we want to remove the dependency on std::aligned_storage or is there another reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced std::aligned_storage since it is deprecated in C++23 (I know, not a problem now but maybe later) and already added similar constructs in variant and UninitializedArray.

{
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : safe access is guaranteed since the array
// is wrapped inside the optional
// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
byte_t data[sizeof(T)];
};
element_t m_data;

private:
template <typename... Targs>
Expand Down
4 changes: 2 additions & 2 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "iceoryx_hoofs/cxx/algorithm.hpp"
#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iox/uninitialized_array.hpp"

#include <cstdint>

Expand Down Expand Up @@ -72,7 +72,7 @@ class stack final // NOLINT(cppcoreguidelines-pro-type-member-init, hicpp-member
stack& copy(const stack& rhs) noexcept;
stack& move(stack&& rhs) noexcept;

containers::UninitializedArray<T, Capacity> m_data;
UninitializedArray<T, Capacity> m_data;
uint64_t m_size{0U};
};
} // namespace cxx
Expand Down
3 changes: 1 addition & 2 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/cxx/type_traits.hpp"
#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iceoryx_hoofs/internal/cxx/string_internal.hpp"

#include <algorithm>
Expand Down Expand Up @@ -609,7 +608,7 @@ class string

// safe access is guaranteed since the char array is wrapped inside the string class
// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
containers::UninitializedArray<char, Capacity + 1U, containers::ZeroedBuffer> m_rawstring;
char m_rawstring[Capacity + 1]{};
uint64_t m_rawstringSize{0U};
};

Expand Down
11 changes: 7 additions & 4 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,13 @@ class variant
constexpr uint64_t index() const noexcept;

private:
// NOLINTJUSTIFICATION safe access is guaranteed since the c-array is wrapped inside the variant class
// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays)
alignas(algorithm::maxVal(alignof(Types)...)) internal::byte_t m_storage[TYPE_SIZE]{0U};
// NOLINTEND(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays)
struct alignas(Types...) storage_t
{
// NOLINTJUSTIFICATION safe access is guaranteed since the c-array is wrapped inside the variant class
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays)
internal::byte_t data[TYPE_SIZE];
};
storage_t m_storage{};
uint64_t m_type_index{INVALID_VARIANT_INDEX};

private:
Expand Down
4 changes: 2 additions & 2 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include "iceoryx_hoofs/cxx/algorithm.hpp"
#include "iceoryx_hoofs/cxx/attributes.hpp"
#include "iceoryx_hoofs/cxx/requires.hpp"
#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iceoryx_hoofs/log/logging.hpp"
#include "iox/uninitialized_array.hpp"

#include <algorithm>
#include <cstdint>
Expand Down Expand Up @@ -219,7 +219,7 @@ class vector final

void clearFrom(const uint64_t startPosition) noexcept;

containers::UninitializedArray<T, Capacity> m_data;
UninitializedArray<T, Capacity> m_data;
uint64_t m_size{0U};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define IOX_HOOFS_CONCURRENT_FIFO_HPP

#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/internal/containers/uninitialized_array.hpp"
#include "iox/uninitialized_array.hpp"

#include <atomic>

Expand Down Expand Up @@ -54,7 +54,7 @@ class FiFo
bool is_full() const noexcept;

private:
containers::UninitializedArray<ValueType, Capacity> m_data;
UninitializedArray<ValueType, Capacity> m_data;
std::atomic<uint64_t> m_write_pos{0};
std::atomic<uint64_t> m_read_pos{0};
};
Expand Down
Loading