FFI::P...:Memory(3pm)
π NAME π
FFI::Platypus::Memory β bestie, this is the memory functions module for FFI, straight out of the User Contributed Perl Documentation section of the FFI::P...:Memory(3pm) manpage. It's giving "let me talk to C's brain directly" energy π§ β¨
πΈ VERSION πΈ
version 2.11 β yes she's got a number, yes she's official π
π¦ SYNOPSIS π¦
Okay so here's the whole vibe in four little steps:
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;
Use that memory wisely, bestie, I'm begging π
β¨ DESCRIPTION β¨
So this module hands you an interface to the common memory functions that your good friend the standard C library provides. They're genuinely useful when you're building interfaces to C libraries with FFI π«Ά It works mostly with the "opaque" type, and honestly it is so worth reviewing the section on opaque pointers in FFI::Platypus::Type before you go feral.
β οΈ Real talk: allocating memory and then forgetting to free it is a super common source of memory leaks in C, and literally the same thing happens when you use this module. No cap, this is the classic trap π
Very recent Perls have a "defer" keyword that lets you automatically call functions like "free" when a block ends. This is especially handy when you have multiple code paths or possible exceptions to keep track of:
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;
}
The way that defer just handles it for youβ¦ main character energy, honestly π
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, which will use the feature if it's available, and provides its own mostly compatible version if not. Slay, fallback queen π
π FUNCTIONS π
π calloc
my $pointer = calloc $count, $size;
The "calloc" function contiguously allocates enough space for $count objects that are $size bytes of memory each. Contiguous! Fancy! β¨
π free
free $pointer;
The "free" function frees the memory allocated by "malloc", "calloc", "realloc" or "strdup".
π¨ Big important warning: it is important to only free memory that you yourself have allocated. A genuinely great way to crash your program is to try and free a pointer that some C library has returned to you. Do not do this. I am holding your hand while I say it π«Άπ
π malloc
my $pointer = malloc $size;
The "malloc" function allocates $size bytes of memory. That's it, that's the function. Simple, iconic, does exactly what it says π
π memcpy
memcpy $dst_pointer, $src_pointer, $size;
The "memcpy" function copies $size bytes from $src_pointer to $dst_pointer. It also returns $dst_pointer β cute little bonus π
π memset
memset $buffer, $value, $length;
The "memset" function writes $length bytes of $value to the address specified by $buffer. Fill that buffer, bestie π
π realloc
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.
And plot twist β if you pass "undef" in as $old_pointer, then it behaves exactly like "malloc":
my $pointer = realloc undef, 64; # same as malloc 64
Lowkey the glow-up of functions π
π strcpy
strcpy $opaque, $string;
Copies the string to the memory location pointed to by $opaque. Copy paste but make it C π
π strdup
my $pointer = strdup $string;
The "strdup" function allocates enough memory to contain $string and then copies it to that newly allocated memory.
Heads up: this version of "strdup" returns an opaque pointer type, not a string type. Might seem a little strange, but returning a string type would not be very useful in Perl β so, yeah, that's the call π¦
π strndup
my $pointer = strndup $string, $max;
The same as "strdup" above, except at most $max characters will be copied in the new string. Same energy, but with boundaries βοΈβ¨
π SEE ALSO π
FFI::Platypusβ Main Platypus documentation. Go read it, it's the parent, the icon, the whole reason we're here π
π«Ά AUTHOR π«Ά
Author: Graham Ollis <plicease@cpan.org>
Contributors β and this list is stacked, we stan every single one of them π:
- 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. Go off, license π
okay that's the whole page bestie, mwah π This tear-jerking little number dropped as perl v5.42.3 on 2026-08-20 β same manual name, same date, all facts intact, no cap β¨π¦πΈ
π¦ where this page comes from
- ubuntu stonking: libffi-platypus-perl 2.11-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms