| 1 | package AI::TensorFlow::Libtensorflow::Buffer; | ||
| 2 | # ABSTRACT: Buffer that holds pointer to data with length | ||
| 3 | $AI::TensorFlow::Libtensorflow::Buffer::VERSION = '0.0.7'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use namespace::autoclean; | ||
| 7 | use AI::TensorFlow::Libtensorflow::Lib qw(arg); | ||
| 8 | |||
| 9 | my $ffi = AI::TensorFlow::Libtensorflow::Lib->ffi; | ||
| 10 | $ffi->mangler(AI::TensorFlow::Libtensorflow::Lib->mangler_default); | ||
| 11 | use FFI::C; | ||
| 12 | FFI::C->ffi($ffi); | ||
| 13 | $ffi->load_custom_type('AI::TensorFlow::Libtensorflow::Lib::FFIType::TFPtrSizeScalarRef' | ||
| 14 | => 'tf_buffer_buffer' | ||
| 15 | ); | ||
| 16 | |||
| 17 | use FFI::Platypus::Buffer; | ||
| 18 | use FFI::Platypus::Memory; | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | FFI::C->struct( 'TF_Buffer' => [ | ||
| 25 | data => 'opaque', | ||
| 26 | length => 'size_t', | ||
| 27 | _data_deallocator => 'opaque', # data_deallocator_t | ||
| 28 | # this does not work? | ||
| 29 | #_data_deallocator => 'data_deallocator_t', | ||
| 30 | ]); | ||
| 31 | use Sub::Delete; | ||
| 32 | delete_sub 'DESTROY'; | ||
| 33 | |||
| 34 | sub data_deallocator { | ||
| 35 | my ($self, $coderef) = shift; | ||
| 36 | |||
| 37 | return $self->{_data_deallocator_closure} unless $coderef; | ||
| 38 | |||
| 39 | my $closure = $ffi->closure( $coderef ); | ||
| 40 | |||
| 41 | $closure->sticky; | ||
| 42 | $self->{_data_deallocator_closure} = $closure; | ||
| 43 | |||
| 44 | my $opaque = $ffi->cast('data_deallocator_t', 'opaque', $closure); | ||
| 45 | $self->_data_deallocator( $opaque ); | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | $ffi->attach( [ 'NewBuffer' => 'New' ] => [] => 'TF_Buffer' ); | ||
| 50 | |||
| 51 | $ffi->attach( [ 'NewBufferFromString' => 'NewFromString' ] => [ | ||
| 52 | arg 'tf_buffer_buffer' => [qw(proto proto_len)] | ||
| 53 | ] => 'TF_Buffer' => sub { | ||
| 54 | my ($xs, $class, @rest) = @_; | ||
| 55 | $xs->(@rest); | ||
| 56 | }); | ||
| 57 | |||
| 58 | |||
| 59 | $ffi->attach( [ 'DeleteBuffer' => 'DESTROY' ] => [ 'TF_Buffer' ], 'void' ); | ||
| 60 | |||
| 61 | 1; | ||
| 62 | |||
| 63 | __END__ |