Re: perl binding for C++ ref-counted object
[email protected] (Marvin Humphrey)
| Newsgroups | perl.xs |
|---|---|
| Message-ID | <[email protected]> |
On Fri, Oct 21, 2011 at 06:44:50AM +0000, YangXi wrote:
> I've got a more nasty idea: all I need is refcount and garbage collection,
> so I can just embed an perl HV inside my object, and use the reference count
> of that HV. Woo! Too ugly!
Haha, this is a technique that we use in Clownfish, the object model used by
Apache Lucy (which is C rather than C++, but needs to solve similar problems).
Your C++ refcount manipulation methods will need to wrap invocations of
SvREFCNT_dec(), SvREFCNT_inc(), and SvREFCNT(), and you need an XS DESTROY
method which invokes the C++ destructor.
The key is, as you have surmised, maintaining a unified refcount for the
combination of a Perl object and a C++ object which point to each other.
Here's the code which implements our refcount manipulation routines:
http://svn.apache.org/repos/asf/incubator/lucy/trunk/perl/xs/Lucy/Object/Obj.c
And here's a thread on the Lucy developer's list where the design was
discussed:
http://lucy.markmail.org/message/jkst23okksyynzss
Our code is probably more complex than than yours will be because we lazily
create the Perl object only when the refcount reaches 4 or it's needed because
access has been requested from Perl-space. We also use only the inner portion
of a Perl blessed scalar, avoiding the outer RV.
Marvin Humphrey