1pub mod alloc;
2
3pub struct FFIVec {
4 ptr : *mut u8,
5 managed : bool,
6}
7
8impl FFIVec {
9 pub fn new(size: usize) -> Self {
10 let ptr = unsafe { alloc::allocate(size) };
11 Self { ptr, managed: true }
12 }
13
14 pub unsafe fn new_unmanaged(size: usize) -> Self {
15 let ptr = unsafe { alloc::allocate(size) };
16 Self { ptr, managed: false }
17 }
18
19 pub unsafe fn from_ptr(ptr: *mut u8, managed: bool) -> Self {
20 Self { ptr , managed }
21 }
22
23 pub fn as_ptr(&self) -> *mut u8 {
24 return self.ptr
25 }
26
27 pub unsafe fn make_leak(&mut self) {
28 self.managed = false;
29 }
30
31 pub fn copy_to_new_vec(&self) -> Vec<u8> {
32 unsafe { alloc::get_data(self.ptr) }
33 }
34}
35
36impl From<Vec<u8>> for FFIVec {
37 fn from(v: Vec<u8>) -> Self {
38 let ptr = unsafe { alloc::new_data(v.as_ptr(), v.len()) };
39 Self { ptr, managed: true }
40 }
41}
42
43impl Drop for FFIVec {
44 fn drop(&mut self) {
45 if self.managed {
46 unsafe { alloc::deallocate(self.ptr) }
47 }
48 }
49}