Skip to main content

Adding native C Code to Squawk

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
3 replies [Last post]
squawker
Offline
Joined: 2011-07-26

Hello,
 
I am currently trying to add functionality, that is for performance reasons coded in C, to Squawk.
As far as I understood there are currently two ways of enhancing Squawk with C code.
 
1.there is the way of JNA, where libraries can be loaded, and are executed with a TaskExecutor (the native code is run within a native thread)
2.The C code from the VM base, where native method identifiers (defined in Native.java) are mapped to C functions (mostly basic VM functions)
I am looking for a way to keep my C code modular and separated from the basic Squawk code (keep it out of bytecodes.c).
 
My idea was to link the C code statically to the Squawk binary, and now I am looking for a way, how to call the C functions from Java/ inside Squawk.
Am I overlooking something with this approach?
Are there other successful approaches with the same goal?
Is somewhere a (basic) example available?
I dug through the source code, and googled quite a bit but wasn't able come up with anything.
 
any help is much appreciated
 
Squawker

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
derek_white
Offline
Joined: 2006-09-08

[My earlier reply seems to have vanished, so here's a shorted version:]

The best bet would be to use JNA. There are basic examples in the javadoc (see http://www.sunspotworld.com/javadoc/com/sun/cldc/jna/package-summary.html).

You can also look at the code in com.sun.squawk.platfrom.windows and com.sun.squawk.platfrom.window.natives. The posix version is more functional, but the factoring makes it harder to see the overall picture.

derek_white
Offline
Joined: 2006-09-08

JNA would be the best approach for this. Some basic examples are in the javadoc:

http://www.sunspotworld.com/javadoc/com/sun/cldc/jna/package-summary.html

The basic steps are:

  • Compile and link your C code as a shared library
  • Create a Java wrapper class for that library
  • Look up the shared library at runtime using the NativeLibrary.findInstance()
  • Create Function pointers to the C functions
  • Create methods that call the C functions using the Function pointer.
  • Create structures and constants as needed.

There are more complicated examples in the CLDC code. Look at com.sun.squawk.platform.windows and com.sun.squawk.platform.windows.natives. The posix code is harder to follow due to the factoring, but is a complete implementation.

mortens
Offline
Joined: 2011-08-08

derek_white wrote:

JNA would be the best approach for this. Some basic examples are in the javadoc:

[my reply yesterday appears to be lost, too... hmm... trying again...]

True that JNA is probably the easiest way, but for deployment complexity one does not want to bundle an additional .so and care about potential platform dynamic linker issues.

So, to make things "just work" one links the C code statically to the Squawk binary. Easy. Then Java needs some wrapper method definitions to refer to, ok. But, intuitively, at some point when jumping to a new method the VM must decide whether it lives in Java land or whether this is a C native function.

So, how to mark methods to be a C native implementation?
How to map Java method names to C native names?
Where does one put the translation table from method names to C function
pointers?
Of course, the C code has to unwrap passed arguments as they are
stored how the VM likes them.

Can anyone point to a simple example of calling a statically linked portion of C code, passing in and out some data - or where does this magic happen in Squawk?

Thanks!