[gambit-list] FFI C out parameters

Marc Feeley feeley at iro.umontreal.ca
Tue Jan 13 14:30:50 EST 2015


> On Jan 13, 2015, at 1:48 PM, Jacob Horrocks <neggy6 at gmail.com> wrote:
> 
> Hi Gambit.
> 
> I'm new to C programming, and am struggling in thinking up a way to wrap c functions that have out parameters via pointers. My strongest desire would to be able to call a c-lambda like-so:
> 
> (receive return-value-1 return-value-2 (my-c-lambda-wrapper-call some args) ....)
> 
> But I am completely stumped.
> 
> Thanks for any help/advice you can give.

Returning multiple values from a C function can be accomplished by passing a preallocated vector to the C function and having the C function fill the content of the vector with the values to return.  Here is an example.

Marc


(c-declare "

void add_mul(int x, int y, ___SCMOBJ res) {

   int add = x+y;
   int mul = x*y;

   ___VECTORSET(res, ___FIX(0), ___FIX(add))
   ___VECTORSET(res, ___FIX(1), ___FIX(mul))
}
")

(define (add-mul x y)
  (let ((res (vector 0 0)))
    ((c-lambda (int int scheme-object) void "add_mul") x y res)
    (values (vector-ref res 0)
            (vector-ref res 1))))

(receive (add mul) (add-mul 3 7)
 (pp (list 'add= add 'mul= mul)))

;; prints: (add= 10 mul= 21)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4839 bytes
Desc: not available
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20150113/71185bef/attachment.bin>


More information about the Gambit-list mailing list