iceoryx_doc  1.0.1
base_relative_pointer.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 
18 #ifndef IOX_UTILS_RELOCATABLE_POINTER_BASE_RELATIVE_POINTER_HPP
19 #define IOX_UTILS_RELOCATABLE_POINTER_BASE_RELATIVE_POINTER_HPP
20 
21 #include "pointer_repository.hpp"
22 
23 #include <cstdint>
24 #include <limits>
25 
26 namespace iox
27 {
28 namespace rp
29 {
53 {
54  public:
55  using id_t = uint64_t;
56  using ptr_t = void*;
57  using const_ptr_t = const void* const;
58  using offset_t = std::uintptr_t;
59 
63  BaseRelativePointer(ptr_t ptr, id_t id) noexcept;
64 
68  BaseRelativePointer(offset_t offset, id_t id) noexcept;
69 
72  BaseRelativePointer(ptr_t ptr = nullptr) noexcept;
73 
76  BaseRelativePointer(const BaseRelativePointer& other) noexcept;
77 
81 
85  BaseRelativePointer& operator=(const BaseRelativePointer& other) noexcept;
86 
90  BaseRelativePointer& operator=(void* ptr) noexcept;
91 
95  BaseRelativePointer& operator=(BaseRelativePointer&& other) noexcept;
96 
99  ptr_t get() const noexcept;
100 
103  id_t getId() const noexcept;
104 
107  offset_t getOffset() const noexcept;
108 
111  ptr_t getBasePtr() const noexcept;
112 
117  static id_t registerPtr(const ptr_t ptr, uint64_t size = 0U) noexcept;
118 
124  static bool registerPtr(const id_t id, const ptr_t ptr, uint64_t size = 0U) noexcept;
125 
129  static bool unregisterPtr(const id_t id) noexcept;
130 
134  static ptr_t getBasePtr(const id_t id) noexcept;
135 
137  static void unregisterAll() noexcept;
138 
143  static offset_t getOffset(const id_t id, const_ptr_t ptr) noexcept;
144 
149  static ptr_t getPtr(const id_t id, const offset_t offset) noexcept;
150 
154  static id_t searchId(ptr_t ptr) noexcept;
155 
159  static bool isValid(id_t id) noexcept;
160 
163  static PointerRepository<id_t, ptr_t>& getRepository() noexcept;
164 
168  offset_t computeOffset(ptr_t ptr) const noexcept;
169 
172  ptr_t computeRawPtr() const noexcept;
173 
174  static constexpr id_t NULL_POINTER_ID = std::numeric_limits<id_t>::max();
175  static constexpr offset_t NULL_POINTER_OFFSET = std::numeric_limits<offset_t>::max();
176 
177  protected:
178  id_t m_id{NULL_POINTER_ID};
179  offset_t m_offset{NULL_POINTER_OFFSET};
180 };
181 } // namespace rp
182 } // namespace iox
183 
184 #endif // IOX_UTILS_RELOCATABLE_POINTER_BASE_RELATIVE_POINTER_HPP
185 
pointer class to use when pointer and pointee are located in different shared memory segments We can ...
Definition: base_relative_pointer.hpp:53
static PointerRepository< id_t, ptr_t > & getRepository() noexcept
returns the pointer repository
BaseRelativePointer(offset_t offset, id_t id) noexcept
constructs a BaseRelativePointer from a given offset and segment id
id_t getId() const noexcept
returns the id which identifies the segment
ptr_t computeRawPtr() const noexcept
get the pointer from stored id and offset
BaseRelativePointer(ptr_t ptr=nullptr) noexcept
constructs a BaseRelativePointer pointing to the same pointer as ptr
static bool unregisterPtr(const id_t id) noexcept
unregisters ptr with given id
BaseRelativePointer(ptr_t ptr, id_t id) noexcept
constructs a BaseRelativePointer pointing to the same pointee as ptr in a segment identified by id
ptr_t get() const noexcept
access to the underlying object
static bool isValid(id_t id) noexcept
checks if given id is valid
static void unregisterAll() noexcept
unregisters all ptr id pairs (leads to initial state)
ptr_t getBasePtr() const noexcept
get the base pointer associated with this' id
static id_t registerPtr(const ptr_t ptr, uint64_t size=0U) noexcept
registers a memory segment at ptr with size of a new id
offset_t computeOffset(ptr_t ptr) const noexcept
get the offset from the start address of the segment and ptr
offset_t getOffset() const noexcept
returns the offset
static ptr_t getPtr(const id_t id, const offset_t offset) noexcept
get the pointer from id and offset ("inverse" to getOffset)
static id_t searchId(ptr_t ptr) noexcept
get the id for a given ptr
Allows registration of memory segments with their start pointers and size. This class is used to reso...
Definition: pointer_repository.hpp:38
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28