Creating a C/C++ library using Java

Jeff Sturm jsturm@one-point.com
Mon Aug 5 10:45:00 GMT 2002


On Mon, 5 Aug 2002, Juan Altmayer Pizzorno wrote:
> has anybody thought about creating a C/C++ library using Java and gcj?

It won't work in general.
C/C++ interfaces to Java code via JNI. G++ can also use CNI to interface
to gcj-compiled code.
At the function call level, those are the only interfaces you can use.
Higher-level mechanisms like CORBA are possible too.
> I haven't looked into the internals closely yet, but it seems to me that at
> least the garbage collector would make it difficult (right?). I am not
> sure whether there would be actual conflicts, or whether the library would
> just behave in an unexpected way (e.g., seem to initiallly leak memory).

Well, the GC needs to know about Java pointers from native code. With
CNI, the GC scans all thread stacks and static data areas. With JNI,
pointers must be registered explicitly.
> Replacing the GC with a reference-counting (pseudo-)GC for that seems difficult
> since object references are mapped to C++ pointers and some kind of wrapping
> to count the references up and down would be necessary. Also, I imagine the
> JVM initialization (System.* object(s), threading package, etc.) might cause
> conflicts with the application linking with the (so created) library.

Reference counting is just another form of GC. However RC is difficult to
use with Java for several reasons.
Library conflicts are a possibility. The threading libraries need to be
compatible, for instance. Any DSO you create with gcj will depend on
libgcj.so, which will bring in its own GC and thread libraries (pthreads
on GNU/Linux).
Jeff


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /