now all of the WirePointer logic is in static methods
This commit is contained in:
parent
5ee4fe295c
commit
c4b6bad176
5 changed files with 28 additions and 102 deletions
|
@ -3,32 +3,14 @@ package org.capnproto;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
final class ListPointer {
|
final class ListPointer {
|
||||||
public WirePointer ptr;
|
|
||||||
|
|
||||||
public ListPointer(WirePointer ptr) {
|
|
||||||
this.ptr = ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte elementSize() {
|
|
||||||
return (byte)(this.ptr.buffer.getInt(this.ptr.buffer_offset * 8 + 4) & 7);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte elementSize(int elementSizeAndCount) {
|
public static byte elementSize(int elementSizeAndCount) {
|
||||||
return (byte) (elementSizeAndCount & 7);
|
return (byte) (elementSizeAndCount & 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int elementCount() {
|
|
||||||
return this.ptr.buffer.getInt(this.ptr.buffer_offset * 8 + 4) >> 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int elementCount(int elementSizeAndCount) {
|
public static int elementCount(int elementSizeAndCount) {
|
||||||
return elementSizeAndCount >> 3;
|
return elementSizeAndCount >> 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int inlineCompositeWordCount() {
|
|
||||||
return this.elementCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int inlineCompositeWordCount(int elementSizeAndCount) {
|
public static int inlineCompositeWordCount(int elementSizeAndCount) {
|
||||||
return elementCount(elementSizeAndCount);
|
return elementCount(elementSizeAndCount);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,17 +29,15 @@ public class PointerReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
public StructReader getStruct() {
|
public StructReader getStruct() {
|
||||||
WirePointer ref = new WirePointer(this.segment.ptr, this.pointer);
|
|
||||||
return WireHelpers.readStructPointer(this.segment,
|
return WireHelpers.readStructPointer(this.segment,
|
||||||
ref,
|
this.pointer,
|
||||||
this.nestingLimit);
|
this.nestingLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ListReader getList(byte expectedElementSize) {
|
public ListReader getList(byte expectedElementSize) {
|
||||||
// TODO check nullness
|
// TODO check nullness
|
||||||
WirePointer ref = new WirePointer(this.segment.ptr, this.pointer);
|
|
||||||
return WireHelpers.readListPointer(this.segment,
|
return WireHelpers.readListPointer(this.segment,
|
||||||
ref,
|
this.pointer,
|
||||||
expectedElementSize,
|
expectedElementSize,
|
||||||
this.nestingLimit);
|
this.nestingLimit);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,32 +3,14 @@ package org.capnproto;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
final class StructPointer{
|
final class StructPointer{
|
||||||
public WirePointer ptr;
|
|
||||||
|
|
||||||
public StructPointer(WirePointer ptr) {
|
|
||||||
this.ptr = ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
public short dataSize() {
|
|
||||||
return this.ptr.buffer.getShort(this.ptr.buffer_offset * 8 + 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static short dataSize(int structRef) {
|
public static short dataSize(int structRef) {
|
||||||
return (short)(structRef & 0xffff);
|
return (short)(structRef & 0xffff);
|
||||||
}
|
}
|
||||||
|
|
||||||
public short ptrCount() {
|
|
||||||
return this.ptr.buffer.getShort(this.ptr.buffer_offset * 8 + 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static short ptrCount(int structRef) {
|
public static short ptrCount(int structRef) {
|
||||||
return (short)(structRef >> 16);
|
return (short)(structRef >> 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int wordSize() {
|
|
||||||
return this.dataSize() + this.ptrCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int wordSize(int structRef) {
|
public static int wordSize(int structRef) {
|
||||||
return (int)dataSize(structRef) + (int)ptrCount(structRef);
|
return (int)dataSize(structRef) + (int)ptrCount(structRef);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,23 @@
|
||||||
package org.capnproto;
|
package org.capnproto;
|
||||||
|
|
||||||
class WireHelpers {
|
final class WireHelpers {
|
||||||
|
|
||||||
public static StructReader readStructPointer(SegmentReader segment,
|
public static StructReader readStructPointer(SegmentReader segment,
|
||||||
WirePointer ref,
|
int refOffset,
|
||||||
int nestingLimit) {
|
int nestingLimit) {
|
||||||
|
|
||||||
// TODO error handling
|
// TODO error handling
|
||||||
|
|
||||||
WordPointer ptr = ref.target();
|
long ref = WirePointer.get(segment.ptr, refOffset);
|
||||||
StructPointer structPtr = new StructPointer(ref);
|
int ptrOffset = WirePointer.target(refOffset, ref);
|
||||||
int dataSizeWords = structPtr.dataSize();
|
int structPtr = WirePointer.structPointer(ref);
|
||||||
|
int dataSizeWords = StructPointer.dataSize(structPtr);
|
||||||
|
|
||||||
return new StructReader(segment,
|
return new StructReader(segment,
|
||||||
ptr.offset * 8,
|
ptrOffset * 8,
|
||||||
(ptr.offset + dataSizeWords),
|
(ptrOffset + dataSizeWords),
|
||||||
dataSizeWords * 64,
|
dataSizeWords * 64,
|
||||||
structPtr.ptrCount(),
|
StructPointer.ptrCount(structPtr),
|
||||||
(byte)0,
|
(byte)0,
|
||||||
nestingLimit - 1);
|
nestingLimit - 1);
|
||||||
|
|
||||||
|
@ -24,43 +25,46 @@ class WireHelpers {
|
||||||
|
|
||||||
|
|
||||||
public static ListReader readListPointer(SegmentReader segment,
|
public static ListReader readListPointer(SegmentReader segment,
|
||||||
WirePointer ref,
|
int refOffset,
|
||||||
byte expectedElementSize,
|
byte expectedElementSize,
|
||||||
int nestingLimit) {
|
int nestingLimit) {
|
||||||
|
|
||||||
|
long ref = WirePointer.get(segment.ptr, refOffset);
|
||||||
|
|
||||||
// TODO check for null, follow fars, nestingLimit
|
// TODO check for null, follow fars, nestingLimit
|
||||||
if (ref.isNull()) {
|
if (WirePointer.isNull(ref)) {
|
||||||
return new ListReader();
|
return new ListReader();
|
||||||
}
|
}
|
||||||
|
|
||||||
ListPointer listPtr = new ListPointer(ref);
|
int listPtr = WirePointer.listPointer(ref);
|
||||||
|
|
||||||
WordPointer ptr = ref.target();
|
int ptrOffset = WirePointer.target(refOffset, ref);
|
||||||
|
long ptr = WirePointer.get(segment.ptr, ptrOffset);
|
||||||
|
|
||||||
switch (listPtr.elementSize()) {
|
switch (ListPointer.elementSize(listPtr)) {
|
||||||
case FieldSize.INLINE_COMPOSITE : {
|
case FieldSize.INLINE_COMPOSITE : {
|
||||||
int wordCount = listPtr.inlineCompositeWordCount();
|
int wordCount = ListPointer.inlineCompositeWordCount(listPtr);
|
||||||
|
|
||||||
WirePointer tag = new WirePointer(ptr);
|
long tag = ptr;
|
||||||
ptr.offset += 1;
|
ptrOffset += 1;
|
||||||
|
|
||||||
// TODO bounds check
|
// TODO bounds check
|
||||||
|
|
||||||
int size = tag.inlineCompositeListElementCount();
|
int size = WirePointer.inlineCompositeListElementCount(tag);
|
||||||
|
|
||||||
StructPointer structPtr = new StructPointer(tag);
|
int structPtr = WirePointer.structPointer(tag);
|
||||||
int wordsPerElement = structPtr.wordSize();
|
int wordsPerElement = StructPointer.wordSize(structPtr);
|
||||||
|
|
||||||
// TODO check that elemements do not overrun word count
|
// TODO check that elemements do not overrun word count
|
||||||
|
|
||||||
// TODO check whether the size is compatible
|
// TODO check whether the size is compatible
|
||||||
|
|
||||||
return new ListReader(segment, // TODO follow fars
|
return new ListReader(segment, // TODO follow fars
|
||||||
ptr.offset * 8, //
|
ptrOffset * 8, //
|
||||||
size,
|
size,
|
||||||
wordsPerElement * 64,
|
wordsPerElement * 64,
|
||||||
structPtr.dataSize() * 64,
|
StructPointer.dataSize(structPtr) * 64,
|
||||||
structPtr.ptrCount(),
|
StructPointer.ptrCount(structPtr),
|
||||||
nestingLimit - 1);
|
nestingLimit - 1);
|
||||||
}
|
}
|
||||||
case FieldSize.VOID : break;
|
case FieldSize.VOID : break;
|
||||||
|
@ -75,7 +79,6 @@ class WireHelpers {
|
||||||
int refOffset) {
|
int refOffset) {
|
||||||
long ref = WirePointer.get(segment.ptr, refOffset);
|
long ref = WirePointer.get(segment.ptr, refOffset);
|
||||||
int ptrOffset = WirePointer.target(refOffset, ref);
|
int ptrOffset = WirePointer.target(refOffset, ref);
|
||||||
long ptr = WirePointer.get(segment.ptr, ptrOffset);
|
|
||||||
int listPtr = WirePointer.listPointer(ref);
|
int listPtr = WirePointer.listPointer(ref);
|
||||||
int size = ListPointer.elementCount(listPtr);
|
int size = ListPointer.elementCount(listPtr);
|
||||||
return new Text.Reader(segment.ptr, ptrOffset, size);
|
return new Text.Reader(segment.ptr, ptrOffset, size);
|
||||||
|
|
|
@ -3,70 +3,31 @@ package org.capnproto;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
final class WirePointer {
|
final class WirePointer {
|
||||||
public final ByteBuffer buffer;
|
|
||||||
public final int buffer_offset; // in words
|
|
||||||
|
|
||||||
public static final byte STRUCT = 0;
|
public static final byte STRUCT = 0;
|
||||||
public static final byte LIST = 1;
|
public static final byte LIST = 1;
|
||||||
public static final byte FAR = 2;
|
public static final byte FAR = 2;
|
||||||
public static final byte OTHER = 3;
|
public static final byte OTHER = 3;
|
||||||
|
|
||||||
public WirePointer(ByteBuffer buffer, int offset) {
|
|
||||||
this.buffer = buffer;
|
|
||||||
this.buffer_offset = offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
public WirePointer(WordPointer word) {
|
|
||||||
this.buffer = word.buffer;
|
|
||||||
this.buffer_offset = word.offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isNull() {
|
|
||||||
return this.buffer.getLong(this.buffer_offset * 8) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isNull(long wirePointer) {
|
public static boolean isNull(long wirePointer) {
|
||||||
return wirePointer == 0;
|
return wirePointer == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int offsetAndKind() {
|
|
||||||
return this.buffer.getInt(this.buffer_offset * 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int offsetAndKind(long wirePointer) {
|
public static int offsetAndKind(long wirePointer) {
|
||||||
return (int)(wirePointer & 0xffffffff);
|
return (int)(wirePointer & 0xffffffff);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte kind() {
|
|
||||||
return (byte) (this.offsetAndKind() & 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte kind(long wirePointer) {
|
public static byte kind(long wirePointer) {
|
||||||
return (byte)(offsetAndKind(wirePointer) & 3);
|
return (byte)(offsetAndKind(wirePointer) & 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
public WordPointer target() {
|
|
||||||
return new WordPointer(buffer,
|
|
||||||
this.buffer_offset + 1 + (this.offsetAndKind() >> 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int target(int offset, long wirePointer) {
|
public static int target(int offset, long wirePointer) {
|
||||||
return offset + 1 + (offsetAndKind(wirePointer) >> 2);
|
return offset + 1 + (offsetAndKind(wirePointer) >> 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int inlineCompositeListElementCount() {
|
|
||||||
return this.offsetAndKind() >> 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int inlineCompositeListElementCount(long wirePointer) {
|
public static int inlineCompositeListElementCount(long wirePointer) {
|
||||||
return offsetAndKind(wirePointer) >> 2;
|
return offsetAndKind(wirePointer) >> 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// offset is in words
|
|
||||||
public static int upper32Bits(ByteBuffer buffer, int offset) {
|
|
||||||
return buffer.getInt(offset * 8 + 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int upper32Bits(long wirePointer) {
|
public static int upper32Bits(long wirePointer) {
|
||||||
return (int)(wirePointer >> 32);
|
return (int)(wirePointer >> 32);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue