I'm writing some code that is a wrapper around a C library that provides
various ways of looking up and returning a C structure.  I then flatten
the structure and return it on the perl stack.  Because the 'flatten'
code is always the same, I want to push it down into a function.  This
function however will need to access the perl stack to push the return
values onto it.  I've passed SP into the flatten function so that it
looks something like this:

static int
pushret_mystruct(const struct mystruct *myst)
{
        dTARG;
        EXTEND(SP, 3);
        PUSHs(sv_2mortal(newSVpv(myst->fielda, 0)));
        PUSHs(sv_2mortal(newSViv(myst->fieldb)));
        PUSHs(sv_2mortal(newSVpv(myst->fieldc, 0)));
        return(3);
}

One of the calling calling XS routine then looks something like this:

SV*
getmystructbyid(id)
        char    *id
PREINIT:
        struct mystruct  myst, *mystp;
PPCODE:
        if (mystp = getmystructbyid(id)) {
                XSRETURN(pushret_mystruct(sp, mystp));
        } else {
                XSRETURN_EMPTY;
        }

I've read all the dire warnings in perlcall about bracketing things with
various macros to make sure the stack is balanced, calling SPAGAIN to
refetch SP etc, but as I'm not actually calling a perl routine, and as
I'm returning immediately after the call to  pushret_myspec, does the
above look sane?

Thanks,

Alan Burlison

Reply via email to