Skip to content

Commit 4abc7de

Browse files
committed
Use sun.misc.Unsafe in pointers and not in indexers
1 parent e2b39ad commit 4abc7de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+381
-488
lines changed

src/main/java/org/bytedeco/javacpp/BoolPointer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public BoolPointer(long size) {
7171
public BoolPointer() { }
7272
/** @see Pointer#Pointer(Pointer) */
7373
public BoolPointer(Pointer p) { super(p); }
74-
private native void allocateArray(long size);
7574

7675
/** @see Pointer#position(long) */
7776
@Override public BoolPointer position(long position) {
@@ -90,7 +89,7 @@ public BoolPointer() { }
9089
public boolean get() { return get(0); }
9190
/** @return the i-th {@code bool} value of a native array
9291
* @param i*/
93-
@Cast("bool") public native boolean get(long i);
92+
@Cast("bool") public boolean get(long i) { return Raw.getInstance().getBool(address + i * sizeof()); }
9493
/** @return {@code put(0, b)} */
9594
public BoolPointer put(boolean b) { return put(0, b); }
9695
/**
@@ -100,5 +99,8 @@ public BoolPointer() { }
10099
* @param b the {@code bool} value to copy
101100
* @return this
102101
*/
103-
public native BoolPointer put(long i, boolean b);
102+
public BoolPointer put(long i, boolean b) {
103+
Raw.getInstance().putBool(address + i * sizeof(), b);
104+
return this;
105+
}
104106
}

src/main/java/org/bytedeco/javacpp/BooleanPointer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public BooleanPointer(long size) {
9898
public BooleanPointer() { }
9999
/** @see Pointer#Pointer(Pointer) */
100100
public BooleanPointer(Pointer p) { super(p); }
101-
private native void allocateArray(long size);
102101

103102
/** @see Pointer#position(long) */
104103
@Override public BooleanPointer position(long position) {
@@ -119,7 +118,7 @@ public BooleanPointer() { }
119118
/** @return {@code get(0)} */
120119
public boolean get() { return get(0); }
121120
/** @return the i-th {@code boolean} value of a native array */
122-
public native boolean get(long i);
121+
public boolean get(long i) { return Raw.getInstance().getByte(address + i * sizeof()) != 0; }
123122
/** @return {@code put(0, b)} */
124123
public BooleanPointer put(boolean b) { return put(0, b); }
125124
/**
@@ -129,7 +128,10 @@ public BooleanPointer() { }
129128
* @param b the {@code boolean} value to copy
130129
* @return this
131130
*/
132-
public native BooleanPointer put(long i, boolean b);
131+
public BooleanPointer put(long i, boolean b) {
132+
Raw.getInstance().putByte(address + i * sizeof(), (byte) (b ? 1 : 0));
133+
return this;
134+
}
133135

134136
/** @return {@code get(array, 0, array.length)} */
135137
public BooleanPointer get(boolean[] array) { return get(array, 0, array.length); }

src/main/java/org/bytedeco/javacpp/BytePointer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public BytePointer(long size) {
138138
public BytePointer() { }
139139
/** @see Pointer#Pointer(Pointer) */
140140
public BytePointer(Pointer p) { super(p); }
141-
private native void allocateArray(long size);
142141

143142
/** @see Pointer#position(long) */
144143
@Override public BytePointer position(long position) {
@@ -248,7 +247,7 @@ public BytePointer putString(String s) {
248247
/** @return {@code get(0)} */
249248
public byte get() { return get(0); }
250249
/** @return the i-th {@code byte} value of a native array */
251-
public native byte get(long i);
250+
public byte get(long i) { return Raw.getInstance().getByte(address + i * sizeof()); }
252251
/** @return {@code put(0, b)} */
253252
public BytePointer put(byte b) { return put(0, b); }
254253
/**
@@ -258,7 +257,10 @@ public BytePointer putString(String s) {
258257
* @param b the {@code byte} value to copy
259258
* @return this
260259
*/
261-
public native BytePointer put(long i, byte b);
260+
public BytePointer put(long i, byte b) {
261+
Raw.getInstance().putByte(address + i * sizeof(), b);
262+
return this;
263+
}
262264

263265
/** @return {@code get(array, 0, array.length)} */
264266
public BytePointer get(byte[] array) { return get(array, 0, array.length); }

src/main/java/org/bytedeco/javacpp/CLongPointer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public CLongPointer(long size) {
8282
public CLongPointer() { }
8383
/** @see Pointer#Pointer(Pointer) */
8484
public CLongPointer(Pointer p) { super(p); }
85-
private native void allocateArray(long size);
8685

8786
/** @see Pointer#position(long) */
8887
@Override public CLongPointer position(long position) {
@@ -100,7 +99,7 @@ public CLongPointer() { }
10099
/** @return {@code get(0)} */
101100
public long get() { return get(0); }
102101
/** @return the i-th {@code long} value of a native array */
103-
@Cast("long") public native long get(long i);
102+
@Cast("long") public long get(long i) { return Raw.getInstance().getCLong(address + i * sizeof()); }
104103
/** @return {@code put(0, l)} */
105104
public CLongPointer put(long l) { return put(0, l); }
106105
/**
@@ -110,7 +109,10 @@ public CLongPointer() { }
110109
* @param l the {@code long} value to copy
111110
* @return this
112111
*/
113-
public native CLongPointer put(long i, long l);
112+
public CLongPointer put(long i, long l) {
113+
Raw.getInstance().putCLong(address + i * sizeof(), l);
114+
return this;
115+
}
114116

115117
/** @return {@code get(array, 0, array.length)} */
116118
public CLongPointer get(long[] array) { return get(array, 0, array.length); }

src/main/java/org/bytedeco/javacpp/CharPointer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public CharPointer(long size) {
104104
public CharPointer() { }
105105
/** @see Pointer#Pointer(Pointer) */
106106
public CharPointer(Pointer p) { super(p); }
107-
private native void allocateArray(long size);
108107

109108
/** @see Pointer#position(long) */
110109
@Override public CharPointer position(long position) {
@@ -166,7 +165,7 @@ public CharPointer putString(String s) {
166165
/** @return {@code get(0)} */
167166
public char get() { return get(0); }
168167
/** @return the i-th {@code char} value of a native array */
169-
public native char get(long i);
168+
public char get(long i) { return Raw.getInstance().getChar(address + i * sizeof()); }
170169
/** @return {@code put(0, c)} */
171170
public CharPointer put(char c) { return put(0, c); }
172171
/**
@@ -176,7 +175,10 @@ public CharPointer putString(String s) {
176175
* @param c the {@code char} value to copy
177176
* @return this
178177
*/
179-
public native CharPointer put(long i, char c);
178+
public CharPointer put(long i, char c) {
179+
Raw.getInstance().putChar(address + i * sizeof(), c);
180+
return this;
181+
}
180182

181183
/** @return {@code get(array, 0, array.length)} */
182184
public CharPointer get(char[] array) { return get(array, 0, array.length); }

src/main/java/org/bytedeco/javacpp/DoublePointer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public DoublePointer(long size) {
9494
public DoublePointer() { }
9595
/** @see Pointer#Pointer(Pointer) */
9696
public DoublePointer(Pointer p) { super(p); }
97-
private native void allocateArray(long size);
9897

9998
/** @see Pointer#position(long) */
10099
@Override public DoublePointer position(long position) {
@@ -115,7 +114,7 @@ public DoublePointer() { }
115114
/** @return {@code get(0)} */
116115
public double get() { return get(0); }
117116
/** @return the i-th {@code double} value of a native array */
118-
public native double get(long i);
117+
public double get(long i) { return Raw.getInstance().getDouble(address + i * sizeof()); }
119118
/** @return {@code put(0, d)} */
120119
public DoublePointer put(double d) { return put(0, d); }
121120
/**
@@ -125,7 +124,10 @@ public DoublePointer() { }
125124
* @param d the {@code double} value to copy
126125
* @return this
127126
*/
128-
public native DoublePointer put(long i, double d);
127+
public DoublePointer put(long i, double d) {
128+
Raw.getInstance().putDouble(address + i * sizeof(), d);
129+
return this;
130+
}
129131

130132
/** @return {@code get(array, 0, array.length)} */
131133
public DoublePointer get(double[] array) { return get(array, 0, array.length); }

src/main/java/org/bytedeco/javacpp/FloatPointer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public FloatPointer(long size) {
9494
public FloatPointer() { }
9595
/** @see Pointer#Pointer(Pointer) */
9696
public FloatPointer(Pointer p) { super(p); }
97-
private native void allocateArray(long size);
9897

9998
/** @see Pointer#position(long) */
10099
@Override public FloatPointer position(long position) {
@@ -115,7 +114,7 @@ public FloatPointer() { }
115114
/** @return {@code get(0)} */
116115
public float get() { return get(0); }
117116
/** @return the i-th {@code float} value of a native array */
118-
public native float get(long i);
117+
public float get(long i) { return Raw.getInstance().getFloat(address + i * sizeof()); }
119118
/** @return {@code put(0, f)} */
120119
public FloatPointer put(float f) { return put(0, f); }
121120
/**
@@ -125,7 +124,10 @@ public FloatPointer() { }
125124
* @param f the {@code float} value to copy
126125
* @return this
127126
*/
128-
public native FloatPointer put(long i, float f);
127+
public FloatPointer put(long i, float f) {
128+
Raw.getInstance().putFloat(address + i * sizeof(), f);
129+
return this;
130+
}
129131

130132
/** @return {@code get(array, 0, array.length)} */
131133
public FloatPointer get(float[] array) { return get(array, 0, array.length); }

src/main/java/org/bytedeco/javacpp/IntPointer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public IntPointer(long size) {
104104
public IntPointer() { }
105105
/** @see Pointer#Pointer(Pointer) */
106106
public IntPointer(Pointer p) { super(p); }
107-
private native void allocateArray(long size);
108107

109108
/** @see Pointer#position(long) */
110109
@Override public IntPointer position(long position) {
@@ -170,7 +169,7 @@ public IntPointer putString(String s) {
170169
/** @return {@code get(0)} */
171170
public int get() { return get(0); }
172171
/** @return the i-th {@code int} value of a native array */
173-
public native int get(long i);
172+
public int get(long i) { return Raw.getInstance().getInt(address + i * sizeof()); }
174173
/** @return {@code put(0, j)} */
175174
public IntPointer put(int j) { return put(0, j); }
176175
/**
@@ -180,7 +179,10 @@ public IntPointer putString(String s) {
180179
* @param j the {@code int} value to copy
181180
* @return this
182181
*/
183-
public native IntPointer put(long i, int j);
182+
public IntPointer put(long i, int j) {
183+
Raw.getInstance().putInt(address + i * sizeof(), j);
184+
return this;
185+
}
184186

185187
/** @return {@code get(array, 0, array.length)} */
186188
public IntPointer get(int[] array) { return get(array, 0, array.length); }

src/main/java/org/bytedeco/javacpp/LongPointer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public LongPointer(long size) {
9494
public LongPointer() { }
9595
/** @see Pointer#Pointer(Pointer) */
9696
public LongPointer(Pointer p) { super(p); }
97-
private native void allocateArray(long size);
9897

9998
/** @see Pointer#position(long) */
10099
@Override public LongPointer position(long position) {

src/main/java/org/bytedeco/javacpp/Pointer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public Pointer(final Buffer b) {
110110
}
111111
private native void allocate(Buffer b);
112112

113+
protected void allocateArray(long size) {
114+
this.address = Raw.getInstance().allocateMemory(size * sizeof());
115+
}
116+
113117
/**
114118
* Called by native libraries to initialize the object fields.
115119
*

0 commit comments

Comments
 (0)