.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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::Platypus::Memory 3pm" .TH FFI::Platypus::Memory 3pm 2024-03-07 "perl v5.38.2" "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::Platypus::Memory \- Memory functions for FFI .SH VERSION .IX Header "VERSION" version 2.08 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use FFI::Platypus::Memory; \& \& # allocate 64 bytes of memory using the \& # libc malloc function. \& my $pointer = malloc 64; \& \& # use that memory wisely \& ... \& \& # free the memory when you are done. \& free $pointer; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This module provides an interface to common memory functions provided by the standard C library. They may be useful when constructing interfaces to C libraries with FFI. It works mostly with the \f(CW\*(C`opaque\*(C'\fR type and it is worth reviewing the section on opaque pointers in FFI::Platypus::Type. .PP Allocating memory and forgetting to free it is a common source of memory leaks in C and when using this module. Very recent Perls have a \f(CW\*(C`defer\*(C'\fR keyword that lets you automatically call functions like \f(CW\*(C`free\*(C'\fR when a block ends. This can be especially handy when you have multiple code paths or possible exceptions to keep track of. .PP .Vb 2 \& use feature \*(Aqdefer\*(Aq; \& use FFI::Platypus::Memory qw( malloc free ); \& \& sub run { \& my $ptr = malloc 66; \& defer { free $ptr }; \& \& my $data = do_something($ptr); \& \& # do not need to remember to place free $ptr here, as it will \& # run through defer. \& \& return $data; \& } .Ve .PP If you are not lucky enough to have the \f(CW\*(C`defer\*(C'\fR feature in your version of Perl you may be able to use Feature::Compat::Defer, which will use the feature if available, and provides its own mostly compatible version if not. .SH FUNCTIONS .IX Header "FUNCTIONS" .SS calloc .IX Subsection "calloc" .Vb 1 \& my $pointer = calloc $count, $size; .Ve .PP The \f(CW\*(C`calloc\*(C'\fR function contiguously allocates enough space for \fR\f(CI$count\fR\fI\fR objects that are \fI\fR\f(CI$size\fR\fI\fR bytes of memory each. .SS free .IX Subsection "free" .Vb 1 \& free $pointer; .Ve .PP The \f(CW\*(C`free\*(C'\fR function frees the memory allocated by \f(CW\*(C`malloc\*(C'\fR, \f(CW\*(C`calloc\*(C'\fR, \&\f(CW\*(C`realloc\*(C'\fR or \f(CW\*(C`strdup\*(C'\fR. It is important to only free memory that you yourself have allocated. A good way to crash your program is to try and free a pointer that some C library has returned to you. .SS malloc .IX Subsection "malloc" .Vb 1 \& my $pointer = malloc $size; .Ve .PP The \f(CW\*(C`malloc\*(C'\fR function allocates \fR\f(CI$size\fR\fI\fR bytes of memory. .SS memcpy .IX Subsection "memcpy" .Vb 1 \& memcpy $dst_pointer, $src_pointer, $size; .Ve .PP The \f(CW\*(C`memcpy\*(C'\fR function copies \fR\f(CI$size\fR\fI\fR bytes from \fI\fR\f(CI$src_pointer\fR\fI\fR to \&\fI\fR\f(CI$dst_pointer\fR\fI\fR. It also returns \fI\fR\f(CI$dst_pointer\fR\fI\fR. .SS memset .IX Subsection "memset" .Vb 1 \& memset $buffer, $value, $length; .Ve .PP The \f(CW\*(C`memset\*(C'\fR function writes \fR\f(CI$length\fR\fI\fR bytes of \fI\fR\f(CI$value\fR\fI\fR to the address specified by \fI\fR\f(CI$buffer\fR\fI\fR. .SS realloc .IX Subsection "realloc" .Vb 1 \& my $new_pointer = realloc $old_pointer, $size; .Ve .PP The \f(CW\*(C`realloc\*(C'\fR function reallocates enough memory to fit \fR\f(CI$size\fR\fI\fR bytes. It copies the existing data and frees \fI\fR\f(CI$old_pointer\fR\fI\fR. .PP If you pass \f(CW\*(C`undef\*(C'\fR in as \fR\f(CI$old_pointer\fR\fI\fR, then it behaves exactly like \&\f(CW\*(C`malloc\*(C'\fR: .PP .Vb 1 \& my $pointer = realloc undef, 64; # same as malloc 64 .Ve .SS strcpy .IX Subsection "strcpy" .Vb 1 \& strcpy $opaque, $string; .Ve .PP Copies the string to the memory location pointed to by \f(CW$opaque\fR. .SS strdup .IX Subsection "strdup" .Vb 1 \& my $pointer = strdup $string; .Ve .PP The \f(CW\*(C`strdup\*(C'\fR function allocates enough memory to contain \fR\f(CI$string\fR\fI\fR and then copies it to that newly allocated memory. This version of \&\f(CW\*(C`strdup\*(C'\fR returns an opaque pointer type, not a string type. This may seem a little strange, but returning a string type would not be very useful in Perl. .SS strndup .IX Subsection "strndup" .Vb 1 \& my $pointer = strndup $string, $max; .Ve .PP The same as \f(CW\*(C`strdup\*(C'\fR above, except at most \f(CW$max\fR characters will be copied in the new string. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP FFI::Platypus 4 .IX Item "FFI::Platypus" Main Platypus documentation. .SH AUTHOR .IX Header "AUTHOR" Author: Graham Ollis .PP Contributors: .PP Bakkiaraj Murugesan (bakkiaraj) .PP Dylan Cali (calid) .PP pipcet .PP Zaki Mughal (zmughal) .PP Fitz Elliott (felliott) .PP Vickenty Fesunov (vyf) .PP Gregor Herrmann (gregoa) .PP Shlomi Fish (shlomif) .PP Damyan Ivanov .PP Ilya Pavlov (Ilya33) .PP Petr Písař (ppisar) .PP Mohammad S Anwar (MANWAR) .PP Håkon Hægland (hakonhagland, HAKONH) .PP Meredith (merrilymeredith, MHOWARD) .PP Diab Jerius (DJERIUS) .PP Eric Brine (IKEGAMI) .PP szTheory .PP José Joaquín Atria (JJATRIA) .PP Pete Houston (openstrike, HOUSTON) .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2015\-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.