FFI::Platypus::Memory - Memory functions for FFI
version 0.57_01
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;
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.
my $pointer = calloc $count, $size;
The calloc function contiguously allocates enough space for $count objects that are $size bytes of memory each.
calloc
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.
free
malloc
realloc
strdup
my $pointer = malloc $size;
The malloc function allocates $size bytes of memory.
memcpy $dst_pointer, $src_pointer, $size;
The memcpy function copies $size bytes from $src_pointer to $dst_pointer. It also returns $dst_pointer.
memcpy
memset $buffer, $value, $length;
The memset function writes $length bytes of $value to the address specified by $buffer.
memset
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:
undef
my $pointer = realloc undef, 64; # same as malloc 64
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.
Platforms that do not support strdup will be provided with an equivalent using malloc and memcpy written in Perl. This version is slower.
strdup isn't always supported by all platforms. On platforms that do not support it, it is emulated using calls to malloc and memcpy which are part of the standard C library. Because this requires two function calls it is probably not as fast on most platforms.
If you experience problems with the strdup provided by your platform, you can force the emulated implementation using the FFI_PLATYPUS_MEMORY_STRDUP_IMPL environment variable.
# bash: $ export FFI_PLATYPUS_MEMORY_STRDUP_IMPL=perl # tcsh: % setenv FFI_PLATYPUS_MEMORY_STRDUP_IMPL perl # Windows: > SET FFI_PLATYPUS_MEMORY_STRDUP_IMPL=perl
Main Platypus documentation.
Author: Graham Ollis <plicease@cpan.org>
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)
This software is copyright (c) 2015,2016,2017,2018 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.
To install FFI::Platypus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm FFI::Platypus
CPAN shell
perl -MCPAN -e shell install FFI::Platypus
For more information on module installation, please visit the detailed CPAN module installation guide.