2014-10-08 20:20:15 +00:00
|
|
|
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
|
|
|
// Licensed under the MIT License:
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
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;
|
2019-03-17 23:39:54 +00:00
|
|
|
private Allocator allocator;
|
2014-06-15 18:17:32 +00:00
|
|
|
|
|
|
|
public BuilderArena(int firstSegmentSizeWords, AllocationStrategy allocationStrategy) {
|
2014-06-15 20:38:26 +00:00
|
|
|
this.segments = new ArrayList<SegmentBuilder>();
|
2019-03-17 23:39:54 +00:00
|
|
|
{
|
|
|
|
DefaultAllocator allocator = new DefaultAllocator(allocationStrategy);
|
|
|
|
allocator.setNextAllocationSizeBytes(firstSegmentSizeWords * Constants.BYTES_PER_WORD);
|
|
|
|
this.allocator = allocator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public BuilderArena(Allocator allocator) {
|
|
|
|
this.segments = new ArrayList<SegmentBuilder>();
|
|
|
|
this.allocator = allocator;
|
2014-05-24 14:12:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 12:49:10 +00:00
|
|
|
@Override
|
2014-10-08 19:16:17 +00:00
|
|
|
public final 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
|
|
|
}
|
2014-10-08 19:16:17 +00:00
|
|
|
public final SegmentBuilder getSegment(int id) {
|
2014-06-16 23:46:33 +00:00
|
|
|
return this.segments.get(id);
|
2014-05-24 14:12:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 12:49:10 +00:00
|
|
|
@Override
|
2014-10-08 19:16:17 +00:00
|
|
|
public final void checkReadLimit(int numBytes) { }
|
|
|
|
|
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();
|
|
|
|
|
2019-03-17 23:39:54 +00:00
|
|
|
// we allocate the first segment in the constructor.
|
|
|
|
if (len > 0) {
|
|
|
|
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-15 20:38:26 +00:00
|
|
|
}
|
|
|
|
SegmentBuilder newSegment = new SegmentBuilder(
|
2019-03-17 23:39:54 +00:00
|
|
|
this.allocator.allocateSegment(amount * Constants.BYTES_PER_WORD),
|
2014-06-15 20:38:26 +00:00
|
|
|
this);
|
2014-06-16 18:49:12 +00:00
|
|
|
|
2014-06-15 20:38:26 +00:00
|
|
|
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
|
|
|
|
2014-05-24 18:33:25 +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);
|
2014-09-04 15:41:37 +00:00
|
|
|
segment.buffer.rewind();
|
2014-05-24 18:33:25 +00:00
|
|
|
ByteBuffer slice = segment.buffer.slice();
|
2014-06-30 14:30:04 +00:00
|
|
|
slice.limit(segment.currentSize() * Constants.BYTES_PER_WORD);
|
2015-03-03 15:46:42 +00:00
|
|
|
slice.order(ByteOrder.LITTLE_ENDIAN);
|
2014-05-24 18:33:25 +00:00
|
|
|
result[ii] = slice;
|
|
|
|
}
|
|
|
|
return result;
|
2014-05-24 14:45:55 +00:00
|
|
|
}
|
2014-05-24 14:12:44 +00:00
|
|
|
}
|