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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) 2024 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
//! A [`ResizableSharedMemory`] is identified by a name and allows multiple processes to share
//! memory between them (inter-process memory). One process owns the [`ResizableSharedMemory`]
//! which can be created via the [`ResizableSharedMemoryBuilder`] and many processes can have a
//! [`ResizableSharedMemoryView`] that can be constructed via [`ResizableSharedMemoryViewBuilder`].
//!
//! The [`ResizableSharedMemoryView`] never owns the [`ResizableSharedMemory`] and has only
//! read-only access to it while the [`ResizableSharedMemory`] can use
//! [`ResizableSharedMemory::allocate()`] to acquire memory and distribute it between the
//! [`ResizableSharedMemoryView`]s.
//!
//! Whenever the [`ResizableSharedMemoryView`] receives an offset it must be registered via
//! [`ResizableSharedMemoryView::register_and_translate_offset()`] and unregistered via
//! [`ResizableSharedMemoryView::unregister_offset()`]. As soon as the [`ResizableSharedMemory`]
//! calls [`ResizableSharedMemory::deallocate()`] unused [`SharedMemory`] segments may be recycled.
//!
//! # Example
//!
//! ```
//! // owner of the ResizableSharedMemory
//! use iceoryx2_bb_posix::access_mode::AccessMode;
//! use iceoryx2_cal::resizable_shared_memory::*;
//! use iceoryx2_bb_system_types::file_name::FileName;
//! use iceoryx2_cal::shm_allocator::ShmAllocator;
//! use iceoryx2_cal::shared_memory::SharedMemory;
//! use iceoryx2_cal::named_concept::*;
//! use core::alloc::Layout;
//! use core::time::Duration;
//!
//! fn example<Alloc: ShmAllocator, Shm: SharedMemory<Alloc>, Memory: ResizableSharedMemory<Alloc, Shm>>(
//! name: &FileName
//! ) {
//! // owner process creates a new memory
//! let memory = Memory::MemoryBuilder::new(name)
//! // hint to the underlying allocator that we need up to 128 chunks of memory
//! //
//! // note: as soon as there are more than 128 chunks requested a new resized segment is
//! // created
//! .max_number_of_chunks_hint(128)
//! // hint to the underlying allocator that the chunks are not exceeding the Layout of
//! // [`u16`]
//! //
//! // note: as soon as there is a chunk requested that exceeds the provided layout hint a
//! // new resized segment is created
//! .max_chunk_layout_hint(Layout::new::<u32>())
//! // defines the strategy how segments are resized
//! .allocation_strategy(AllocationStrategy::PowerOfTwo)
//! .create().unwrap();
//!
//! let chunk = memory.allocate(Layout::new::<u16>()).unwrap();
//! // store the value 123 in the newly allocated chunk
//! unsafe { (chunk.data_ptr as *mut u16).write(123) };
//!
//! // since this exceeds the the chunk layout hint, the underlying segment is resized
//! // following the provided allocation strategy
//! let another_chunk = memory.allocate(Layout::new::<u64>()).unwrap();
//! // release allocated chunk
//! unsafe { memory.deallocate(another_chunk.offset, Layout::new::<u64>()) };
//!
//!
//! let view = Memory::ViewBuilder::new(name)
//! // defines how long the builder shall wait to open the corresponding segments when
//! // the creator is concurrently creating them.
//! .timeout(Duration::from_secs(1))
//! .open(AccessMode::ReadWrite).unwrap();
//!
//! // before we can consume the received offset we need to translate it into our local
//! // process space
//! // this operation also maps unmapped segments into the process space if required.
//! let ptr = unsafe { view.register_and_translate_offset(chunk.offset).unwrap() };
//! println!("received {}", unsafe {*(ptr as *const u16)});
//!
//! // when we are finished consuming the memory we need to unregister the offset. this
//! // unmaps segments that are no longer used.
//! unsafe { view.unregister_offset(chunk.offset) };
//! }
//! ```
pub use crate;
use Layout;
use Debug;
use Duration;
use enum_gen;
use Abandonable;
use AccessMode;
use crate*;
use crate;
use crate;
enum_gen!
/// Creates a [`ResizableSharedMemoryView`] to an existing [`ResizableSharedMemory`] and maps the
/// [`ResizableSharedMemory`] read-only into the process space.
/// Creates a new [`ResizableSharedMemory`] which the process will own. As soon as the
/// corresponding object goes out-of-scope the underlying [`SharedMemory`] resources will be
/// removed.
/// A read-only view to a [`ResizableSharedMemory`]. Can be created by arbitrary many processes.
/// The [`ResizableSharedMemory`] can be only owned by exactly one process that is allowed to
/// [`ResizableSharedMemory::allocate()`] memory and distribute the memory to all
/// [`ResizableSharedMemoryView`]s.