iceoryx_doc  1.0.1
loffli.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_UTILS_CONCURRENT_LOFFLI_HPP
18 #define IOX_UTILS_CONCURRENT_LOFFLI_HPP
19 
20 #include "iceoryx_utils/cxx/helplets.hpp"
21 #include "iceoryx_utils/internal/relocatable_pointer/relative_pointer.hpp"
22 
23 #include <atomic>
24 #include <cstdint>
25 
26 namespace iox
27 {
28 namespace concurrent
29 {
30 class LoFFLi
31 {
32  public:
33  using Index_t = uint32_t;
34 
35  private:
36  struct alignas(8) Node
37  {
38  Index_t indexToNextFreeIndex;
39  uint32_t abaCounter;
40  };
41 
42  static_assert(sizeof(Node) <= 8U,
43  "The size of 'Node' must not exceed 8 bytes in order to be lock-free on 64 bit systems!");
44 
56 
59 
61 
67 
68  uint32_t m_size{0U};
69  Index_t m_invalidIndex{0U};
70  std::atomic<Node> m_head{{0U, 1U}};
71  iox::rp::RelativePointer<Index_t> m_nextFreeIndex;
72 
73  public:
74  LoFFLi() = default;
76 
80  void init(cxx::not_null<Index_t*> freeIndicesMemory, const uint32_t capacity) noexcept;
81 
85  bool pop(Index_t& index) noexcept;
86 
90  bool push(const Index_t index) noexcept;
91 
95  static inline constexpr std::size_t requiredIndexMemorySize(const uint32_t capacity) noexcept;
96 };
97 
98 } // namespace concurrent
99 } // namespace iox
100 
101 #include "loffli.inl"
102 
103 #endif // IOX_UTILS_CONCURRENT_LOFFLI_HPP
Definition: loffli.hpp:31
static constexpr std::size_t requiredIndexMemorySize(const uint32_t capacity) noexcept
Definition: loffli.inl:24
void init(cxx::not_null< Index_t * > freeIndicesMemory, const uint32_t capacity) noexcept
bool pop(Index_t &index) noexcept
bool push(const Index_t index) noexcept
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Definition: helplets.hpp:84