从本机指针中获取数据

Getting data out of Native pointers

获取数据到Perl 6 Native指针没什么大不了的:

sub memcpy( Pointer[void] $source, Pointer[void] $destination, int32 $size ) is native { * };
my Blob $blob = Blob.new(0x22, 0x33);
my Pointer[void] $src-memcpy = nativecast(Pointer[void], $blob);
my Pointer[void] $dest-memcpy = malloc( 32 );
memcpy($src-memcpy,$dest-memcpy,2);
my Pointer[int] $inter = nativecast(Pointer[int], $dest-memcpy);
say $inter; # prints NativeCall::Types::Pointer[int]<0x4499560>

但是,除了创建一个函数来执行此操作外,我看不出有什么办法可以让它们脱离 Pointer[int],因为 nativecast 显然是在相反的方向上工作,或者至少不是在转换为非本地类型的方向(通过名称应该很明显)。你会怎么做?

更新:例如,使用数组会使它更可行。然而

my $inter = nativecast(CArray[int16], $dest);
.say for $inter.list;

这有效,但会产生错误:Don't know how many elements a C array returned from a library

更新 2:关注 (thanks!), we can ellaborate it a little bit more into this, and we get to put the values back into a Buf

sub malloc(size_t $size --> Pointer) is native {*}
sub memcpy(Pointer $dest, Pointer $src, size_t $size --> Pointer) is native {*}

my $blob = Blob.new(0x22, 0x33);
my $src = nativecast(Pointer, $blob);
my $dest = malloc( $blob.bytes );
memcpy($dest, $src, $blob.bytes);
my $inter = nativecast(Pointer[int8], $dest);

my $cursor = $inter;

my Buf $new-blob .= new() ;
for 1..$blob.bytes {
    $new-blob.append: $cursor.deref;
    $cursor++;
}

say $new-blob;

我们需要将指针转换为与缓冲区使用的类型完全相同的类型,然后 we use pointer arithmetic 转换为 运行。但是,我们使用 $blob.bytes 来知道何时结束循环,这仍然有点麻烦。会有更直接的方法吗?或者只是一种使用 Bufs/Blobs 的方式,以便可以轻松地将它们复制到本地领域并返回?

完整示例:

use NativeCall;

sub malloc(size_t $size --> Pointer) is native {*}
sub memcpy(Pointer $dest, Pointer $src, size_t $size --> Pointer) is native {*}

my $blob = Blob.new(0x22, 0x33);
my $src = nativecast(Pointer, $blob);
my $dest = malloc(nativesizeof(int16));
memcpy($dest, $src, nativesizeof(int16));
my $inter = nativecast(Pointer[int16], $dest);
say $inter.deref.fmt('%x');

我假设您正在寻找 deref 方法,但您的代码还有一些其他问题:

  • memcpy
  • 的声明中使用 int32 而不是 size_t
  • memcpy 的参数顺序错误,这意味着您将从未初始化的内存中读取的两个字节复制到您所谓的不可变 Blob
  • 使用纯 int,您可能应该使用 int16
  • 等大小整数类型

还要注意在 memcpy 的声明中使用普通的 Pointer,这将允许您传入任何指针类型而无需不断地转换。

使用NativeHelpers::Blob:

use NativeCall;
use NativeHelpers::Blob;

sub malloc(size_t $size --> Pointer) is native {*}
sub memcpy(Pointer $dest, Pointer $src, size_t $size --> Pointer) is native {*}

my $blob = Blob.new(0x22, 0x33);
my $src = nativecast(Pointer, $blob);
my $dest = malloc( $blob.bytes );
memcpy($dest, $src, $blob.bytes);
my $inter = nativecast(Pointer[int8], $dest);

my Blob $esponja = blob-from-pointer( $inter, :2elems, :type(Blob[int8]));
say $esponja;

这个节目 adding the blob-from-pointer function, which will return the original blob: Blob[int8]:0x<22 33>. This answer is also now in the documentation