Subject: [clisp-list] Changing variables in a C dynamic library

From: Nico <[email-masked]>
Date: Tue Feb 3 18:06:05 2004

hi,

I am trying to change variables in a C dynamic link library. Simplified 
it looks like this :

------------------------------C code-----------------------------------
struct bar {
    double x, y;
    double out;
};

struct bar my_struct = {10.0, 20.5, 0.0};

double test_dll2(struct bar *ptr)
{
   return ptr->out = ptr->out + ptr->x + ptr->y;
}
 --> compiles to libtest2.so.0 (or libtest2.dll, doesn't matter)
----------------------------- Lisp code----------------------------------
 (use-package "FFI")

(def-c-struct bar
  (x double-float)
  (y double-float)
  (out double-float))
 
(def-call-out get-own-c-float-1
   (:library "libtest2.so.0")
   (:language :stdc)
   (:name "test_dll2")
   (:arguments (ptr (c-ptr bar)))
   (:return-type double-float))

(def-c-var p_my_struct (:LIBRARY "libtest2.so.0") (:type (c-ptr bar)))

-----------------------------------------
If one evaluates (get-own-c-float-1 p_my_struct) multiple times from 
lisp then the result stays
30.5d0 and  does not increase as I expected.  Is this because of 'fresh' 
variables or what am I
missing ?

Thank you
Nico Swart.

---

From: Arseny Slobodjuck <[email-masked]>
Date: Wed Feb 4 03:10:24 2004

> I am trying to change variables in a C dynamic link library. Simplified 

Even more simplified:

----- C ------
struct xyptr {
    double x, y;
    double out;
};
extern EXTRN LISPDLL_API struct xyptr my_xystruct;
extern EXTRN LISPDLL_API struct xyptr * pmy_xystruct;
----- C ------

----- Lisp -----
(ffi:def-c-struct xyptr
  (x double-float)
  (y double-float)
  (out double-float))
(def-c-var my_xystruct (:LIBRARY "lispdll.dll") (:type xyptr))
----- Lisp -----

[2]> my_xystruct
#S(XYPTR :X 10.0d0 :Y 20.5d0 :OUT 0.0d0)
[3]> (setf (xyptr-x pmy_xystruct) 13)
13
[4]> my_xystruct
#S(XYPTR :X 10.0d0 :Y 20.5d0 :OUT 0.0d0)
[5]> (setf (xyptr-x pmy_xystruct) 13.0d0)
13.0d0
[6]> my_xystruct
#S(XYPTR :X 10.0d0 :Y 20.5d0 :OUT 0.0d0)
[7]> (xyptr-x pmy_xystruct)
10.0d0
[8]> (type-of my_xystruct)
XYPTR
[9]> ;(and not foreign-variable)

-- 
Best regards,
 Arseny

---

From: Arseny Slobodjuck <[email-masked]>
Date: Wed Feb 4 03:21:20 2004

[4]>> my_xystruct
> #S(XYPTR :X 10.0d0 :Y 20.5d0 :OUT 0.0d0)
[5]>> (setf (xyptr-x pmy_xystruct) 13.0d0)
oops

same result with my_xystruct instead of
pmy_xystruct.

-- 
Best regards,
 Arseny

---

From: Nico <[email-masked]>
Date: Wed Feb 4 18:56:01 2004

>same result with my_xystruct instead of
Thanks for the comments. What finally produced what I required was this 
though :

_____________________C code____________________________________
struct bar {
    double x, y;
    double out;
};

double test_dll2(struct bar *ptr)
{
   return ptr->out = ptr->out + ptr->x + ptr->y;
}
______________________Lisp______________________________________
(use-package "FFI")

(def-c-struct bar
  (x double-float)
  (y double-float)
  (out double-float))
 
(def-call-out get-own-c-float-1
   (:library "libtest2.so.0")
   (:language :stdc)
   (:name "test_dll2")
   (:arguments (ptr c-pointer :in :alloca))
   (:return-type double-float))

(def-c-var c-var-1 (:name "my_struct") (:LIBRARY "libtest2.so.0") (:type 
(c-ptr bar)))

[14]> (c-var-address c-var-1)
#<FOREIGN-ADDRESS #x282935D8>
[15]> (get-own-c-float-1 (c-var-address c-var-1))
30.5d0
[16]> (get-own-c-float-1 (c-var-address c-var-1))
61.0d0
[17]> (get-own-c-float-1 (c-var-address c-var-1))
91.5d0
[18]> (get-own-c-float-1 (c-var-address c-var-1))
122.0d0
[19]>

I wanted to change the actual C location from Lisp and also pass it as a 
pointer value to the C functions (the
c-pointer declaration did the trick).

Thanks again

Nico Swart