Hi everybody,
I have a problem with CFFI where I would like your input as it is
probably concerned with dynamic extent behaviour in SBCL. (If not,
please correct me if my assumption is wrong).
I have my problem reduced to the following test. I have a simple C
function which is compiled to a dynamic lib. It is a wrapper to some C++
code:
extern "C" {
void tryit(const int *ints, unsigned count) {
for(unsigned i=0; i<count; i++)
{ cerr << "index " << i << ": " << ints[i] << "\n"; }
}
} /* extern "C" */
Now I have two variants of a function which allocates an integer array
and calls the C function via CFFI:
(defcfun ("tryit" try-it) :void
(vars :pointer)
(count :unsigned-int))
(defun test-1 ()
(let* ((l (list 1 2 3))
(idx (foreign-alloc :int :initial-contents l)))
(print (mem-ref idx :int 2))
(try-it idx (length l))
(foreign-free idx)))
This works as expected and prints
index 0: 1
index 1: 2
index 2: 3
to stderr.
However, the following function:
(defun test-2 ()
(let* ((l (list 1 2 3))
(len (length l))
(i 0))
(with-foreign-object (idx :int len)
(map nil (lambda (x)
(setf (mem-ref idx :int i) x)
(incf i))
l)
(print (mem-ref idx :int 2))
(try-it idx len))))
prints the following:
index 0: 197121
index 1: 0
index 2: 6548480
Where the number for 'index 2' toggles between two numbers for each
call; the numbers for the first two indices do not change.
Now, the only difference I can see, is that with-foreign-object tries to
allocate the buffer on the stack and not on the heap. At least this is
how I understand CFFI's manual.
I'm I doing something wrong or have a completely wrong understanding on
how this is supposed to work? I'm on SBCL 1.0.51, 64 bit.
Any input is greatly appreciated.
Best regards,
Stephan
|