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

46 lines
1.1 KiB
Java
Raw Normal View History

package org.capnproto;
import java.nio.ByteBuffer;
public final class SegmentBuilder extends SegmentReader {
2014-06-15 20:38:26 +00:00
public static final int FAILED_ALLOCATION = -1;
public int pos = 0; // in words
2014-06-16 18:49:12 +00:00
public int id = 0;
2014-06-15 20:38:26 +00:00
public SegmentBuilder(ByteBuffer buf, Arena arena) {
super(buf, arena);
}
// the total number of words the buffer can hold
private final int capacity() {
this.buffer.reset();
return this.buffer.remaining() / 8;
}
// return how many words have already been allocated
public final int currentSize() {
return this.pos;
}
2014-06-15 20:38:26 +00:00
/*
2014-05-24 14:12:44 +00:00
Allocate `amount` words.
*/
public final int allocate(int amount) {
2014-10-02 16:23:29 +00:00
assert amount >= 0 : "tried to allocate a negative number of words";
2014-06-15 20:38:26 +00:00
if (amount > this.capacity() - this.currentSize()) {
return FAILED_ALLOCATION; // no space left;
} else {
int result = this.pos;
this.pos += amount;
return result;
}
}
2014-06-15 20:38:26 +00:00
public final BuilderArena getArena() {
return (BuilderArena)this.arena;
}
}