Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: What difference between malloc and Newx, how attach a C string to SV directly?

by Anonymous Monk
on Oct 01, 2019 at 02:00 UTC ( [id://11106892]=note: print w/replies, xml ) Need Help??


in reply to Re^2: What difference between malloc and Newx, how attach a C string to SV directly?
in thread What difference between malloc and Newx, how attach a C string to SV directly?

Many Thanks, ikegami, just one question, how perl decide to realloc pv or just make a copy?
  • Comment on Re^3: What difference between malloc and Newx, how attach a C string to SV directly?

Replies are listed 'Best First'.
Re^4: What difference between malloc and Newx, how attach a C string to SV directly?
by ikegami (Patriarch) on Oct 01, 2019 at 15:14 UTC

    If the scalar's SvLEN is 0, the buffer exists outside of Perl's memory allocation system, so Perl must allocate a new buffer if it needs a larger buffer.

    If the scalar's copy-on-write count is larger than 1, that means two scalars are sharing the buffer, so Perl must allocate a new buffer if it needs to change the contents or size of the buffer.

    Unless I'm missing something, Perl should otherwise realloc if it needs more space, which will extend the existing buffer if possible.

    Note that Perl typically over-allocates to accommodate future growth.

    There are a couple of optimizations (copy-on-write, mortal buffer stealing) that will replace a scalar's buffer when you assign to it. But that's specifically done to avoid copying, so that's different.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106892]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found