Skip to content

Commit

Permalink
* Upon Pointer.getPointer(Class<P>) scale position, limit, and…
Browse files Browse the repository at this point in the history
… `capacity` with `sizeof()` (pull #476)
  • Loading branch information
saudet committed May 12, 2021
1 parent 0ce97fc commit 577239c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Upon `Pointer.getPointer(Class<P>)` scale `position`, `limit`, and `capacity` with `sizeof()` ([pull #476](https://github.com/bytedeco/javacpp/pull/476))
* Fix `Parser` incorrectly translating non-documentation comments as part of documentation comments ([issue #475](https://github.com/bytedeco/javacpp/issues/475))
* Set `Pointer.maxPhysicalBytes` to `4 * Runtime.maxMemory()` by default as workaround for memory-mapped files, ZGC, etc ([issue #468](https://github.com/bytedeco/javacpp/issues/468))
* Ensure `synchronized` code in `Pointer` gets skipped with "org.bytedeco.javacpp.nopointergc" ([issue tensorflow/java#313](https://github.com/tensorflow/java/issues/313))
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -895,15 +895,20 @@ public <P extends Pointer> P getPointer(long i) {
return (P)getPointer(getClass(), i);
}

/** Returns {@code getPointer(cls, 0)}. */
public <P extends Pointer> P getPointer(Class<P> cls) {
return getPointer(cls, 0);
/** Returns {@code getPointer(type, 0)}. */
public <P extends Pointer> P getPointer(Class<P> type) {
return getPointer(type, 0);
}

/** Returns {@code new P(this).offsetAddress(i)}. Throws RuntimeException if constructor is missing. */
public <P extends Pointer> P getPointer(Class<P> cls, long i) {
/** Returns {@code new P(this).offsetAddress(i)} after scaling {@link #position}, {@link #limit}, and {@link #capacity}
* with {@link #sizeof()}. Throws RuntimeException if the cast constructor is missing from the subclass type. */
public <P extends Pointer> P getPointer(Class<P> type, long i) {
try {
return cls.getDeclaredConstructor(Pointer.class).newInstance(this).offsetAddress(i);
P p = type.getDeclaredConstructor(Pointer.class).newInstance(this);
p.position = position != 0 ? Math.max(0, position * this.sizeof() / p.sizeof()) : 0;
p.limit = limit != 0 ? Math.max(0, limit * this.sizeof() / p.sizeof()) : 0;
p.capacity = capacity != 0 ? Math.max(0, capacity * this.sizeof() / p.sizeof()) : 0;
return p.offsetAddress(i);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/bytedeco/javacpp/PointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 10, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
Pointer p3 = pointer.getPointer(Pointer.class, 20);
assertEquals(pointer.address() + 20, p3.address());
assertEquals(1 * array.length - 20, p3.limit());
assertEquals(1 * array.length - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
array[i] = (byte)i;
Expand Down Expand Up @@ -279,6 +283,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 20, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
Pointer p3 = pointer.getPointer(Pointer.class, 20);
assertEquals(pointer.address() + 20, p3.address());
assertEquals(2 * array.length - 20, p3.limit());
assertEquals(2 * array.length - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
array[i] = (short)i;
Expand Down Expand Up @@ -367,6 +375,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 40, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
Pointer p3 = pointer.getPointer(Pointer.class, 20);
assertEquals(pointer.address() + 20, p3.address());
assertEquals(4 * array.length - 20, p3.limit());
assertEquals(4 * array.length - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
array[i] = i;
Expand Down Expand Up @@ -455,6 +467,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 80, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
Pointer p3 = pointer.getPointer(Pointer.class, 20);
assertEquals(pointer.address() + 20, p3.address());
assertEquals(8 * array.length - 20, p3.limit());
assertEquals(8 * array.length - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
array[i] = i;
Expand Down Expand Up @@ -543,6 +559,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 40, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
Pointer p3 = pointer.getPointer(Pointer.class, 20);
assertEquals(pointer.address() + 20, p3.address());
assertEquals(4 * array.length - 20, p3.limit());
assertEquals(4 * array.length - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
array[i] = i;
Expand Down Expand Up @@ -631,6 +651,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 80, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
Pointer p3 = pointer.getPointer(Pointer.class, 20);
assertEquals(pointer.address() + 20, p3.address());
assertEquals(8 * array.length - 20, p3.limit());
assertEquals(8 * array.length - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
array[i] = i;
Expand Down Expand Up @@ -719,6 +743,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 20, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
Pointer p3 = pointer.getPointer(Pointer.class, 20);
assertEquals(pointer.address() + 20, p3.address());
assertEquals(2 * array.length - 20, p3.limit());
assertEquals(2 * array.length - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
array[i] = (char)i;
Expand Down Expand Up @@ -807,6 +835,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 10, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
LongPointer p3 = pointer.getPointer(LongPointer.class, 20);
assertEquals(pointer.address() + 8 * 20, p3.address());
assertEquals(array.length / 8 - 20, p3.limit());
assertEquals(array.length / 8 - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
array[i] = i % 2 != 0;
Expand Down Expand Up @@ -894,6 +926,10 @@ static class TestFunction extends FunctionPointer {
assertEquals(pointer.address() + 10 * pointerSize, p2.address());
assertEquals(array.length - 10, p2.limit());
assertEquals(array.length - 10, p2.capacity());
BytePointer p3 = pointer.getPointer(BytePointer.class, 20);
assertEquals(pointer.address() + 20, p3.address());
assertEquals(pointerSize * array.length - 20, p3.limit());
assertEquals(pointerSize * array.length - 20, p3.capacity());

for (int i = 0; i < array.length; i++) {
final int j = i;
Expand Down

0 comments on commit 577239c

Please sign in to comment.