Welcome to the CHICKEN Scheme pasting service

making stack-allocatied chicken-managed objects from C added by klm` on Tue Mar 6 16:52:08 2012


;; chicken-bind generates helpers when it encounters structs.
;; the generated helper to create a new struct look like this:

(define make-point
    (foreign-lambda*
      (c-pointer (struct "point"))
      ((float x) (float y))
      "struct point *tmp_ = (struct point *)C_malloc(sizeof(struct point));\ntmp_->x = x;\ntmp_->y = y;\nC_return(tmp_);")))

;; This is not ideal, since you have to remember to free that struct-pointer later. 
;; I would like the new struct to be a scheme-object automatically GC'ed
;; by chicken instead. I also want to take advantage of Chicken's fast
;; memory-allocation by using the stack.
;; Here is my suggestion:



;; <------------
;; returns a blob:
(define %make-point (foreign-primitive scheme-object ((float x) (float y))
#<<END
C_bytes *ab = (C_bytes*)C_alloc(sizeof(C_header) + sizeof(struct point))];
struct point *_struct = (struct point*) C_data_pointer (ab);

_struct->x = x;
_struct->y = y;
*((C_header*)ab) = C_BYTEVECTOR_TYPE | sizeof (struct point);

C_return (ab);
END
))

;; returns a locative
(define (make-point x y) (location (%make-point x y)))
;; --------------->

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Which R5RS procedure can be used to concatenate strings?
Visually impaired? Let me spell it for you (wav file) download WAV