Subject: Re: Passing Lisp objects to through the FLI?
Date: Monday 17th November 2008 14:09:25 UTC (over 8 years ago)
The static arrays will be GCed, but maybe not as often as for a non-static arrays. You could also do it with FLI arrays (I've not tested the performance): (defun fun-3 (array) (fli:with-dynamic-foreign-objects () (let* ((size (array-dimension array 0)) (f-array (fli:allocate-dynamic-foreign-object :type '(:unsigned :char) ; assumes array elts are (unsigned-byte 8) :nelems size))) (fli:replace-foreign-array f-array array :end1 size) (fli-function f-array size)))) __Martin >>>>> On Thu, 13 Nov 2008 14:25:49 +0800, Chun Tian (binghe) said: > > Hi, Martin > > I have a related question, please give me some advice: > > I wrote a function which accept a array as arguments then pass it into > FLI functions. The array arguments usually is not "allocated in static > area", so I have to copy elements into another static array then pass > the static array to FLI functions. > > I think out two way to do that: one is create the static array each > the function be called, the other is just create one static array > outside of the function on load-time, and always use it. Like > following sample: > > (defun fun-1 (array) > (let* ((size (array-dimension array 0)) > (s-array (make-array size :allocation :static))) > (replace s-array array) > (fli-function s-array size))) > > (defvar *buffer* (make-array +max-size+ :allocation :static)) > (defvar *lock* (mp:make-lock)) > > (defun fun-2 (array) > (let ((size (array-dimension array 0))) > (mp:with-lock (*lock*) > (replace *buffer* array) > (fli-function *buffer* size)))) > > Above code cannot run, just to indicate. In FUN-1, the S-ARRAY be > create each time, I don't know if the "static array" would be GCed > when program run out of the scope of FUN-1. In FUN-2, I think I have > to use a lock to protect the global *BUFFER* from thread-safe. > > For heavily called of FUN-1/FUN-2, my test shows FUN-2's performance > is much better then FUN-1 because no additional array allocated, so > the FUN-2 is my current choice. > > Is there any better solution from the view of you, a LW expert? > > On 2008-11-13, at 1:19, Martin Simmons wrote: > > > > >>>>>> On Tue, 11 Nov 2008 12:59:12 -0800, massung said: > >> > >> Is it possible to pass a Lisp object (not a foreign object or a > >> simple > >> literal ala fixnum/string) to a foreign function and be returned > >> later? I > >> assume the big concern would be the garbage collector moving (or even > >> releasing) the memory for the Lisp object, but perhaps #'in-static- > >> area can > >> help here? > >> > >> Basically, there are some functions I'd like to call that accept a > >> void* > >> for user data. It would be incredibly convenient if I could send a > >> specific > >> or class instance or struct through the user data and get it back > >> later via > >> another foreign function call. > >> > >> Is that doable somehow? Or, if someone knows a another method, I'm > >> all > >> ears. ;-) > > > > The only kinds of Lisp object that are safe to pass directly to C > > are static > > arrays (see http://www.lispworks.com/documentation/lw51/FLI/html/fli-130.htm > > and http://www.lispworks.com/documentation/lw51/FLI/html/fli-152.htm > > for ways > > to do that). > > > > For other objects, you need to pass a handle, e.g. an integer with a > > lookup > > table as suggested by Luke. > > > > -- > > Martin Simmons > > LispWorks Ltd > > http://www.lispworks.com/ > > > > -- > Chun Tian (binghe) > NetEase.com, Inc. > P. R. China > > > > > -- Martin Simmons LispWorks Ltd http://www.lispworks.com/