simplify List and Struct Pointer getters
This commit is contained in:
parent
df2233a52b
commit
58417744ff
4 changed files with 27 additions and 39 deletions
|
@ -3,16 +3,16 @@ package org.capnproto;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
final class ListPointer {
|
final class ListPointer {
|
||||||
public static byte elementSize(int elementSizeAndCount) {
|
public static byte elementSize(long ref) {
|
||||||
return (byte) (elementSizeAndCount & 7);
|
return (byte) (WirePointer.upper32Bits(ref) & 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int elementCount(int elementSizeAndCount) {
|
public static int elementCount(long ref) {
|
||||||
return elementSizeAndCount >>> 3;
|
return WirePointer.upper32Bits(ref) >>> 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int inlineCompositeWordCount(int elementSizeAndCount) {
|
public static int inlineCompositeWordCount(long ref) {
|
||||||
return elementCount(elementSizeAndCount);
|
return elementCount(ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void set(ByteBuffer buffer, int offset, byte elementSize, int elementCount) {
|
public static void set(ByteBuffer buffer, int offset, byte elementSize, int elementCount) {
|
||||||
|
|
|
@ -3,16 +3,16 @@ package org.capnproto;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
final class StructPointer{
|
final class StructPointer{
|
||||||
public static short dataSize(int structRef) {
|
public static short dataSize(long ref) {
|
||||||
return (short)(structRef & 0xffff);
|
return (short)(WirePointer.upper32Bits(ref) & 0xffff);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static short ptrCount(int structRef) {
|
public static short ptrCount(long ref) {
|
||||||
return (short)(structRef >>> 16);
|
return (short)(WirePointer.upper32Bits(ref) >>> 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int wordSize(int structRef) {
|
public static int wordSize(long ref) {
|
||||||
return (int)dataSize(structRef) + (int)ptrCount(structRef);
|
return (int)dataSize(ref) + (int)ptrCount(ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setFromStructSize(ByteBuffer buffer, int offset, StructSize size) {
|
public static void setFromStructSize(ByteBuffer buffer, int offset, StructSize size) {
|
||||||
|
|
|
@ -116,8 +116,8 @@ final class WireHelpers {
|
||||||
}
|
}
|
||||||
FollowBuilderFarsResult resolved = followBuilderFars(ref, target, segment);
|
FollowBuilderFarsResult resolved = followBuilderFars(ref, target, segment);
|
||||||
|
|
||||||
short oldDataSize = StructPointer.dataSize(WirePointer.structPointer(resolved.ref));
|
short oldDataSize = StructPointer.dataSize(resolved.ref);
|
||||||
short oldPointerCount = StructPointer.ptrCount(WirePointer.structPointer(resolved.ref));
|
short oldPointerCount = StructPointer.ptrCount(resolved.ref);
|
||||||
int oldPointerSectionOffset = resolved.ptr + oldDataSize;
|
int oldPointerSectionOffset = resolved.ptr + oldDataSize;
|
||||||
|
|
||||||
if (oldDataSize < size.data || oldPointerCount < size.pointers) {
|
if (oldDataSize < size.data || oldPointerCount < size.pointers) {
|
||||||
|
@ -206,7 +206,7 @@ final class WireHelpers {
|
||||||
throw new DecodeException("Called getList{Field,Element}() but existing pointer is not a list");
|
throw new DecodeException("Called getList{Field,Element}() but existing pointer is not a list");
|
||||||
}
|
}
|
||||||
|
|
||||||
byte oldSize = ListPointer.elementSize(WirePointer.listPointer(resolved.ref));
|
byte oldSize = ListPointer.elementSize(resolved.ref);
|
||||||
|
|
||||||
if (oldSize == FieldSize.INLINE_COMPOSITE) {
|
if (oldSize == FieldSize.INLINE_COMPOSITE) {
|
||||||
//# The existing element size is InlineComposite, which
|
//# The existing element size is InlineComposite, which
|
||||||
|
@ -232,7 +232,7 @@ final class WireHelpers {
|
||||||
int step = dataSize + pointerCount * Constants.BITS_PER_POINTER;
|
int step = dataSize + pointerCount * Constants.BITS_PER_POINTER;
|
||||||
|
|
||||||
return new ListBuilder(resolved.segment, resolved.ptr * Constants.BYTES_PER_WORD,
|
return new ListBuilder(resolved.segment, resolved.ptr * Constants.BYTES_PER_WORD,
|
||||||
ListPointer.elementCount(WirePointer.listPointer(resolved.ref)),
|
ListPointer.elementCount(resolved.ref),
|
||||||
step, dataSize, (short) pointerCount);
|
step, dataSize, (short) pointerCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -281,7 +281,7 @@ final class WireHelpers {
|
||||||
if (WirePointer.kind(resolved.ref) != WirePointer.LIST) {
|
if (WirePointer.kind(resolved.ref) != WirePointer.LIST) {
|
||||||
throw new DecodeException("Called getText{Field,Element} but existing pointer is not a list.");
|
throw new DecodeException("Called getText{Field,Element} but existing pointer is not a list.");
|
||||||
}
|
}
|
||||||
if (ListPointer.elementSize(WirePointer.listPointer(resolved.ref)) != FieldSize.BYTE) {
|
if (ListPointer.elementSize(resolved.ref) != FieldSize.BYTE) {
|
||||||
throw new DecodeException(
|
throw new DecodeException(
|
||||||
"Called getText{Field,Element} but existing list pointer is not byte-sized.");
|
"Called getText{Field,Element} but existing list pointer is not byte-sized.");
|
||||||
}
|
}
|
||||||
|
@ -289,7 +289,7 @@ final class WireHelpers {
|
||||||
|
|
||||||
//# Subtract 1 from the size for the NUL terminator.
|
//# Subtract 1 from the size for the NUL terminator.
|
||||||
return new Text.Builder(resolved.segment.buffer, resolved.ptr * Constants.BYTES_PER_WORD,
|
return new Text.Builder(resolved.segment.buffer, resolved.ptr * Constants.BYTES_PER_WORD,
|
||||||
ListPointer.elementCount(WirePointer.listPointer(resolved.ref)) - 1);
|
ListPointer.elementCount(resolved.ref) - 1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -305,14 +305,13 @@ final class WireHelpers {
|
||||||
|
|
||||||
long ref = WirePointer.get(segment.buffer, refOffset);
|
long ref = WirePointer.get(segment.buffer, refOffset);
|
||||||
int ptrOffset = WirePointer.target(refOffset, ref);
|
int ptrOffset = WirePointer.target(refOffset, ref);
|
||||||
int structPtr = WirePointer.structPointer(ref);
|
int dataSizeWords = StructPointer.dataSize(ref);
|
||||||
int dataSizeWords = StructPointer.dataSize(structPtr);
|
|
||||||
|
|
||||||
return new StructReader(segment,
|
return new StructReader(segment,
|
||||||
ptrOffset * 8,
|
ptrOffset * 8,
|
||||||
(ptrOffset + dataSizeWords),
|
(ptrOffset + dataSizeWords),
|
||||||
dataSizeWords * 64,
|
dataSizeWords * 64,
|
||||||
StructPointer.ptrCount(structPtr),
|
StructPointer.ptrCount(ref),
|
||||||
(byte)0,
|
(byte)0,
|
||||||
nestingLimit - 1);
|
nestingLimit - 1);
|
||||||
|
|
||||||
|
@ -331,14 +330,13 @@ final class WireHelpers {
|
||||||
return new ListReader();
|
return new ListReader();
|
||||||
}
|
}
|
||||||
|
|
||||||
int listPtr = WirePointer.listPointer(ref);
|
|
||||||
|
|
||||||
int ptrOffset = WirePointer.target(refOffset, ref);
|
int ptrOffset = WirePointer.target(refOffset, ref);
|
||||||
long ptr = WirePointer.get(segment.buffer, ptrOffset);
|
long ptr = WirePointer.get(segment.buffer, ptrOffset);
|
||||||
|
|
||||||
switch (ListPointer.elementSize(listPtr)) {
|
switch (ListPointer.elementSize(ref)) {
|
||||||
case FieldSize.INLINE_COMPOSITE : {
|
case FieldSize.INLINE_COMPOSITE : {
|
||||||
int wordCount = ListPointer.inlineCompositeWordCount(listPtr);
|
int wordCount = ListPointer.inlineCompositeWordCount(ref);
|
||||||
|
|
||||||
long tag = ptr;
|
long tag = ptr;
|
||||||
ptrOffset += 1;
|
ptrOffset += 1;
|
||||||
|
@ -347,8 +345,7 @@ final class WireHelpers {
|
||||||
|
|
||||||
int size = WirePointer.inlineCompositeListElementCount(tag);
|
int size = WirePointer.inlineCompositeListElementCount(tag);
|
||||||
|
|
||||||
int structPtr = WirePointer.structPointer(tag);
|
int wordsPerElement = StructPointer.wordSize(tag);
|
||||||
int wordsPerElement = StructPointer.wordSize(structPtr);
|
|
||||||
|
|
||||||
// TODO check that elemements do not overrun word count
|
// TODO check that elemements do not overrun word count
|
||||||
|
|
||||||
|
@ -358,8 +355,8 @@ final class WireHelpers {
|
||||||
ptrOffset * 8, //
|
ptrOffset * 8, //
|
||||||
size,
|
size,
|
||||||
wordsPerElement * 64,
|
wordsPerElement * 64,
|
||||||
StructPointer.dataSize(structPtr) * 64,
|
StructPointer.dataSize(tag) * 64,
|
||||||
StructPointer.ptrCount(structPtr),
|
StructPointer.ptrCount(tag),
|
||||||
nestingLimit - 1);
|
nestingLimit - 1);
|
||||||
}
|
}
|
||||||
case FieldSize.VOID : break;
|
case FieldSize.VOID : break;
|
||||||
|
@ -380,14 +377,13 @@ final class WireHelpers {
|
||||||
}
|
}
|
||||||
|
|
||||||
int ptrOffset = WirePointer.target(refOffset, ref);
|
int ptrOffset = WirePointer.target(refOffset, ref);
|
||||||
int listPtr = WirePointer.listPointer(ref);
|
int size = ListPointer.elementCount(ref);
|
||||||
int size = ListPointer.elementCount(listPtr);
|
|
||||||
|
|
||||||
if (WirePointer.kind(ref) != WirePointer.LIST) {
|
if (WirePointer.kind(ref) != WirePointer.LIST) {
|
||||||
throw new DecodeException("Message contains non-list pointer where text was expected.");
|
throw new DecodeException("Message contains non-list pointer where text was expected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ListPointer.elementSize(listPtr) != FieldSize.BYTE) {
|
if (ListPointer.elementSize(ref) != FieldSize.BYTE) {
|
||||||
throw new DecodeException("Message contains list pointer of non-bytes where text was expected.");
|
throw new DecodeException("Message contains list pointer of non-bytes where text was expected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,14 +44,6 @@ final class WirePointer {
|
||||||
return (int)(wirePointer >>> 32);
|
return (int)(wirePointer >>> 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int listPointer(long wirePointer) {
|
|
||||||
return upper32Bits(wirePointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int structPointer(long wirePointer) {
|
|
||||||
return upper32Bits(wirePointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static long get(ByteBuffer buffer, int offset) {
|
public static long get(ByteBuffer buffer, int offset) {
|
||||||
return buffer.getLong(offset * 8);
|
return buffer.getLong(offset * 8);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue