Click here to get back home

Mortals and refcounts in XS

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Mortals and refcounts in XS Matthias Bethke 02-29-2008
Posted by Matthias Bethke on February 29, 2008, 2:32 pm
Please log in for more thread options

I'm currently writing my first from-scratch Perl/XS module and have some
problems with the perlxs and perlguts docs. The question is when exactly
to change reference counts or mortalize values.
My module implements a trie structure in C, storing arbitrary scalar
values under string keys. My idea so far was that I had to increment a
value's refcount when storing it in the trie to keep it from being GC'd
if it should go out of scope, and decrement it when I remove it. But
I've been using RETVAL as copied from some perlxstut example, which is
said to mortalize values it returns. So to avoid "Attempt to free
unreferenced scalar" errors I have to
a) increment refcounts before insertion
b) increment refcounts for values returned in a lookup
c) do nothing when returning a value that has been removed from the trie

This seems to work but it's kinda unintutitive and looks clumsy to me.
For example, here is the lookup XS code:

SV *lookup(self, key)
btrie_nodep_t self;
char *key;
CODE:
{
SV *value;
bool_t isctrie = FALSE;
if(btrie_lookup((btrie_trienodep_t*)&self,&isctrie,&key,&value)) {
SvREFCNT_inc(value);
RETVAL = value;
} else XSRETURN_UNDEF;
}
} /* why the fuck do I need this brace?! */
OUTPUT:
RETVAL

(also, the code won't compile unless I insert an extra closing brace as
above)
Any ideas how to do this better? To do it more efficiently than just
countering the mortalizing effect of RETVAL with SvREFCNT_inc I tried to
replace it with XPUSHs and a simple return:

if(btrie_lookup((btrie_trienodep_t*)&self,&isctrie,&key,&value))
XPUSHs(value)
else
XSRETURN_UNDEF;

but this puts a reference to seemingly random values on the stack. I'm
surely missing something here?

kind regards,
        Matthias

Posted by Ilya Zakharevich on February 29, 2008, 8:47 pm
Please log in for more thread options
[A complimentary Cc of this posting was sent to
Matthias Bethke
> if(btrie_lookup((btrie_trienodep_t*)&self,&isctrie,&key,&value))
> XPUSHs(value)
> else
> XSRETURN_UNDEF;
>
> but this puts a reference to seemingly random values on the stack. I'm
> surely missing something here?

You are missing (at least) that you need to cut & paste THE REAL CODE
to have a hope to get meaningful replys.

Hope this helps,
Ilya

Posted by Matthias Bethke on March 2, 2008, 7:24 pm
Please log in for more thread options
begin followup to Ilya Zakharevich:
> Hope this helps,

Yup. Thanks.
Matthias

--
end


Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap