```{index} single: FFI::Platypus::Memory ``` # FFI::Platypus::Memory Memory functions for FFI ## VERSION version 2.12 ## SYNOPSIS ```perl 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; ``` ## 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 `opaque` type and it is worth reviewing the section on opaque pointers in [FFI::Platypus::Type](Type.md). 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 `defer` keyword that lets you automatically call functions like `free` when a block ends. This can be especially handy when you have multiple code paths or possible exceptions to keep track of. ```perl use feature 'defer'; 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; } ``` If you are not lucky enough to have the `defer` feature in your version of Perl you may be able to use [Feature::Compat::Defer](https://metacpan.org/pod/Feature::Compat::Defer), which will use the feature if available, and provides its own mostly compatible version if not. ## FUNCTIONS ### calloc ```perl my $pointer = calloc $count, $size; ``` The `calloc` function contiguously allocates enough space for _$count_ objects that are _$size_ bytes of memory each. ### free ```perl free $pointer; ``` The `free` function frees the memory allocated by `malloc`, `calloc`, `realloc` or `strdup`. 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. ### malloc ```perl my $pointer = malloc $size; ``` The `malloc` function allocates _$size_ bytes of memory. ### memcpy ```perl memcpy $dst_pointer, $src_pointer, $size; ``` The `memcpy` function copies _$size_ bytes from _$src\_pointer_ to _$dst\_pointer_. It also returns _$dst\_pointer_. ### memset ```perl memset $buffer, $value, $length; ``` The `memset` function writes _$length_ bytes of _$value_ to the address specified by _$buffer_. ### realloc ```perl my $new_pointer = realloc $old_pointer, $size; ``` The `realloc` function reallocates enough memory to fit _$size_ bytes. It copies the existing data and frees _$old\_pointer_. If you pass `undef` in as _$old\_pointer_, then it behaves exactly like `malloc`: ```perl my $pointer = realloc undef, 64; # same as malloc 64 ``` ### strcpy ```perl strcpy $opaque, $string; ``` Copies the string to the memory location pointed to by `$opaque`. ### strdup ```perl my $pointer = strdup $string; ``` The `strdup` function allocates enough memory to contain _$string_ and then copies it to that newly allocated memory. This version of `strdup` 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. ### strndup ```perl my $pointer = strndup $string, $max; ``` The same as `strdup` above, except at most `$max` characters will be copied in the new string. ## SEE ALSO - [FFI::Platypus](../Platypus.md) Main Platypus documentation. ## AUTHOR Author: Graham Ollis Contributors: Bakkiaraj Murugesan (bakkiaraj) Dylan Cali (calid) pipcet Zaki Mughal (zmughal) Fitz Elliott (felliott) Vickenty Fesunov (vyf) Gregor Herrmann (gregoa) Shlomi Fish (shlomif) Damyan Ivanov Ilya Pavlov (Ilya33) Petr Písař (ppisar) Mohammad S Anwar (MANWAR) Håkon Hægland (hakonhagland, HAKONH) Meredith (merrilymeredith, MHOWARD) Diab Jerius (DJERIUS) Eric Brine (IKEGAMI) szTheory José Joaquín Atria (JJATRIA) Pete Houston (openstrike, HOUSTON) Lukas Mai (MAUKE) ## COPYRIGHT AND LICENSE This software is copyright (c) 2015-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.