How do I write wrapper for foreign-alloc, that frees it....

Discussion of Common Lisp

How do I write wrapper for foreign-alloc, that frees it....

Postby joeish80829 » Wed Jul 23, 2014 3:46 am

automatically when it goes out of scope(or the functions that can still access the data created by foreign-alloc go out of scope).

I'm trying to write a loose wrapper for cffi:foreign-alloc that finalizes(calls cffi:foreign-free on it) it automatically. For instance if I have a defcfun named "test" that expects a pointer to some data, I would run this to use it:

(test (foreign-alloc :int :initial-contents '(1 2 3)))


I would rather make it so I just have to run:

(test '(1 2 3))

but I would have to free foreign-alloc with foreign-free inside whatever defun wrapper I make for the decfun "test" and If I do so all the data is lost when I go to access the object created by (test '(1 2 3)).
I have been contemplating this for a while, can someone write me up a quick example of how to accomplish this.
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do I write wrapper for foreign-alloc, that frees it.

Postby logxor » Fri Jul 25, 2014 8:59 pm

I'm not familiar with CFFI, but doesn't DEFCFUN do the freeing automatically with Foreign Type Translators?
logxor
 
Posts: 20
Joined: Tue May 27, 2014 10:56 am
Location: Portland, OR

Re: How do I write wrapper for foreign-alloc, that frees it.

Postby joeish80829 » Sat Jul 26, 2014 5:21 am

well i noticed even creating a million of them(foreign-alloc) doesnt make my ram go up, and when I am done using the matrix eg it is deleted(my matrices are created with a new operator) the data is still in the pointer created by foreign-alloc, I was hoping to free that data automatically when the matrix goes out of scope. CFFI doc. says you should always free it is the reason.
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do I write wrapper for foreign-alloc, that frees it.

Postby logxor » Sat Jul 26, 2014 12:12 pm

I'm not yet sure what you mean by "scope"—if you mean dynamic extent, then it looks like CFFI can do stack allocation via the WITH-FOREIGN-OBJECT macro. If you mean finalization—that is, garbage-collecting data when it's not reachable from the Lisp—then that's apparently outside the domain of CFFI, and into implementation-specific territory, or some other kind of library. Clozure CL, for example, has a facility called "termination" which can be used to free a foreign vector when the garbage collector goes into effect. For this to work, it's necessary to wrap the foreign object within a Lisp object, so that it can participate in the termination protocol. The foreign allocation system in Allegro CL has a :FOREIGN-STATIC-GC option that appears to do something similar with a foreign-array thing that tracks the actual memory. Then there's this package called Trivial Garbage, which I haven't used, but it's supposed to provide a "portable API to finalizers" for the various implementations.
logxor
 
Posts: 20
Joined: Tue May 27, 2014 10:56 am
Location: Portland, OR

Re: How do I write wrapper for foreign-alloc, that frees it.

Postby Goheeca » Sun Jul 27, 2014 6:01 am

I'd firstly ask why you don't want to use with-foreign-objects?
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 220
Joined: Thu May 10, 2012 12:54 pm


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 2 guests