Simplify recovery code for memory segment based buffers

This commit is contained in:
Chris Vest 2021-03-19 17:43:05 +01:00
parent 824d3e0137
commit 0ed5575fb4
2 changed files with 6 additions and 25 deletions

View File

@ -19,7 +19,7 @@ import io.netty.buffer.api.AllocatorControl;
import io.netty.buffer.api.Buffer;
import io.netty.buffer.api.Drop;
import io.netty.buffer.api.MemoryManager;
import io.netty.buffer.api.memseg.MemSegBuffer.RecoverableMemory;
import io.netty.buffer.api.internal.ArcDrop;
import jdk.incubator.foreign.MemorySegment;
import java.lang.ref.Cleaner;
@ -63,12 +63,12 @@ public abstract class AbstractMemorySegmentManager implements MemoryManager {
@Override
public int capacityOfRecoverableMemory(Object memory) {
return ((RecoverableMemory) memory).capacity();
return (int) ((MemorySegment) memory).byteSize();
}
@Override
public Buffer recoverMemory(AllocatorControl allocatorControl, Object recoverableMemory, Drop<Buffer> drop) {
var recovery = (RecoverableMemory) recoverableMemory; // TODO get rid of RecoverableMemory
return recovery.recover(convert(drop));
var segment = (MemorySegment) recoverableMemory;
return new MemSegBuffer(segment, segment, convert(ArcDrop.acquire(drop)), allocatorControl);
}
}

View File

@ -508,8 +508,7 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
// Allocate a bigger buffer.
long newSize = capacity() + size - (long) writableBytes();
BufferAllocator.checkSize(newSize);
RecoverableMemory recoverableMemory = (RecoverableMemory) alloc.allocateUntethered(this, (int) newSize);
var newSegment = recoverableMemory.segment;
MemorySegment newSegment = (MemorySegment) alloc.allocateUntethered(this, (int) newSize);
// Copy contents.
newSegment.copyFrom(seg);
@ -1180,7 +1179,7 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
}
Object recoverableMemory() {
return new RecoverableMemory(base, alloc);
return base;
}
// <editor-fold name="BufferIntegratable methods">
@ -1237,22 +1236,4 @@ class MemSegBuffer extends RcSupport<Buffer, MemSegBuffer> implements Buffer, Re
return !isAccessible();
}
// </editor-fold>
static final class RecoverableMemory {
private final MemorySegment segment;
private final AllocatorControl alloc;
RecoverableMemory(MemorySegment segment, AllocatorControl alloc) {
this.segment = segment;
this.alloc = alloc;
}
Buffer recover(Drop<MemSegBuffer> drop) {
return new MemSegBuffer(segment, segment, ArcDrop.acquire(drop), alloc);
}
int capacity() {
return (int) segment.byteSize();
}
}
}