One possibility is to apply the PERL_MAGIC_ext magic to the SV, as described in perlguts:
int m_free (pTHX_ SV *sv, MAGIC* mg){ ... } STATIC MGVTBL my_vtbl = { 0, 0, 0, 0, m_free, 0, 0, 0 }; struct ctx; XS(XS_some_func) { ... MAGIC *mg; if ((mg = mg_findext((SV*)cv, PERL_MAGIC_ext, &my_vtbl))) { ctx *priv = (ctx *)mg->mg_ptr; } ... }
And the magic assignment when creating a CV through newXS ():
ctx c; ... CV *cv = newXS(index, XS_some_func, __FILE__); MAGIC *mg = sv_magicext((SV *)cv, 0, PERL_MAGIC_ext, &my_vtbl, (const char*)&c, sizeof(c));
source share