in reply to Using multiple XS objects together
The XS part of AlarmZone::new(SV* security_credentials, int zone) increases the reference count of security_credentials with SvREFCNT_inc() or similar, retains a pointer to the SV*, and decreases the reference count in AlarmZone->DESTROY(). This is the cleanest idea I can think of (reference counting stays in the Perl area of the program, C++ part continues unaware of Perl's existence.). I am stuck working out the details though, in particular where to store the security_credentials pointer (so it can be used later in DESTROY()).You have to add magic to the inner blessed SV with sv_magicext, here is an example.
The other ideas is to have the inner SV be an AV or HV instead of an SVIV as is typically for XSPP wrapped C++ objs./* declare as 5 member, not normal 8 to save image space*/ const static struct { int (*svt_get)(SV* sv, MAGIC* mg); int (*svt_set)(SV* sv, MAGIC* mg); U32 (*svt_len)(SV* sv, MAGIC* mg); int (*svt_clear)(SV* sv, MAGIC* mg); int (*svt_free)(SV* sv, MAGIC* mg); } my_vtbl = { NULL, NULL, NULL, NULL, NULL }; /* puts newsv, refcnt++ed (caller doesn't have to do it), in sv as hid +den magic SV */ STATIC void setMgSV(pTHX_ SV * sv, SV * newsv) { MAGIC * mg; if(SvRMAGICAL(sv)) { /* implies SvTYPE >= SVt_PVMG */ mg = mg_findext(sv, PERL_MAGIC_ext, &my_vtbl); if(mg) { SV * oldsv; SvREFCNT_inc_simple_void_NN(newsv); oldsv = mg->mg_obj; mg->mg_obj = newsv; SvREFCNT_dec(oldsv); } else { goto addmg; } } else { addmg: sv_magicext(sv,newsv,PERL_MAGIC_ext,&my_vtbl,NULL,0); } } # xsub to attach a hidden SV in RV inside to the target SV of RV outsi +de # both params must be references # void SetMagicSV(outside, inside) void SetMagicSV(...) PREINIT: SV * outside; SV * inside; CODE: if(items != 2) croak_xs_usage(cv, "outside, inside"); inside = POPs; outside = POPs; PUTBACK; if(SvROK(outside) && SvROK(inside)) { outside = SvRV(outside); inside = SvRV(inside); } else{ croak_xs_usage(cv, "outside, inside"); } setMgSV(aTHX_ outside, inside); return;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using multiple XS objects together
by Anonymous Monk on May 10, 2013 at 22:58 UTC |