[Openmcl-devel] best practice for C++ class access from CCL?

Jason E. Aten j.e.aten at gmail.com
Fri Mar 25 13:22:16 PDT 2011


...or how to get Lisp size of class instance, and override the GC memory
allocator?...

Details:  I'm working interfacing CCL code to a large pre-established code
source in C++.  Is there a standard means to do this, or can somebody
recommend an approach that works?

Here's the backgorund, and a description of where I'm stuck right now.  I
really like Clozure's h-to-ffi.sh and CDB database mechanism, but (please
correct me if I'm wrong), this only applies to C code. I could not find
switches to get it to also handle C++.

So next I've been trying SWIG, which uses CFFI and automatically generates
extern "C" wrappers for C++ classes. Then I can link the SWIG wrapper C
functions and the original C++ code into a shared object and dynamically
loaded with (open-shared-library <file> ).

In addition, SWIG seems to automatically generate a .lisp file with defclass
declarations. i.e. this declaration in a testlib.h file

class CDemo {
 public:
  double b;
};

when run with "swig -c++ -cffi" produces this declaration in
testlib-clos.lisp, using swig 2.0.2.  CCL

(cl:defclass cdemo()
  ((ff-pointer :reader ff-pointer)))

Now to where I'm stuck, and probably because I'm new to CLOS.  I read
section 12.11 of the CCL manual, and realize that I need to use
(make-heap-ivector) to allocate these CLOS bridge objects on.

But how do I figure out the size of a class instance, so I know how many
bytes to ask make-heap-ivector for?  And then, where would I override the
default allocation mechanism to use make-heap-ivector instead?

Of course, if there are better approaches, I'm totally open to hearing them.

Thank you.

Jason

 Clozure Common Lisp Version 1.7-dev-r14684M-trunk  (Linuxx8664)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20110325/9bfcfd0a/attachment.htm>


More information about the Openmcl-devel mailing list