Does unpack('P', ...) create a copy?

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--
 
T

Tassilo v. Parseval

Also sprach 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.

No copy is created in case of 'P' and 'p'. Note that creating a copy of
a structure when you only have a pointer to it (and not the type of it,
that is, its size) isn't even possible.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top