Skip to content

refactor building owned tables #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2022
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
43 changes: 43 additions & 0 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,49 @@ macro_rules! handle_metadata_return {
};
}

macro_rules! build_owned_tables {
($name: ty, $deref: ident, $llname: ty, $init: ident, $free: ident) => {
impl $name {
fn new() -> Self {
let temp = unsafe { libc::malloc(std::mem::size_of::<$llname>()) as *mut $llname };
let nonnull = match std::ptr::NonNull::<$llname>::new(temp) {
Some(x) => x,
None => panic!("out of memory"),
};
let mut table = unsafe { mbox::MBox::from_non_null_raw(nonnull) };
let rv = unsafe { $init(&mut (*table), 0) };
assert_eq!(rv, 0);
Self { table }
}
}

impl Default for $name {
fn default() -> Self {
Self::new()
}
}

impl std::ops::Deref for $name {
type Target = $deref<'static>;

fn deref(&self) -> &Self::Target {
// SAFETY: that T* and &T have same layout,
// and Target is repr(transparent).
unsafe { std::mem::transmute(&self.table) }
}
}

impl Drop for $name {
fn drop(&mut self) {
let rv = unsafe { $free(&mut (*self.table)) };
if rv != 0 {
panic!("error when calling {}: {}", stringify!(free), rv);
}
}
}
};
}

#[cfg(test)]
mod test {
use crate::error::TskitError;
Expand Down
47 changes: 8 additions & 39 deletions src/edge_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::metadata;
use crate::Position;
use crate::{tsk_id_t, TskitError};
use crate::{EdgeId, NodeId};
use ll_bindings::{tsk_edge_table_free, tsk_edge_table_init};

/// Row of an [`EdgeTable`]
pub struct EdgeTableRow {
Expand Down Expand Up @@ -217,21 +218,6 @@ pub struct OwnedEdgeTable {
}

impl OwnedEdgeTable {
fn new() -> Self {
let temp = unsafe {
libc::malloc(std::mem::size_of::<ll_bindings::tsk_edge_table_t>())
as *mut ll_bindings::tsk_edge_table_t
};
let nonnull = match std::ptr::NonNull::<ll_bindings::tsk_edge_table_t>::new(temp) {
Some(x) => x,
None => panic!("out of memory"),
};
let mut table = unsafe { mbox::MBox::from_non_null_raw(nonnull) };
let rv = unsafe { ll_bindings::tsk_edge_table_init(&mut (*table), 0) };
assert_eq!(rv, 0);
Self { table }
}

pub fn add_row(
&mut self,
left: impl Into<Position>,
Expand Down Expand Up @@ -279,27 +265,10 @@ impl OwnedEdgeTable {
}
}

impl std::ops::Deref for OwnedEdgeTable {
type Target = EdgeTable<'static>;

fn deref(&self) -> &Self::Target {
// SAFETY: that T* and &T have same layout,
// and Target is repr(transparent).
unsafe { std::mem::transmute(&self.table) }
}
}

impl Default for OwnedEdgeTable {
fn default() -> Self {
Self::new()
}
}

impl Drop for OwnedEdgeTable {
fn drop(&mut self) {
let rv = unsafe { ll_bindings::tsk_edge_table_free(&mut (*self.table)) };
if rv != 0 {
panic!("error when calling tsk_edge_table_free: {}", rv);
}
}
}
build_owned_tables!(
OwnedEdgeTable,
EdgeTable,
ll_bindings::tsk_edge_table_t,
tsk_edge_table_init,
tsk_edge_table_free
);
36 changes: 8 additions & 28 deletions src/population_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::tsk_id_t;
use crate::PopulationId;
use crate::SizeType;
use crate::TskitError;
use ll_bindings::{tsk_population_table_free, tsk_population_table_init};

/// Row of a [`PopulationTable`]
#[derive(Eq)]
Expand Down Expand Up @@ -164,19 +165,6 @@ pub struct OwnedPopulationTable {
}

impl OwnedPopulationTable {
fn new() -> Self {
let temp = unsafe {
libc::malloc(std::mem::size_of::<ll_bindings::tsk_population_table_t>())
as *mut ll_bindings::tsk_population_table_t
};
let nonnull = match std::ptr::NonNull::<ll_bindings::tsk_population_table_t>::new(temp) {
Some(x) => x,
None => panic!("out of memory"),
};
let table = unsafe { mbox::MBox::from_non_null_raw(nonnull) };
Self { table }
}

pub fn add_row(&mut self) -> Result<PopulationId, TskitError> {
let rv = unsafe {
ll_bindings::tsk_population_table_add_row(&mut (*self.table), std::ptr::null(), 0)
Expand All @@ -202,18 +190,10 @@ impl OwnedPopulationTable {
}
}

impl std::ops::Deref for OwnedPopulationTable {
type Target = PopulationTable<'static>;

fn deref(&self) -> &Self::Target {
// SAFETY: that T* and &T have same layout,
// and Target is repr(transparent).
unsafe { std::mem::transmute(&self.table) }
}
}

impl Default for OwnedPopulationTable {
fn default() -> Self {
Self::new()
}
}
build_owned_tables!(
OwnedPopulationTable,
PopulationTable,
ll_bindings::tsk_population_table_t,
tsk_population_table_init,
tsk_population_table_free
);