From 0d99987eff493e5f27890b7e340562239cd00786 Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Wed, 14 May 2014 20:51:26 -0400 Subject: [PATCH] SegmentBuilder.currentSize() and capacity() --- src/capnp/InputStreamMessageReader.java | 3 +++ src/capnp/SegmentBuilder.java | 22 ++++++++++++++++++---- src/capnp/WireHelpers.java | 13 ++++++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/capnp/InputStreamMessageReader.java b/src/capnp/InputStreamMessageReader.java index ff3d3f6..1d8f796 100644 --- a/src/capnp/InputStreamMessageReader.java +++ b/src/capnp/InputStreamMessageReader.java @@ -27,6 +27,7 @@ public class InputStreamMessageReader { static ByteBuffer makeByteBuffer(byte[] bytes) { ByteBuffer result = ByteBuffer.wrap(bytes); result.order(ByteOrder.LITTLE_ENDIAN); + result.mark(); return result; } @@ -66,12 +67,14 @@ public class InputStreamMessageReader { segmentSlices[0] = ByteBuffer.wrap(allSegments, 0, segment0Size * 8); segmentSlices[0].order(ByteOrder.LITTLE_ENDIAN); + segmentSlices[0].mark(); int offset = segment0Size; for (int ii = 1; ii < segmentCount; ++ii) { segmentSlices[ii] = ByteBuffer.wrap(allSegments, offset * 8, moreSizes.get(ii - 1) * 8); segmentSlices[ii].order(ByteOrder.LITTLE_ENDIAN); + segmentSlices[ii].mark(); offset += moreSizes.get(ii - 1); } diff --git a/src/capnp/SegmentBuilder.java b/src/capnp/SegmentBuilder.java index db4c7e4..5854857 100644 --- a/src/capnp/SegmentBuilder.java +++ b/src/capnp/SegmentBuilder.java @@ -3,18 +3,32 @@ package org.capnproto; import java.nio.ByteBuffer; public class SegmentBuilder extends SegmentReader { - public int pos = 0; + public int pos = 0; // in words + + public static final int FAILED_ALLOCATION = -1; public SegmentBuilder(ByteBuffer buf) { super(buf); } + // the total number of words the buffer can hold + private final int capacity() { + this.buffer.reset(); + return (this.buffer.limit() - this.buffer.position()) / 8; + } + + // return how many words have already been allocated private final int currentSize() { - throw new Error("unimplemented"); + return this.pos; } public final int allocate(int amount) { - throw new Error("unimplemented"); - // if (amount > ... this.currentSize() + if (amount > this.capacity() - this.currentSize()) { + return FAILED_ALLOCATION; // no space left; + } else { + int result = this.pos; + this.pos += amount; + return result; + } } } diff --git a/src/capnp/WireHelpers.java b/src/capnp/WireHelpers.java index 02f85e2..6fb2ddd 100644 --- a/src/capnp/WireHelpers.java +++ b/src/capnp/WireHelpers.java @@ -6,7 +6,18 @@ final class WireHelpers { SegmentBuilder segment, int amount, byte kind) { - throw new Error("unimplemented"); + + // TODO check for nullness, amount == 0 case. + + int allocation = segment.allocate(amount); + if (allocation == SegmentBuilder.FAILED_ALLOCATION) { + //# Need to allocate in a new segment. We'll need to + //# allocate an extra pointer worth of space to act as + //# the landing pad for a far pointer. + throw new Error("unimplemented"); + } else { + throw new Error("unimplemented"); + } } public static ListBuilder initListPointer(int refOffset,