Skip to content

Commit

Permalink
* Add static long Pointer.getDirectBufferAddress(Buffer) method fo…
Browse files Browse the repository at this point in the history
…r convenience (pull #629)
  • Loading branch information
ds58 committed Oct 27, 2022
1 parent 45a6a4b commit 7fc94e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
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") protected static native long getDirectBufferAddress(@Raw(withEnv = true) Buffer b);

/** 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 @@ -664,6 +664,10 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println(" return size;");
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();
out.println("static inline jint JavaCPP_totalProcessors() {");
out.println(" jint total = 0;");
out.println("#ifdef __linux__");
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

0 comments on commit 7fc94e3

Please sign in to comment.