From 58417744ff4ed5feea95ab3372985bed220ff996 Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Mon, 16 Jun 2014 20:00:01 -0400 Subject: [PATCH] simplify List and Struct Pointer getters --- .../main/java/org/capnproto/ListPointer.java | 12 +++---- .../java/org/capnproto/StructPointer.java | 12 +++---- .../main/java/org/capnproto/WireHelpers.java | 34 ++++++++----------- .../main/java/org/capnproto/WirePointer.java | 8 ----- 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/runtime/src/main/java/org/capnproto/ListPointer.java b/runtime/src/main/java/org/capnproto/ListPointer.java index 22f4dde..5ee2767 100644 --- a/runtime/src/main/java/org/capnproto/ListPointer.java +++ b/runtime/src/main/java/org/capnproto/ListPointer.java @@ -3,16 +3,16 @@ package org.capnproto; import java.nio.ByteBuffer; final class ListPointer { - public static byte elementSize(int elementSizeAndCount) { - return (byte) (elementSizeAndCount & 7); + public static byte elementSize(long ref) { + return (byte) (WirePointer.upper32Bits(ref) & 7); } - public static int elementCount(int elementSizeAndCount) { - return elementSizeAndCount >>> 3; + public static int elementCount(long ref) { + return WirePointer.upper32Bits(ref) >>> 3; } - public static int inlineCompositeWordCount(int elementSizeAndCount) { - return elementCount(elementSizeAndCount); + public static int inlineCompositeWordCount(long ref) { + return elementCount(ref); } public static void set(ByteBuffer buffer, int offset, byte elementSize, int elementCount) { diff --git a/runtime/src/main/java/org/capnproto/StructPointer.java b/runtime/src/main/java/org/capnproto/StructPointer.java index 66b5f25..91c062c 100644 --- a/runtime/src/main/java/org/capnproto/StructPointer.java +++ b/runtime/src/main/java/org/capnproto/StructPointer.java @@ -3,16 +3,16 @@ package org.capnproto; import java.nio.ByteBuffer; final class StructPointer{ - public static short dataSize(int structRef) { - return (short)(structRef & 0xffff); + public static short dataSize(long ref) { + return (short)(WirePointer.upper32Bits(ref) & 0xffff); } - public static short ptrCount(int structRef) { - return (short)(structRef >>> 16); + public static short ptrCount(long ref) { + return (short)(WirePointer.upper32Bits(ref) >>> 16); } - public static int wordSize(int structRef) { - return (int)dataSize(structRef) + (int)ptrCount(structRef); + public static int wordSize(long ref) { + return (int)dataSize(ref) + (int)ptrCount(ref); } public static void setFromStructSize(ByteBuffer buffer, int offset, StructSize size) { diff --git a/runtime/src/main/java/org/capnproto/WireHelpers.java b/runtime/src/main/java/org/capnproto/WireHelpers.java index 9f4938d..0d1c576 100644 --- a/runtime/src/main/java/org/capnproto/WireHelpers.java +++ b/runtime/src/main/java/org/capnproto/WireHelpers.java @@ -116,8 +116,8 @@ final class WireHelpers { } FollowBuilderFarsResult resolved = followBuilderFars(ref, target, segment); - short oldDataSize = StructPointer.dataSize(WirePointer.structPointer(resolved.ref)); - short oldPointerCount = StructPointer.ptrCount(WirePointer.structPointer(resolved.ref)); + short oldDataSize = StructPointer.dataSize(resolved.ref); + short oldPointerCount = StructPointer.ptrCount(resolved.ref); int oldPointerSectionOffset = resolved.ptr + oldDataSize; 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"); } - byte oldSize = ListPointer.elementSize(WirePointer.listPointer(resolved.ref)); + byte oldSize = ListPointer.elementSize(resolved.ref); if (oldSize == FieldSize.INLINE_COMPOSITE) { //# The existing element size is InlineComposite, which @@ -232,7 +232,7 @@ final class WireHelpers { int step = dataSize + pointerCount * Constants.BITS_PER_POINTER; 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); } } @@ -281,7 +281,7 @@ final class WireHelpers { if (WirePointer.kind(resolved.ref) != WirePointer.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( "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. 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); int ptrOffset = WirePointer.target(refOffset, ref); - int structPtr = WirePointer.structPointer(ref); - int dataSizeWords = StructPointer.dataSize(structPtr); + int dataSizeWords = StructPointer.dataSize(ref); return new StructReader(segment, ptrOffset * 8, (ptrOffset + dataSizeWords), dataSizeWords * 64, - StructPointer.ptrCount(structPtr), + StructPointer.ptrCount(ref), (byte)0, nestingLimit - 1); @@ -331,14 +330,13 @@ final class WireHelpers { return new ListReader(); } - int listPtr = WirePointer.listPointer(ref); int ptrOffset = WirePointer.target(refOffset, ref); long ptr = WirePointer.get(segment.buffer, ptrOffset); - switch (ListPointer.elementSize(listPtr)) { + switch (ListPointer.elementSize(ref)) { case FieldSize.INLINE_COMPOSITE : { - int wordCount = ListPointer.inlineCompositeWordCount(listPtr); + int wordCount = ListPointer.inlineCompositeWordCount(ref); long tag = ptr; ptrOffset += 1; @@ -347,8 +345,7 @@ final class WireHelpers { int size = WirePointer.inlineCompositeListElementCount(tag); - int structPtr = WirePointer.structPointer(tag); - int wordsPerElement = StructPointer.wordSize(structPtr); + int wordsPerElement = StructPointer.wordSize(tag); // TODO check that elemements do not overrun word count @@ -358,8 +355,8 @@ final class WireHelpers { ptrOffset * 8, // size, wordsPerElement * 64, - StructPointer.dataSize(structPtr) * 64, - StructPointer.ptrCount(structPtr), + StructPointer.dataSize(tag) * 64, + StructPointer.ptrCount(tag), nestingLimit - 1); } case FieldSize.VOID : break; @@ -380,14 +377,13 @@ final class WireHelpers { } int ptrOffset = WirePointer.target(refOffset, ref); - int listPtr = WirePointer.listPointer(ref); - int size = ListPointer.elementCount(listPtr); + int size = ListPointer.elementCount(ref); if (WirePointer.kind(ref) != WirePointer.LIST) { 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."); } diff --git a/runtime/src/main/java/org/capnproto/WirePointer.java b/runtime/src/main/java/org/capnproto/WirePointer.java index 47d8a5d..7dd2757 100644 --- a/runtime/src/main/java/org/capnproto/WirePointer.java +++ b/runtime/src/main/java/org/capnproto/WirePointer.java @@ -44,14 +44,6 @@ final class 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); }