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
use super::ChunkChain;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::{fmt, mem, slice};
pub trait Sliceable<T>: AsMut<[T]> + AsRef<[T]> {}
impl<T, V> Sliceable<T> for V where V: AsRef<[T]> + AsMut<[T]> {}
pub struct SlicePool<T> {
chain: Rc<ChunkChain>,
slice: Rc<Sliceable<T>>,
}
impl<T: 'static> SlicePool<T> {
pub fn new<S: Sliceable<T> + 'static>(slice: S) -> Self {
let size = slice.as_ref().len();
SlicePool {
chain: Rc::new(ChunkChain::new(size)),
slice: Rc::new(slice),
}
}
pub fn alloc(&self, size: usize) -> Option<SliceBox<T>> {
let chunk = self.chain.allocate(size)?;
let data: &'static mut [T] = unsafe {
let offset = chunk.offset as isize;
let base = (*self.slice).as_ref().as_ptr().offset(offset);
slice::from_raw_parts_mut(base as *mut _, size)
};
Some(SliceBox {
chain: self.chain.clone(),
slice: self.slice.clone(),
data,
})
}
pub fn as_ptr(&self) -> *const T {
(*self.slice).as_ref().as_ptr()
}
pub fn len(&self) -> usize {
(*self.slice).as_ref().len()
}
}
pub struct SliceBox<T: 'static> {
#[allow(unused)]
slice: Rc<Sliceable<T>>,
chain: Rc<ChunkChain>,
data: &'static mut [T],
}
impl<T> Deref for SliceBox<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> DerefMut for SliceBox<T> {
fn deref_mut<'b>(&'b mut self) -> &'b mut [T] {
self.data
}
}
impl<T> Drop for SliceBox<T> {
fn drop(&mut self) {
let base = (*self.slice).as_ref().as_ptr();
let diff = (self.data.as_ptr() as isize).wrapping_sub(base as isize);
self.chain.release(diff as usize / mem::size_of::<T>())
}
}
impl<T: fmt::Debug> fmt::Debug for SliceBox<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.deref())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pool_owned_lifetime() {
let alloc = {
let values = vec![10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
let memory = SlicePool::new(values);
let alloc = {
let alloc = memory.alloc(2).unwrap();
assert_eq!(*alloc, [10, 20]);
{
let alloc = memory.alloc(5).unwrap();
assert_eq!(*alloc, [30, 40, 50, 60, 70]);
}
let alloc = memory.alloc(1).unwrap();
assert_eq!(*alloc, [30]);
alloc
};
assert_eq!(*alloc, [30]);
alloc
};
assert_eq!(*alloc, [30]);
}
#[test]
fn pool_fragmentation() {
let pool = SlicePool::new(vec![10, 20, 30, 40, 50, 60, 70, 80, 90, 100]);
let val1 = pool.alloc(2).unwrap();
assert_eq!(*val1, [10, 20]);
let val2 = pool.alloc(4).unwrap();
assert_eq!(*val2, [30, 40, 50, 60]);
let val3 = pool.alloc(2).unwrap();
assert_eq!(*val3, [70, 80]);
mem::drop(val2);
let val4 = pool.alloc(2).unwrap();
assert_eq!(*val4, [90, 100]);
let val5 = pool.alloc(4).unwrap();
assert_eq!(*val5, [30, 40, 50, 60]);
}
}