}
if (align <= 0) {
throw new IllegalArgumentException("Align must be >= 1");
}
Offset offset = Offset.zero();
final Word alignMask = Word.fromIntZeroExtend(align - 1);
while (true) {
final Address addr = this.start.add(offset);
final MemoryResourceImpl child = new MemoryResourceImpl(this, getOwner(), addr, size);
final MemoryResourceImpl existingChild = (MemoryResourceImpl) get(this.children, child);
if (existingChild == null) {
// We found a free region
this.children = (MemoryResourceImpl) add(this.children, child);
return child;
}
// We found an existing child, skip over that.
offset = existingChild.getOffset().add(existingChild.getSize());
// Align the new offset
if (!offset.toWord().and(alignMask).isZero()) {
offset = offset.toWord().add(alignMask).and(alignMask.not()).toOffset();
}
// Do we have space left?
if (offset.toWord().add(size).GT(this.size.toWord())) {
throw new ResourceNotFreeException();
}
}
}