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

Discussion in 'Perl Misc' started by Sherm Pendley, Jan 27, 2004.

  1. 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--
     
    Sherm Pendley, Jan 27, 2004
    #1
    1. Advertisements

  2. 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
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Jan 27, 2004
    #2
    1. Advertisements

  3. Tassilo v. Parseval wrote:

    > No copy is created in case of 'P' and 'p'.


    Most excellent, thank you.

    sherm--
     
    Sherm Pendley, Jan 27, 2004
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    4
    Views:
    1,277
    John Saunders
    Jan 12, 2005
  2. Steve Richter
    Replies:
    4
    Views:
    5,901
    Steve Richter
    Apr 18, 2005
  3. Steve Franks

    Copy Web tool does not copy sub dirs?

    Steve Franks, Sep 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    615
    Steve Franks
    Sep 15, 2005
  4. Alex
    Replies:
    2
    Views:
    1,633
  5. Replies:
    26
    Views:
    2,608
    Roland Pibinger
    Sep 1, 2006
  6. Replies:
    2
    Views:
    2,668
    John Harrison
    May 26, 2007
  7. chak

    Why does a web service create a copy at runtime ?

    chak, Nov 22, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    153
    Scott M.
    Nov 22, 2004
  8. fhscobey
    Replies:
    0
    Views:
    356
    fhscobey
    Nov 3, 2005
Loading...