Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static access to JNI GetDirectBufferAddress #629

Merged
merged 3 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;
import org.bytedeco.javacpp.annotation.Name;
import org.bytedeco.javacpp.annotation.Raw;
import org.bytedeco.javacpp.tools.Generator;
import org.bytedeco.javacpp.tools.Logger;

Expand Down Expand Up @@ -589,6 +590,10 @@ public static long maxPhysicalBytes() {
/** Returns the amount of physical memory that is free according to the operating system, or 0 if unknown. */
@Name("JavaCPP_availablePhysicalBytes") public static native long availablePhysicalBytes();

/** Returns the starting address of the memory region referenced by the given direct {@link java.nio.Buffer}.
* An alternative to allocating a new Pointer object if all you need is the memory address. */
@Name("JavaCPP_getDirectBufferAddress") public static native long getDirectBufferAddress(@Raw(withEnv = true) Buffer b);
saudet marked this conversation as resolved.
Show resolved Hide resolved

/** The native address of this Pointer, which can be an array. */
protected long address = 0;
/** The index of the element of a native array that should be accessed. */
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println(" env->DeleteGlobalRef(globalRef);");
out.println("}");
out.println();
out.println("static inline jlong JavaCPP_getDirectBufferAddress(JNIEnv *env, jclass cls, jobject obj) {");
out.println(" return (jlong)env->GetDirectBufferAddress(obj);");
out.println("}");
out.println();
ds58 marked this conversation as resolved.
Show resolved Hide resolved
}
out.println("static JavaCPP_noinline jclass JavaCPP_getClass(JNIEnv* env, int i) {");
out.println(" if (JavaCPP_classes[i] == NULL && env->PushLocalFrame(1) == 0) {");
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/bytedeco/javacpp/BufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public ByteBuffer call(ByteBuffer buffer) {
BytePointer pointer = new BytePointer(arrayBuffer);
ByteBuffer directBuffer = pointer.asBuffer();
assertEquals(pointer.address(), new BytePointer(directBuffer).address());
assertEquals(pointer.address(), Pointer.getDirectBufferAddress(directBuffer));

assertTrue(directBuffer.compareTo(arrayBuffer) == 0);
assertEquals(arrayBuffer.position(), directBuffer.position());
Expand All @@ -123,6 +124,7 @@ public ByteBuffer call(ByteBuffer buffer) {
ShortPointer pointer = new ShortPointer(arrayBuffer);
ShortBuffer directBuffer = pointer.asBuffer();
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), new ShortPointer(directBuffer).address());
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), Pointer.getDirectBufferAddress(directBuffer));

assertTrue(directBuffer.compareTo(arrayBuffer) == 0);
assertEquals(arrayBuffer.limit() - arrayBuffer.position(), directBuffer.limit());
Expand All @@ -144,6 +146,7 @@ public ByteBuffer call(ByteBuffer buffer) {
IntPointer pointer = new IntPointer(arrayBuffer);
IntBuffer directBuffer = pointer.asBuffer();
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), new IntPointer(directBuffer).address());
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), Pointer.getDirectBufferAddress(directBuffer));

assertTrue(directBuffer.compareTo(arrayBuffer) == 0);
assertEquals(arrayBuffer.limit() - arrayBuffer.position(), directBuffer.limit());
Expand All @@ -165,6 +168,7 @@ public ByteBuffer call(ByteBuffer buffer) {
LongPointer pointer = new LongPointer(arrayBuffer);
LongBuffer directBuffer = pointer.asBuffer();
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), new LongPointer(directBuffer).address());
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), Pointer.getDirectBufferAddress(directBuffer));

assertTrue(directBuffer.compareTo(arrayBuffer) == 0);
assertEquals(arrayBuffer.limit() - arrayBuffer.position(), directBuffer.limit());
Expand All @@ -186,6 +190,7 @@ public ByteBuffer call(ByteBuffer buffer) {
FloatPointer pointer = new FloatPointer(arrayBuffer);
FloatBuffer directBuffer = pointer.asBuffer();
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), new FloatPointer(directBuffer).address());
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), Pointer.getDirectBufferAddress(directBuffer));

assertTrue(directBuffer.compareTo(arrayBuffer) == 0);
assertEquals(arrayBuffer.limit() - arrayBuffer.position(), directBuffer.limit());
Expand All @@ -207,6 +212,7 @@ public ByteBuffer call(ByteBuffer buffer) {
DoublePointer pointer = new DoublePointer(arrayBuffer);
DoubleBuffer directBuffer = pointer.asBuffer();
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), new DoublePointer(directBuffer).address());
assertEquals(pointer.address() + pointer.position() * pointer.sizeof(), Pointer.getDirectBufferAddress(directBuffer));

assertTrue(directBuffer.compareTo(arrayBuffer) == 0);
assertEquals(arrayBuffer.limit() - arrayBuffer.position(), directBuffer.limit());
Expand Down