Skip to content

Commit

Permalink
Store position,limit,capacity in Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
HGuillemet committed May 8, 2021
1 parent 9b91ff0 commit 12fdf01
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 119 deletions.
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
75 changes: 33 additions & 42 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 @@ -601,13 +603,13 @@ public void setNull() {
}

/** Returns {@link #address}. */
public long address() {
public final long address() {
return address;
}

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

/** Returns {@link #limit}. */
public long limit() {
return limit;
public final long limit() {
return limit/sizeof();
}
/**
* Sets the limit and returns this.
Expand All @@ -632,14 +634,14 @@ public long limit() {
* @param limit the new limit
* @return this
*/
public <P extends Pointer> P limit(long limit) {
this.limit = limit;
public final <P extends Pointer> P limit(long limit) {
this.limit = limit * sizeof();
return (P)this;
}

/** Returns {@link #capacity}. */
public long capacity() {
return capacity;
public final long capacity() {
return capacity/sizeof();
}
/**
* Sets the capacity and returns this.
Expand All @@ -648,9 +650,8 @@ public long capacity() {
* @param capacity the new capacity
* @return this
*/
public <P extends Pointer> P capacity(long capacity) {
this.limit = capacity;
this.capacity = capacity;
public final <P extends Pointer> P capacity(long 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 + "]";
}
}

0 comments on commit 12fdf01

Please sign in to comment.