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