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

WIP: Use sun.misc.Unsafe in pointers and not in indexers #408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/BoolPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public BoolPointer(long size) {
public BoolPointer() { }
/** @see Pointer#Pointer(Pointer) */
public BoolPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public BoolPointer position(long position) {
Expand All @@ -90,7 +89,7 @@ public BoolPointer() { }
public boolean get() { return get(0); }
/** @return the i-th {@code bool} value of a native array
* @param i*/
@Cast("bool") public native boolean get(long i);
@Cast("bool") public boolean get(long i) { return Raw.getInstance().getBool(address + i * sizeof()); }
/** @return {@code put(0, b)} */
public BoolPointer put(boolean b) { return put(0, b); }
/**
Expand All @@ -100,5 +99,8 @@ public BoolPointer() { }
* @param b the {@code bool} value to copy
* @return this
*/
public native BoolPointer put(long i, boolean b);
public BoolPointer put(long i, boolean b) {
Raw.getInstance().putBool(address + i * sizeof(), b);
return this;
}
}
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/BooleanPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public BooleanPointer(long size) {
public BooleanPointer() { }
/** @see Pointer#Pointer(Pointer) */
public BooleanPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public BooleanPointer position(long position) {
Expand All @@ -119,7 +118,7 @@ public BooleanPointer() { }
/** @return {@code get(0)} */
public boolean get() { return get(0); }
/** @return the i-th {@code boolean} value of a native array */
public native boolean get(long i);
public boolean get(long i) { return Raw.getInstance().getByte(address + i * sizeof()) != 0; }
/** @return {@code put(0, b)} */
public BooleanPointer put(boolean b) { return put(0, b); }
/**
Expand All @@ -129,7 +128,10 @@ public BooleanPointer() { }
* @param b the {@code boolean} value to copy
* @return this
*/
public native BooleanPointer put(long i, boolean b);
public BooleanPointer put(long i, boolean b) {
Raw.getInstance().putByte(address + i * sizeof(), (byte) (b ? 1 : 0));
return this;
}

/** @return {@code get(array, 0, array.length)} */
public BooleanPointer get(boolean[] array) { return get(array, 0, array.length); }
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/BytePointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public BytePointer(long size) {
public BytePointer() { }
/** @see Pointer#Pointer(Pointer) */
public BytePointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public BytePointer position(long position) {
Expand Down Expand Up @@ -248,7 +247,7 @@ public BytePointer putString(String s) {
/** @return {@code get(0)} */
public byte get() { return get(0); }
/** @return the i-th {@code byte} value of a native array */
public native byte get(long i);
public byte get(long i) { return Raw.getInstance().getByte(address + i * sizeof()); }
/** @return {@code put(0, b)} */
public BytePointer put(byte b) { return put(0, b); }
/**
Expand All @@ -258,7 +257,10 @@ public BytePointer putString(String s) {
* @param b the {@code byte} value to copy
* @return this
*/
public native BytePointer put(long i, byte b);
public BytePointer put(long i, byte b) {
Raw.getInstance().putByte(address + i * sizeof(), b);
return this;
}

/** @return {@code get(array, 0, array.length)} */
public BytePointer get(byte[] array) { return get(array, 0, array.length); }
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/CLongPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public CLongPointer(long size) {
public CLongPointer() { }
/** @see Pointer#Pointer(Pointer) */
public CLongPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public CLongPointer position(long position) {
Expand All @@ -100,7 +99,7 @@ public CLongPointer() { }
/** @return {@code get(0)} */
public long get() { return get(0); }
/** @return the i-th {@code long} value of a native array */
@Cast("long") public native long get(long i);
@Cast("long") public long get(long i) { return Raw.getInstance().getCLong(address + i * sizeof()); }
/** @return {@code put(0, l)} */
public CLongPointer put(long l) { return put(0, l); }
/**
Expand All @@ -110,7 +109,10 @@ public CLongPointer() { }
* @param l the {@code long} value to copy
* @return this
*/
public native CLongPointer put(long i, long l);
public CLongPointer put(long i, long l) {
Raw.getInstance().putCLong(address + i * sizeof(), l);
return this;
}

/** @return {@code get(array, 0, array.length)} */
public CLongPointer get(long[] array) { return get(array, 0, array.length); }
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/CharPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public CharPointer(long size) {
public CharPointer() { }
/** @see Pointer#Pointer(Pointer) */
public CharPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public CharPointer position(long position) {
Expand Down Expand Up @@ -166,7 +165,7 @@ public CharPointer putString(String s) {
/** @return {@code get(0)} */
public char get() { return get(0); }
/** @return the i-th {@code char} value of a native array */
public native char get(long i);
public char get(long i) { return Raw.getInstance().getChar(address + i * sizeof()); }
/** @return {@code put(0, c)} */
public CharPointer put(char c) { return put(0, c); }
/**
Expand All @@ -176,7 +175,10 @@ public CharPointer putString(String s) {
* @param c the {@code char} value to copy
* @return this
*/
public native CharPointer put(long i, char c);
public CharPointer put(long i, char c) {
Raw.getInstance().putChar(address + i * sizeof(), c);
return this;
}

/** @return {@code get(array, 0, array.length)} */
public CharPointer get(char[] array) { return get(array, 0, array.length); }
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/DoublePointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public DoublePointer(long size) {
public DoublePointer() { }
/** @see Pointer#Pointer(Pointer) */
public DoublePointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public DoublePointer position(long position) {
Expand All @@ -115,7 +114,7 @@ public DoublePointer() { }
/** @return {@code get(0)} */
public double get() { return get(0); }
/** @return the i-th {@code double} value of a native array */
public native double get(long i);
public double get(long i) { return Raw.getInstance().getDouble(address + i * sizeof()); }
/** @return {@code put(0, d)} */
public DoublePointer put(double d) { return put(0, d); }
/**
Expand All @@ -125,7 +124,10 @@ public DoublePointer() { }
* @param d the {@code double} value to copy
* @return this
*/
public native DoublePointer put(long i, double d);
public DoublePointer put(long i, double d) {
Raw.getInstance().putDouble(address + i * sizeof(), d);
return this;
}

/** @return {@code get(array, 0, array.length)} */
public DoublePointer get(double[] array) { return get(array, 0, array.length); }
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/FloatPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public FloatPointer(long size) {
public FloatPointer() { }
/** @see Pointer#Pointer(Pointer) */
public FloatPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public FloatPointer position(long position) {
Expand All @@ -115,7 +114,7 @@ public FloatPointer() { }
/** @return {@code get(0)} */
public float get() { return get(0); }
/** @return the i-th {@code float} value of a native array */
public native float get(long i);
public float get(long i) { return Raw.getInstance().getFloat(address + i * sizeof()); }
/** @return {@code put(0, f)} */
public FloatPointer put(float f) { return put(0, f); }
/**
Expand All @@ -125,7 +124,10 @@ public FloatPointer() { }
* @param f the {@code float} value to copy
* @return this
*/
public native FloatPointer put(long i, float f);
public FloatPointer put(long i, float f) {
Raw.getInstance().putFloat(address + i * sizeof(), f);
return this;
}

/** @return {@code get(array, 0, array.length)} */
public FloatPointer get(float[] array) { return get(array, 0, array.length); }
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/IntPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public IntPointer(long size) {
public IntPointer() { }
/** @see Pointer#Pointer(Pointer) */
public IntPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public IntPointer position(long position) {
Expand Down Expand Up @@ -170,7 +169,7 @@ public IntPointer putString(String s) {
/** @return {@code get(0)} */
public int get() { return get(0); }
/** @return the i-th {@code int} value of a native array */
public native int get(long i);
public int get(long i) { return Raw.getInstance().getInt(address + i * sizeof()); }
/** @return {@code put(0, j)} */
public IntPointer put(int j) { return put(0, j); }
/**
Expand All @@ -180,7 +179,10 @@ public IntPointer putString(String s) {
* @param j the {@code int} value to copy
* @return this
*/
public native IntPointer put(long i, int j);
public IntPointer put(long i, int j) {
Raw.getInstance().putInt(address + i * sizeof(), j);
return this;
}

/** @return {@code get(array, 0, array.length)} */
public IntPointer get(int[] array) { return get(array, 0, array.length); }
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/bytedeco/javacpp/LongPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public LongPointer(long size) {
public LongPointer() { }
/** @see Pointer#Pointer(Pointer) */
public LongPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public LongPointer position(long position) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public Pointer(final Buffer b) {
}
private native void allocate(Buffer b);

protected void allocateArray(long size) {
this.address = Raw.getInstance().allocateMemory(size * sizeof());
}

/**
* Called by native libraries to initialize the object fields.
*
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/bytedeco/javacpp/PointerPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ public PointerPointer(long size) {
public PointerPointer() { }
/** @see Pointer#Pointer(Pointer) */
public PointerPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** This is just to keep references to Pointer objects and prevent premature deallocation. */
private P[] pointerArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bytedeco.javacpp.indexer;
package org.bytedeco.javacpp;

/**
* The raw memory interface supporting long indexing.
Expand All @@ -41,6 +41,9 @@ static Raw getInstance() {
return INSTANCE;
}

abstract long allocateMemory(long capacity);
abstract void freeMemory(long address);

abstract byte getByte(long address);
abstract void putByte(long address, byte b);
abstract short getShort(long address);
Expand All @@ -57,6 +60,12 @@ static Raw getInstance() {
abstract void putChar(long address, char c);
abstract boolean getBoolean(long address);
abstract void putBoolean(long address, boolean b);
abstract boolean getBool(long address);
abstract void putBool(long address, boolean b);
abstract long getCLong(long address);
abstract void putCLong(long address, long l);
abstract long getSizeT(long address);
abstract void putSizeT(long address, long st);

abstract byte getByte(byte[] array, long offset);
abstract void putByte(byte[] array, long offset, byte b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bytedeco.javacpp.indexer;
package org.bytedeco.javacpp;

/**
* A raw memory interface based on {@link UnsafeRaw} that swaps the bytes.
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/ShortPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public ShortPointer(long size) {
public ShortPointer() { }
/** @see Pointer#Pointer(Pointer) */
public ShortPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public ShortPointer position(long position) {
Expand All @@ -115,7 +114,7 @@ public ShortPointer() { }
/** @return {@code get(0)} */
public short get() { return get(0); }
/** @return the i-th {@code short} value of a native array */
public native short get(long i);
public short get(long i) { return Raw.getInstance().getShort(address + i * sizeof()); }
/** @return {@code put(0, s)} */
public ShortPointer put(short s) { return put(0, s); }
/**
Expand All @@ -125,7 +124,10 @@ public ShortPointer() { }
* @param s the {@code short} value to copy
* @return this
*/
public native ShortPointer put(long i, short s);
public ShortPointer put(long i, short s) {
Raw.getInstance().putShort(address + i * sizeof(), s);
return this;
}

/** @return {@code get(array, 0, array.length)} */
public ShortPointer get(short[] array) { return get(array, 0, array.length); }
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/bytedeco/javacpp/SizeTPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public SizeTPointer(long size) {
public SizeTPointer() { }
/** @see Pointer#Pointer(Pointer) */
public SizeTPointer(Pointer p) { super(p); }
private native void allocateArray(long size);

/** @see Pointer#position(long) */
@Override public SizeTPointer position(long position) {
Expand All @@ -99,7 +98,7 @@ public SizeTPointer() { }
/** @return {@code get(0)} */
public long get() { return get(0); }
/** @return the i-th {@code size_t} value of a native array */
@Cast("size_t") public native long get(long i);
@Cast("size_t") public long get(long i) { return Raw.getInstance().getSizeT(address + i * sizeof()); }
/** @return {@code put(0, s)} */
public SizeTPointer put(long s) { return put(0, s); }
/**
Expand All @@ -109,7 +108,10 @@ public SizeTPointer() { }
* @param s the {@code size_t} value to copy
* @return this
*/
public native SizeTPointer put(long i, long s);
public SizeTPointer put(long i, long s) {
Raw.getInstance().putSizeT(address + i * sizeof(), s);
return this;
}

/** @return {@code get(array, 0, array.length)} */
public SizeTPointer get(long[] array) { return get(array, 0, array.length); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bytedeco.javacpp.indexer;
package org.bytedeco.javacpp;

import java.lang.reflect.Field;
import sun.misc.Unsafe;
Expand All @@ -37,7 +37,7 @@ class UnsafeRaw extends Raw {
Unsafe o;
long offset;
try {
Class c = Class.forName("sun.misc.Unsafe");
Class<?> c = Class.forName("sun.misc.Unsafe");
Field f = c.getDeclaredField("theUnsafe");
c.getDeclaredMethod("getByte", long.class);
c.getDeclaredMethod("getShort", long.class);
Expand All @@ -61,6 +61,9 @@ class UnsafeRaw extends Raw {

static boolean isAvailable() { return UNSAFE != null; }

@Override long allocateMemory(long capacity) { return UNSAFE.allocateMemory(capacity); }
@Override void freeMemory(long address) { UNSAFE.freeMemory(address); }

@Override byte getByte(long address) { return UNSAFE.getByte(address); }
@Override void putByte(long address, byte b) { UNSAFE.putByte(address, b); }
@Override short getShort(long address) { return UNSAFE.getShort(address); }
Expand All @@ -77,6 +80,12 @@ class UnsafeRaw extends Raw {
@Override void putChar(long address, char c) { UNSAFE.putChar(address, c); }
@Override boolean getBoolean(long address) { return UNSAFE.getByte(address) != 0; }
@Override void putBoolean(long address, boolean b) { UNSAFE.putByte(address, b ? (byte)1 : (byte)0); }
@Override boolean getBool(long address) { throw new UnsupportedOperationException(); }
@Override void putBool(long address, boolean b) { throw new UnsupportedOperationException(); }
@Override long getCLong(long address) { throw new UnsupportedOperationException(); }
@Override void putCLong(long address, long l) { throw new UnsupportedOperationException(); }
@Override long getSizeT(long address) { throw new UnsupportedOperationException(); }
@Override void putSizeT(long address, long st) { throw new UnsupportedOperationException(); }

@Override byte getByte(byte[] array, long offset) { return UNSAFE.getByte(array, arrayOffset + offset); }
@Override void putByte(byte[] array, long offset, byte b) { UNSAFE.putByte(array, arrayOffset + offset, b); }
Expand Down