[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Best way to share memory between C and Chicken
From: |
Thomas Chust |
Subject: |
Re: [Chicken-users] Best way to share memory between C and Chicken |
Date: |
Fri, 03 May 2013 20:24:31 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 |
On 2013-05-03 20:04, Pedro Melendez wrote:
> [...]
> I am developing a prototype of a server that would serve 3D seismic
> images across the network.
> [...]
Hello Pedro,
it's nice to see I'm not the only geophysicist who likes to use Scheme :-)
> [...]
> Giving the size of the file, I want to share the memory space between C
> and Chicken and avoid copying values between areas. Is that even
> possible?
> [...]
Both CHICKEN's blobs and SRFI-4 homogeneous vectors can be converted to
native pointers to their data contents through the FFI.
As a silly example, this will allocate a big array of floating point
numbers, set one of the array elements using native code and read it
bacl from Scheme:
(use srfi-4)
(define big-array
(make-f64vector (expt 2 20)))
(define set-some-element!
(foreign-lambda* void ((nonnull-f64vector v))
"v[1024] = 42.23;"))
(set-some-element! big-array)
(display (f64vector-ref big-array 1024))
(newline)
If your data is not a homogeneous numeric array, you could use blobs
instead.
If you absolutely have to allocate unmanaged memory on the C side an
pass pointers there back to Scheme rather than creating managed Scheme
objects and passing pointers to their contents to the C code, take a
look at CHICKEN's lolevel unit. CHICKEN Scheme can also deal with raw
native pointers to a certain degree!
Ciao,
Thomas
--
When C++ is your hammer, every problem looks like your thumb.