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-11-17 15:38:11 +01:00
|
|
|
package io.netty.buffer.api;
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-10-06 15:54:48 +02:00
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
2020-08-24 16:03:02 +02:00
|
|
|
/**
|
2021-02-12 18:22:07 +01:00
|
|
|
* Interface for {@link Buffer} allocators.
|
2020-08-24 16:03:02 +02:00
|
|
|
*/
|
2021-02-12 18:22:07 +01:00
|
|
|
public interface BufferAllocator extends AutoCloseable {
|
2021-02-15 17:46:23 +01:00
|
|
|
/**
|
|
|
|
* Check that the given {@code size} argument is a valid buffer size, or throw an {@link IllegalArgumentException}.
|
|
|
|
*
|
|
|
|
* @param size The size to check.
|
|
|
|
* @throws IllegalArgumentException if the size is not possitive, or if the size is too big (over ~2 GB) for a
|
|
|
|
* buffer to accomodate.
|
|
|
|
*/
|
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
|
|
|
/**
|
2021-02-12 18:22:07 +01:00
|
|
|
* Allocate a {@link Buffer} 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 Buffer} 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
|
|
|
*
|
2021-02-12 18:22:07 +01:00
|
|
|
* @param size The size of {@link Buffer} to allocate.
|
|
|
|
* @return The newly allocated {@link Buffer}.
|
2020-08-24 16:03:02 +02:00
|
|
|
*/
|
2021-02-12 18:22:07 +01:00
|
|
|
Buffer allocate(int size);
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-10-06 15:54:48 +02:00
|
|
|
/**
|
2021-02-12 18:22:07 +01:00
|
|
|
* Allocate a {@link Buffer} 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 Buffer} of the requested size.
|
2020-10-06 15:54:48 +02:00
|
|
|
* <p>
|
|
|
|
* The buffer will use the given byte order by default.
|
|
|
|
*
|
2021-02-12 18:22:07 +01:00
|
|
|
* @param size The size of {@link Buffer} to allocate.
|
2020-10-06 15:54:48 +02:00
|
|
|
* @param order The default byte order used by the accessor methods that don't have an explicit byte order.
|
2021-02-12 18:22:07 +01:00
|
|
|
* @return The newly allocated {@link Buffer}.
|
2020-10-06 15:54:48 +02:00
|
|
|
*/
|
2021-02-12 18:22:07 +01:00
|
|
|
default Buffer allocate(int size, ByteOrder order) {
|
2020-10-06 15:54:48 +02:00
|
|
|
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() {
|
|
|
|
}
|
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
static BufferAllocator heap() {
|
|
|
|
return new ManagedBufferAllocator(MemoryManager.getHeapMemoryManager(), Statics.CLEANER);
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
static BufferAllocator direct() {
|
|
|
|
return new ManagedBufferAllocator(MemoryManager.getNativeMemoryManager(), Statics.CLEANER);
|
2020-09-25 16:56:03 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
static BufferAllocator pooledHeap() {
|
2020-10-07 11:07:34 +02:00
|
|
|
return new SizeClassedMemoryPool(MemoryManager.getHeapMemoryManager());
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 18:22:07 +01:00
|
|
|
static BufferAllocator pooledDirect() {
|
2020-10-07 11:07:34 +02:00
|
|
|
return new SizeClassedMemoryPool(MemoryManager.getNativeMemoryManager());
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
}
|