Top Description Methods
io.netty5.buffer

public Interface MemoryManager

Known Direct Implementers
io.netty5.buffer.bytebuffer.ByteBufferMemoryManager, io.netty5.buffer.unsafe.UnsafeMemoryManager
Annotations
@UnstableApi
Imports
io.netty5.buffer.internal.ArcDrop, .LeakDetection, .MemoryManagerLoader, .MemoryManagerOverride, .WrappingAllocation, io.netty5.util.Resource, .SafeCloseable, io.netty5.util.internal.UnstableApi, io.netty5.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.util.Optional, .ServiceConfigurationError, .ServiceLoader.Provider, java.util.function.Consumer, .Function, .Supplier, java.util.stream.Stream

The choice of MemoryManager implementation also determines the choice of Buffer implementation. It is the MemoryManager that implement memory allocation, and how to wrap the allocated memory in a Buffer interface.

API Note

This is a low-level, unstable, API that is used for BufferAllocator implementations to build upon. The methods in this interface are unsafe, because they can be used to violate the safety guarantees of the Buffer API, and potentially also the safety guarantees of the JVM.

Method Summary

Modifier and TypeMethod and Description
public Buffer

Returns:

A const buffer with the same size, contents, and read-only state of the given parent buffer.
allocateConstChild
(Buffer
The read-only parent buffer for which a const buffer should be created. The parent buffer is allocated in the usual way, with allocateShared(AllocatorControl, long, Function, AllocationType), initialised with contents, and then made read-only.
readOnlyConstParent
)

Allocates a constant buffer based on the given parent.

public Buffer

Returns:

A Buffer instance with the given configuration.
allocateShared
(AllocatorControl
Call-back interface for controlling the allocator that requested the allocation of this buffer.
allocatorControl
,
long
The size of the buffer to allocate. This size is assumed to be valid for the implementation.
size
,
Function<Drop<Buffer>, Drop<Buffer>>
A function to decorate the memory managers Drop instance. The Drop instance returned by this function will be used when the buffer is closed.
dropDecorator
,
AllocationType
The type of allocation to perform. Typically, one of the StandardAllocationTypes.
allocationType
)

Allocates a shared buffer.

public static Stream<ServiceLoader.Provider<MemoryManager>>

Returns:

A stream of providers of memory managers instances.
availableManagers
()

Get a stream of all available memory managers.

public void
clearMemory(Object
The memory that should be overwritten.
memory
)

Overwrite the given recoverable memory object with zeroes, erasing all data that it contains.

public String

Returns:

The name of this memory managers implementation.
implementationName
()

Get the name for this implementation, which can be used for finding this particular implementation via the lookupImplementation(String) method.

public static MemoryManager

Returns:

A MemoryManagers instance.
instance
()

Get the default, or currently configured, memory managers instance.

public static Optional<MemoryManager>

Returns:

A MemoryManager implementation, if any was found.
lookupImplementation
(String
The named implementation to look for.
implementationName
)

Find a MemoryManager implementation by its implementation name.

public static SafeCloseable

Returns:

An SafeCloseable instance that, when closed, removes the given callback again.
onLeakDetected
(Consumer<LeakInfo>
The callback that will be called when a buffer leak is detected.
callback
)

Register a callback that will be called whenever a Buffer instance is leaked.

public Buffer

Returns:

A Buffer instance backed by the given recovered memory.
recoverMemory
(AllocatorControl
The allocator control to attach to the buffer.
allocatorControl
,
Object
The opaque memory to use for the buffer.
recoverableMemory
,
Drop<Buffer>
The Drop instance to use when the buffer is closed.
drop
)

Recover the memory from a prior unwrapRecoverableMemory(Buffer) call, and wrap it in a Buffer instance.

public Object

Returns:

A new opaque memory instance that represents the given slice of the original.
sliceMemory
(Object
The opaque memory to slice.
memory
,
int
The offset into the memory to slice from.
offset
,
int
The length of the slice.
length
)

Produces a slice of the given internal memory representation object.

public static Buffer

Returns:

A buffer that wraps the given byte array
unsafeWrap
(byte[]
The byte array that will be embedded in the created buffer.
array
)

Create a new on-heap Buffer instance that directly wraps the given array.

public Object

Returns:

The internal memory of the given buffer, as an opaque object.
unwrapRecoverableMemory
(Buffer
The buffer to unwrap.
buf
)

Create an object that represents the internal memory of the given buffer.

public static <
The result type from the supplier.
T
>
T

Returns:

The result from the supplier.
using
(MemoryManager
Override the default configured managers instance with this instance.
manager
,
Supplier<T>
The supplier function to be called while the override is in place.
supplier
)

Temporarily override the default configured memory managers instance.

Method Detail

allocateConstChildback to summary
public Buffer allocateConstChild(Buffer readOnlyConstParent)

Allocates a constant buffer based on the given parent. A "constant" buffer is conceptually similar to a read-only buffer, but the implementation may share the underlying memory across multiple buffer instance - something that is normally not allowed by the API. This allows efficient implementation of the BufferAllocator#constBufferSupplier(byte[]) method.

Note

The const-parent buffer must be allocated by this memory manager.

Parameters
readOnlyConstParent:Buffer

The read-only parent buffer for which a const buffer should be created. The parent buffer is allocated in the usual way, with allocateShared(AllocatorControl, long, Function, AllocationType), initialised with contents, and then made read-only.

Returns:Buffer

A const buffer with the same size, contents, and read-only state of the given parent buffer.

allocateSharedback to summary
public Buffer allocateShared(AllocatorControl allocatorControl, long size, Function<Drop<Buffer>, Drop<Buffer>> dropDecorator, AllocationType allocationType)

Allocates a shared buffer. "Shared" is the normal type of buffer, and means the buffer permit concurrent access from multiple threads, within the limited thread-safety guarantees of the Buffer interface.

Parameters
allocatorControl:AllocatorControl

Call-back interface for controlling the allocator that requested the allocation of this buffer.

size:long

The size of the buffer to allocate. This size is assumed to be valid for the implementation.

dropDecorator:Function<Drop<Buffer>, Drop<Buffer>>

A function to decorate the memory managers Drop instance. The Drop instance returned by this function will be used when the buffer is closed.

allocationType:AllocationType

The type of allocation to perform. Typically, one of the StandardAllocationTypes.

Returns:Buffer

A Buffer instance with the given configuration.

Exceptions
IllegalArgumentException:
For unknown AllocationTypes.
availableManagersback to summary
public static Stream<ServiceLoader.Provider<MemoryManager>> availableManagers()

Get a stream of all available memory managers.

Returns:Stream<ServiceLoader.Provider<MemoryManager>>

A stream of providers of memory managers instances.

clearMemoryback to summary
public void clearMemory(Object memory)

Overwrite the given recoverable memory object with zeroes, erasing all data that it contains.

This is used by the SensitiveBufferAllocator to erase data on deallocation.

Parameters
memory:Object

The memory that should be overwritten.

implementationNameback to summary
public String implementationName()

Get the name for this implementation, which can be used for finding this particular implementation via the lookupImplementation(String) method.

Returns:String

The name of this memory managers implementation.

instanceback to summary
public static MemoryManager instance()

Get the default, or currently configured, memory managers instance.

Returns:MemoryManager

A MemoryManagers instance.

lookupImplementationback to summary
public static Optional<MemoryManager> lookupImplementation(String implementationName)

Find a MemoryManager implementation by its implementation name.

Parameters
implementationName:String

The named implementation to look for.

Returns:Optional<MemoryManager>

A MemoryManager implementation, if any was found.

onLeakDetectedback to summary
public static SafeCloseable onLeakDetected(Consumer<LeakInfo> callback)

Register a callback that will be called whenever a Buffer instance is leaked.

Be mindful that the callback must be fast, and not take any locks or call any blocking methods, as this might interfere with the garbage collectors reference processing and cleaning.

This also applies to callbacks that perform logging. In these cases, asynchronous logging should be preferred, for the avoidance of blocking calls and IO.

If the same callback object is registered multiple times, it will only be informed once for each leak, but each of the associated SafeCloseable objects will need to be closed before the callback is removed.

Parameters
callback:Consumer<LeakInfo>

The callback that will be called when a buffer leak is detected.

Returns:SafeCloseable

An SafeCloseable instance that, when closed, removes the given callback again.

Annotations
@UnstableApi
recoverMemoryback to summary
public Buffer recoverMemory(AllocatorControl allocatorControl, Object recoverableMemory, Drop<Buffer> drop)

Recover the memory from a prior unwrapRecoverableMemory(Buffer) call, and wrap it in a Buffer instance.

Parameters
allocatorControl:AllocatorControl

The allocator control to attach to the buffer.

recoverableMemory:Object

The opaque memory to use for the buffer.

drop:Drop<Buffer>

The Drop instance to use when the buffer is closed.

Returns:Buffer

A Buffer instance backed by the given recovered memory.

sliceMemoryback to summary
public Object sliceMemory(Object memory, int offset, int length)

Produces a slice of the given internal memory representation object.

Parameters
memory:Object

The opaque memory to slice.

offset:int

The offset into the memory to slice from.

length:int

The length of the slice.

Returns:Object

A new opaque memory instance that represents the given slice of the original.

unsafeWrapback to summary
public static Buffer unsafeWrap(byte[] array)

Create a new on-heap Buffer instance that directly wraps the given array.

This is unsafe because it allows the memory (the array) to be aliased (multiple references to the same memory) in an uncontrolled way.

The returned buffer will be read-only, but changes to the byte array will be reflected in the buffers contents.

Note

Wrapping buffers created with this method are not subject to leak detection, and if they are garbage collected without being closed first, then no callback will be issued to any on-leak callback handler.

Parameters
array:byte[]

The byte array that will be embedded in the created buffer.

Returns:Buffer

A buffer that wraps the given byte array

Annotations
@UnstableApi
unwrapRecoverableMemoryback to summary
public Object unwrapRecoverableMemory(Buffer buf)

Create an object that represents the internal memory of the given buffer.

Parameters
buf:Buffer

The buffer to unwrap.

Returns:Object

The internal memory of the given buffer, as an opaque object.

usingback to summary
public static <T> T using(MemoryManager manager, Supplier<T> supplier)

Temporarily override the default configured memory managers instance.

Calls to instance() from within the given supplier will get the given managers instance.

Parameters
<T>
The result type from the supplier.
manager:MemoryManager

Override the default configured managers instance with this instance.

supplier:Supplier<T>

The supplier function to be called while the override is in place.

Returns:T

The result from the supplier.