[Top][All Lists]

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

[Chicken-users] Making stack-allocated Chicken-managed objects from C

From: Kristian Lein-Mathisen
Subject: [Chicken-users] Making stack-allocated Chicken-managed objects from C
Date: Tue, 6 Mar 2012 17:31:42 +0100

Hi guys!

I would like to get a deeper understanding of Chicken's GC and its stack-allocation feature. I've numbered by questions, feel free to answer just one or two of them!

I have been poking into the chicken-bind egg and I'd like to modify it slightly for my libraries' (Box2D, Chipmunk) heavy use of structs.

Chicken-bind will generate helper utils for making C-structs inside Chicken. Please take a peek at

My C-snipped in foreign-primitive that's doing the stack-allocation is heavily inspired by However, I've been getting feedback about this approach about it not being completely safe because it can't guarantee the stack won't overflow. From what I can make out of the docs, foreign-primitive automatically does a minor GC and we should be safe.

1. Is this method completely safe? If not, why is that the case? If not, how can you create blobs on the stack from C safely?

2. So let's say that we drop C_alloc for being unsafe. Could C_alloc be replaced with C_malloc and at least we'd get the struct/blob automatically garbage-collected by Chicken? Or are scheme-objects reserved for stack-allocated pointers?

3. When C-code returns a new scheme-object, does Chicken treat it like any other scheme-object created from within Chicken? (Along with GC and all the rest of it)

4. And just for clarity, is there any difference between

C_bytes *ab = C_alloc( .. size ..) ;     vs.
C_bytes ab [ .. size ... ];              ?

5. And finally, how fast is stack-allocation/C_alloc compared to heap-allocation/C_malloc? (I'm interested in very small structs here!)

And for reference, an easier approach probably goes something like this, where the blob is created in Chicken-land:
(define %make-point (foreign-primitive void
                                       (((c-pointer (struct point)) dest) (float x) (float y))
dest->x = x;
dest->y = y;

(define (make-point x y) 
        (let ((loc (location (make-blob %get struct-size somehow%))))
           (%make-point loc x y)

This is what I'm looking for, but I'd love to see make-blob lowered into C so that there is less overhead in creating structs (I'm a performance junkie).  I've looked at runtime.c's C_allocate_vector and I must admit don't understand much!


reply via email to

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