On Mar 6, 2012, at 10:31 AM, Kristian Lein-Mathisen wrote:

> (define %make-point (foreign-primitive void
>                                        (((c-pointer (struct point)) dest) 
> (float x) (float y))
> #<<END
> dest->x = x;
> dest->y = y;
> C_return();
> END
> ))
> 
> (define (make-point x y) 
>         (let ((loc (location (make-blob %get struct-size somehow%))))
>            (%make-point loc x y)
>             loc))


%get struct-size somehow%
 -> (foreign-value "sizeof(struct point)" int)

Avoid the locative by using scheme-pointer instead of c-pointer.

There's no reason I can think of to use foreign-primitive there instead of 
foreign-lambda*.

Ideally wrap the blob in a define-record for safety issues (not done below,
we stick with a bare pointer as in your example).

Code:

#>
struct point { float x; float y; };
<#

(define %make-point (foreign-lambda* void
                        ((scheme-pointer b) (float x) (float y))
#<<END
((struct point *)b)->x = x;
((struct point *)b)->y = y;
END
))

(define (make-point x y)
  (let ((b (make-blob (foreign-value "sizeof(struct point)" int))))
    (%make-point b x y)
    b))

(define point-y (foreign-lambda* float ((scheme-pointer b))
                 "return(((struct point *)b)->y);"))

(print (point-y (make-point 1.5 2.5)))

_______________________________________________
Chicken-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to