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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//! A reference counting smart pointer.
use crate::{
    alloc::NSTDAllocator,
    core::{mem::nstd_core_mem_copy, optional::NSTDOptional},
    NSTDAny, NSTDAnyMut, NSTDUInt,
};
use nstdapi::nstdapi;

/// The size (in bytes) of [usize].
const USIZE_SIZE: usize = core::mem::size_of::<usize>();

/// A reference counting smart pointer.
#[nstdapi]
pub struct NSTDSharedPtr<'a> {
    /// The memory allocator.
    allocator: &'a NSTDAllocator,
    /// A raw pointer to private data about the shared object.
    ptr: NSTDAnyMut,
    /// The size of the shared pointer's memory buffer.
    size: NSTDUInt,
}
impl NSTDSharedPtr<'_> {
    /// Returns a copy of the number of pointers sharing the object.
    #[inline]
    fn ptrs(&self) -> usize {
        // SAFETY:
        // - Shared pointers are always non-null.
        // - Shared pointers never allocate more than `isize::MAX` bytes for their value.
        unsafe { core::ptr::read_unaligned(self.ptr.add(nstd_shared_ptr_size(self)).cast()) }
    }

    /// Returns a mutable pointer to the number of pointers sharing the object.
    ///
    /// # Note
    ///
    /// The returned pointer may be unaligned, so reading/writing must be done with
    /// [core::ptr::read_unaligned] and [core::ptr::write_unaligned].
    #[inline]
    fn ptrs_mut(&self) -> *mut usize {
        // SAFETY:
        // - Shared pointers are always non-null.
        // - Shared pointers never allocate more than `isize::MAX` bytes for their value.
        unsafe { self.ptr.add(nstd_shared_ptr_size(self)).cast() }
    }
}
impl Drop for NSTDSharedPtr<'_> {
    /// [NSTDSharedPtr]'s destructor.
    fn drop(&mut self) {
        // SAFETY: Shared pointers are always non-null.
        unsafe {
            // Update the pointer count.
            let ptrs = self.ptrs_mut();
            let new_size = self.ptrs() - 1;
            core::ptr::write_unaligned(ptrs, new_size);
            // If the pointer count is zero, free the data.
            if new_size == 0 {
                (self.allocator.deallocate)(self.allocator.state, &mut self.ptr, self.size);
            }
        }
    }
}

/// Represents an optional value of type `NSTDSharedPtr`.
pub type NSTDOptionalSharedPtr<'a> = NSTDOptional<NSTDSharedPtr<'a>>;

/// Creates a new initialized instance of a shared pointer.
///
/// # Parameters:
///
/// - `const NSTDAllocator *allocator` - The memory allocator.
///
/// - `NSTDUInt element_size` - The size of the shared object.
///
/// - `NSTDAny init` - A pointer to the object to initialize the shared pointer with.
///
/// # Returns
///
/// `NSTDOptionalSharedPtr shared_ptr` - The new shared pointer, or an uninitialized "none" variant
/// if allocating fails.
///
/// # Safety
///
/// `init` must be a pointer to a value that is valid for reads of `element_size` bytes.
///
/// # Example
///
/// ```
/// use core::ptr::addr_of;
/// use nstd_sys::{
///     alloc::NSTD_ALLOCATOR,
///     shared_ptr::{nstd_shared_ptr_get, nstd_shared_ptr_new},
/// };
///
/// const SIZE: usize = core::mem::size_of::<i16>();
///
/// let v = i16::MIN;
/// unsafe {
///     let shared_ptr = nstd_shared_ptr_new(&NSTD_ALLOCATOR, SIZE, addr_of!(v).cast()).unwrap();
///     assert!(*nstd_shared_ptr_get(&shared_ptr).cast::<i16>() == v);
/// }
/// ```
#[nstdapi]
pub unsafe fn nstd_shared_ptr_new(
    allocator: &NSTDAllocator,
    element_size: NSTDUInt,
    init: NSTDAny,
) -> NSTDOptionalSharedPtr {
    // Allocate a region of memory for the object and the pointer count.
    let buffer_size = element_size + USIZE_SIZE;
    let raw = (allocator.allocate)(allocator.state, buffer_size);
    if raw.is_null() {
        return NSTDOptional::None;
    }
    // Initialize the shared object.
    nstd_core_mem_copy(raw.cast(), init.cast(), element_size);
    // Set the pointer count to one.
    let ptrs = raw.add(element_size).cast::<usize>();
    core::ptr::write_unaligned(ptrs, 1);
    // Construct the pointer.
    NSTDOptional::Some(NSTDSharedPtr {
        allocator,
        ptr: raw,
        size: buffer_size,
    })
}

/// Creates a new zero-initialized instance of a shared pointer.
///
/// # Parameters:
///
/// - `const NSTDAllocator *allocator` - The memory allocator.
///
/// - `NSTDUInt element_size` - The size of the shared object.
///
/// # Returns
///
/// `NSTDOptionalSharedPtr shared_ptr` - The yet to be shared pointer, or an uninitialized "none"
/// variant if allocating fails.
///
/// # Safety
///
/// The data to be stored in the shared pointer must be safely representable by an all-zero byte
/// pattern.
///
/// # Example
///
/// ```
/// use nstd_sys::{
///     alloc::NSTD_ALLOCATOR,
///     shared_ptr::{nstd_shared_ptr_get, nstd_shared_ptr_new_zeroed},
/// };
///
/// const SIZE: usize = core::mem::size_of::<u128>();
///
/// unsafe {
///     let shared_ptr = nstd_shared_ptr_new_zeroed(&NSTD_ALLOCATOR, SIZE).unwrap();
///     assert!(*nstd_shared_ptr_get(&shared_ptr).cast::<u128>() == 0);
/// }
/// ```
#[nstdapi]
pub unsafe fn nstd_shared_ptr_new_zeroed(
    allocator: &NSTDAllocator,
    element_size: NSTDUInt,
) -> NSTDOptionalSharedPtr {
    // SAFETY: The allocated memory is validated after allocation.
    unsafe {
        // Allocate a region of memory for the object and the pointer count.
        let buffer_size = element_size + USIZE_SIZE;
        let raw = (allocator.allocate_zeroed)(allocator.state, buffer_size);
        if raw.is_null() {
            return NSTDOptional::None;
        }
        // Set the pointer count to one.
        let ptrs = raw.add(element_size).cast::<usize>();
        core::ptr::write_unaligned(ptrs, 1);
        // Construct the pointer.
        NSTDOptional::Some(NSTDSharedPtr {
            allocator,
            ptr: raw,
            size: buffer_size,
        })
    }
}

/// Shares `shared_ptr`.
///
/// # Parameters:
///
/// - `const NSTDSharedPtr *shared_ptr` - The shared object to share.
///
/// # Returns
///
/// `NSTDSharedPtr shared` - A new pointer pointing to the shared data.
///
/// # Example
///
/// ```
/// use core::ptr::addr_of;
/// use nstd_sys::{
///     alloc::NSTD_ALLOCATOR,
///     shared_ptr::{nstd_shared_ptr_get, nstd_shared_ptr_new, nstd_shared_ptr_share},
/// };
///
/// const SIZE: usize = core::mem::size_of::<u64>();
///
/// unsafe {
///     let v = 39u64;
///     let share;
///     {
///         let addr = addr_of!(v).cast();
///         let shared_ptr = nstd_shared_ptr_new(&NSTD_ALLOCATOR, SIZE, addr).unwrap();
///         share = nstd_shared_ptr_share(&shared_ptr);
///     }
///     assert!(*nstd_shared_ptr_get(&share).cast::<u64>() == v);
/// }
/// ```
#[inline]
#[nstdapi]
pub fn nstd_shared_ptr_share<'a>(shared_ptr: &NSTDSharedPtr<'a>) -> NSTDSharedPtr<'a> {
    // SAFETY: Shared pointers are always non-null.
    unsafe {
        // Update the pointer count.
        let ptrs = shared_ptr.ptrs_mut();
        core::ptr::write_unaligned(ptrs, shared_ptr.ptrs() + 1);
        // Construct the new shared pointer instance.
        NSTDSharedPtr {
            allocator: shared_ptr.allocator,
            ptr: shared_ptr.ptr,
            size: shared_ptr.size,
        }
    }
}

/// Returns an immutable reference to a shared object's allocator.
///
/// # Parameters:
///
/// - `const NSTDSharedPtr *shared_ptr` - The shared object.
///
/// # Returns
///
/// `const NSTDAllocator *allocator` - The shared object's allocator.
#[inline]
#[nstdapi]
pub fn nstd_shared_ptr_allocator<'a>(shared_ptr: &NSTDSharedPtr<'a>) -> &'a NSTDAllocator {
    shared_ptr.allocator
}

/// Returns the number of pointers that share `shared_ptr`'s data.
///
/// # Parameters:
///
/// - `const NSTDSharedPtr *shared_ptr` - An instance of a shared pointer.
///
/// # Returns
///
/// `NSTDUInt owners` - The number of pointers that share `shared_ptr`'s data.
///
/// # Example
///
/// ```
/// use core::ptr::addr_of;
/// use nstd_sys::{
///     alloc::NSTD_ALLOCATOR,
///     shared_ptr::{
///         nstd_shared_ptr_get, nstd_shared_ptr_new, nstd_shared_ptr_owners, nstd_shared_ptr_share,
///     },
/// };
///
/// const SIZE: usize = core::mem::size_of::<i128>();
///
/// unsafe {
///     let v = i128::MIN;
///     let share;
///     {
///         let addr = addr_of!(v).cast();
///         let shared_ptr = nstd_shared_ptr_new(&NSTD_ALLOCATOR, SIZE, addr).unwrap();
///         assert!(nstd_shared_ptr_owners(&shared_ptr) == 1);
///
///         share = nstd_shared_ptr_share(&shared_ptr);
///         assert!(nstd_shared_ptr_owners(&shared_ptr) == 2);
///
///         let temp = nstd_shared_ptr_share(&shared_ptr);
///         assert!(nstd_shared_ptr_owners(&temp) == 3);
///     }
///     assert!(nstd_shared_ptr_owners(&share) == 1);
///     assert!(*nstd_shared_ptr_get(&share).cast::<i128>() == v);
/// }
/// ```
#[inline]
#[nstdapi]
pub fn nstd_shared_ptr_owners(shared_ptr: &NSTDSharedPtr) -> NSTDUInt {
    shared_ptr.ptrs()
}

/// Returns the size of the shared object.
///
/// # Parameters:
///
/// - `const NSTDSharedPtr *shared_ptr` - The shared pointer.
///
/// # Returns
///
/// `NSTDUInt size` - The size of the shared object.
///
/// # Example
///
/// ```
/// use nstd_sys::{
///     alloc::NSTD_ALLOCATOR,
///     shared_ptr::{nstd_shared_ptr_new_zeroed, nstd_shared_ptr_size},
/// };
///
/// const SIZE: usize = core::mem::size_of::<f64>();
///
/// unsafe {
///     let shared_ptr = nstd_shared_ptr_new_zeroed(&NSTD_ALLOCATOR, SIZE).unwrap();
///     assert!(nstd_shared_ptr_size(&shared_ptr) == SIZE);
/// }
/// ```
#[inline]
#[nstdapi]
pub fn nstd_shared_ptr_size(shared_ptr: &NSTDSharedPtr) -> NSTDUInt {
    shared_ptr.size - USIZE_SIZE
}

/// Returns an immutable raw pointer to the shared object.
///
/// # Parameters:
///
/// - `const NSTDSharedPtr *shared_ptr` - The shared pointer.
///
/// # Returns
///
/// `NSTDAny ptr` - A raw pointer to the shared object.
///
/// # Example
///
/// ```
/// use core::ptr::addr_of;
/// use nstd_sys::{
///     alloc::NSTD_ALLOCATOR,
///     shared_ptr::{nstd_shared_ptr_get, nstd_shared_ptr_new},
/// };
///
/// const SIZE: usize = core::mem::size_of::<u128>();
///
/// let v = u128::MAX;
/// unsafe {
///     let shared_ptr = nstd_shared_ptr_new(&NSTD_ALLOCATOR, SIZE, addr_of!(v).cast()).unwrap();
///     assert!(*nstd_shared_ptr_get(&shared_ptr).cast::<u128>() == v);
/// }
/// ```
#[inline]
#[nstdapi]
pub fn nstd_shared_ptr_get(shared_ptr: &NSTDSharedPtr) -> NSTDAny {
    shared_ptr.ptr
}

/// Frees an instance of `NSTDSharedPtr`.
///
/// # Parameters:
///
/// - `NSTDSharedPtr shared_ptr` - The shared object to free.
#[inline]
#[nstdapi]
#[allow(unused_variables)]
pub fn nstd_shared_ptr_free(shared_ptr: NSTDSharedPtr) {}

/// Frees an instance of `NSTDSharedPtr` after invoking `callback` with the shared object.
///
/// # Parameters:
///
/// - `NSTDSharedPtr shared_ptr` - The shared object to free.
///
/// - `void (*callback)(NSTDAnyMut)` - The shared object's destructor.
///
/// # Safety
///
/// This operation makes a direct call on a C function pointer (`callback`).
#[inline]
#[nstdapi]
pub unsafe fn nstd_shared_ptr_drop(
    shared_ptr: NSTDSharedPtr,
    callback: unsafe extern "C" fn(NSTDAnyMut),
) {
    callback(shared_ptr.ptr);
}