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
use crate::alloc::MemPool;
use crate::alloc::PmemUsage;
use crate::{PSafe, TxOutSafe};
use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;
use std::ops::*;
use std::ptr::NonNull;

#[derive(Eq)]
/// A wrapper around a raw persistent pointer that indicates that the possessor
/// of this wrapper owns the referent. Useful for building abstractions like
/// [`Pbox<T,P>`], [`Vec<T,P>`], and [`String<P>`].
///
/// Just like raw pointers, it contains the address of the object, but the
/// address is static within a file.
///
/// Note that, memory pools are types, not objects. For more information,
/// please see [`MemPool`](../alloc/struct.MemPool.html).
///
/// [`Pbox<T,P>`]: ../boxed/struct.Pbox.html
/// [`Vec<T,P>`]: ../vec/struct.Vec.html
/// [`String<P>`]: ../str/struct.String.html
/// 
pub struct Ptr<T: PSafe + ?Sized, A: MemPool> {
    off: u64,
    marker: PhantomData<(A, T)>,
}

/// `Ptr` pointers are not `Send` because the data they reference may be aliased.
// N.B., this impl is unnecessary, but should provide better error messages.
impl<A: MemPool, T> !Send for Ptr<T, A> {}

/// `Ptr` pointers are not `Sync` because the data they reference may be aliased.
// N.B., this impl is unnecessary, but should provide better error messages.
impl<A: MemPool, T> !Sync for Ptr<T, A> {}
impl<A: MemPool, T> !TxOutSafe for Ptr<T, A> {}

/// The allocator does not need to implement `PSafe`
unsafe impl<A: MemPool, T: PSafe + ?Sized> PSafe for Ptr<T, A> {}

impl<A: MemPool, T: PSafe> Ptr<T, A> {
    #[inline]
    /// Gives a reference to the inner value if it is not dangling, otherwise, None.
    pub(crate) fn try_deref(&self) -> Option<&T> {
        if self.is_dangling() {
            None
        } else {
            Some(&*self)
        }
    }

    #[inline]
    /// Gives a mutable reference to the inner value if it is not dangling, otherwise, None.
    pub(crate) fn try_deref_mut(&mut self) -> Option<&mut T> {
        if self.is_dangling() {
            None
        } else {
            Some(&mut *self)
        }
    }
}

impl<A: MemPool, T: PSafe + ?Sized> Ptr<T, A> {
    #[inline]
    /// Creates new `Ptr` if `p` is valid
    pub(crate) fn new(p: &T) -> Option<Ptr<T, A>> {
        if let Ok(off) = A::off(p) {
            Some(Self {
                off,
                marker: PhantomData,
            })
        } else {
            None
        }
    }

    #[inline]
    /// Returns the mutable reference of the value
    pub(crate) fn as_mut(&mut self) -> &mut T {
        unsafe { A::get_mut_unchecked(self.off) }
    }

    #[inline]
    /// Returns the reference of the value
    pub(crate) fn as_ref(&self) -> &T {
        unsafe { A::get_unchecked(self.off) }
    }

    #[inline]
    /// Returns the mutable raw pointer of the value
    pub(crate) fn as_mut_ptr(&mut self) -> *mut T {
        unsafe { A::get_mut_unchecked(self.off) }
    }

    #[inline]
    /// Returns the mutable raw pointer of the value
    pub(crate) fn as_ptr(&self) -> *const T {
        unsafe { A::get_mut_unchecked(self.off) }
    }

    #[inline]
    #[allow(clippy::mut_from_ref)]
    /// Returns the mutable reference of the value
    pub(crate) fn get_mut(&self) -> &mut T {
        unsafe { A::get_mut_unchecked(self.off) }
    }

    /// Creates a new copy of data and returns a `Ptr` pointer
    ///
    /// # Safety
    ///
    /// The compiler would not drop the copied data. Developer has the
    /// responsibility of deallocating inner value. Also, it does not clone the
    /// inner value. Instead, it just copies the memory.
    pub(crate) unsafe fn dup(&self) -> Ptr<T, A> {
        let src = self.as_ref();
        let dst = A::alloc_for_value(src);
        let trg = A::off_unchecked(dst);
        std::ptr::copy_nonoverlapping(
            src as *const T as *const u8,
            dst as *mut T as *mut u8,
            std::alloc::Layout::for_value(src).size(),
        );
        Ptr::from_off_unchecked(trg)
    }

    #[inline]
    /// Checks if this pointer is dangling
    pub fn is_dangling(&self) -> bool {
        self.off == u64::MAX
    }

    #[inline]
    /// Returns the file offset
    pub fn off(&self) -> u64 {
        self.off
    }


    #[inline]
    /// Returns a reference to the file offset
    pub fn off_ref(&self) -> &u64 {
        &self.off
    }

    #[inline]
    /// Returns a reference to the file offset
    pub(crate) fn off_mut(&self) -> &mut u64 {
        unsafe { &mut *(&self.off as *const u64 as *mut u64) }
    }

    #[inline]
    pub(crate) fn replace(&self, new: u64) -> u64 {
        let old = self.off;
        *self.off_mut() = new;
        old
    }

    /// Creates a new `Ptr`.
    ///
    /// # Safety
    ///
    /// `ptr` must be non-null and in the valid address range.
    #[inline]
    pub(crate) unsafe fn new_unchecked(ptr: *const T) -> Self {
        Self {
            off: A::off_unchecked(ptr),

            marker: PhantomData,
        }
    }

    #[inline]
    /// Creates a new `Ptr` that is dangling.
    pub(crate) const fn dangling() -> Ptr<T, A> {
        Self {
            off: u64::MAX,
            marker: PhantomData,
        }
    }

    #[inline]
    /// Creates new `Ptr` from file offset and len
    ///
    /// # Safety
    ///
    /// `off` should be valid
    pub(crate) const unsafe fn from_off_unchecked(off: u64) -> Ptr<T, A> {
        Self {
            off,
            marker: PhantomData,
        }
    }

    #[inline]
    /// Consumes self as converts in to `Option<Self>` considering it whether
    /// points to a valid address or not.
    pub(crate) fn as_option(&mut self) -> Option<&mut Self> {
        if self.is_dangling() {
            None
        } else {
            debug_assert!(A::allocated(self.off(), 1), "Access Violation at address 0x{:x}", self.off());
            Some(self)
        }
    }

    #[inline]
    /// Instantiates a `Ptr` pointer. It is dangling if input is None.
    pub(crate) fn from_option<U: PSafe>(p: Option<Self>) -> Ptr<U, A> {
        if let Some(p) = p {
            Ptr {
                off: p.off,
                marker: PhantomData,
            }
        } else {
            Ptr::<U, A>::dangling()
        }
    }

    #[inline]
    /// Creates a `Ptr` type if the input `off` is valid
    pub(crate) fn try_from<'a, U: 'a + PSafe>(off: u64) -> Option<Ptr<U, A>> {
        if off == u64::MAX || !A::contains(off) {
            None
        } else {
            unsafe { Some(Ptr::<U, A>::from_off_unchecked(off)) }
        }
    }

    #[inline]
    /// Casts to a pointer of another type.
    pub(crate) fn cast<U: PSafe>(&self) -> Ptr<U, A> {
        Ptr::<U, A> {
            off: self.off,
            marker: PhantomData,
        }
    }
}

// impl<A: MemPool, T: PSafe + ?Sized> MemLayout for Ptr<T, A> {
//     fn layout(&self) -> Layout {
//         Layout::new::<32>()
//     }
// }

unsafe impl<A: MemPool, T: PSafe> PSafe for Ptr<[T], A> {}

impl<A: MemPool, T: PSafe + ?Sized> Copy for Ptr<T, A> {}

impl<A: MemPool, T: PSafe + ?Sized> Clone for Ptr<T, A> {
    fn clone(&self) -> Self {
        Self {
            off: self.off,
            marker: PhantomData,
        }
    }
}

impl<A: MemPool, T: PSafe> PmemUsage for Ptr<T, A> {
    fn size_of() -> usize {
        std::mem::size_of::<T>() + std::mem::size_of::<Self>()
    }
}

impl<A: MemPool, T: PSafe + ?Sized> Ptr<T, A> {    
    #[inline]
    #[track_caller]
    pub(crate) fn from_raw(other: *const T) -> Self {
        let off = if !other.is_null() {
            A::off(other).unwrap()
        } else {
            0
        };
        Self {
            off,
            marker: PhantomData,
        }
    }
    
    #[inline]
    #[track_caller]
    pub(crate) fn from_ref(other: &T) -> Self {
        Self {
            off: A::off(other as *const T).unwrap(),
            marker: PhantomData,
        }
    }
    
    #[inline]
    #[track_caller]
    pub(crate) fn from_mut(other: &mut T) -> Self {
        Self {
            off: A::off(other as *const T).unwrap(),
            marker: PhantomData,
        }
    }
    
    #[inline]
    #[track_caller]
    pub(crate) fn from_non_null(other: NonNull<T>) -> Self {
        Self {
            off: A::off(other.as_ptr()).unwrap(),
            marker: PhantomData,
        }
    }
}

// impl<A: MemPool + Eq, T: Ord + PSafe + ?Sized> Ord for Ptr<T, A> {
//     #[inline]
//     fn cmp(&self, other: &Self) -> Ordering {
//         self.off.cmp(&other.off)
//     }
// }

// impl<A: MemPool, T: PSafe + ?Sized> PartialOrd for Ptr<T, A> {
//     #[inline]
//     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
//         Some(self.off.cmp(&other.off))
//     }
// }

impl<A: MemPool, T: PSafe + ?Sized> PartialEq for Ptr<T, A> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.off == other.off
    }
}

// impl<A: MemPool, T: PSafe> PartialOrd<u64> for Ptr<T, A> {
//     #[inline]
//     fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
//         Some(self.off.cmp(&other))
//     }
// }

// impl<A: MemPool, T: PSafe> PartialEq<u64> for Ptr<T, A> {
//     #[inline]
//     fn eq(&self, other: &u64) -> bool {
//         self.off == *other
//     }
// }

impl<A: MemPool, T: PSafe + ?Sized> Deref for Ptr<T, A> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T {
        self.as_ref()
    }
}

impl<A: MemPool, T: PSafe + ?Sized> DerefMut for Ptr<T, A> {
    #[inline]
    fn deref_mut(&mut self) -> &mut T {
        self.as_mut()
    }
}

impl<A: MemPool, T: Debug + PSafe + ?Sized> Debug for Ptr<T, A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.as_ref())
    }
}

impl<A: MemPool, T: Display + PSafe + ?Sized> Display for Ptr<T, A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_ref())
    }
}