<div dir="ltr"><div><div><div>Hi Joeish,<br><br></div>have a look at trivial-garbage (<a href="http://common-lisp.net/project/trivial-garbage/">http://common-lisp.net/project/trivial-garbage/</a>). This project wraps the finalizers of different Lisp implementations with a common interface.<br>
<br></div>So in your case it is best to wrap the pointer in a Lisp struct and attach that the finalizer like this (not tested, so syntax errors may apply). You can remove the type annotation of the struct slot if you don't want any SBCL-specific code.<br>
<br></div><pre><div class="" id="LC60"><span class="">(</span><span class="">defstruct</span> <span class="">(</span>cvmatrix <span class="">(</span><span class="">:constructor</span> <span class="">%make-cvmatrix</span><span class="">))</span></div>
<span class="">(</span><span class="">sap</span> <span class="">(</span><span class="">mat</span><span class="">)</span> <span class="">:type</span> <span class="">sb-sys:system-area-pointer</span> <span class="">:read-only</span> <span class="">t</span><span class="">))</span><div class="" id="LC64">
<br><pre><div class="" id="LC70"></div><div class="" id="LC71"><span class="">(</span><span class="">defun</span> <span class="">make-cvmatrix</span> <span class="">()</span></div><div class="" id="LC72"> <span class="">(</span><span class="">let*</span> <span class="">((</span><span class="">matrix</span> <span class="">(</span><span class="">%make-cvmatrix</span><span class="">))</span></div>
<div class="" id="LC73"> <span class="">(</span><span class="">sap</span> <span class="">(</span><span class="">cvmatrix-sap</span> <span class="">matrix</span><span class="">)))</span></div><div class="" id="LC74">
<span class="">(</span><span class="">tg:finalize</span> <span class="">matrix</span> <span class="">(lambda (x)</span><span class=""></span> <br>(del-mat <span class="">sap</span><span class="">)))</span></div><div class="" id="LC75">
<span class="">matrix</span><span class="">))</span></div></pre><br></div><div class="" id="LC64">Now you create matrices with make-cvmatrix which returns you a cvmatrix struct. Whenever this returned struct is garbage collected the finalizer is called.<br>
If you prefer classes to structs, of course a similar approach with make-sintance and after methods can be easily implemented.<br><br></div><div class="" id="LC64">Regs,<br></div><div class="" id="LC64">Stephan<br><br></div>
<div class="" id="LC64"><span class=""></span><span class=""></span><span class=""></span></div></pre></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-04-10 5:41 GMT+02:00 Joeish W <span dir="ltr"><<a href="mailto:joeish80829@yahoo.com" target="_blank">joeish80829@yahoo.com</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="font-size:10pt;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif"><div>
I have Lisp functions that wrap C wrappers for C++ functions that contain a new operator. The C wrappers have to stay the same I can't change those but I was advised to update my method of GC.</div><div><br></div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif">
Here are the C wrappers:</div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif"><br></div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif">
Mat* cv_create_Mat() {<br> return new Mat();<br>}<br><br>void cv_delete_Mat(void* ptr) {<br> delete (~Mat*)ptr;<br>}</div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif">
<br></div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif">Here are the Lisp wrappers for them:</div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif">
<br></div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif">(defcfun ("cv_create_Mat" mat) (:pointer mat)
<br> "MAT constructor")
<br></div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif"><br>(defcfun ("cv_delete_Mat" del-mat) :void
<br> (ptr :pointer)) </div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif"><br></div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif">
In a loop, as below, I would normally after calling a MAT function, delete it when I'm done with it manually with DEL-MAT, but I heard there was a better way by actually entering in the DEL-MAT function into Lisps natural GC methods so it would know when it comes across a function with a new
it should call delete to get rid of it when it know i'm done with it.. The thing is I could use help to get started. Hopefully a concrete example of how to do this so I don't have to call DEL-MAT manually</div>
<div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:HelveticaNeue,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif"><br>(dotimes (n 1000)<br>(setf m (mat))<br>(del-mat m))</div>
</div></div><br>_______________________________________________<br>
Cffi-devel mailing list<br>
<a href="mailto:Cffi-devel@common-lisp.net">Cffi-devel@common-lisp.net</a><br>
<a href="http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel">http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel</a><br>
<br></blockquote></div><br></div>