Access to memory managed by C module over JNI

S

Sune

Hi all,

I'm not a Java programmer so please be gentle:

Prereq:
---------------
- The lookup service (see below) implemented by a C module cannot be
re-written in Java ;-)
- The lookup service is to be loaded into the process of the Java
application, i.e. I want to avoid expensive IPC

1)
I got a C module that organizes a big block of memory into a fairly
nice lookup service. This C module exposes a C API that I guess I
would best access from a Java application by using JNI, right? Are
there any other choices than JNI?

2)
From my understanding, Java being a pointerless language, Java client
code cannot by any means, by mistake, access the memory block managed
by the C module. Is this correct? Does JNI in any way make this less
true? For example, are there mechanisms in JNI that uses physical
pointers for efficiency?

In C/C++, a rouge pointer can very easily damage the lookup service
and this would not be noticed until several days have passed. This is
not acceptable.

Thank you for your time
/Sune
 
M

Mike Baranczak

Sune said:
Hi all,

I'm not a Java programmer so please be gentle:

Prereq:
---------------
- The lookup service (see below) implemented by a C module cannot be
re-written in Java ;-)
- The lookup service is to be loaded into the process of the Java
application, i.e. I want to avoid expensive IPC

1)
I got a C module that organizes a big block of memory into a fairly
nice lookup service. This C module exposes a C API that I guess I
would best access from a Java application by using JNI, right? Are
there any other choices than JNI?

With those prerequisites, no, I don't think there is.


2)
code cannot by any means, by mistake, access the memory block managed
by the C module. Is this correct? Does JNI in any way make this less
true? For example, are there mechanisms in JNI that uses physical
pointers for efficiency?

In C/C++, a rouge pointer can very easily damage the lookup service
and this would not be noticed until several days have passed. This is
not acceptable.

An oversimplified view of how JNI works:

1. In some Java source file, you write a method stub with the "native"
modifier.
2. Run the javah tool, which creates a C header file containing a
function signature. If the native Java method has any arguments, those
will be passed to the C function, along with some data structures that
contain information about the Java runtime.
3. Implement this C function, creating a bridge between the JNI function
and whatever C library you want to use.
4. Your Java code can now call the native method - this call will be
forwarded to the C code you just wrote.

So there won't be any rogue pointers unless you introduce them in step 3.

Also keep in mind that a JNI call can be moderately expensive. Don't
just assume that it'll be faster than some IPC scheme.
 
S

Sune

With those prerequisites, no, I don't think there is.



An oversimplified view of how JNI works:

1. In some Java source file, you write a method stub with the "native"
modifier.
2. Run the javah tool, which creates a C header file containing a
function signature. If the native Java method has any arguments, those
will be passed to the C function, along with some data structures that
contain information about the Java runtime.
3. Implement this C function, creating a bridge between the JNI function
and whatever C library you want to use.
4. Your Java code can now call the native method - this call will be
forwarded to the C code you just wrote.

So there won't be any rogue pointers unless you introduce them in step 3.

Also keep in mind that a JNI call can be moderately expensive. Don't
just assume that it'll be faster than some IPC scheme.




--http://www.pocketgorilla.com- search engine for freelance programmers

Thanks Mike!

I knew JNI wasn't that efficient, but if you say it may be as bad as
IPC I get the chills...

Can Java in any way, without JNI, access write/read a shared memory
block created by a C module?

By shared memory block I mean what is acquired by shmem, shmat etc in
UNIX, LINUX...

BRs
/Sune
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Sune said:
Prereq:
---------------
- The lookup service (see below) implemented by a C module cannot be
re-written in Java ;-)
- The lookup service is to be loaded into the process of the Java
application, i.e. I want to avoid expensive IPC

You asked the same question in the C# group. Do you need the
solution in Java or C# or both ?
1)
I got a C module that organizes a big block of memory into a fairly
nice lookup service. This C module exposes a C API that I guess I
would best access from a Java application by using JNI, right? Are
there any other choices than JNI?

I believe JNI is the only solution.
2)
code cannot by any means, by mistake, access the memory block managed
by the C module. Is this correct? Does JNI in any way make this less
true? For example, are there mechanisms in JNI that uses physical
pointers for efficiency?

A Java reference is more or less a pointer.

But Java throws exceptions if you try and go over an arrays
boundaries.

So you can not overwrite memory by accident.

JNI code is C code and it can overwrite just any other C code.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Sune said:
Can Java in any way, without JNI, access write/read a shared memory
block created by a C module?

By shared memory block I mean what is acquired by shmem, shmat etc in
UNIX, LINUX...

No.

Java is not the right language for something as platform
specific / close to the OS like this.

Arne
 
R

Roedy Green

1)
I got a C module that organizes a big block of memory into a fairly
nice lookup service. This C module exposes a C API that I guess I
would best access from a Java application by using JNI, right? Are
there any other choices than JNI?
The other choices, probably not suited in your case are:
1. execing a program and communicating with stdin/stdout.
2. talking to another program via TCP/IP.
2)
code cannot by any means, by mistake, access the memory block managed
by the C module. Is this correct? Does JNI in any way make this less
true? For example, are there mechanisms in JNI that uses physical
pointers for efficiency?
Java has pointers. It calls them references. You can't do pointer
arithmetic in Java. However in C you can do what you please and glue
it to Java with JNI C calls. I suggest you read up on JNI to find out
what is possible. See http://mindprod.com/jgloss/jni.html
In C/C++, a rogue pointer can very easily damage the lookup service
and this would not be noticed until several days have passed. This is
not acceptable.
Java can't generate rogue pointers. This is perhaps the main reason
for coding in Java over C. Of course you can generate them in your C
JNI glue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top