capnproto-java-rpc/runtime/src/main/java/org/capnproto/BuilderArena.java

101 lines
3 KiB
Java
Raw Normal View History

2014-05-24 14:12:44 +00:00
package org.capnproto;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
2014-06-15 20:38:26 +00:00
import java.util.ArrayList;
2014-05-24 14:12:44 +00:00
public final class BuilderArena implements Arena {
2014-06-15 18:17:32 +00:00
public enum AllocationStrategy {
FIXED_SIZE,
GROW_HEURISTICALLY
}
public static final int SUGGESTED_FIRST_SEGMENT_WORDS = 1024;
public static final AllocationStrategy SUGGESTED_ALLOCATION_STRATEGY =
AllocationStrategy.GROW_HEURISTICALLY;
2014-06-15 20:38:26 +00:00
public final ArrayList<SegmentBuilder> segments;
2014-05-24 14:12:44 +00:00
2014-06-15 18:17:32 +00:00
public int nextSize;
public final AllocationStrategy allocationStrategy;
public BuilderArena(int firstSegmentSizeWords, AllocationStrategy allocationStrategy) {
2014-06-15 20:38:26 +00:00
this.segments = new ArrayList<SegmentBuilder>();
2014-06-15 18:17:32 +00:00
this.nextSize = firstSegmentSizeWords;
this.allocationStrategy = allocationStrategy;
SegmentBuilder segment0 = new SegmentBuilder(
2014-06-15 20:38:26 +00:00
ByteBuffer.allocate(firstSegmentSizeWords * Constants.BYTES_PER_WORD), this);
2014-05-24 14:12:44 +00:00
segment0.buffer.mark();
2014-05-24 18:52:27 +00:00
segment0.buffer.order(ByteOrder.LITTLE_ENDIAN);
2014-05-24 14:12:44 +00:00
this.segments.add(segment0);
}
public SegmentReader tryGetSegment(int id) {
2014-06-18 01:06:50 +00:00
return this.segments.get(id);
2014-05-24 14:12:44 +00:00
}
public SegmentBuilder getSegment(int id) {
return this.segments.get(id);
2014-05-24 14:12:44 +00:00
}
public static class AllocateResult {
public final SegmentBuilder segment;
2014-06-15 20:38:26 +00:00
// offset to the beginning the of allocated memory
2014-05-24 14:12:44 +00:00
public final int offset;
2014-06-15 20:38:26 +00:00
2014-05-24 14:12:44 +00:00
public AllocateResult(SegmentBuilder segment, int offset) {
this.segment = segment;
this.offset = offset;
}
}
public AllocateResult allocate(int amount) {
2014-06-15 20:38:26 +00:00
int len = this.segments.size();
// we allocate the first segment in the constructor.
int result = this.segments.get(len - 1).allocate(amount);
if (result != SegmentBuilder.FAILED_ALLOCATION) {
return new AllocateResult(this.segments.get(len - 1), result);
}
2014-06-16 18:49:12 +00:00
// allocate_owned_memory
int size = Math.max(amount, this.nextSize);
2014-06-15 20:38:26 +00:00
SegmentBuilder newSegment = new SegmentBuilder(
2014-06-16 18:49:12 +00:00
ByteBuffer.allocate(size * Constants.BYTES_PER_WORD),
2014-06-15 20:38:26 +00:00
this);
2014-06-16 18:49:12 +00:00
switch (this.allocationStrategy) {
case GROW_HEURISTICALLY:
this.nextSize += size;
break;
default:
break;
}
// --------
2014-06-15 20:38:26 +00:00
newSegment.buffer.mark();
newSegment.buffer.order(ByteOrder.LITTLE_ENDIAN);
2014-06-16 18:49:12 +00:00
newSegment.id = len;
2014-06-15 20:38:26 +00:00
this.segments.add(newSegment);
return new AllocateResult(newSegment, newSegment.allocate(amount));
2014-05-24 14:12:44 +00:00
}
2014-05-24 14:45:55 +00:00
public final ByteBuffer[] getSegmentsForOutput() {
ByteBuffer[] result = new ByteBuffer[this.segments.size()];
for (int ii = 0; ii < this.segments.size(); ++ii) {
SegmentBuilder segment = segments.get(ii);
segment.buffer.reset();
ByteBuffer slice = segment.buffer.slice();
slice.limit(segment.currentSize() * 8);
result[ii] = slice;
}
return result;
2014-05-24 14:45:55 +00:00
}
2014-05-24 14:12:44 +00:00
}