FFI::C::Util - Utility functions for dealing with structured C data
version 0.10
use FFI::C::Util qw( perl_to_c take ); use FFI::C::StructDef; use FFI::Platypus::Memory qw( free ); my $def = FFI::C::StructDef->new( members => [ x => 'uint8', y => 'sint64', ], ); my $inst = $def->create; # initalize members perl_to_c($inst, { x => 1, y => 2 }); # take ownership my $ptr = take $inst; # since we took ownership, we are responsible for freeing the memory: free $ptr;
This module provides some useful utility functions for dealing with the various def instances provided by FFI::C
perl_to_c $instance, \%values; # for Struct/Union perl_to_c $instance, \@values; # for Array
This function initializes the members of an instance.
my $perl = c_to_perl $instance;
This function takes an instance and returns the nested members as Perl data structures.
my $bool = owned $instance;
Returns true of the $instance owns its allocated memory. That is, it will free up the allocated memory when it falls out of scope. Reasons an instance might not be owned are:
$instance
take
my $ptr = take $instance;
This function takes ownership of the instance pointer, and returns the opaque pointer. This means a couple of things:
You should call free on it manually to free the memory it is using.
free
So don't try to get/set any of its members, or pass it into a function.
The returned pointer can be cast into something else or passed into a function that takes an opaque argument.
opaque
set_array_count $inst, $count;
This function sets the element count on a variable array returned from C (where normally there is no way to know from just the return value). If you try to set a count on a non-array or a fixed sized array an exception will be thrown.
Graham Ollis <plicease@cpan.org>
This software is copyright (c) 2020 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install FFI::C, copy and paste the appropriate command in to your terminal.
cpanm
cpanm FFI::C
CPAN shell
perl -MCPAN -e shell install FFI::C
For more information on module installation, please visit the detailed CPAN module installation guide.