.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "FFI::C::Util 3pm" .TH FFI::C::Util 3pm "2023-02-06" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" FFI::C::Util \- Utility functions for dealing with structured C data .SH "VERSION" .IX Header "VERSION" version 0.15 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& 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 => \*(Aquint8\*(Aq, \& y => \*(Aqsint64\*(Aq, \& ], \& ); \& 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; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module provides some useful utility functions for dealing with the various def instances provided by \s-1FFI::C\s0 .SH "FUNCTIONS" .IX Header "FUNCTIONS" .SS "perl_to_c" .IX Subsection "perl_to_c" .Vb 2 \& perl_to_c $instance, \e%values; # for Struct/Union \& perl_to_c $instance, \e@values; # for Array .Ve .PP This function initializes the members of an instance. .SS "c_to_perl" .IX Subsection "c_to_perl" .Vb 1 \& my $perl = c_to_perl $instance; .Ve .PP This function takes an instance and returns the nested members as Perl data structures. .SS "owned" .IX Subsection "owned" .Vb 1 \& my $bool = owned $instance; .Ve .PP Returns true of the \f(CW$instance\fR 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: .IP "the instance is nested inside another object that owns the memory" 4 .IX Item "the instance is nested inside another object that owns the memory" .PD 0 .IP "the instance was returned from a C function that owns the memory" 4 .IX Item "the instance was returned from a C function that owns the memory" .ie n .IP "ownership was taken away by the ""take"" function below." 4 .el .IP "ownership was taken away by the \f(CWtake\fR function below." 4 .IX Item "ownership was taken away by the take function below." .PD .SS "take" .IX Subsection "take" .Vb 1 \& my $ptr = take $instance; .Ve .PP This function takes ownership of the instance pointer, and returns the opaque pointer. This means a couple of things: .ie n .IP "$instance will not free its data automatically" 4 .el .IP "\f(CW$instance\fR will not free its data automatically" 4 .IX Item "$instance will not free its data automatically" You should call \f(CW\*(C`free\*(C'\fR on it manually to free the memory it is using. .ie n .IP "$instance cannot be used anymore" 4 .el .IP "\f(CW$instance\fR cannot be used anymore" 4 .IX Item "$instance cannot be used anymore" So don't try to get/set any of its members, or pass it into a function. .PP The returned pointer can be cast into something else or passed into a function that takes an \f(CW\*(C`opaque\*(C'\fR argument. .SS "addressof" .IX Subsection "addressof" [version 0.11] .PP .Vb 1 \& my $ptr = addressof $instance; .Ve .PP This function returns the address (as an \f(CW\*(C`opaque\*(C'\fR type) of the instance object. This is similar to \f(CW\*(C`take\*(C'\fR 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 \f(CW$ptr\fR that the object is still allocated. .SS "set_array_count" .IX Subsection "set_array_count" .Vb 1 \& set_array_count $inst, $count; .Ve .PP 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. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\s-1FFI::C\s0" 4 .IX Item "FFI::C" .PD 0 .IP "FFI::C::Array" 4 .IX Item "FFI::C::Array" .IP "FFI::C::ArrayDef" 4 .IX Item "FFI::C::ArrayDef" .IP "FFI::C::Def" 4 .IX Item "FFI::C::Def" .IP "FFI::C::File" 4 .IX Item "FFI::C::File" .IP "FFI::C::PosixFile" 4 .IX Item "FFI::C::PosixFile" .IP "FFI::C::Struct" 4 .IX Item "FFI::C::Struct" .IP "FFI::C::StructDef" 4 .IX Item "FFI::C::StructDef" .IP "FFI::C::Union" 4 .IX Item "FFI::C::Union" .IP "FFI::C::UnionDef" 4 .IX Item "FFI::C::UnionDef" .IP "FFI::C::Util" 4 .IX Item "FFI::C::Util" .IP "FFI::Platypus::Record" 4 .IX Item "FFI::Platypus::Record" .PD .SH "AUTHOR" .IX Header "AUTHOR" Graham Ollis .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2020\-2022 by Graham Ollis. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.