1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2023 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Building block to handle inter process communication with multiple shared memory object. Every
//! process has mapped them to a different virtual memory location therefore pointer inside that
//! memory region should be distances starting from a fix point which maybe different in every
//! process.
//!
//! # Example
//!
//! ```
//! use iceoryx2_bb_elementary::relocatable_ptr::*;
//! use iceoryx2_bb_elementary::math::align_to;
//!
//! #[repr(C)]
//! pub struct Container {
//! data_ptr: RelocatablePointer<u128>,
//! capacity: usize,
//! }
//!
//! impl Container {
//! pub fn new(capacity: usize, distance: isize) -> Self {
//! Self {
//! data_ptr: RelocatablePointer::new(distance),
//! capacity
//! }
//! }
//!
//! pub fn get_mut(&mut self, index: usize) -> &mut u128 {
//! unsafe { &mut *self.data_ptr.as_mut_ptr() }
//! }
//! }
//!
//! #[repr(C)]
//! pub struct FixedSizeContainer {
//! base: Container,
//! data: [u128; 128],
//! }
//!
//! impl FixedSizeContainer {
//! pub fn new() -> Self {
//! Self {
//! base: Container::new(128,
//! // the data_ptr is the first member of container. The distance from
//! // the memory location of the RelocatablePointer `data_ptr` is
//! // therefore the size of `Container` aligned to the type `u128`
//! align_to::<u128>(core::mem::size_of::<Container>()) as isize),
//! data: [0; 128]
//! }
//! }
//!
//! pub fn get_mut(&mut self, index: usize) -> &mut u128 {
//! self.base.get_mut(index)
//! }
//! }
//! ```
use ;
pub use PointerTrait;
use AtomicIsize;
use GenericPointer;
;
/// A [`RelocatablePointer`] stores only the distance from its memory starting position to the
/// memory location it is pointing to. When the [`RelocatablePointer`] is now shared between
/// processes its virtual memory starting position changes but the distance to the object it is
/// pointing to is the same.
///
/// **Important:**
/// 1. Every construct which relies on a [`RelocatablePointer`] must be declared with
/// `[repr(C)]`. Otherwise different compilation units may have a different structural layout of
/// the data type which is shared between processes which leads to undefined behavior.
/// 2. The construct which is using the [`RelocatablePointer`] and the pointee must be stored in
/// the same shared memory object. Pointing to a different shared memory segment most likely
/// leads to crashes since it can be mapped in a different order, at a different position and
/// the distance to the memory destination is off.