Skip to content

Commit

Permalink
Merge pull request eclipse-iceoryx#1749 from ApexAI/iox-1614-implemen…
Browse files Browse the repository at this point in the history
…t-uninitialized-array

iox-eclipse-iceoryx#1614 implement uninitialized array
  • Loading branch information
FerdinandSpitzschnueffler authored Nov 1, 2022
2 parents d4ea6f4 + be3a678 commit 3d96597
Show file tree
Hide file tree
Showing 32 changed files with 614 additions and 399 deletions.
3 changes: 3 additions & 0 deletions .clang-tidy-diff-scans.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
./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_*

# IMPORTANT:
# after the first # everything is considered a comment, add new files and
# directories only at the top of this file
Expand Down
19 changes: 19 additions & 0 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- Support [Bazel](https://bazel.build/) as optional build system [\#1542](https://github.com/eclipse-iceoryx/iceoryx/issues/1542)
- Support user defined platforms with cmake switch `-DIOX_PLATFORM_PATH` [\#1619](https://github.com/eclipse-iceoryx/iceoryx/issues/1619)
- Added 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)

**Bugfixes:**

Expand Down Expand Up @@ -783,3 +784,21 @@

* `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

```cpp
// before
char myCharArray[Capacity];

using element_t = uint8_t[sizeof(T)];
alignas(T) element_t myAlignedArray[Capacity];

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

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

containers::UninitializedArray<T, Capacity> myAlignedArray;

```
11 changes: 4 additions & 7 deletions iceoryx_dust/include/iceoryx_dust/cxx/forward_list.hpp
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 All @@ -19,6 +19,7 @@
#define IOX_DUST_CXX_FORWARD_LIST_HPP

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

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

// @todo iox-#1614 will be replaced by uninitialized array
// NOLINTBEGIN(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
NodeLink m_links[NODE_LINK_COUNT];
using element_t = uint8_t[sizeof(T)];
alignas(T) element_t m_data[Capacity];
// NOLINTEND(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
containers::UninitializedArray<NodeLink, NODE_LINK_COUNT> m_links;
containers::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,6 +19,7 @@
#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"
Expand Down Expand Up @@ -141,18 +142,14 @@ class NamedPipe : public DesignPattern::Creation<NamedPipe, IpcChannelError>
MessageQueue_t messages;

private:
static constexpr uint64_t SEND_SEMAPHORE = 0U;
static constexpr uint64_t RECEIVE_SEMAPHORE = 1U;

static constexpr uint64_t INVALID_DATA = 0xBAADF00DAFFEDEAD;
static constexpr uint64_t VALID_DATA = 0xBAD0FF1CEBEEFBEE;
static constexpr units::Duration WAIT_FOR_INIT_TIMEOUT = units::Duration::fromSeconds(1);
static constexpr units::Duration WAIT_FOR_INIT_SLEEP_TIME = units::Duration::fromMilliseconds(1);

std::atomic<uint64_t> initializationGuard{INVALID_DATA};
/// NOLINTJUSTIFICATION @todo iox-#1614 replace with cxx::array implementation
/// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
cxx::optional<UnnamedSemaphore> semaphores[2U];
cxx::optional<UnnamedSemaphore> m_sendSemaphore;
cxx::optional<UnnamedSemaphore> m_receiveSemaphore;
};


Expand Down
8 changes: 4 additions & 4 deletions iceoryx_dust/source/posix_wrapper/named_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ NamedPipe::NamedPipeData::NamedPipeData(bool& isInitialized,
UnnamedSemaphoreBuilder()
.initialValue(maxMsgNumber)
.isInterProcessCapable(true)
.create(semaphores[SEND_SEMAPHORE])
.create(m_sendSemaphore)
.or_else([&](auto) { signalError("send"); });

if (!isInitialized)
Expand All @@ -352,7 +352,7 @@ NamedPipe::NamedPipeData::NamedPipeData(bool& isInitialized,
UnnamedSemaphoreBuilder()
.initialValue(0U)
.isInterProcessCapable(true)
.create(semaphores[RECEIVE_SEMAPHORE])
.create(m_receiveSemaphore)
.or_else([&](auto) { signalError("receive"); });

if (!isInitialized)
Expand All @@ -365,12 +365,12 @@ NamedPipe::NamedPipeData::NamedPipeData(bool& isInitialized,

UnnamedSemaphore& NamedPipe::NamedPipeData::sendSemaphore() noexcept
{
return *semaphores[SEND_SEMAPHORE];
return *m_sendSemaphore;
}

UnnamedSemaphore& NamedPipe::NamedPipeData::receiveSemaphore() noexcept
{
return *semaphores[RECEIVE_SEMAPHORE];
return *m_receiveSemaphore;
}

bool NamedPipe::NamedPipeData::waitForInitialization() const noexcept
Expand Down
1 change: 1 addition & 0 deletions iceoryx_hoofs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The module structure is a logical grouping. It is replicated for `concurrent` an
|:---------------------:|:--------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|`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 |

### Vocabulary types (vocabulary)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#define IOX_HOOFS_CONCURRENT_LOCKFREE_QUEUE_HPP

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

#include <atomic>

Expand Down Expand Up @@ -102,7 +102,6 @@ class LockFreeQueue

protected:
using Queue = IndexQueue<Capacity>;
using BufferIndex = typename Queue::value_t;

// remark: actually m_freeIndices do not have to be in a queue, it could be another
// multi-push multi-pop capable lockfree container (e.g. a stack or a list)
Expand All @@ -111,24 +110,24 @@ class LockFreeQueue
// required to be a queue for LockFreeQueue to exhibit FIFO behaviour
Queue m_usedIndices;

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

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

// template is needed to distinguish between lvalue and rvalue T references
// (universal reference type deduction)
template <typename T>
void writeBufferAt(const BufferIndex& index, T&& value) noexcept;
void writeBufferAt(const uint64_t& index, T&& value) noexcept;

// needed to avoid code duplication (via universal reference type deduction)
template <typename T>
iox::cxx::optional<ElementType> pushImpl(T&& value) noexcept;

cxx::optional<ElementType> readBufferAt(const BufferIndex& index) noexcept;
cxx::optional<ElementType> readBufferAt(const uint64_t& index) noexcept;
};
} // namespace concurrent
} // namespace iox

#include "iceoryx_hoofs/internal/concurrent/lockfree_queue/lockfree_queue.inl"

#endif // IOX_HOOFS_CONCURRENT_LOCKFREE_QUEUE_HPP
#endif // IOX_HOOFS_CONCURRENT_LOCKFREE_QUEUE_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class ResizeableLockFreeQueue : protected LockFreeQueue<ElementType, MaxCapacity
bool setCapacity(const uint64_t newCapacity) noexcept;

private:
using BufferIndex = typename Base::BufferIndex;
using BufferIndex = uint64_t;
std::atomic<uint64_t> m_capacity{MaxCapacity};
// must be operator= otherwise it is undefined, see https://en.cppreference.com/w/cpp/atomic/ATOMIC_FLAG_INIT
std::atomic_flag m_resizeInProgress = ATOMIC_FLAG_INIT;
Expand Down
11 changes: 3 additions & 8 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define IOX_HOOFS_CXX_LIST_HPP

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

#include <cstdint>
#include <iostream>
Expand Down Expand Up @@ -382,14 +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())

/// @NOLINTJUSTIFICATION @todo iox-#1614 will be replaced by uninitialized array
/// @NOLINTBEGIN(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
NodeLink m_links[NODE_LINK_COUNT];
using element_t = uint8_t[sizeof(T)];
alignas(T) element_t m_data[Capacity];
/// @NOLINTEND(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)

containers::UninitializedArray<NodeLink, NODE_LINK_COUNT> m_links;
containers::UninitializedArray<T, Capacity> m_data;
size_type m_size{0U};
}; // list

Expand Down
8 changes: 1 addition & 7 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#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 @@ -230,12 +229,7 @@ class optional final : public FunctionalInterface<optional<T>, T, void>
// initHandle(&handle);
// }
bool m_hasValue{false};

private:
// 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)
alignas(T) byte_t m_data[sizeof(T)];
typename std::aligned_storage<sizeof(T), alignof(T)>::type m_data;

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

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

#include <cstdint>

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

// AXIVION Next Construct AutosarC++19_03-A18.1.1 : safe access is guaranteed since the char array is wrapped inside
// the stack class
/// @NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
using element_t = uint8_t[sizeof(T)];
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : safe access is guaranteed since the char array is wrapped inside
// the stack class
/// @NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
alignas(T) element_t m_data[Capacity];
containers::UninitializedArray<T, Capacity> m_data;
uint64_t m_size{0U};
};
} // namespace cxx
Expand Down
3 changes: 2 additions & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#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 @@ -608,7 +609,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)
char m_rawstring[Capacity + 1U]{'\0'};
containers::UninitializedArray<char, Capacity + 1U, containers::ZeroedBuffer> m_rawstring;
uint64_t m_rawstringSize{0U};
};

Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class variant
constexpr uint64_t index() const noexcept;

private:
/// @todo iox-#1614 Replace with UninitializedArray
// 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)
Expand Down
13 changes: 2 additions & 11 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#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 <algorithm>
Expand All @@ -36,8 +37,6 @@ namespace cxx
/// @attention Out of bounds access or accessing an empty vector can lead to a program termination!
///
template <typename T, uint64_t Capacity>
// NOLINTJUSTIFICATION @todo iox-#1614 will be solved with upcoming uninitialized array
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
class vector final
{
public:
Expand Down Expand Up @@ -220,15 +219,7 @@ class vector final

void clearFrom(const uint64_t startPosition) noexcept;

// AXIVION Next Construct AutosarC++19_03-A18.1.1 : safe access is guaranteed since the C-style array is wrapped
// inside the vector class
/// @todo iox-#1614 Replace with UninitializedArray
// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays)
using element_t = uint8_t[sizeof(T)];
// AXIVION Next Construct AutosarC++19_03-A18.1.1 : safe access is guaranteed since the C-style array is wrapped
// inside the vector class
alignas(T) element_t m_data[Capacity];
// NOLINTEND(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays)
containers::UninitializedArray<T, Capacity> m_data;
uint64_t m_size{0U};
};

Expand Down
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 All @@ -18,6 +18,7 @@
#define IOX_HOOFS_CONCURRENT_FIFO_HPP

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

#include <atomic>

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

private:
// safe access is guaranteed since the char array is wrapped inside the FiFo class
// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
ValueType m_data[Capacity];
containers::UninitializedArray<ValueType, Capacity> m_data;
std::atomic<uint64_t> m_write_pos{0};
std::atomic<uint64_t> m_read_pos{0};
};
Expand Down
Loading

0 comments on commit 3d96597

Please sign in to comment.