| 1 |
package FFI::Platypus::Memory; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use 5.008004; |
| 6 |
use FFI::Platypus; |
| 7 |
use Exporter qw( import ); |
| 8 |
|
| 9 |
# ABSTRACT: Memory functions for FFI |
| 10 |
our $VERSION = '2.12'; # VERSION |
| 11 |
|
| 12 |
|
| 13 |
our @EXPORT = qw( malloc free calloc realloc memcpy memset strdup strndup strcpy ); |
| 14 |
|
| 15 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 16 |
$ffi->lib(undef); |
| 17 |
$ffi->bundle; |
| 18 |
sub _ffi { $ffi } |
| 19 |
|
| 20 |
$ffi->attach(malloc => ['size_t'] => 'opaque' => '$'); |
| 21 |
$ffi->attach(free => ['opaque'] => 'void' => '$'); |
| 22 |
$ffi->attach(calloc => ['size_t', 'size_t'] => 'opaque' => '$$'); |
| 23 |
$ffi->attach(realloc => ['opaque', 'size_t'] => 'opaque' => '$$'); |
| 24 |
$ffi->attach(memcpy => ['opaque', 'opaque', 'size_t'] => 'opaque' => '$$$'); |
| 25 |
$ffi->attach(memset => ['opaque', 'int', 'size_t'] => 'opaque' => '$$$'); |
| 26 |
$ffi->attach(strcpy => ['opaque', 'string'] => 'opaque' => '$$'); |
| 27 |
|
| 28 |
my $_strdup_impl = 'not-loaded'; |
| 29 |
sub _strdup_impl { $_strdup_impl } |
| 30 |
|
| 31 |
eval { |
| 32 |
die "do not use c impl" if ($ENV{FFI_PLATYPUS_MEMORY_STRDUP_IMPL}||'libc') eq 'ffi'; |
| 33 |
$ffi->attach(strdup => ['string'] => 'opaque' => '$'); |
| 34 |
$_strdup_impl = 'libc'; |
| 35 |
}; |
| 36 |
if($@ && $^O eq 'MSWin32') |
| 37 |
{ |
| 38 |
eval { |
| 39 |
die "do not use c impl" if ($ENV{FFI_PLATYPUS_MEMORY_STRDUP_IMPL}||'libc') eq 'ffi'; |
| 40 |
$ffi->attach([ _strdup => 'strdup' ] => ['string'] => 'opaque' => '$'); |
| 41 |
$_strdup_impl = 'libc'; |
| 42 |
}; |
| 43 |
} |
| 44 |
if($@) |
| 45 |
{ |
| 46 |
warn "using bundled strdup"; |
| 47 |
$_strdup_impl = 'ffi'; |
| 48 |
$ffi->attach([ ffi_platypus_memory__strdup => 'strdup' ] => ['string'] => 'opaque' => '$'); |
| 49 |
} |
| 50 |
|
| 51 |
my $_strndup_impl = 'not-loaded'; |
| 52 |
sub _strndup_impl { $_strndup_impl } |
| 53 |
|
| 54 |
eval { |
| 55 |
die "do not use c impl" if ($ENV{FFI_PLATYPUS_MEMORY_STRDUP_IMPL}||'libc') eq 'ffi'; |
| 56 |
$ffi->attach(strndup => ['string','size_t'] => 'opaque' => '$$'); |
| 57 |
$_strndup_impl = 'libc'; |
| 58 |
}; |
| 59 |
if($@) |
| 60 |
{ |
| 61 |
$_strndup_impl = 'ffi'; |
| 62 |
$ffi->attach([ ffi_platypus_memory__strndup => 'strndup' ] => ['string','size_t'] => 'opaque' => '$$'); |
| 63 |
} |
| 64 |
|
| 65 |
# used internally by FFI::Platypus::Type::WideString, may go away. |
| 66 |
eval { $ffi->attach( [ wcslen => '_wcslen' ] => [ 'opaque' ] => 'size_t' => '$' ) }; |
| 67 |
eval { $ffi->attach( [ wcsnlen => '_wcsnlen' ] => [ 'string', 'size_t' ] => 'size_t' => '$$' ) }; |
| 68 |
|
| 69 |
1; |
| 70 |
|
| 71 |
__END__ |
| 72 |
|
| 73 |
=pod |
| 74 |
|
| 75 |
=encoding UTF-8 |
| 76 |
|
| 77 |
=head1 NAME |
| 78 |
|
| 79 |
FFI::Platypus::Memory - Memory functions for FFI |
| 80 |
|
| 81 |
=head1 VERSION |
| 82 |
|
| 83 |
version 2.12 |
| 84 |
|
| 85 |
=head1 SYNOPSIS |
| 86 |
|
| 87 |
use FFI::Platypus::Memory; |
| 88 |
|
| 89 |
# allocate 64 bytes of memory using the |
| 90 |
# libc malloc function. |
| 91 |
my $pointer = malloc 64; |
| 92 |
|
| 93 |
# use that memory wisely |
| 94 |
... |
| 95 |
|
| 96 |
# free the memory when you are done. |
| 97 |
free $pointer; |
| 98 |
|
| 99 |
=head1 DESCRIPTION |
| 100 |
|
| 101 |
This module provides an interface to common memory functions provided by |
| 102 |
the standard C library. They may be useful when constructing interfaces |
| 103 |
to C libraries with FFI. It works mostly with the C<opaque> type and it |
| 104 |
is worth reviewing the section on opaque pointers in L<FFI::Platypus::Type>. |
| 105 |
|
| 106 |
Allocating memory and forgetting to free it is a common source of memory |
| 107 |
leaks in C and when using this module. Very recent Perls have a C<defer> |
| 108 |
keyword that lets you automatically call functions like C<free> when a |
| 109 |
block ends. This can be especially handy when you have multiple code |
| 110 |
paths or possible exceptions to keep track of. |
| 111 |
|
| 112 |
use feature 'defer'; |
| 113 |
use FFI::Platypus::Memory qw( malloc free ); |
| 114 |
|
| 115 |
sub run { |
| 116 |
my $ptr = malloc 66; |
| 117 |
defer { free $ptr }; |
| 118 |
|
| 119 |
my $data = do_something($ptr); |
| 120 |
|
| 121 |
# do not need to remember to place free $ptr here, as it will |
| 122 |
# run through defer. |
| 123 |
|
| 124 |
return $data; |
| 125 |
} |
| 126 |
|
| 127 |
If you are not lucky enough to have the C<defer> feature in your version |
| 128 |
of Perl you may be able to use L<Feature::Compat::Defer>, which will use |
| 129 |
the feature if available, and provides its own mostly compatible version |
| 130 |
if not. |
| 131 |
|
| 132 |
=head1 FUNCTIONS |
| 133 |
|
| 134 |
=head2 calloc |
| 135 |
|
| 136 |
my $pointer = calloc $count, $size; |
| 137 |
|
| 138 |
The C<calloc> function contiguously allocates enough space for I<$count> |
| 139 |
objects that are I<$size> bytes of memory each. |
| 140 |
|
| 141 |
=head2 free |
| 142 |
|
| 143 |
free $pointer; |
| 144 |
|
| 145 |
The C<free> function frees the memory allocated by C<malloc>, C<calloc>, |
| 146 |
C<realloc> or C<strdup>. It is important to only free memory that you |
| 147 |
yourself have allocated. A good way to crash your program is to try and |
| 148 |
free a pointer that some C library has returned to you. |
| 149 |
|
| 150 |
=head2 malloc |
| 151 |
|
| 152 |
my $pointer = malloc $size; |
| 153 |
|
| 154 |
The C<malloc> function allocates I<$size> bytes of memory. |
| 155 |
|
| 156 |
=head2 memcpy |
| 157 |
|
| 158 |
memcpy $dst_pointer, $src_pointer, $size; |
| 159 |
|
| 160 |
The C<memcpy> function copies I<$size> bytes from I<$src_pointer> to |
| 161 |
I<$dst_pointer>. It also returns I<$dst_pointer>. |
| 162 |
|
| 163 |
=head2 memset |
| 164 |
|
| 165 |
memset $buffer, $value, $length; |
| 166 |
|
| 167 |
The C<memset> function writes I<$length> bytes of I<$value> to the address |
| 168 |
specified by I<$buffer>. |
| 169 |
|
| 170 |
=head2 realloc |
| 171 |
|
| 172 |
my $new_pointer = realloc $old_pointer, $size; |
| 173 |
|
| 174 |
The C<realloc> function reallocates enough memory to fit I<$size> bytes. |
| 175 |
It copies the existing data and frees I<$old_pointer>. |
| 176 |
|
| 177 |
If you pass C<undef> in as I<$old_pointer>, then it behaves exactly like |
| 178 |
C<malloc>: |
| 179 |
|
| 180 |
my $pointer = realloc undef, 64; # same as malloc 64 |
| 181 |
|
| 182 |
=head2 strcpy |
| 183 |
|
| 184 |
strcpy $opaque, $string; |
| 185 |
|
| 186 |
Copies the string to the memory location pointed to by C<$opaque>. |
| 187 |
|
| 188 |
=head2 strdup |
| 189 |
|
| 190 |
my $pointer = strdup $string; |
| 191 |
|
| 192 |
The C<strdup> function allocates enough memory to contain I<$string> and |
| 193 |
then copies it to that newly allocated memory. This version of |
| 194 |
C<strdup> returns an opaque pointer type, not a string type. This may |
| 195 |
seem a little strange, but returning a string type would not be very |
| 196 |
useful in Perl. |
| 197 |
|
| 198 |
=head2 strndup |
| 199 |
|
| 200 |
my $pointer = strndup $string, $max; |
| 201 |
|
| 202 |
The same as C<strdup> above, except at most C<$max> characters will be |
| 203 |
copied in the new string. |
| 204 |
|
| 205 |
=head1 SEE ALSO |
| 206 |
|
| 207 |
=over 4 |
| 208 |
|
| 209 |
=item L<FFI::Platypus> |
| 210 |
|
| 211 |
Main Platypus documentation. |
| 212 |
|
| 213 |
=back |
| 214 |
|
| 215 |
=head1 AUTHOR |
| 216 |
|
| 217 |
Author: Graham Ollis E<lt>[email protected]<gt> |
| 218 |
|
| 219 |
Contributors: |
| 220 |
|
| 221 |
Bakkiaraj Murugesan (bakkiaraj) |
| 222 |
|
| 223 |
Dylan Cali (calid) |
| 224 |
|
| 225 |
pipcet |
| 226 |
|
| 227 |
Zaki Mughal (zmughal) |
| 228 |
|
| 229 |
Fitz Elliott (felliott) |
| 230 |
|
| 231 |
Vickenty Fesunov (vyf) |
| 232 |
|
| 233 |
Gregor Herrmann (gregoa) |
| 234 |
|
| 235 |
Shlomi Fish (shlomif) |
| 236 |
|
| 237 |
Damyan Ivanov |
| 238 |
|
| 239 |
Ilya Pavlov (Ilya33) |
| 240 |
|
| 241 |
Petr Písař (ppisar) |
| 242 |
|
| 243 |
Mohammad S Anwar (MANWAR) |
| 244 |
|
| 245 |
Håkon Hægland (hakonhagland, HAKONH) |
| 246 |
|
| 247 |
Meredith (merrilymeredith, MHOWARD) |
| 248 |
|
| 249 |
Diab Jerius (DJERIUS) |
| 250 |
|
| 251 |
Eric Brine (IKEGAMI) |
| 252 |
|
| 253 |
szTheory |
| 254 |
|
| 255 |
José Joaquín Atria (JJATRIA) |
| 256 |
|
| 257 |
Pete Houston (openstrike, HOUSTON) |
| 258 |
|
| 259 |
Lukas Mai (MAUKE) |
| 260 |
|
| 261 |
=head1 COPYRIGHT AND LICENSE |
| 262 |
|
| 263 |
This software is copyright (c) 2015-2022 by Graham Ollis. |
| 264 |
|
| 265 |
This is free software; you can redistribute it and/or modify it under |
| 266 |
the same terms as the Perl 5 programming language system itself. |
| 267 |
|
| 268 |
=cut |