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(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 WritableDirectHandle whether or not to
// actually close the resource.
memReqSvr.requestClose(mem, newMem);
mem = newMem; //update your reference to memoty
}
//continue with the add process
}
}
| Constructor and Description |
|---|
DefaultMemoryRequestServer() |
| Modifier and Type | Method and Description |
|---|---|
WritableMemory |
request(long capacityBytes)
Request new WritableMemory with the given capacity.
|
void |
requestClose(WritableMemory memToRelease,
WritableMemory newMemory)
Request close the AutoCloseable resource.
|
public WritableMemory request(long capacityBytes)
By default this allocates new memory requests on the Java heap.
request in interface MemoryRequestServercapacityBytes - The capacity being requested.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.newMemory - the newly allocated WritableMemory. This is returned from the client
for the convenience of the resource owner. It is optional and may be null.Copyright © 2015–2018 Yahoo! Inc.. All rights reserved.