wirehelpers

This commit is contained in:
David Renshaw 2014-05-10 13:33:57 -04:00
parent 836ecf00cd
commit 82602b7ca5
8 changed files with 116 additions and 10 deletions

View file

@ -1,12 +1,14 @@
CXX=g++ -std=c++11 CXX=g++ -std=c++11
CAPNP_SOURCES=\ CAPNP_SOURCES=\
src/capnp/FieldSize.java\
src/capnp/FromStructReader.java\ src/capnp/FromStructReader.java\
src/capnp/ListPointer.java\ src/capnp/ListPointer.java\
src/capnp/ListReader.java\ src/capnp/ListReader.java\
src/capnp/PointerReader.java\ src/capnp/PointerReader.java\
src/capnp/SegmentReader.java\ src/capnp/SegmentReader.java\
src/capnp/StructReader.java\ src/capnp/StructReader.java\
src/capnp/StructPointer.java\
src/capnp/StructList.java\ src/capnp/StructList.java\
src/capnp/Text.java\ src/capnp/Text.java\
src/capnp/WireHelpers.java\ src/capnp/WireHelpers.java\

12
src/capnp/FieldSize.java Normal file
View file

@ -0,0 +1,12 @@
package capnp;
public class FieldSize {
public static final byte VOID = 0;
public static final byte BIT = 1;
public static final byte BYTE = 2;
public static final byte TWO_BYTES = 3;
public static final byte FOUR_BYTES = 4;
public static final byte EIGHT_BYTES = 5;
public static final byte POINTER = 6;
public static final byte INLINE_COMPOSITE = 7;
}

View file

@ -8,7 +8,15 @@ class ListPointer extends WirePointer {
super(buffer, buffer_offset); super(buffer, buffer_offset);
} }
public byte elementSize() {
return (byte)(this.buffer.getInt(buffer_offset * 2 + 1) & 7);
}
public int elementCount() { public int elementCount() {
return this.buffer.getInt(buffer_offset * 2) >> 3; return this.buffer.getInt(buffer_offset * 2 + 1) >> 3;
}
public int inlineCompositeWordCount() {
return this.elementCount();
} }
} }

View file

@ -7,7 +7,7 @@ public class PointerReader {
public PointerReader() { public PointerReader() {
this.segment = null; this.segment = null;
this.pointer = 0; // XXX this.pointer = 0; // XXX ?
this.nestingLimit = 0x7fffffff; this.nestingLimit = 0x7fffffff;
} }
@ -17,7 +17,17 @@ public class PointerReader {
this.nestingLimit = nestingLimit; this.nestingLimit = nestingLimit;
} }
public void getText() { public ListReader getList(byte expectedElementSize) {
throw new Error(); // TODO check nullness
WirePointer ref = new WirePointer(this.segment.ptr, this.pointer);
return WireHelpers.readListPointer(this.segment,
ref,
expectedElementSize);
}
public Text.Reader getText() {
return WireHelpers.readTextPointer(this.segment,
new WirePointer(this.segment.ptr,
this.pointer));
} }
} }

View file

@ -0,0 +1,22 @@
package capnp;
import java.nio.ByteBuffer;
class StructPointer extends WirePointer {
public StructPointer(ByteBuffer buffer, int buffer_offset) {
super(buffer, buffer_offset);
}
public short dataSize() {
return this.buffer.getShort(this.buffer_offset * 4 + 2);
}
public short ptrCount() {
return this.buffer.getShort(this.buffer_offset * 4 + 3);
}
public int wordSize() {
return this.dataSize() + this.ptrCount();
}
}

View file

@ -1,8 +1,52 @@
package capnp; package capnp;
class WireHelpers { class WireHelpers {
public static ListReader readListPointer(SegmentReader segment,
WirePointer ref,
int nestingLimit) {
// TODO check for null, follow fars, nestingLimit
ListPointer listPtr = (ListPointer)ref;
WordPointer ptr = ref.target();
switch (listPtr.elementSize()) {
case FieldSize.INLINE_COMPOSITE : {
int wordCount = listPtr.inlineCompositeWordCount();
WirePointer tag = new WirePointer(ptr);
ptr.offset += 1;
// TODO bounds check
int size = tag.inlineCompositeListElementCount();
StructPointer structPtr = (StructPointer)tag;
int wordsPerElement = structPtr.wordSize();
// 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, //
size,
wordsPerElement * 64,
structPtr.dataSize() * 64,
structPtr.ptrCount(),
nestingLimit - 1);
}
case FieldSize.VOID : break;
default :
throw new Error("unrecognized element size");
}
throw new Error();
}
public static Text.Reader readTextPointer(SegmentReader segment, public static Text.Reader readTextPointer(SegmentReader segment,
WirePointer ref) { WirePointer ref) {
ref.target(); ref.target();
ListPointer listPtr = (ListPointer)ref; ListPointer listPtr = (ListPointer)ref;
return new Text.Reader(listPtr); return new Text.Reader(listPtr);

View file

@ -6,16 +6,21 @@ class WirePointer {
public final ByteBuffer buffer; public final ByteBuffer buffer;
public final int buffer_offset; // in words public final int buffer_offset; // in words
public final byte STRUCT = 0; public static final byte STRUCT = 0;
public final byte LIST = 1; public static final byte LIST = 1;
public final byte FAR = 2; public static final byte FAR = 2;
public final byte OTHER = 3; public static final byte OTHER = 3;
public WirePointer(ByteBuffer buffer, int offset) { public WirePointer(ByteBuffer buffer, int offset) {
this.buffer = buffer; this.buffer = buffer;
this.buffer_offset = offset; this.buffer_offset = offset;
} }
public WirePointer(WordPointer word) {
this.buffer = word.buffer;
this.buffer_offset = word.offset;
}
public int offset_and_kind() { public int offset_and_kind() {
return this.buffer.getInt(buffer_offset * 2); return this.buffer.getInt(buffer_offset * 2);
} }
@ -27,6 +32,9 @@ class WirePointer {
public WordPointer target() { public WordPointer target() {
return new WordPointer(buffer, return new WordPointer(buffer,
1 + (this.offset_and_kind() >> 2)); 1 + (this.offset_and_kind() >> 2));
}
public int inlineCompositeListElementCount() {
return this.offset_and_kind() >> 2;
} }
} }

View file

@ -4,7 +4,7 @@ import java.nio.ByteBuffer;
class WordPointer { class WordPointer {
public final ByteBuffer buffer; public final ByteBuffer buffer;
public final int offset; // in words public int offset; // in words
public WordPointer(ByteBuffer buffer, int offset) { public WordPointer(ByteBuffer buffer, int offset) {
this.buffer = buffer; this.buffer = buffer;