Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Perl, C and deallocation responsibility

by Marshall (Canon)
on Apr 29, 2023 at 06:31 UTC ( [id://11151907]=note: print w/replies, xml ) Need Help??


in reply to Perl, C and deallocation responsibility

There is a typedef mechanism that translates standard C types into an appropriate Perl type. I haven't used that myself. I have only written XS code using the Perl types directly. I find it less confusing as to who is doing what in terms of memory management. You will need to use Perl functions to allocate memory if you expect Perl to manage that memory. Of course, Perl will never release any memory back to the O/S, but it will reuse it for its own purposes!

See Inline Cookbook Pod for the greeting() program (shown below). This code works on my machine and discusses memory management. I would use SV types for input to your proc_desc() subroutine and manipulate them appropriately. Perhaps allowing for something more complex than just an 8 bit ASCII string (UTF-8?).

I think you will wind up with 2 input SV's and one output SV: SV* proc_desc(SV* a, SV* b) { ....code... }

use strict; use warnings; use Inline 'C'; print greeting('Ingy'); __END__ See https://metacpan.org/dist/Inline-C/view/lib/Inline/C/Cookbook.pod and the example for "greeting" subroutine. "I would urge you to stay away from mallocing your own buffer. Just us +e Perl's built in memory management. In other words, just create a new Perl string scala +r. The function newSVpv does just that. And newSVpvf includes sprintf functionality. The other problem is getting rid of this new scalar. How will the ref +count get decremented after we pass the scalar back? Perl also provides a functi +on called sv_2mortal. Mortal variables die when the context goes out of scope. I +n other words, Perl will wait until the new scalar gets passed back and then decrement the + ref count for you, thereby making it eligible for garbage collection. See perldoc perlgut +s. In this example the sv_2mortal call gets done under the hood by XS, be +cause we declared the return type to be SV*." __C__ SV* greeting(SV* sv_name) { return (newSVpvf("Hello %s!\n", SvPV(sv_name, PL_na))); }

Replies are listed 'Best First'.
Re^2: Perl, C and deallocation responsibility (Returning Memory back to the OS References)
by eyepopslikeamosquito (Archbishop) on Apr 29, 2023 at 07:20 UTC
      Thanks for the info!
      I haven't seen memory go back to the O/S, but I haven't had the need to look closely on Win 10. The last time I got really anxious about memory was when developing a Perl application for Win XP. I had a memory budget for that one. Often for Perl applications, I see memory usage approach a limit in an asymptotic way and then just stay flat forever. Of course, mileage varies a lot depending on the application's needs.

      I think in the context of the OP's question, one question is whether anybody is going to do anything to either free or reuse the memory allocated for the char*. I was happy to have found an explicit statement about what is going to happen if the listed memory allocation routine is used. I was and am not at all clear what actually happens with the char* declaration (instead of an SV that Perl will reference count upon). There is obviously some type mangling that happens underneath the covers, but I'm not sure how.

      BTW, I have found that SQLite is very good at returning memory if you adjust its memory usage during run time. Giving an SQL indexing operation a lot of memory can have a huge positive performance effect. And then shrink down memory size for normal usage.

Re^2: Perl, C and deallocation responsibility
by SheWolf (Novice) on Apr 30, 2023 at 23:02 UTC

    Thank you. I hadn't thought of doing that. I think I was stuck on the whole 'must be a PV' thing.

    I'll try to play with that when I can!

      I would say what's most relevant in your decision is how Perl-dependent you want your C code to be:
      • If you're comfortable with it being Perl-dependent, you would probably want to have all memory allocation/management done by Perl, using probably-mortal SVs, and PVs from the start.
      • If you want to maintain the C code as purely C, then you can use the typemap stuff in the various notes here to copy the C-created data into Perl-land, and then free the C-allocated memory.
        Unfortunately I need to make this work in both worlds. I still need to keep the compiled C running, and I need to have it called by Perl. I never choose the easy path!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found