@@ -375,24 +375,16 @@ macro_rules! handle_metadata_return {
375375}
376376
377377macro_rules! build_owned_tables {
378- ( $name: ty, $deref: ident, $llname : ty, $init : ident , $free : ident , $clear : expr ) => {
378+ ( $name: ty, $deref: ident, $lltype : ty, $tsktable : ty ) => {
379379 impl $name {
380380 fn new( ) -> Self {
381- let temp = unsafe { libc:: malloc( std:: mem:: size_of:: <$llname>( ) ) as * mut $llname } ;
382- let nonnull = match std:: ptr:: NonNull :: <$llname>:: new( temp) {
383- Some ( x) => x,
384- None => panic!( "out of memory" ) ,
385- } ;
386- let mut table = unsafe { mbox:: MBox :: from_non_null_raw( nonnull) } ;
387- let rv = unsafe { $init( & mut ( * table) , 0 ) } ;
388- assert_eq!( rv, 0 ) ;
381+ let table = <$lltype>:: new( ) ;
389382 Self { table }
390383 }
391384
392385 /// Clear the table.
393386 pub fn clear( & mut self ) -> $crate:: TskReturnValue {
394- let rv = unsafe { $clear( self . as_mut_ptr( ) ) } ;
395- handle_tsk_return_value!( rv)
387+ self . table. clear( ) . map_err( |e| e. into( ) )
396388 }
397389 }
398390
@@ -420,22 +412,13 @@ macro_rules! build_owned_tables {
420412 }
421413 }
422414
423- impl Drop for $name {
424- fn drop( & mut self ) {
425- let rv = unsafe { $free( & mut ( * self . table) ) } ;
426- if rv != 0 {
427- panic!( "error when calling {}: {}" , stringify!( free) , rv) ;
428- }
429- }
430- }
431-
432415 impl $name {
433- pub fn as_ptr( & self ) -> * const $llname {
434- & * self . table
416+ pub fn as_ptr( & self ) -> * const $tsktable {
417+ self . table. as_ptr ( )
435418 }
436419
437- pub fn as_mut_ptr( & mut self ) -> * mut $llname {
438- & mut * self . table as * mut $llname
420+ pub fn as_mut_ptr( & mut self ) -> * mut $tsktable {
421+ self . table. as_mut_ptr ( )
439422 }
440423 }
441424 } ;
@@ -451,7 +434,7 @@ macro_rules! node_table_add_row_details {
451434 $table: expr) => { {
452435 let rv = unsafe {
453436 $crate:: bindings:: tsk_node_table_add_row(
454- & mut $table,
437+ $table,
455438 $flags. into( ) . bits( ) ,
456439 $time. into( ) . into( ) ,
457440 $population. into( ) . into( ) ,
@@ -532,7 +515,7 @@ macro_rules! edge_table_add_row_details {
532515 $table: expr) => { {
533516 let rv = unsafe {
534517 $crate:: bindings:: tsk_edge_table_add_row(
535- & mut $table,
518+ $table,
536519 $left. into( ) . into( ) ,
537520 $right. into( ) . into( ) ,
538521 $parent. into( ) . into( ) ,
@@ -605,7 +588,7 @@ macro_rules! edge_table_add_row_with_metadata {
605588macro_rules! population_table_add_row_details {
606589 ( $metadata: expr, $metadata_len: expr, $table: expr) => { {
607590 let rv = unsafe {
608- $crate:: bindings:: tsk_population_table_add_row( & mut $table, $metadata, $metadata_len)
591+ $crate:: bindings:: tsk_population_table_add_row( $table, $metadata, $metadata_len)
609592 } ;
610593 handle_tsk_return_value!( rv, rv. into( ) )
611594 } } ;
@@ -640,7 +623,7 @@ macro_rules! individual_table_add_row_details {
640623 $table: expr) => { {
641624 let rv = unsafe {
642625 $crate:: bindings:: tsk_individual_table_add_row(
643- & mut $table,
626+ $table,
644627 $flags. into( ) . bits( ) ,
645628 $location. get_slice( ) . as_ptr( ) . cast:: <f64 >( ) ,
646629 $location. get_slice( ) . len( ) as $crate:: bindings:: tsk_size_t,
@@ -715,7 +698,7 @@ macro_rules! mutation_table_add_row_details {
715698 let dstate = process_state_input!( $derived_state) ;
716699 let rv = unsafe {
717700 $crate:: bindings:: tsk_mutation_table_add_row(
718- & mut $table,
701+ $table,
719702 $site. into( ) . into( ) ,
720703 $node. into( ) . into( ) ,
721704 $parent. into( ) . into( ) ,
@@ -796,7 +779,7 @@ macro_rules! site_table_add_row_details {
796779 let astate = process_state_input!( $ancestral_state) ;
797780 let rv = unsafe {
798781 $crate:: bindings:: tsk_site_table_add_row(
799- & mut $table,
782+ $table,
800783 $position. into( ) . into( ) ,
801784 astate. 0 ,
802785 astate. 1 ,
@@ -853,7 +836,7 @@ macro_rules! migration_table_add_row_details {
853836 $table: expr) => { {
854837 let rv = unsafe {
855838 $crate:: bindings:: tsk_migration_table_add_row(
856- & mut $table,
839+ $table,
857840 $span. 0 . into( ) . into( ) ,
858841 $span. 1 . into( ) . into( ) ,
859842 $node. into( ) . into( ) ,
@@ -928,7 +911,7 @@ macro_rules! provenance_table_add_row {
928911 let timestamp = humantime:: format_rfc3339( std:: time:: SystemTime :: now( ) ) . to_string( ) ;
929912 let rv = unsafe {
930913 $crate:: bindings:: tsk_provenance_table_add_row(
931- & mut $table,
914+ $table,
932915 timestamp. as_ptr( ) as * mut i8 ,
933916 timestamp. len( ) as tsk_size_t,
934917 record. as_ptr( ) as * mut i8 ,
@@ -943,22 +926,18 @@ macro_rules! provenance_table_add_row {
943926macro_rules! build_owned_table_type {
944927 ( $( #[ $attr: meta] ) * => $name: ident,
945928 $deref_type: ident,
946- $tskname: ident,
947- $tskinit: ident,
948- $tskfree: ident,
949- $tskclear: expr) => {
929+ $lltype: ty,
930+ $tsktable: ty) => {
950931 $( #[ $attr] ) *
951932 pub struct $name {
952- table: mbox :: MBox <$crate :: bindings :: $tskname> ,
933+ table: $lltype
953934 }
954935
955936 build_owned_tables!(
956937 $name,
957938 $deref_type,
958- $crate:: bindings:: $tskname,
959- $tskinit,
960- $tskfree,
961- $tskclear
939+ $lltype,
940+ $tsktable
962941 ) ;
963942 } ;
964943}
0 commit comments