in reply to Perl, C and deallocation responsibility
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 | |
by Marshall (Canon) on Apr 30, 2023 at 07:11 UTC | |
Re^2: Perl, C and deallocation responsibility
by SheWolf (Novice) on Apr 30, 2023 at 23:02 UTC | |
by etj (Priest) on May 02, 2023 at 13:38 UTC | |
by SheWolf (Novice) on May 03, 2023 at 00:38 UTC |