chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

## Re: [Chicken-users] Making stack-allocated Chicken-managed objects from

 From: Jim Ursetto Subject: Re: [Chicken-users] Making stack-allocated Chicken-managed objects from C Date: Tue, 6 Mar 2012 12:33:17 -0600

 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)) #<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))#<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)))

reply via email to

 [Prev in Thread] Current Thread [Next in Thread]