[Openmcl-devel] FF call by reference

Gary Byers gb at clozure.com
Tue Jul 15 16:22:03 PDT 2008



On Tue, 15 Jul 2008, Ron Garret wrote:

> I'm trying to call an ObjC method that takes a pointer to a pointer to
> an ns-error as an argument.  I tried this:
>
> ? (rlet ((err (:* ns:ns-error))) (#/foo: thing err)))
> > Error: Unknown foreign type: NS:NS-ERROR

(rlet ((err (:* #>NSError +null-ptr+))) (#/foo: thing err))

should complain about #/foo being undefined.

Initializing the pointer (err) to a null-pointer isn't strictly
necessary (in CCL or ObjC), but might be good style: you're allocating
space (64 or 32 bits) for a pointer, but unless that those bits are
explicitly initialized they'll just be "whatever was on the stack"
and unlikely to be meaningful.

>
> And yet:
>
> ? ns:ns-error
> #<OBJC:OBJC-CLASS NS:NS-ERROR (#x7FFF702C9D60)>
>
> Even if this worked, it seems wrong because I'm passing in the pointer
> directly, not its address.  What's the Right Way to do this?

That is the right way to do it.

One way to think about this is to note that

(rlet ((<ptr> <type>))

always creates a "pointer to type" by reserving space for an instance
of the specified type.

(rlet ((p :int))

reserves 4 bytes (the size of an "int" on virtually all platforms these 
days) and binds P to a pointer to that reserved memory, but doesn't
generally initialize that little block of memory in any meaningful
way: if we do

  (pref p :int)

as the first thing in the body of the RLET, we'll get back a generally
random integer value; that's meaningless, but harmless.

If we instead do:

(rlet ((q (:* int))) ; or any foreign pointer type
   (pref q :address)  ; note that wants its last arg to be a symbol

we'll return an equally meaningless "random pointer", and it might
well be dangerous to try to indirect through that.


>
> Thanks,
> rg
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel
>
>



More information about the Openmcl-devel mailing list