2020-07-24 19:38:48 +02:00
|
|
|
package io.netty.buffer.b2;
|
|
|
|
|
|
|
|
import jdk.incubator.foreign.MemorySegment;
|
|
|
|
|
2020-07-29 10:30:03 +02:00
|
|
|
import static io.netty.buffer.b2.BBuf.*;
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-08-24 16:03:02 +02:00
|
|
|
/**
|
|
|
|
* Interface for {@link BBuf} allocators.
|
|
|
|
*/
|
2020-08-17 16:09:38 +02:00
|
|
|
@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
|
2020-07-24 19:38:48 +02:00
|
|
|
public interface Allocator extends AutoCloseable {
|
2020-08-17 16:09:38 +02:00
|
|
|
static void checkSize(long size) {
|
|
|
|
if (size < 1) {
|
|
|
|
throw new IllegalArgumentException("Buffer size must be positive, but was " + size + '.');
|
|
|
|
}
|
|
|
|
// We use max array size because on-heap buffers will be backed by byte-arrays.
|
|
|
|
int maxArraySize = Integer.MAX_VALUE - 8;
|
|
|
|
if (size > maxArraySize) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"Buffer size cannot be greater than " + maxArraySize + ", but was " + size + '.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:03:02 +02:00
|
|
|
/**
|
|
|
|
* Allocate a {@link BBuf} of the given size in bytes. This method may throw an {@link OutOfMemoryError} if there is
|
|
|
|
* not enough free memory available to allocate a {@link BBuf} of the requested size.
|
|
|
|
*
|
|
|
|
* @param size The size of {@link BBuf} to allocate.
|
|
|
|
* @return The newly allocated {@link BBuf}.
|
|
|
|
*/
|
2020-07-29 10:30:03 +02:00
|
|
|
BBuf allocate(long size);
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-08-24 16:03:02 +02:00
|
|
|
/**
|
|
|
|
* Close this allocator, freeing all of its internal resources. It is not specified if the allocator can still be
|
|
|
|
* used after this method has been called on it.
|
|
|
|
*/
|
2020-07-24 19:38:48 +02:00
|
|
|
@Override
|
|
|
|
default void close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
static Allocator heap() {
|
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
2020-07-29 10:30:03 +02:00
|
|
|
public BBuf allocate(long size) {
|
2020-08-17 16:09:38 +02:00
|
|
|
checkSize(size);
|
2020-07-24 19:38:48 +02:00
|
|
|
var segment = MemorySegment.ofArray(new byte[Math.toIntExact(size)]);
|
2020-07-29 10:30:03 +02:00
|
|
|
return new BBuf(segment, SEGMENT_CLOSE);
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Allocator direct() {
|
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
2020-07-29 10:30:03 +02:00
|
|
|
public BBuf allocate(long size) {
|
2020-08-17 16:09:38 +02:00
|
|
|
checkSize(size);
|
2020-08-10 12:15:02 +02:00
|
|
|
var segment = MemorySegment.allocateNative(size);
|
|
|
|
Statics.MEM_USAGE_NATIVE.add(size);
|
|
|
|
return new BBuf(segment, SEGMENT_CLOSE_NATIVE);
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Allocator pooledHeap() {
|
2020-08-10 12:15:02 +02:00
|
|
|
return new SizeClassedMemoryPool(false) {
|
2020-07-24 19:38:48 +02:00
|
|
|
@Override
|
|
|
|
protected MemorySegment createMemorySegment(long size) {
|
2020-08-17 16:09:38 +02:00
|
|
|
checkSize(size);
|
2020-07-24 19:38:48 +02:00
|
|
|
return MemorySegment.ofArray(new byte[Math.toIntExact(size)]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Allocator pooledDirect() {
|
2020-08-10 12:15:02 +02:00
|
|
|
return new SizeClassedMemoryPool(true) {
|
2020-07-24 19:38:48 +02:00
|
|
|
@Override
|
|
|
|
protected MemorySegment createMemorySegment(long size) {
|
2020-08-17 16:09:38 +02:00
|
|
|
checkSize(size);
|
2020-07-24 19:38:48 +02:00
|
|
|
return MemorySegment.allocateNative(size);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-08-10 12:15:02 +02:00
|
|
|
|
|
|
|
static Allocator pooledDirectWithCleaner() {
|
|
|
|
return new SizeClassedMemoryPool(true) {
|
|
|
|
@Override
|
|
|
|
protected MemorySegment createMemorySegment(long size) {
|
2020-08-17 16:09:38 +02:00
|
|
|
checkSize(size);
|
2020-08-10 12:15:02 +02:00
|
|
|
return MemorySegment.allocateNative(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected BBuf createBBuf(MemorySegment segment) {
|
|
|
|
var drop = new NativeMemoryCleanerDrop();
|
|
|
|
var buf = new BBuf(segment, drop);
|
|
|
|
drop.accept(buf);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|