-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.rs
307 lines (257 loc) · 7.97 KB
/
data.rs
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
use std::{ffi::c_void, ptr::slice_from_raw_parts};
use crate::{arc, define_obj_type, dispatch, ns};
#[cfg(feature = "blocks")]
use crate::blocks;
#[doc(alias = "dispatch_data_applier_t")]
pub type Applier<Attr> = blocks::Block<fn(&dispatch::Data, usize, *const u8, usize) -> bool, Attr>;
define_obj_type!(
#[doc(alias = "dispatch_data_t")]
pub Data(dispatch::Object)
);
unsafe impl Send for Data {}
unsafe impl Sync for Data {}
impl Data {
/// The singleton dispatch data object representing a zero-length
/// memory region.
#[doc(alias = "dispatch_data_empty")]
#[inline]
pub fn empty() -> &'static Self {
unsafe { &_dispatch_data_empty }
}
#[doc(alias = "dispatch_data_get_size")]
#[inline]
pub fn len(&self) -> usize {
unsafe { dispatch_data_get_size(self) }
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[cfg(feature = "blocks")]
#[doc(alias = "dispatch_data_apply")]
#[inline]
pub fn apply_block(&self, applier: &mut dispatch::DataApplier<blocks::NoEsc>) -> bool {
unsafe { dispatch_data_apply(self, applier) }
}
#[cfg(feature = "blocks")]
#[doc(alias = "dispatch_data_apply")]
#[inline]
pub fn apply(
&self,
mut applier: impl FnMut(&dispatch::Data, usize, *const u8, usize) -> bool,
) -> bool {
unsafe {
let mut block = dispatch::DataApplier::<blocks::NoEsc>::stack4(&mut applier);
dispatch_data_apply(self, &mut block)
}
}
#[inline]
pub fn as_ns(&self) -> &ns::Data {
unsafe { std::mem::transmute(self) }
}
#[doc(alias = "dispatch_data_create_subrange")]
#[inline]
pub fn subrange(&self, offset: usize, length: usize) -> arc::R<Self> {
unsafe { dispatch_data_create_subrange(self, offset, length) }
}
#[doc(alias = "dispatch_data_create_concat")]
#[inline]
pub fn concat(a: &Self, b: &Self) -> arc::R<Self> {
unsafe { dispatch_data_create_concat(a, b) }
}
#[doc(alias = "dispatch_data_create")]
#[inline]
pub fn copy_from_slice(data: &[u8]) -> arc::R<Self> {
unsafe { dispatch_data_create(data.as_ptr(), data.len(), None, None) }
}
#[inline]
pub fn from_static(bytes: &'static [u8]) -> arc::R<Self> {
let mut b = blocks::StaticBlock::new0(destructor_noop);
unsafe { dispatch_data_create(bytes.as_ptr(), bytes.len(), None, Some(b.as_esc_mut())) }
}
#[inline]
pub fn with_bytes_no_copy(
bytes: *const u8,
len: usize,
queue: Option<&dispatch::Queue>,
destructor: &mut dispatch::Block<blocks::Esc>,
) -> arc::R<Self> {
unsafe { dispatch_data_create(bytes, len, queue, Some(destructor)) }
}
#[inline]
pub fn map(&self) -> Map {
unsafe {
let mut data = std::ptr::null();
let mut len = 0;
let map = dispatch_data_create_map(self, &mut data, &mut len);
Map { map, data, len }
}
}
}
extern "C" fn destructor_noop(_ctx: *const c_void) {}
impl From<&'static [u8]> for arc::R<Data> {
fn from(slice: &'static [u8]) -> arc::R<Data> {
Data::from_static(slice)
}
}
impl From<&'static str> for arc::R<Data> {
fn from(slice: &'static str) -> arc::R<Data> {
Data::from_static(slice.as_bytes())
}
}
impl From<Vec<u8>> for arc::R<Data> {
fn from(val: Vec<u8>) -> arc::R<Data> {
let len = val.len();
let ptr = val.as_ptr();
if len == 0 {
return Data::empty().retained();
}
let mut destruct = dispatch::Block::<blocks::Esc>::new0(move || {
let _f = &val;
});
Data::with_bytes_no_copy(ptr, len, None, &mut destruct)
}
}
impl From<Box<[u8]>> for arc::R<Data> {
fn from(val: Box<[u8]>) -> arc::R<Data> {
let len = val.len();
let ptr = val.as_ptr();
if len == 0 {
return Data::empty().retained();
}
let mut destruct = dispatch::Block::<blocks::Esc>::new0(move || {
let _f = &val;
});
Data::with_bytes_no_copy(ptr, len, None, &mut destruct)
}
}
#[link(name = "System", kind = "dylib")]
extern "C" {
static _dispatch_data_empty: Data;
fn dispatch_data_create(
buffer: *const u8,
size: usize,
queue: Option<&dispatch::Queue>,
destructor: Option<&mut dispatch::Block<blocks::Esc>>,
) -> arc::R<Data>;
fn dispatch_data_get_size(data: &Data) -> usize;
fn dispatch_data_create_subrange(data: &Data, offset: usize, length: usize) -> arc::R<Data>;
fn dispatch_data_create_concat(data1: &Data, data2: &Data) -> arc::R<Data>;
#[cfg(feature = "blocks")]
fn dispatch_data_apply(data: &Data, applier: &mut dispatch::DataApplier<blocks::NoEsc>)
-> bool;
fn dispatch_data_create_map(
data: &Data,
buffer_ptr: *mut *const u8,
size: *mut usize,
) -> arc::R<Data>;
}
#[derive(Debug)]
pub struct Map {
map: arc::R<Data>,
data: *const u8,
len: usize,
}
impl Map {
#[inline]
pub fn data(&self) -> &Data {
&self.map
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
self
}
}
impl Clone for Map {
fn clone(&self) -> Self {
Self {
map: self.map.retained(),
data: self.data,
len: self.len,
}
}
}
impl std::ops::Deref for Map {
type Target = [u8];
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { &*slice_from_raw_parts(self.data, self.len) }
}
}
#[cfg(test)]
mod tests {
use crate::{arc, dispatch};
#[test]
fn basics() {
let data = dispatch::Data::empty();
assert!(data.is_empty());
let data = data.subrange(5, 10);
assert!(data.is_empty());
let data = dispatch::Data::concat(&data, &data);
let data = data.as_ns();
assert!(data.is_empty());
}
#[test]
fn statics() {
let data1 = dispatch::Data::from_static(b"data1");
let data2 = dispatch::Data::from_static(b"data2");
assert_eq!(data1.len(), 5);
assert_eq!(data2.len(), 5);
let data3 = dispatch::Data::concat(&data1, &data2);
assert_eq!(data3.len(), 10);
assert_eq!(data3.as_ns().len(), 10);
let mut ranges = vec![];
data3.as_ns().enum_ranges(|ptr, range, _done| {
assert!(!ptr.is_null());
ranges.push(range);
});
assert_eq!(ranges.len(), 2);
ranges.clear();
data3.as_ns().enum_ranges(|ptr, range, done| {
assert!(!ptr.is_null());
ranges.push(range);
*done = true;
});
assert_eq!(ranges.len(), 1);
}
#[test]
fn from() {
{
let b: Box<[u8]> = Box::new(*b"12345");
let data = arc::R::<dispatch::Data>::from(b);
assert_eq!(data.len(), 5);
}
{
let vec = vec![1; 1000];
let data = arc::R::<dispatch::Data>::from(vec);
assert_eq!(data.len(), 1000);
}
{
let data = arc::R::<dispatch::Data>::from("hello!");
assert_eq!(data.len(), 6);
}
}
#[test]
fn map() {
let data1 = dispatch::Data::from_static(b"data1");
let data2 = dispatch::Data::from_static(b"data2");
let data3 = dispatch::Data::concat(&data1, &data2);
let mut regions_offsets = vec![];
data3.apply(|_data, offset, _ptr, _size| {
regions_offsets.push(offset);
true
});
assert_eq!(regions_offsets.len(), 2);
let map = data3.map();
let slice = map.as_slice();
assert_eq!(data3.len(), slice.len());
assert_eq!(slice[0], b"d"[0]);
assert_eq!(slice[5], b"d"[0]);
let slice = &map[..];
assert_eq!(slice[1], b"a"[0]);
assert_eq!(slice[6], b"a"[0]);
let empty_map = dispatch::Data::empty().map();
let slice = &empty_map[..];
assert!(slice.is_empty());
}
}