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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde = {version = "1.0.118", features = ["derive"], optional = true}
serde_json = {version = "1.0.67", optional = true}
bincode = {version = "1.3.1", optional = true}
tskit-derive = {version = "0.2.0", path = "tskit-derive", optional = true}
mbox = "0.6.0"

[dev-dependencies]
clap = "~2.34.0"
Expand Down
19 changes: 9 additions & 10 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ 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 @@ -145,11 +141,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 {
self.inner
&mut *self.inner
}
}
};
Expand All @@ -159,12 +155,15 @@ macro_rules! build_tskit_type {
($name: ident, $ll_name: ty, $drop: ident) => {
impl $crate::ffi::WrapTskitType<$ll_name> for $name {
fn wrap() -> Self {
use mbox::MBox;
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 }
let nonnull = match std::ptr::NonNull::<$ll_name>::new(temp) {
Some(x) => x,
None => panic!("out of memory"),
};
let mbox = unsafe { MBox::from_non_null_raw(nonnull) };
$name { inner: mbox }
}
}
drop_for_tskit_type!($name, $drop);
Expand Down
9 changes: 4 additions & 5 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ mod tests {
use crate::bindings as ll_bindings;
use crate::TskitTypeAccess;
use ll_bindings::tsk_table_collection_free;
use mbox::MBox;

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

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

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

s
}

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

Expand Down
Loading