Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ macro_rules! drop_for_tskit_type {
fn drop(&mut self) {
let rv = unsafe { $drop(&mut *self.inner) };
panic_on_tskit_error!(rv);
unsafe {
libc::free(self.inner as *mut libc::c_void);
}
self.inner = std::ptr::null_mut();
}
}
};
Expand All @@ -138,11 +142,11 @@ macro_rules! tskit_type_access {
($name: ident, $ll_name: ty) => {
impl $crate::TskitTypeAccess<$ll_name> for $name {
fn as_ptr(&self) -> *const $ll_name {
&*self.inner
self.inner
}

fn as_mut_ptr(&mut self) -> *mut $ll_name {
&mut *self.inner
self.inner
}
}
};
Expand All @@ -152,10 +156,12 @@ macro_rules! build_tskit_type {
($name: ident, $ll_name: ty, $drop: ident) => {
impl $crate::ffi::WrapTskitType<$ll_name> for $name {
fn wrap() -> Self {
let temp: std::mem::MaybeUninit<$ll_name> = std::mem::MaybeUninit::uninit();
$name {
inner: unsafe { Box::<$ll_name>::new(temp.assume_init()) },
let temp =
unsafe { libc::malloc(std::mem::size_of::<$ll_name>()) as *mut $ll_name };
if temp.is_null() {
panic!("out of memory");
}
$name { inner: temp }
}
}
drop_for_tskit_type!($name, $drop);
Expand Down
8 changes: 5 additions & 3 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod tests {
use ll_bindings::tsk_table_collection_free;

pub struct TableCollectionMock {
inner: Box<ll_bindings::tsk_table_collection_t>,
inner: *mut ll_bindings::tsk_table_collection_t,
}

build_tskit_type!(
Expand All @@ -41,13 +41,15 @@ mod tests {
let rv = unsafe { ll_bindings::tsk_table_collection_init(s.as_mut_ptr(), 0) };
assert_eq!(rv, 0);

s.inner.sequence_length = len;
unsafe {
(*s.inner).sequence_length = len;
}

s
}

fn sequence_length(&self) -> f64 {
unsafe { (*self.as_ptr()).sequence_length }
unsafe { (*self.inner).sequence_length }
}
}

Expand Down
Loading