diff --git a/runtime/src/main/java/org/capnproto/BuilderArena.java b/runtime/src/main/java/org/capnproto/BuilderArena.java index bcd5cf5..cb0da8a 100644 --- a/runtime/src/main/java/org/capnproto/BuilderArena.java +++ b/runtime/src/main/java/org/capnproto/BuilderArena.java @@ -6,12 +6,28 @@ import java.util.Vector; public final class BuilderArena implements Arena { + 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; + // Maybe this should be ArrayList? public final Vector segments; - public BuilderArena() { + public int nextSize; + public final AllocationStrategy allocationStrategy; + + + public BuilderArena(int firstSegmentSizeWords, AllocationStrategy allocationStrategy) { this.segments = new Vector(); - SegmentBuilder segment0 = new SegmentBuilder(ByteBuffer.allocate(1024 * 8)); + this.nextSize = firstSegmentSizeWords; + this.allocationStrategy = allocationStrategy; + SegmentBuilder segment0 = new SegmentBuilder( + ByteBuffer.allocate(firstSegmentSizeWords * Constants.BYTES_PER_WORD)); segment0.buffer.mark(); segment0.buffer.order(ByteOrder.LITTLE_ENDIAN); this.segments.add(segment0); diff --git a/runtime/src/main/java/org/capnproto/MessageBuilder.java b/runtime/src/main/java/org/capnproto/MessageBuilder.java index fa9f172..4f4f3bd 100644 --- a/runtime/src/main/java/org/capnproto/MessageBuilder.java +++ b/runtime/src/main/java/org/capnproto/MessageBuilder.java @@ -5,7 +5,18 @@ public final class MessageBuilder { private final BuilderArena arena; public MessageBuilder() { - this.arena = new BuilderArena(); + this.arena = new BuilderArena(BuilderArena.SUGGESTED_FIRST_SEGMENT_WORDS, + BuilderArena.SUGGESTED_ALLOCATION_STRATEGY); + } + + public MessageBuilder(int firstSegmentWords) { + this.arena = new BuilderArena(firstSegmentWords, + BuilderArena.SUGGESTED_ALLOCATION_STRATEGY); + } + + public MessageBuilder(int firstSegmentWords, BuilderArena.AllocationStrategy allocationStrategy) { + this.arena = new BuilderArena(firstSegmentWords, + allocationStrategy); } public T getRoot(FromStructBuilder factory) {