[Ecls-list] how to cache a cl_object in C?

Juan Jose Garcia-Ripoll juanjose.garciaripoll at googlemail.com
Sat Mar 27 09:47:45 UTC 2010


On Sat, Mar 27, 2010 at 8:34 AM, yami <yamisoe at gmail.com> wrote:

> What will happen if some_object is a data member of a 'new'ed C++ class
> instance? Another question, code likes following:
>   for (i=0; i<n; i++) {
>     cl_object object_from_ecl = si_safe_eval(...);
>   }
>
> if object_from_ecl is cached in C, how ECL knows when to gc this object?
>

It has no way to find it out by itself and it will normally assume the
object is lost. This is in general not a good practice, because you are
coupling things which are allocated by different beasts: C++'s memory
manager or ECL's.

Solutions:

1) Allocate the classes using the Boehm-Weiser garbage collector. That
implies using a newly defined "new" and "delete" operator pairs.

2) If there are only a few classes, register the location of the pointers
with ECL using
  extern ECL_API void ecl_register_root(cl_object *p);

3) Even better, keep the objects in a lisp array and keep the array in a C++
global variable, and register this one using ecl_register_root(). Your
classes can then keep pointers or indices into the array data.

4) Keep the objects in an array but store the array in a lisp global
variable, with a given symbol. The symbol will not disappear if it is in a
package so you can keep a pointer to the symbol

There are many more which may suit one or another problem better.

-- 
Instituto de Física Fundamental, CSIC
c/ Serrano, 113b, Madrid 28006 (Spain)
http://tream.dreamhosters.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/ecl-devel/attachments/20100327/925cdcc4/attachment.html>


More information about the ecl-devel mailing list