FFI::C::Util(3pm)
β¨ FFI::C::Util β¨
okay bestie, welcome to FFI::C::Util β that's a section 3pm Perl module, and it is literally the utility belt for dealing with structured C data π
πΈ VERSION
version 0.15 β yes queen, that's the one π
π¦ SYNOPSIS
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;
# initialize 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;
likeβ¦ look at her go πβ¨
π DESCRIPTION
So this module is handing you some genuinely useful utility functions for dealing with the various def instances that FFI::C gives you. That's the whole vibe. No cap π«Ά
π FUNCTIONS
π perl_to_c
perl_to_c $instance, \%values; # for Struct/Union
perl_to_c $instance, \@values; # for Array
This one initializes the members of an instance for you. Struct/Union? Pass a hashref. Array? Pass an arrayref. Simple as, bestie π
π§ c_to_perl
my $perl = c_to_perl $instance;
Hand it an instance, and it hands you back the nested members as good old Perl data structures. It's giving translator energy β¨
π owned
my $bool = owned $instance;
Returns true if $instance owns its allocated memory β meaning it will free up that allocated memory when it falls out of scope. iconic behaviour π
Reasons an instance might not be owned:
- the instance is nested inside another object that owns the memory
- the instance was returned from a C function that owns the memory
- ownership was taken away by the
takefunction below
so yeah, if owned says false, that memory is somebody else's problem, not yours π
π₯ take
my $ptr = take $instance;
This function takes ownership of the instance pointer and returns the opaque pointer. And that means a couple of things, bestie, listen up:
$instancewill not free its data automatically- You should call
freeon it manually to free the memory it is using.
- You should call
$instancecannot be used anymore- So don't try to get/set any of its members, or pass it into a function.
β οΈ fr fr, once you take, that instance is done. touching it is not it.
The returned pointer can be cast into something else, or passed into a function that takes an opaque argument. slay π
π addressof
[version 0.11]
my $ptr = addressof $instance;
This one returns the address (as an opaque type) of the instance object. It's similar to take above in that it gets you the address of the object β but it doesn't take ownership of it. So care needs to be taken when using $ptr that the object is still allocated π
β οΈ no ownership transferred here. you are borrowing, not keeping. keep that object alive or it's over π
β set_array_count
set_array_count $inst, $count;
This sets the element count on a variable array returned from C β which, normally, there's literally no way to know just from the return value. So thank u for this one π«Ά
β οΈ If you try to set a count on a non-array, or on a fixed sized array, an exception will be thrown. periodt.
π SEE ALSO
FFI::CFFI::C::ArrayFFI::C::ArrayDefFFI::C::DefFFI::C::FileFFI::C::PosixFileFFI::C::StructFFI::C::StructDefFFI::C::UnionFFI::C::UnionDefFFI::C::UtilFFI::Platypus::Record
go read the whole friend group, iykyk π
π¦ AUTHOR
Graham Ollis <plicease@cpan.org> β the main character who wrote all this π β¨
π COPYRIGHT AND LICENSE
This software is copyright (c) 2020-2022 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.
okay that's the whole page bestie, mwah π«Ά this was FFI::C::Util, perl v5.36.0, dated 2023-02-06, and honestly? iconic, no notes πβ¨π
π¦ where this page comes from
- debian bookworm: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms