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
use super::generic::*;
use crate::base::*;
use core::ptr::NonNull;

/// 1-word reference to an array on the heap that takes ownership of its contained
/// data.
pub type ThinPtrArray<E, L> = SafeArray<E, L, ThinArrayPtr<E, L>>;

/// 2-word reference to an array on the heap that takes ownership of its contained
/// data.
pub type FatPtrArray<E, L> = SafeArray<E, L, FatArrayPtr<E, L>>;

struct LenLabel<L> {
    len: usize,
    label: L,
}

type ThinPtr<E, L> = NonNull<MemBlock<E, LenLabel<L>>>;

/// Thin pointer to a memory block, that implements the `BaseArrayPtr` and
/// `SafeArrayPtr` traits.
#[repr(transparent)]
pub struct ThinArrayPtr<E, L> {
    data: ThinPtr<E, L>,
}

unsafe impl<E, L> BaseArrayPtr<E, L> for ThinArrayPtr<E, L> {
    unsafe fn alloc(len: usize) -> Self {
        Self {
            data: ThinPtr::alloc(len),
        }
    }

    unsafe fn dealloc(&mut self, len: usize) {
        self.data.dealloc(len)
    }

    unsafe fn from_ptr(ptr: *mut u8) -> Self {
        Self {
            data: ThinPtr::from_ptr(ptr),
        }
    }

    fn as_ptr(&self) -> *mut u8 {
        (&self.data).as_ptr()
    }

    fn is_null(&self) -> bool {
        self.data.is_null()
    }

    fn lbl_ptr(&self) -> *mut L {
        unsafe { &mut (&mut *self.data.lbl_ptr()).label }
    }

    fn elem_ptr(&self, idx: usize) -> *mut E {
        self.data.elem_ptr(idx)
    }
}

unsafe impl<E, L> SafeArrayPtr<E, L> for ThinArrayPtr<E, L> {
    fn set_len(&mut self, len: usize) {
        unsafe { (&mut *self.data.lbl_ptr()).len = len }
    }
    fn get_len(&self) -> usize {
        unsafe { (*self.data.lbl_ptr()).len }
    }
}

/// Fat pointer to a memory block, that implements the `BaseArrayPtr` and
/// `SafeArrayPtr` traits.
pub struct FatArrayPtr<E, L> {
    data: NonNull<MemBlock<E, L>>,
    len: usize,
}

unsafe impl<E, L> BaseArrayPtr<E, L> for FatArrayPtr<E, L> {
    unsafe fn alloc(len: usize) -> Self {
        Self {
            data: NonNull::alloc(len),
            len: len,
        }
    }

    unsafe fn dealloc(&mut self, len: usize) {
        self.data.dealloc(len)
    }

    unsafe fn from_ptr(ptr: *mut u8) -> Self {
        Self {
            data: NonNull::from_ptr(ptr),
            len: 0,
        }
    }

    fn as_ptr(&self) -> *mut u8 {
        (&self.data).as_ptr()
    }

    fn is_null(&self) -> bool {
        self.data.is_null()
    }

    fn lbl_ptr(&self) -> *mut L {
        self.data.lbl_ptr()
    }

    fn elem_ptr(&self, idx: usize) -> *mut E {
        self.data.elem_ptr(idx)
    }
}

unsafe impl<E, L> SafeArrayPtr<E, L> for FatArrayPtr<E, L> {
    fn set_len(&mut self, len: usize) {
        self.len = len;
    }
    fn get_len(&self) -> usize {
        self.len
    }
}