Repost -- can someone take a moment and review this and make sure I'm
doing it correctly?  I'm about to release this code in a new version of
Swish, so it would be a huge help to have it reviewed.

The actual code can be seen at:

  http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/swishe/swish-e/perl/

The point of this code is to prevent "destruction" of a parent data
structure before the child structures are destroyed.

First, when creating the "parent" structure I save the address of the SV
in the C structure with SwishSetRefPtr().  Then later when children are
created from this object the children can SvREFCNT_inc and SvREFCNT_dec on
the parent object:

This returns SW_HANDLE which is blessed into SWISH::API package:

void 
new(CLASS, index_file_list )
    char *CLASS
    char *index_file_list

    PREINIT:
        SW_HANDLE handle;

    PPCODE:
        SwishErrorsToStderr();
        handle = SwishInit( index_file_list );
        ST(0) = sv_newmortal();
        sv_setref_pv( ST(0), CLASS, (void *)handle );
        SwishSetRefPtr( handle, (void *)SvRV(ST(0)) );
        XSRETURN(1);

SwishSetRefPtr() stores the address of the SV in the C struct so child
objects can access the refcount.

Then when creating a child:

SW_SEARCH
New_Search_Object(swish_handle, query = NULL)
    SW_HANDLE swish_handle
    char *query

    PREINIT:
        char * CLASS = "SWISH::API::Search";

    CODE:
        RETVAL = New_Search_Object( swish_handle, query );
        if ( RETVAL )
            SvREFCNT_inc( (SV *)SwishSearch_parent( RETVAL ) );

    OUTPUT:
        RETVAL

SwishSearch_parent() returns the address saved in SwishSetRefPtr() call
above -- it returns the SV address if the SWISH::API object.

Then in the Destroy:

void
DESTROY(search)
    SW_SEARCH search

    CODE:
        if ( search )
        {
            SV *parent = (SV *)SwishSearch_parent( search );
            Free_Search_Object( search );
            SvREFCNT_dec( parent );
        }

There's two more layers of objects, but basically the same thing.  Short
of my choice for function names, is there anything else that could be
cleaned up?

Thanks much,

-- 
Bill Moseley [EMAIL PROTECTED]


Reply via email to