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
use std::mem;
use std::ptr;
use std::slice;

/// This is used as starting point to implement other types. See for example the code for `RcArray`.
pub struct PrefixedArray<I: PrefixedArrayInfo, T> {
    inner: *mut Inner<I, T>,
}

pub trait PrefixedArrayInfo {
    fn len(&self) -> usize;

    fn capacity(&self) -> usize;
}

#[repr(C)]
struct Inner<I, T> {
    info: I,
    data: [T; 0],
}

impl<I: PrefixedArrayInfo, T> PrefixedArray<I, T> {
    pub fn allocate(info: I) -> Self {
        assert_ne!(0, ::std::mem::size_of::<T>(), "ZST not supported");
        let mut data = Vec::<I>::with_capacity(Self::vec_cap(info.capacity()));
        let inner = data.as_mut_ptr();
        unsafe {
            ptr::write(&mut *inner, info);
        }
        mem::forget(data);
        PrefixedArray { inner: inner as _ }
    }

    fn vec_cap(len: usize) -> usize {
        // TODO: wrapping
        // TODO: allocate bytes, alignment?
        let bytes = mem::size_of::<I>() + mem::size_of::<T>() * len;
        div_round_up(bytes, mem::size_of::<I>())
    }

    /// Write `value` to inner data at `offset` using `std::ptr::write`.
    #[inline]
    pub unsafe fn write(&mut self, offset: usize, value: T) {
        ptr::write(self.data_mut().offset(offset as isize), value);
    }

    /// Returns the inner data as a slice with length `info.len()`.
    #[inline]
    pub fn as_slice(&self) -> &[T] {
        unsafe { slice::from_raw_parts(self.data(), self.len()) }
    }

    /// Returns the inner data as a mutable slice with length `info.len()`.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { slice::from_raw_parts_mut(self.data_mut(), self.len()) }
    }

    /// Creates a new `PrefixedArray` that share the inner data with `self`.
    #[inline]
    pub unsafe fn clone_shallow(&self) -> Self {
        PrefixedArray { inner: self.inner }
    }

    /// Drop the array elements, the info and deallocate.
    #[inline]
    pub unsafe fn drop_and_deallocate(&mut self) {
        let cap = self.info().capacity();
        ptr::drop_in_place(self.as_mut_slice());
        ptr::drop_in_place(&mut *self);
        Vec::from_raw_parts(self.inner, 0, Self::vec_cap(cap));
    }

    pub fn info(&self) -> &I {
        &self.inner().info
    }

    pub fn info_mut(&mut self) -> &mut I {
        &mut self.inner_mut().info
    }

    fn data(&self) -> *const T {
        self.inner().data.as_ptr()
    }

    fn data_mut(&mut self) -> *mut T {
        self.inner_mut().data.as_mut_ptr()
    }

    fn len(&self) -> usize {
        self.info().len()
    }

    fn inner(&self) -> &Inner<I, T> {
        unsafe { &*self.inner }
    }

    fn inner_mut(&mut self) -> &mut Inner<I, T> {
        unsafe { &mut *self.inner }
    }
}

#[inline]
fn div_round_up(divident: usize, divisor: usize) -> usize {
    1 + ((divident - 1) / divisor)
}

#[cfg(test)]
mod tests {
    use super::{PrefixedArray, PrefixedArrayInfo, div_round_up};
    use testdrop::{self, TestDrop};

    #[test]
    fn test_div_round_up() {
        assert_eq!(6, div_round_up(6, 1));
        assert_eq!(7, div_round_up(7, 1));
        assert_eq!(8, div_round_up(8, 1));

        assert_eq!(3, div_round_up(6, 2));
        assert_eq!(4, div_round_up(7, 2));
        assert_eq!(4, div_round_up(8, 2));

        assert_eq!(2, div_round_up(6, 3));
        assert_eq!(3, div_round_up(7, 3));
        assert_eq!(3, div_round_up(8, 3));
        assert_eq!(3, div_round_up(9, 3));
        assert_eq!(4, div_round_up(10, 3));
    }

    struct Info {
        len: usize,
        cap: usize,
    }

    impl PrefixedArrayInfo for Info {
        fn len(&self) -> usize {
            self.len
        }

        fn capacity(&self) -> usize {
            self.cap
        }
    }

    #[test]
    fn drop_and_deallocate() {
        let test = TestDrop::new();
        let v = &mut PrefixedArray::allocate(Info { len: 0, cap: 5 });

        let (a, item_a) = test.new_item();
        let (b, item_b) = test.new_item();
        let (c, item_c) = test.new_item();
        let (d, item_d) = test.new_item();
        let (e, item_e) = test.new_item();

        fn test_write<'a>(
            v: &mut PrefixedArray<Info, testdrop::Item<'a>>,
            id: usize,
            item: testdrop::Item<'a>,
            index: usize,
        ) {
            unsafe {
                let len = v.inner().info.len;
                v.write(len, item);
                v.inner_mut().info.len = len + 1;
            }
            assert_eq!(index + 1, v.len());
            assert_eq!(index + 1, v.as_slice().len());
            assert_eq!(index + 1, v.as_mut_slice().len());
            assert_eq!(id, v.as_slice()[index].id());
            assert_eq!(id, v.as_mut_slice()[index].id());
        }

        test_write(v, a, item_a, 0);
        test_write(v, b, item_b, 1);
        test_write(v, c, item_c, 2);
        test_write(v, d, item_d, 3);
        test_write(v, e, item_e, 4);

        // change len so d and e will not be dropped
        v.inner_mut().info.len = 3;

        unsafe {
            v.drop_and_deallocate();
        }

        test.assert_drop(a);
        test.assert_drop(b);
        test.assert_drop(c);
        test.assert_no_drop(d);
        test.assert_no_drop(e);
    }
}