Debian Codegraph @

lib/FFI/Platypus/Buffer.pm

raw
1 package FFI::Platypus::Buffer;
2
3 use strict;
4 use warnings;
5 use 5.008004;
6 use FFI::Platypus;
7 use Exporter qw( import );
8
9 our @EXPORT = qw( scalar_to_buffer buffer_to_scalar );
10 our @EXPORT_OK = qw ( scalar_to_pointer grow set_used_length window raw_scalar );
11
12 # ABSTRACT: Convert scalars to C buffers
13 our $VERSION = '2.12'; # VERSION
14
15
16 use constant _incantation =>
17 $^O eq 'MSWin32' && do { require Config; $Config::Config{archname} =~ /MSWin32-x64/ }
18 ? 'Q'
19 : 'L!';
20
21
22 sub scalar_to_buffer ($)
23 {
24 (unpack(_incantation, pack 'P', $_[0]), do { use bytes; length $_[0] });
25 }
26
27
28 sub scalar_to_pointer ($)
29 {
30 unpack(_incantation, pack 'P', $_[0]);
31 }
32
33
34 sub buffer_to_scalar ($$)
35 {
36 unpack 'P'.$_[1], pack _incantation, defined $_[0] ? $_[0] : 0;
37 }
38
39 1;
40
41 __END__
42
43 =pod
44
45 =encoding UTF-8
46
47 =head1 NAME
48
49 FFI::Platypus::Buffer - Convert scalars to C buffers
50
51 =head1 VERSION
52
53 version 2.12
54
55 =head1 SYNOPSIS
56
57 use FFI::Platypus::Buffer;
58 my($pointer, $size) = scalar_to_buffer $scalar;
59 my $scalar2 = buffer_to_scalar $pointer, $size;
60
61 =head1 DESCRIPTION
62
63 A common pattern in C is to pass a "buffer" or region of memory into a
64 function with a pair of arguments, an opaque pointer and the size of the
65 memory region. In Perl the equivalent structure is a scalar containing
66 a string of bytes. This module provides portable functions for
67 converting a Perl string or scalar into a buffer and back.
68
69 These functions are implemented using L<pack and unpack|perlpacktut> and
70 so they should be relatively fast.
71
72 Both functions are exported by default, but you can explicitly export
73 one or neither if you so choose.
74
75 A better way to do this might be with custom types see
76 L<FFI::Platypus::API> and L<FFI::Platypus::Type>. These functions were
77 taken from the now obsolete L<FFI::Util> module, as they may be useful
78 in some cases.
79
80 B<Caution>: This module provides great power in the way that you
81 interact with C code, but with that power comes great responsibility.
82 Since you are dealing with blocks of memory you need to take care to
83 understand the underlying ownership model of these pointers.
84
85 =head1 FUNCTIONS
86
87 =head2 scalar_to_buffer
88
89 my($pointer, $size) = scalar_to_buffer $scalar;
90
91 Convert a string scalar into a buffer. Returned in order are a pointer
92 to the start of the string scalar's memory region and the size of the
93 region.
94
95 You should NEVER try to free C<$pointer>.
96
97 When you pass this pointer and size into a C function, it has direct
98 access to the data stored in your scalar, so it is important that you
99 not resize or free the scalar while it is in use by the C code. Typically
100 if you are passing a buffer into a C function which reads or writes to
101 the buffer, but does not keep the pointer for later use you are okay.
102 If the buffer is in use long term by the C code, then you should consider
103 copying the buffer instead. For example:
104
105 use FFI::Platypus::Buffer qw( scalar_to_buffer );
106 use FFI::Platypus::Memory qw( malloc memcpy free )
107
108 my($ptr, $size) = scalar_to_buffer $string;
109 c_function_that_does_not_keep_ptr( $ptr, $size); # okay
110
111 my($ptr, $size) = scalar_to_buffer $string;
112 my $ptr_copy = malloc($size);
113 memcpy($ptr_copy, $ptr, $size);
114 c_function_that_DOES_keep_ptr( $ptr_copy, $size); # also okay
115
116 ...
117
118 # later when you know that the c code is no longer using the pointer
119 # Since you allocated the copy, you are responsible for free'ing it.
120 free($ptr_copy);
121
122 =head2 scalar_to_pointer
123
124 my $pointer = scalar_to_pointer $scalar;
125
126 Get the pointer to the scalar. (Similar to C<scalar_to_buffer> above, but
127 the size of the scalar is not computed or returned).
128
129 Not exported by default, but may be exported on request.
130
131 =head2 buffer_to_scalar
132
133 my $scalar = buffer_to_scalar $pointer, $size;
134
135 Convert the buffer region defined by the pointer and size into a string
136 scalar.
137
138 Because of the way memory management works in Perl, the buffer is copied
139 from the buffer into the scalar. If this pointer was returned from C
140 land, then you should only free it if you allocated it.
141
142 =head2 grow
143
144 grow $scalar, $size, \%options;
145
146 Ensure that the scalar can contain at least C<$size> bytes. The
147 following are recognized:
148
149 =over
150
151 =item clear => I<boolean>
152
153 If true, C<$scalar> is cleared prior to being enlarged. This
154 avoids copying the existing contents to the reallocated memory
155 if they are not needed.
156
157 For example, after
158
159 $scalar = "my string";
160 grow $scalar, 100, { clear => 0 };
161
162 C<$scalar == "my string">, while after
163
164 $scalar = "my string";
165 grow $scalar, 100;
166
167 C<length($scalar) == 0>
168
169 It defaults to C<true>.
170
171 =item set_length => I<boolean>
172
173 If true, the length of the I<string> in the C<$scalar> is set to C<$size>.
174 (See the discussion in L</set_used_length>.) This is useful if a
175 foreign function writes exactly C<$size> bytes to C<$scalar>, as it avoids
176 a subsequent call to C<set_used_length>. Contrast this
177
178 grow my $scalar, 100;
179 read_exactly_100_bytes_into_scalar( scalar_to_pointer($scalar) );
180 @chars = unpack( 'c*', $scalar );
181
182 with this:
183
184 grow my $scalar, 100, { set_length => 0 };
185 read_exactly_100_bytes_into_scalar( scalar_to_pointer($scalar) );
186 set_used_length( $scalar, 100 );
187 @chars = unpack( 'c*', $scalar );
188
189 It defaults to C<true>.
190
191 =back
192
193 Any pointers obtained with C<scalar_to_pointer> or C<scalar_to_buffer>
194 are no longer valid after growing the scalar.
195
196 Not exported by default, but may be exported on request.
197
198 =head2 set_used_length
199
200 set_used_length $scalar, $length;
201
202 Update Perl's notion of the length of the string in the scalar. A
203 string scalar keeps track of two lengths: the number of available
204 bytes and the number of used bytes. When a string scalar is
205 used as a buffer by a foreign function, it is necessary to indicate
206 to Perl how many bytes were actually written to it so that Perl's
207 string functions (such as C<substr> or C<unpack>) will work correctly.
208
209 If C<$length> is larger than what the scalar can hold, it is set to the
210 maximum possible size.
211
212 In the following example, the foreign routine C<read_doubles>
213 may fill the buffer with up to a set number of doubles, returning the
214 number actually written.
215
216 my $sizeof_double = $ffi->sizeof( 'double' );
217 my $max_doubles = 100;
218 my $max_length = $max_doubles * $sizeof_double;
219
220 my $buffer; # length($buffer) == 0
221 grow $buffer, $max_length; # length($buffer) is still 0
222 my $pointer = scalar_to_pointer($buffer);
223
224 my $num_read = read_doubles( $pointer, $max_doubles );
225 # length($buffer) is still == 0
226
227 set_used_length $buffer, $num_read * $sizeof_double;
228 # length($buffer) is finally != 0
229
230 # unpack the native doubles into a Perl array
231 my @doubles = unpack( 'd*', $buffer ); # @doubles == $num_read
232
233 Not exported by default, but may be exported on request.
234
235 =head2 window
236
237 window $scalar, $pointer;
238 window $scalar, $pointer, $size;
239 window $scalar, $pointer, $size, $utf8;
240
241 This makes the scalar a read-only window into the arbitrary region of
242 memory defined by C<$pointer>, pointing to the start of the region
243 and C<$size>, the size of the region. If C<$size> is omitted then
244 it will assume a C style string and use the C C<strlen> function to
245 determine the size (the terminating C<'\0'> will not be included).
246
247 This can be useful if you have a C function that returns a buffer
248 pair (pointer, size), and want to access it from Perl without having
249 to copy the data. This can also be useful when interfacing with
250 programming languages that store strings as a address/length pair
251 instead of a pointer to null-terminated sequence of bytes.
252
253 You can specify C<$utf8> to set the UTF-8 flag on the scalar. Note
254 that the behavior of setting the UTF-8 flag on a buffer that does
255 not contain UTF-8 as understood by the version of Perl that you are
256 running is undefined.
257
258 I<Hint>: If you have a buffer that needs to be free'd by C once the
259 scalar falls out of scope you can use L<Variable::Magic> to apply
260 magic to the scalar and free the pointer once it falls out of scope.
261
262 use FFI::Platypus::Buffer qw( scalar_to_pointer );
263 use FFI::Platypus::Memory qw( strdup free );
264 use Variable::Magic qw( wizard cast );
265
266 my $free_when_out_of_scope = wizard(
267 free => sub {
268 my $ptr = scalar_to_pointer ${$_[0]};
269 free $ptr;
270 }
271 );
272
273 my $ptr = strdup "Hello Perl";
274 my $scalar;
275 window $scalar, $ptr, 10;
276 cast $scalar, $free_when_out_of_scope;
277 undef $ptr; # don't need to track the pointer anymore.
278
279 # we can now use scalar as a regular read-only Perl variable
280 print $scalar, "\n"; # prints "Hello Perl" without the \0
281
282 # this will free the C pointer
283 undef $scalar;
284
285 I<Hint>: Returning a scalar string from a Perl function actually
286 copies the value. If you want to return a string without copying
287 then you need to return a reference.
288
289 sub c_string
290 {
291 my $ptr = strdup "Hello Perl";
292 my $scalar;
293 window $scalar, $ptr, 10;
294 cast $scalar, $free_when_out_of_scope;
295 \$scalar;
296 }
297
298 my $ref = c_string();
299 print $$ref, "\n"; # prints "Hello Perl" without the \0
300
301 Not exported by default, but may be exported on request.
302
303 =head2 raw_scalar
304
305 my $buffer = raw_scalar $size;
306
307 Returns a new scalar of exactly C<$size> bytes, suitable for use as a
308 buffer. The contents of the returned scalar are B<not> initialized, so
309 it will contain whatever happened to be in the memory that was allocated
310 for it. This makes it cheaper than the equivalent
311
312 my $buffer = "\0" x $size;
313
314 when you are about to have a foreign function overwrite the whole thing
315 anyway. If you need the buffer zeroed out, use the C<"\0" x $size> form
316 instead.
317
318 Not exported by default, but may be exported on request.
319
320 =head1 SEE ALSO
321
322 =over 4
323
324 =item L<FFI::Platypus>
325
326 Main Platypus documentation.
327
328 =back
329
330 =head1 AUTHOR
331
332 Author: Graham Ollis E<lt>[email protected]<gt>
333
334 Contributors:
335
336 Bakkiaraj Murugesan (bakkiaraj)
337
338 Dylan Cali (calid)
339
340 pipcet
341
342 Zaki Mughal (zmughal)
343
344 Fitz Elliott (felliott)
345
346 Vickenty Fesunov (vyf)
347
348 Gregor Herrmann (gregoa)
349
350 Shlomi Fish (shlomif)
351
352 Damyan Ivanov
353
354 Ilya Pavlov (Ilya33)
355
356 Petr Písař (ppisar)
357
358 Mohammad S Anwar (MANWAR)
359
360 Håkon Hægland (hakonhagland, HAKONH)
361
362 Meredith (merrilymeredith, MHOWARD)
363
364 Diab Jerius (DJERIUS)
365
366 Eric Brine (IKEGAMI)
367
368 szTheory
369
370 José Joaquín Atria (JJATRIA)
371
372 Pete Houston (openstrike, HOUSTON)
373
374 Lukas Mai (MAUKE)
375
376 =head1 COPYRIGHT AND LICENSE
377
378 This software is copyright (c) 2015-2022 by Graham Ollis.
379
380 This is free software; you can redistribute it and/or modify it under
381 the same terms as the Perl 5 programming language system itself.
382
383 =cut