public final class DefaultMemoryRequestServer extends Object implements MemoryRequestServer
Using this default implementation could be something like the following:
class OffHeap {
WritableMemory mem;
MemoryRequestServer memReqSvr = null;
void add(Object something) {
if (outOfSpace) { // determine if out-of-space
long spaceNeeded = ...
//Acquire the MemoryRequestServer from the direct Memory the first time.
//Once acquired, this can be reused if more memory is needed later.
//This is required for the default implementation because it returns memory on heap
// and on-heap memory does not carry a reference to the MemoryRequestServer.
memReqSvr = (memReqSvr == null) ? mem.getMemoryRequestServer() : memReqSvr;
//Request bigger memory
WritableMemory newMem = memReqSvr.request(mem, spaceNeeded);
//Copy your data from the current memory to the new one and resize
moveAndResize(mem, newMem);
//You are done with the old memory, so request close.
//Note that it is up to the owner of the WritableHandle whether or not to
// actually close the resource.
memReqSvr.requestClose(mem, newMem);
mem = newMem; //update your reference to memory
}
//continue with the add process
}
}
| Constructor and Description |
|---|
DefaultMemoryRequestServer() |
| Modifier and Type | Method and Description |
|---|---|
WritableMemory |
request(WritableMemory currentWritableMemory,
long capacityBytes)
Request new WritableMemory with the given capacity.
|
void |
requestClose(WritableMemory memToRelease,
WritableMemory newMemory)
Request close the AutoCloseable resource.
|
public WritableMemory request(WritableMemory currentWritableMemory, long capacityBytes)
By default this allocates new memory requests on the Java heap.
request in interface MemoryRequestServercurrentWritableMemory - the current writableMemory of the client. It must be non-null.capacityBytes - The capacity being requested. It must be ≥ 0.public void requestClose(WritableMemory memToRelease, WritableMemory newMemory)
This method does nothing in this default implementation because it is application specific. This method must be overridden to explicitly close if desired. Otherwise, the AutoCloseable will eventually close the resource.
requestClose in interface MemoryRequestServermemToRelease - the relevant WritbleMemory to be considered for closing. It must be non-null.newMemory - the newly allocated WritableMemory. It must be non-null.
This is returned from the client to facilitate tracking for the convenience of the resource owner.Copyright © 2015–2020 The Apache Software Foundation. All rights reserved.