S
Sherm Pendley
Consider the following snippet:
use Socket;
my $bytes = pack('L', $obj->bytes());
my $sockaddr = unpack('P2', $bytes);
my ($size, $family) = unpack('cc', $sockaddr);
if ($family == AF_INET) {
my ($port, $address) = sockaddr_in(unpack("P$size", $bytes));
$address = inet_ntoa($address);
# ... do some stuff with $address and $port ...
} elsif ($family == AF_UNIX) {
my ($port, $path) = sockaddr_un(unpack("P$size", $bytes));
# ... do some stuff with $path and $port ...
}
In the above, $obj is a Perl object wrapper around a native object (in this
case an Objective-C object of class NSData), and the native -bytes method
returns a void*. The XS wrapper for the native method is written and
working, passing the void* to Perl as an integer.
What I'd like to know is whether unpack('P2', $bytes) creates a copy of the
structure pointed to. Obviously in this example it doesn't matter a great
deal, as the above is working with a small, read-only struct.
But, the same XS wrapper can be used with any native object, and some native
methods can return pointers to *huge* buffers - image bitmaps, for example.
Also, a pointer can be returned in anticipation of it being used to make
changes to the pointed-to data - once again, image bitmaps are a good
example. In both cases, copying would be undesirable.
So, does anyone know the answer off the top of their head? Or do I need to
start writing test cases?
sherm--
use Socket;
my $bytes = pack('L', $obj->bytes());
my $sockaddr = unpack('P2', $bytes);
my ($size, $family) = unpack('cc', $sockaddr);
if ($family == AF_INET) {
my ($port, $address) = sockaddr_in(unpack("P$size", $bytes));
$address = inet_ntoa($address);
# ... do some stuff with $address and $port ...
} elsif ($family == AF_UNIX) {
my ($port, $path) = sockaddr_un(unpack("P$size", $bytes));
# ... do some stuff with $path and $port ...
}
In the above, $obj is a Perl object wrapper around a native object (in this
case an Objective-C object of class NSData), and the native -bytes method
returns a void*. The XS wrapper for the native method is written and
working, passing the void* to Perl as an integer.
What I'd like to know is whether unpack('P2', $bytes) creates a copy of the
structure pointed to. Obviously in this example it doesn't matter a great
deal, as the above is working with a small, read-only struct.
But, the same XS wrapper can be used with any native object, and some native
methods can return pointers to *huge* buffers - image bitmaps, for example.
Also, a pointer can be returned in anticipation of it being used to make
changes to the pointed-to data - once again, image bitmaps are a good
example. In both cases, copying would be undesirable.
So, does anyone know the answer off the top of their head? Or do I need to
start writing test cases?
sherm--