Skip to content

Commit

Permalink
Add addressof function to FFI::C::Util
Browse files Browse the repository at this point in the history
  • Loading branch information
plicease committed Mar 10, 2021
1 parent 89a0057 commit 6d4df16
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for {{$dist->name}}},

{{$NEXT}}
- Add addressof function to FFI::C::Util (gh#49)

0.10 2020-09-23 03:16:36 -0600
- Add tmpfile constructor for FFI::C::File (gh#41)
Expand Down
1 change: 1 addition & 0 deletions author.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pod_spelling_system:
stopwords:
- ffi
- nullable
- addressof

pod_coverage:
skip: 0
Expand Down
2 changes: 1 addition & 1 deletion lib/FFI/C/PosixFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use base qw( FFI::C::File );
=head1 SYNOPSIS
use FFI::C::PosixFile;
my $stdout = FFI::C::PosixFile->fdopen(1, "w");
say $stdout->fileno; # prints 1
Expand Down
25 changes: 24 additions & 1 deletion lib/FFI/C/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Carp ();
use Class::Inspector;
use base qw( Exporter );

our @EXPORT_OK = qw( perl_to_c c_to_perl take owned set_array_count );
our @EXPORT_OK = qw( perl_to_c c_to_perl take owned set_array_count addressof );

# ABSTRACT: Utility functions for dealing with structured C data
# VERSION
Expand Down Expand Up @@ -174,6 +174,29 @@ sub take ($)
$ptr;
}

=head2 addressof
[version 0.11]
my $ptr = addressof $instance;
This function returns the address (as an C<opaque> type) of the
instance object. This is similar to C<take> above in that it gets
you the address of the object, but doesn't take ownership of it,
so care needs to be taken when using C<$ptr> that the object
is still allocated.
=cut

sub addressof ($)
{
my $inst = shift;
Carp::croak("Not an object") unless is_blessed_ref $inst;
my $ptr = $inst->{ptr};
Carp::croak("Object pointer went away") unless $ptr;
$ptr;
}

=head2 set_array_count
set_array_count $inst, $count;
Expand Down
21 changes: 18 additions & 3 deletions t/ffi_c_util.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use Test2::V0 0.000081 -no_srand => 1;
use FFI::C::Util qw( owned take perl_to_c c_to_perl set_array_count );
use FFI::C::Util qw( owned take perl_to_c c_to_perl set_array_count addressof );
use FFI::Platypus::Memory qw( free );
use FFI::C::StructDef;
use FFI::C::UnionDef;
Expand Down Expand Up @@ -29,8 +29,23 @@ subtest 'owned / take' => sub {

is owned($inst), T(), 'instance is owned';

my $ptr = take $inst;
is $ptr, match qr/^[0-9]+$/, 'gave us a pointer';
my $ptr1 = addressof $inst;
is $ptr1, match qr/^[0-9]+$/, 'addressof is a pointer';

is
$inst,
object {
call [ isa => 'FFI::C::Struct' ] => T();
field ptr => match qr/^[0-9]+$/;
etc;
},
'object after addressof',
;

my $ptr2 = take $inst;
is $ptr2, match qr/^[0-9]+$/, 'gave us a pointer';

is $ptr2, $ptr1, 'pointer from take and addressof are the same';

is
$inst,
Expand Down

0 comments on commit 6d4df16

Please sign in to comment.