[plt-scheme] Reusing the argv parameter in C extension primitives

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Nov 25 14:52:30 EST 2004

At Thu, 25 Nov 2004 19:00:27 +0900, Daniel Silva wrote:
> Scheme_Object* spy_apply(const char* name, int argc, Scheme_Object* argv[])
> {
>   Scheme_Object* scheme_pyfn = argv[0];
>   PyObject* fnobj = PY(scheme_pyfn);
>   if (fnobj->ob_type == &PySchemeFunction_Type)
>    return _scheme_apply(((PySchemeFunctionObject*)fnobj)->proc,
>                                        argc - 1, argv + 1);
>    ....
> }

The only problem with this is that argv might be GC-allocated. In that
case, you're using pointer arithmetic on GC-allocated memory, which 3m
doesn't allow, even in local variables. (Even with conservative GC, the
target procedure might put argv in an allocated record, and the
interior pointer would be a problem in that case, too.)

> But if the Scheme procedure used call/cc then my code would end up
> jumping to random places. 

I don't think call/cc would cause any trouble in this case.

> Should I always make a copy of argv instead
> of reusing it in another scheme_apply?  If so, can I keep a single
> static argv array for that purpose, like this?

Call/cc would create trouble for a static array, though.

Assuming that you usually have less than five or so arguments, I
recommend allocating a small argv on the stack, and malloc a large argv
when the stack-allocated argv is not large enough.


Posted on the users mailing list.