| 1 | package FFI::Raw::MemPtr; | ||
| 2 | 1 | $FFI::Raw::MemPtr::VERSION = '0.32'; | |
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | FFI::Raw::MemPtr - FFI::Raw memory pointer type | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | version 0.32 | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | A B<FFI::Raw::MemPtr> represents a memory pointer which can be passed to | ||
| 17 | functions taking a C<FFI::Raw::ptr> argument. | ||
| 18 | |||
| 19 | The allocated memory is automatically deallocated once the object is not in use | ||
| 20 | anymore. | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | =head2 new( $length ) | ||
| 25 | |||
| 26 | Allocate a new C<FFI::Raw::MemPtr> of size C<$length> bytes. | ||
| 27 | |||
| 28 | =head2 new_from_buf( $buffer, $length ) | ||
| 29 | |||
| 30 | Allocate a new C<FFI::Raw::MemPtr> of size C<$length> bytes and copy C<$buffer> | ||
| 31 | into it. This can be used, for example, to pass a pointer to a function that | ||
| 32 | takes a C struct pointer, by using C<pack()> or the L<Convert::Binary::C> module | ||
| 33 | to create the actual struct content. | ||
| 34 | |||
| 35 | For example, consider the following C code | ||
| 36 | |||
| 37 | struct some_struct { | ||
| 38 | int some_int; | ||
| 39 | char some_str[]; | ||
| 40 | }; | ||
| 41 | |||
| 42 | extern void take_one_struct(struct some_struct *arg) { | ||
| 43 | if (arg -> some_int == 42) | ||
| 44 | puts(arg -> some_str); | ||
| 45 | } | ||
| 46 | |||
| 47 | It can be called using FFI::Raw as follows: | ||
| 48 | |||
| 49 | use FFI::Raw; | ||
| 50 | |||
| 51 | my $packed = pack('ix![p]p', 42, 'hello'); | ||
| 52 | my $arg = FFI::Raw::MemPtr -> new_from_buf($packed, length $packed); | ||
| 53 | |||
| 54 | my $take_one_struct = FFI::Raw -> new( | ||
| 55 | $shared, 'take_one_struct', | ||
| 56 | FFI::Raw::void, FFI::Raw::ptr | ||
| 57 | ); | ||
| 58 | |||
| 59 | $take_one_struct -> ($arg); | ||
| 60 | |||
| 61 | Which would print C<hello>. | ||
| 62 | |||
| 63 | =head2 new_from_ptr( $ptr ) | ||
| 64 | |||
| 65 | Allocate a new C<FFI::Raw::MemPtr> pointing to the C<$ptr>, which can be either | ||
| 66 | a C<FFI::Raw::MemPtr> or a pointer returned by another function. | ||
| 67 | |||
| 68 | This is the C<FFI::Raw> equivalent of a pointer to a pointer. | ||
| 69 | |||
| 70 | =head2 tostr( [$length] ) | ||
| 71 | |||
| 72 | Convert a C<FFI::Raw::MemPtr> to a Perl string. If C<$length> is not provided, | ||
| 73 | the length of the string will be computed using C<strlen()>. | ||
| 74 | |||
| 75 | =head1 AUTHOR | ||
| 76 | |||
| 77 | Alessandro Ghedini <alexbio@cpan.org> | ||
| 78 | |||
| 79 | =head1 LICENSE AND COPYRIGHT | ||
| 80 | |||
| 81 | Copyright 2013 Alessandro Ghedini. | ||
| 82 | |||
| 83 | This program is free software; you can redistribute it and/or modify it | ||
| 84 | under the terms of either: the GNU General Public License as published | ||
| 85 | by the Free Software Foundation; or the Artistic License. | ||
| 86 | |||
| 87 | See http://dev.perl.org/licenses/ for more information. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | 1 | 1; # End of FFI::Raw::MemPtr | |