2020-08-28 14:02:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2020 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
2020-10-28 14:38:14 +01:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2020-08-28 14:02:51 +02:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2020-07-24 19:38:48 +02:00
|
|
|
package io.netty.buffer.b2;
|
|
|
|
|
2020-10-06 15:54:48 +02:00
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
2020-08-24 16:03:02 +02:00
|
|
|
/**
|
2020-08-24 17:13:04 +02:00
|
|
|
* Interface for {@link Buf} allocators.
|
2020-08-24 16:03:02 +02:00
|
|
|
*/
|
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
|
|
|
/**
|
2020-08-24 17:13:04 +02:00
|
|
|
* Allocate a {@link Buf} 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 Buf} of the requested size.
|
2020-10-06 15:54:48 +02:00
|
|
|
* <p>
|
|
|
|
* The buffer will use the current platform native byte order by default, for accessor methods that don't have an
|
|
|
|
* explicit byte order.
|
2020-08-24 16:03:02 +02:00
|
|
|
*
|
2020-08-24 17:13:04 +02:00
|
|
|
* @param size The size of {@link Buf} to allocate.
|
|
|
|
* @return The newly allocated {@link Buf}.
|
2020-08-24 16:03:02 +02:00
|
|
|
*/
|
2020-08-28 12:17:41 +02:00
|
|
|
Buf allocate(long size);
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-10-06 15:54:48 +02:00
|
|
|
/**
|
|
|
|
* Allocate a {@link Buf} 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 Buf} of the requested size.
|
|
|
|
* <p>
|
|
|
|
* The buffer will use the given byte order by default.
|
|
|
|
*
|
|
|
|
* @param size The size of {@link Buf} to allocate.
|
|
|
|
* @param order The default byte order used by the accessor methods that don't have an explicit byte order.
|
|
|
|
* @return The newly allocated {@link Buf}.
|
|
|
|
*/
|
|
|
|
default Buf allocate(long size, ByteOrder order) {
|
|
|
|
return allocate(size).order(order);
|
|
|
|
}
|
|
|
|
|
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() {
|
2020-10-07 11:07:34 +02:00
|
|
|
var man = MemoryManager.getHeapMemoryManager();
|
2020-07-24 19:38:48 +02:00
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
2020-10-07 11:07:34 +02:00
|
|
|
public Buf allocate(long size) {
|
2020-08-17 16:09:38 +02:00
|
|
|
checkSize(size);
|
2020-10-28 14:38:14 +01:00
|
|
|
return man.allocateConfined(size, man.drop(), null);
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Allocator direct() {
|
2020-10-07 11:07:34 +02:00
|
|
|
var man = MemoryManager.getNativeMemoryManager();
|
2020-07-24 19:38:48 +02:00
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
2020-08-24 17:13:04 +02:00
|
|
|
public Buf allocate(long size) {
|
2020-08-17 16:09:38 +02:00
|
|
|
checkSize(size);
|
2020-10-28 14:38:14 +01:00
|
|
|
return man.allocateConfined(size, man.drop(), null);
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-25 16:56:03 +02:00
|
|
|
static Allocator directWithCleaner() {
|
2020-10-07 11:07:34 +02:00
|
|
|
var man = MemoryManager.getNativeMemoryManager();
|
2020-09-25 16:56:03 +02:00
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
|
|
|
public Buf allocate(long size) {
|
|
|
|
checkSize(size);
|
2020-10-28 14:38:14 +01:00
|
|
|
return man.allocateConfined(size, man.drop(), Statics.CLEANER);
|
2020-09-25 16:56:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-24 19:38:48 +02:00
|
|
|
static Allocator pooledHeap() {
|
2020-10-07 11:07:34 +02:00
|
|
|
return new SizeClassedMemoryPool(MemoryManager.getHeapMemoryManager());
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Allocator pooledDirect() {
|
2020-10-07 11:07:34 +02:00
|
|
|
return new SizeClassedMemoryPool(MemoryManager.getNativeMemoryManager());
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
2020-08-10 12:15:02 +02:00
|
|
|
|
|
|
|
static Allocator pooledDirectWithCleaner() {
|
2020-10-07 11:07:34 +02:00
|
|
|
return new SizeClassedMemoryPool(MemoryManager.getNativeMemoryManager()) {
|
2020-08-10 12:15:02 +02:00
|
|
|
@Override
|
2020-10-07 11:07:34 +02:00
|
|
|
protected Drop<Buf> getDrop() {
|
|
|
|
return new NativeMemoryCleanerDrop(this, getMemoryManager(), super.getDrop());
|
2020-08-10 12:15:02 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|