From 5ee4fe295c9b0d2817ff1566a2619a8ab80a6848 Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Sun, 11 May 2014 20:55:33 -0400 Subject: [PATCH] working WirePointer static methods --- src/capnp/ListPointer.java | 14 +++++++++++- src/capnp/PointerReader.java | 3 +-- src/capnp/StructPointer.java | 14 +++++++++++- src/capnp/Text.java | 6 ++--- src/capnp/WireHelpers.java | 12 +++++----- src/capnp/WirePointer.java | 43 +++++++++++++++++++++++++++++++++++- 6 files changed, 79 insertions(+), 13 deletions(-) diff --git a/src/capnp/ListPointer.java b/src/capnp/ListPointer.java index 37c984e..6427476 100644 --- a/src/capnp/ListPointer.java +++ b/src/capnp/ListPointer.java @@ -2,7 +2,7 @@ package org.capnproto; import java.nio.ByteBuffer; -class ListPointer { +final class ListPointer { public WirePointer ptr; public ListPointer(WirePointer ptr) { @@ -13,11 +13,23 @@ class ListPointer { 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); + } } diff --git a/src/capnp/PointerReader.java b/src/capnp/PointerReader.java index 9083360..4814b1c 100644 --- a/src/capnp/PointerReader.java +++ b/src/capnp/PointerReader.java @@ -46,7 +46,6 @@ public class PointerReader { public Text.Reader getText() { return WireHelpers.readTextPointer(this.segment, - new WirePointer(this.segment.ptr, - this.pointer)); + this.pointer); } } diff --git a/src/capnp/StructPointer.java b/src/capnp/StructPointer.java index 26c8b7f..5902f24 100644 --- a/src/capnp/StructPointer.java +++ b/src/capnp/StructPointer.java @@ -2,7 +2,7 @@ package org.capnproto; import java.nio.ByteBuffer; -class StructPointer{ +final class StructPointer{ public WirePointer ptr; public StructPointer(WirePointer ptr) { @@ -13,11 +13,23 @@ class StructPointer{ 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); + } } diff --git a/src/capnp/Text.java b/src/capnp/Text.java index 00a14c0..f577d87 100644 --- a/src/capnp/Text.java +++ b/src/capnp/Text.java @@ -9,9 +9,9 @@ public class Text { public final int offset; // in bytes public final int size; // in bytes - public Reader(WordPointer ptr, int size) { - this.buffer = ptr.buffer; - this.offset = ptr.offset * 8; + public Reader(ByteBuffer buffer, int offset, int size) { + this.buffer = buffer; + this.offset = offset * 8; this.size = size; } diff --git a/src/capnp/WireHelpers.java b/src/capnp/WireHelpers.java index fd95341..ff989c8 100644 --- a/src/capnp/WireHelpers.java +++ b/src/capnp/WireHelpers.java @@ -72,10 +72,12 @@ class WireHelpers { } public static Text.Reader readTextPointer(SegmentReader segment, - WirePointer ref) { - WordPointer ptr = ref.target(); - ListPointer listPtr = new ListPointer(ref); - int size = listPtr.elementCount(); - return new Text.Reader(ptr, size); + 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); } } diff --git a/src/capnp/WirePointer.java b/src/capnp/WirePointer.java index 5437759..2b4b58a 100644 --- a/src/capnp/WirePointer.java +++ b/src/capnp/WirePointer.java @@ -2,7 +2,7 @@ package org.capnproto; import java.nio.ByteBuffer; -class WirePointer { +final class WirePointer { public final ByteBuffer buffer; public final int buffer_offset; // in words @@ -25,20 +25,61 @@ class WirePointer { 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); + } + + 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) { + return buffer.getLong(offset * 8); + } }