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

position, limit and capacity across Pointer casts #476

Open
wants to merge 6 commits 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
2 changes: 1 addition & 1 deletion src/main/java/org/bytedeco/javacpp/BytePointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public BytePointer() { }

/** Returns the bytes, assuming a null-terminated string if {@code limit <= position}. */
public byte[] getStringBytes() {
long size = limit - position;
long size = limit() - position();
if (size <= 0) {
size = strlen(this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/CharPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public CharPointer() { }

/** Returns the chars, assuming a null-terminated string if {@code limit <= position}. */
public char[] getStringChars() {
if (limit > position) {
char[] array = new char[(int)Math.min(limit - position, Integer.MAX_VALUE)];
if (limit() > position()) {
char[] array = new char[(int)Math.min(limit() - position(), Integer.MAX_VALUE)];
get(array);
return array;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/IntPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public IntPointer() { }

/** Returns the code points, assuming a null-terminated string if {@code limit <= position}. */
public int[] getStringCodePoints() {
if (limit > position) {
int[] array = new int[(int)Math.min(limit - position, Integer.MAX_VALUE)];
if (limit() > position()) {
int[] array = new int[(int)Math.min(limit() - position(), Integer.MAX_VALUE)];
get(array);
return array;
}
Expand Down
61 changes: 26 additions & 35 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ public Pointer(final Buffer b) {
allocate(b);
}
if (!isNull()) {
address -= b.position() * sizeof();
position = b.position();
limit = b.limit();
capacity = b.capacity();
long s = sizeof();
position = b.position() * s;
address -= position;
limit = b.limit() * s;
capacity = b.capacity() * s;
deallocator = new ProxyDeallocator(this, b);
}
}
Expand All @@ -113,7 +114,7 @@ public Pointer(final Buffer b) {
* Called by native libraries to initialize the object fields.
*
* @param allocatedAddress the new address value of allocated native memory
* @param allocatedCapacity the amount of elements allocated (initial limit and capacity)
* @param allocatedCapacity the amount of bytes allocated (initial limit and capacity)
* @param deallocatorAddress the pointer to the native deallocation function
* @see NativeDeallocator
*/
Expand All @@ -129,7 +130,8 @@ void init(long allocatedAddress, long allocatedCapacity, long ownerAddress, long

/** Adds {@code i * sizeof()} to {@link #address} and subtracts {@code i} from {@link #limit} and {@link #capacity}. */
protected <P extends Pointer> P offsetAddress(long i) {
address += i * sizeof();
i *= sizeof();
address += i;
limit = Math.max(0, limit - i);
capacity = Math.max(0, capacity - i);
return (P)this;
Expand Down Expand Up @@ -283,7 +285,7 @@ static class DeallocatorReference extends PhantomReference<Pointer> implements D
DeallocatorReference(Pointer p, Deallocator deallocator) {
super(p, referenceQueue);
this.deallocator = deallocator;
this.bytes = p.capacity * p.sizeof();
this.bytes = p.capacity;
this.count = new AtomicInteger(0);
}

Expand Down Expand Up @@ -578,12 +580,12 @@ public static long maxPhysicalBytes() {

/** 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. */
protected long position = 0;
/** The index of the first element that should not be accessed, or 0 if unknown. */
protected long limit = 0;
/** The number of elements contained in this native array, or 0 if unknown. */
protected long capacity = 0;
/** The offset from address, in bytes, to the element of a native array that should be accessed. */
private long position = 0;
/** The offset from address, in bytes, that should not be accessed, or 0 if unknown. */
private long limit = 0;
/** The length of this native array, in bytes, or 0 if unknown. */
private long capacity = 0;
/** The deallocator associated with this Pointer that should be called on garbage collection. */
private Deallocator deallocator = null;

Expand All @@ -607,7 +609,7 @@ public long address() {

/** Returns {@link #position}. */
public long position() {
return position;
return position/sizeof();
}
/**
* Sets the position and returns this. That makes the {@code array.position(i)}
Expand All @@ -617,13 +619,13 @@ public long position() {
* @return this
*/
public <P extends Pointer> P position(long position) {
this.position = position;
this.position = position * sizeof();
return (P)this;
}

/** Returns {@link #limit}. */
public long limit() {
return limit;
return limit/sizeof();
}
/**
* Sets the limit and returns this.
Expand All @@ -633,13 +635,13 @@ public long limit() {
* @return this
*/
public <P extends Pointer> P limit(long limit) {
this.limit = limit;
this.limit = limit * sizeof();
return (P)this;
}

/** Returns {@link #capacity}. */
public long capacity() {
return capacity;
return capacity/sizeof();
}
/**
* Sets the capacity and returns this.
Expand All @@ -649,8 +651,7 @@ public long capacity() {
* @return this
*/
public <P extends Pointer> P capacity(long capacity) {
this.limit = capacity;
this.capacity = capacity;
this.capacity = this.limit = capacity * sizeof();
return (P)this;
}

Expand Down Expand Up @@ -851,11 +852,10 @@ public ByteBuffer asByteBuffer() {
if (limit > 0 && limit < position) {
throw new IllegalArgumentException("limit < position: (" + limit + " < " + position + ")");
}
int size = sizeof();
Pointer p = new Pointer();
p.address = address;
return p.position(size * position)
.capacity(size * (limit <= 0 ? position + 1 : limit))
return p.position(position)
.capacity(limit <= 0 ? position + 1 : limit)
.asDirectBuffer().order(ByteOrder.nativeOrder());
}
/**
Expand Down Expand Up @@ -922,14 +922,8 @@ public <P extends Pointer> P put(Pointer p) {
if (p.limit > 0 && p.limit < p.position) {
throw new IllegalArgumentException("limit < position: (" + p.limit + " < " + p.position + ")");
}
int size = sizeof();
int psize = p.sizeof();
long length = psize * (p.limit <= 0 ? 1 : p.limit - p.position);
position *= size;
p.position *= psize;
long length = p.limit <= 0 ? 1 : p.limit - p.position;
memcpy(this, p, length);
position /= size;
p.position /= psize;
return (P)this;
}
/**
Expand All @@ -945,11 +939,8 @@ public <P extends Pointer> P fill(int b) {
if (limit > 0 && limit < position) {
throw new IllegalArgumentException("limit < position: (" + limit + " < " + position + ")");
}
int size = sizeof();
long length = size * (limit <= 0 ? 1 : limit - position);
position *= size;
long length = limit <= 0 ? 1 : limit - position;
memset(this, b, length);
position /= size;
return (P)this;
}
/** Returns {@code fill(0)}. */
Expand Down Expand Up @@ -988,6 +979,6 @@ && getClass() != Pointer.class) {
* {@link #position}, {@link #limit}, {@link #capacity}, and {@link #deallocator}. */
@Override public String toString() {
return getClass().getName() + "[address=0x" + Long.toHexString(address) +
",position=" + position + ",limit=" + limit + ",capacity=" + capacity + ",deallocator=" + deallocator + "]";
",position=" + position() + ",limit=" + limit() + ",capacity=" + capacity() + ",deallocator=" + deallocator + "]";
}
}