Class DefaultMemoryRequestServer

  • All Implemented Interfaces:
    MemoryRequestServer

    public final class DefaultMemoryRequestServer
    extends Object
    implements MemoryRequestServer
    This is a simple implementation of the MemoryRequestServer that creates space on the Java heap for the requesting application. This capability is only available for direct, off-heap allocated memory.

    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
       }
     }
     
    Author:
    Lee Rhodes
    • Constructor Detail

      • DefaultMemoryRequestServer

        public DefaultMemoryRequestServer()
    • Method Detail

      • request

        public WritableMemory request​(WritableMemory currentWritableMemory,
                                      long capacityBytes)
        Request new WritableMemory with the given capacity. The current Writable Memory will be used to determine the byte order of the returned WritableMemory and other checks.

        By default this allocates new memory requests on the Java heap.

        Specified by:
        request in interface MemoryRequestServer
        Parameters:
        currentWritableMemory - the current writableMemory of the client. It must be non-null.
        capacityBytes - The capacity being requested. It must be ≥ 0.
        Returns:
        new WritableMemory with the given capacity.
      • requestClose

        public void requestClose​(WritableMemory memToRelease,
                                 WritableMemory newMemory)
        Request close the AutoCloseable resource. This only applies to resources allocated using WritableMemory.allocateDirect(...). This may be ignored depending on the application implementation.

        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.

        Specified by:
        requestClose in interface MemoryRequestServer
        Parameters:
        memToRelease - 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.