chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Re: Structs


From: Felix Winkelmann
Subject: Re: [Chicken-users] Re: Structs
Date: Tue, 15 Oct 2002 09:18:23 +0200
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.0) Gecko/20020530

Peter Keller wrote:
On Mon, Oct 14, 2002 at 10:38:40AM -0500, Peter Keller wrote:

On Mon, Oct 14, 2002 at 08:57:05AM +0200, Felix Winkelmann wrote:

In this case I would create a number of foreign-lambda*s
that access the slots:

So, I actually thought of this, but rejected it because I didn't
understand how gc would affect it. I thought that I could end up with a
way that I'd have a valid z_stream object with a member field pointing
to gc'ed data.


So, I tried this out:

(declare
        (foreign_declare #<< EOF
#include <zlib.h>)
EOF
))

(define-foreign-type z_stream (pointer "z_stream"))

(define make-z_stream
    (let (  (alloc (foreign-lambda* z_stream ()
                        "return (malloc(sizeof(z_stream)));"))
            (dealloc (foreign-lambda void "free" c-pointer)))
        (lambda ()
            (let ((m (alloc)))
                (set-finalizer! m dealloc)
                m))))

(define z_stream-next_in
   (foreign-lambda* c-pointer ((z_stream p))
     "return(p->next_in);") )

(define z_stream-next_in-set!
   (foreign-lambda* void ((z_stream p) (u8vector x))
     "p->next_in = x;") )

(let ((foo (make-z_stream)))
    (z_stream-next_in-set! foo (u8vector 1 2 3 4 5))
    (print (z_stream-next_in foo)))

And this was the output:
#<pointer -1073745280.>

I seem to have lost the knowledge that next_in was a u8vector.


Because the result-type of `z_stream-next_in' is
a c-pointer (number-vectors are not allowed as return-
type), but you pass an u8vector when storing the
value in the field, which gets converted into a char*.
Additionally, GC will invalidate the pointer (as you
correctly pointed out in your previous mail.

You could allocate temporary storage area (or `evict'
the data).


cheers,
felix





reply via email to

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