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
use core::{mem, ptr, usize};
use core::borrow::{Borrow, BorrowMut};
use core::marker::PhantomData;
use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};
pub use soak_derive::Columns;
pub unsafe trait Columns {
const SIZES: &'static [usize];
const ALIGNS: &'static [usize];
type Pointers: BorrowMut<[ptr::NonNull<u8>]>;
fn dangling() -> Self::Pointers;
}
pub struct RawTable<T: Columns> {
pointers: T::Pointers,
capacity: usize,
_marker: PhantomData<T>,
}
pub struct Field<S, F> {
index: usize,
_marker: PhantomData<(*mut S, *mut F)>
}
impl<S, F> Field<S, F> {
pub const unsafe fn new(index: usize) -> Field<S, F> {
Field { index, _marker: PhantomData }
}
}
impl<T: Columns> Default for RawTable<T> {
fn default() -> Self {
let pointers = T::dangling();
let capacity = if mem::size_of::<T>() == 0 { usize::MAX } else { 0 };
RawTable { pointers, capacity, _marker: PhantomData }
}
}
impl<T: Columns> RawTable<T> {
pub fn with_capacity(capacity: usize) -> Self {
unsafe {
let align = T::ALIGNS.iter().cloned().max().unwrap_or(1);
let mask = align - 1;
let size = T::SIZES.iter().try_fold(0, move |sum, &size| {
let array_size = usize::checked_mul(capacity, size)?;
let aligned_size = usize::checked_add(array_size, mask)? & !mask;
Some(usize::checked_add(sum, aligned_size)?)
}).expect("capacity overflow");
let layout = Layout::from_size_align_unchecked(size, align);
let data = if size == 0 { align as *mut u8 } else { alloc(layout) };
if data == ptr::null_mut() {
handle_alloc_error(layout);
}
let mut pointers = T::dangling();
let mut offset = 0;
let dst = pointers.borrow_mut().iter_mut();
for (pointer, size) in Iterator::zip(dst, T::SIZES.iter()) {
*pointer = ptr::NonNull::new_unchecked(data.add(offset));
offset += (capacity * size + mask) & !mask;
}
let capacity = if mem::size_of::<T>() == 0 { usize::MAX } else { capacity };
RawTable { pointers, capacity, _marker: PhantomData }
}
}
pub fn ptr<F>(&mut self, field: Field<T, F>) -> *mut F {
self.pointers.borrow()[field.index].as_ptr() as *mut F
}
pub fn capacity(&self) -> usize { self.capacity }
pub fn reserve_exact(&mut self, used: usize, extra: usize) {
unsafe {
if self.capacity - used >= extra {
return;
}
let capacity = usize::checked_add(used, extra).expect("capacity overflow");
let table = Self::with_capacity(capacity);
let src = self.pointers.borrow().iter();
let dst = table.pointers.borrow().iter();
for ((src, size), dst) in Iterator::zip(Iterator::zip(src, T::SIZES.iter()), dst) {
ptr::copy_nonoverlapping(src.as_ptr(), dst.as_ptr(), capacity * size);
}
mem::replace(self, table);
}
}
}
impl<T: Columns> Drop for RawTable<T> {
fn drop(&mut self) {
unsafe {
let align = *T::ALIGNS.iter().max().unwrap_or(&1);
let mask = align - 1;
let capacity = self.capacity;
let size = T::SIZES.iter().map(move |&size| (capacity * size + mask) & !mask).sum();
let layout = Layout::from_size_align_unchecked(size, align);
if size > 0 { dealloc(self.pointers.borrow()[0].as_ptr(), layout); }
}
}
}