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
|
|
|
/**
|
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-10-30 14:31:45 +01:00
|
|
|
Buf allocate(int 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}.
|
|
|
|
*/
|
2020-10-30 14:31:45 +01:00
|
|
|
default Buf allocate(int size, ByteOrder order) {
|
2020-10-06 15:54:48 +02:00
|
|
|
return allocate(size).order(order);
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:00:32 +01:00
|
|
|
/**
|
|
|
|
* Compose the given sequence of buffers and present them as a single buffer.
|
|
|
|
* <p>
|
|
|
|
* <strong>Note:</strong> The composite buffer increments the reference count on all the constituent buffers,
|
|
|
|
* and holds a reference to them until the composite buffer is deallocated.
|
|
|
|
* This means the constituent buffers must still have their outside-reference count decremented as normal.
|
|
|
|
* If the buffers are allocated for the purpose of participating in the composite buffer,
|
|
|
|
* then they should be closed as soon as the composite buffer has been created, like in this example:
|
|
|
|
* <pre>{@code
|
|
|
|
* try (Buf a = allocator.allocate(size);
|
|
|
|
* Buf b = allocator.allocate(size)) {
|
|
|
|
* return Buf.compose(a, b); // Reference counts for 'a' and 'b' incremented here.
|
|
|
|
* } // Reference count for 'a' and 'b' decremented here; composite buffer now holds the last references.
|
|
|
|
* }</pre>
|
|
|
|
* <p>
|
|
|
|
* {@linkplain Buf#send() Sending} a composite buffer implies sending all of its constituent buffers.
|
|
|
|
* For sending to be possible, both the composite buffer itself, and all of its constituent buffers, must be in an
|
|
|
|
* {@linkplain Rc#isOwned() owned state}.
|
|
|
|
* This means that the composite buffer must be the only reference to the constituent buffers.
|
|
|
|
* <p>
|
|
|
|
* All of the constituent buffers must have the same {@linkplain Buf#order() byte order}.
|
|
|
|
* An exception will be thrown if you attempt to compose buffers that have different byte orders,
|
|
|
|
* and changing the byte order of the constituent buffers so they become inconsistent after construction,
|
|
|
|
* will result in unspecified behaviour.
|
|
|
|
* <p>
|
|
|
|
* The read and write offsets of the constituent buffers must be arranged such that there are no "gaps" when viewed
|
|
|
|
* as a single connected chunk of memory.
|
|
|
|
* Specifically, there can be at most one buffer whose write offset is neither zero nor at capacity,
|
|
|
|
* and all buffers prior to it must have their write offsets at capacity, and all buffers after it must have a write
|
|
|
|
* offset of zero.
|
|
|
|
* Likewise, there can be at most one buffer whose read offset is neither zero nor at capacity,
|
|
|
|
* and all buffers prior to it must have their read offsets at capacity, and all buffers after it must have a read
|
|
|
|
* offset of zero.
|
|
|
|
* Furthermore, the sum of the read offsets must be less than or equal to the sum of the write offsets.
|
|
|
|
* <p>
|
|
|
|
* Reads and writes to the composite buffer that modifies the read or write offsets, will also modify the relevant
|
|
|
|
* offsets in the constituent buffers.
|
|
|
|
* <p>
|
|
|
|
* It is not a requirement that the buffers have the same size.
|
|
|
|
* <p>
|
|
|
|
* It is not a requirement that the buffers are allocated by this allocator, but if {@link Buf#ensureWritable(int)}
|
|
|
|
* is called on the composed buffer, and the composed buffer needs to be expanded, then this allocator instance
|
|
|
|
* will be used for allocation the extra memory.
|
|
|
|
*
|
|
|
|
* @param bufs The buffers to compose into a single buffer view.
|
|
|
|
* @return A buffer composed of, and backed by, the given buffers.
|
|
|
|
* @throws IllegalArgumentException if the given buffers have an inconsistent {@linkplain Buf#order() byte order}.
|
|
|
|
*/
|
|
|
|
default Buf compose(Buf... bufs) {
|
|
|
|
return new CompositeBuf(this, bufs);
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:48:28 +01:00
|
|
|
/**
|
|
|
|
* Extend the given composite buffer with the given extension buffer.
|
|
|
|
* This works as if the extension had originally been included at the end of the list of constituent buffers when
|
|
|
|
* the composite buffer was created.
|
|
|
|
* The composite buffer is modified in-place.
|
|
|
|
*
|
|
|
|
* @see #compose(Buf...)
|
|
|
|
* @param composite The composite buffer (from a prior {@link #compose(Buf...)} call) to extend with the given
|
|
|
|
* extension buffer.
|
|
|
|
* @param extension The buffer to extend the composite buffer with.
|
|
|
|
*/
|
|
|
|
static void extend(Buf composite, Buf extension) {
|
2020-12-14 14:07:11 +01:00
|
|
|
if (!isComposite(composite)) {
|
2020-12-03 17:48:28 +01:00
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"Expected the first buffer to be a composite buffer, " +
|
|
|
|
"but it is a " + composite.getClass() + " buffer: " + composite + '.');
|
|
|
|
}
|
|
|
|
CompositeBuf buf = (CompositeBuf) composite;
|
|
|
|
buf.extendWith(extension);
|
|
|
|
}
|
|
|
|
|
2020-12-14 14:07:11 +01:00
|
|
|
/**
|
|
|
|
* Check if the given buffer is a {@linkplain #compose(Buf...) composite} buffer or not.
|
|
|
|
* @param composite The buffer to check.
|
|
|
|
* @return {@code true} if the given buffer was created with {@link #compose(Buf...)}, {@code false} otherwise.
|
|
|
|
*/
|
|
|
|
static boolean isComposite(Buf composite) {
|
|
|
|
return composite.getClass() == CompositeBuf.class;
|
|
|
|
}
|
|
|
|
|
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-12-17 16:13:43 +01:00
|
|
|
return new ManagedAllocator(MemoryManager.getHeapMemoryManager(), Statics.CLEANER);
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Allocator direct() {
|
2020-11-16 18:00:32 +01:00
|
|
|
return new ManagedAllocator(MemoryManager.getNativeMemoryManager(), 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
|
|
|
}
|
|
|
|
}
|