forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative.rs
More file actions
49 lines (41 loc) · 1.25 KB
/
native.rs
File metadata and controls
49 lines (41 loc) · 1.25 KB
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
use std::{alloc::Layout, marker::PhantomData};
use crate::thread_pinned::ThreadPinned;
use super::{PluginPtr, PluginPtrMut};
pub struct NativePluginContext {
bump: ThreadPinned<Vec<(*mut u8, Layout)>>,
}
impl NativePluginContext {
pub fn new() -> Self {
Self {
bump: ThreadPinned::new(Vec::new()),
}
}
pub unsafe fn deref_bytes(&self, ptr: PluginPtr<u8>, len: u32) -> anyhow::Result<&[u8]> {
Ok(std::slice::from_raw_parts(ptr.as_native(), len as usize))
}
pub unsafe fn deref_bytes_mut(
&self,
ptr: PluginPtrMut<u8>,
len: u32,
) -> anyhow::Result<&mut [u8]> {
Ok(std::slice::from_raw_parts_mut(
ptr.as_native(),
len as usize,
))
}
pub fn bump_allocate(&self, layout: Layout) -> anyhow::Result<PluginPtrMut<u8>> {
let ptr = unsafe { std::alloc::alloc(layout) };
self.bump.borrow_mut().push((ptr, layout));
Ok(PluginPtrMut {
ptr: ptr as usize as u64,
_marker: PhantomData,
})
}
pub fn bump_reset(&self) {
for (ptr, layout) in self.bump.borrow_mut().drain(..) {
unsafe {
std::alloc::dealloc(ptr, layout);
}
}
}
}