org.terracotta.offheapstore.paging
Class UpfrontAllocatingPageSource

java.lang.Object
  extended by org.terracotta.offheapstore.paging.UpfrontAllocatingPageSource
All Implemented Interfaces:
PageSource

public class UpfrontAllocatingPageSource
extends Object
implements PageSource

An upfront allocating direct byte buffer source.

This buffer source implementation allocates all of its required storage up-front in fixed size chunks. Runtime allocations are then satisfied using slices from these initial chunks.

Author:
Chris Dennis

Nested Class Summary
static class UpfrontAllocatingPageSource.ThresholdDirection
           
 
Field Summary
static String ALLOCATION_LOG_LOCATION
           
 
Constructor Summary
UpfrontAllocatingPageSource(BufferSource source, long max, int chunk)
          Create an up-front allocating buffer source of max total bytes, in chunk byte chunks.
UpfrontAllocatingPageSource(BufferSource source, long max, int maxChunk, int minChunk)
          Create an up-front allocating buffer source of max total bytes, in maximally sized chunks, within the given bounds.
 
Method Summary
 Runnable addAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold, Runnable action)
          Adds an allocation threshold action.
 Page allocate(int size, boolean thief, boolean victim, OffHeapStorageArea owner)
          Allocates a byte buffer of at least the given size.
 void free(Page page)
          Frees the supplied buffer.
 long getAllocatedSize()
           
 long getAllocatedSizeUnSync()
           
 Runnable removeAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction, long threshold)
          Removes an allocation threshold action.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ALLOCATION_LOG_LOCATION

public static final String ALLOCATION_LOG_LOCATION
Constructor Detail

UpfrontAllocatingPageSource

public UpfrontAllocatingPageSource(BufferSource source,
                                   long max,
                                   int chunk)
Create an up-front allocating buffer source of max total bytes, in chunk byte chunks.

Parameters:
source - source from which initial buffers will be allocated
max - total space to allocate
chunk - chunk size to allocate in

UpfrontAllocatingPageSource

public UpfrontAllocatingPageSource(BufferSource source,
                                   long max,
                                   int maxChunk,
                                   int minChunk)
Create an up-front allocating buffer source of max total bytes, in maximally sized chunks, within the given bounds.

Parameters:
source - source from which initial buffers will be allocated
max - total space to allocate
maxChunk - the largest chunk size
minChunk - the smallest chunk size
Method Detail

allocate

public Page allocate(int size,
                     boolean thief,
                     boolean victim,
                     OffHeapStorageArea owner)
Allocates a byte buffer of at least the given size.

This BufferSource is limited to allocating regions that are a power of two in size. Supplied sizes are therefore rounded up to the next largest power of two.

Specified by:
allocate in interface PageSource
Parameters:
size - size of page to allocate
thief - true if the allocation can steal space from victims
victim - true if the allocated page should be eligible for stealing
owner - owner from which subsequent steal should occur
Returns:
a buffer of at least the given size

free

public void free(Page page)
Frees the supplied buffer.

If the given buffer was not allocated by this source or has already been freed then an AssertionError is thrown.

Specified by:
free in interface PageSource

getAllocatedSize

public long getAllocatedSize()

getAllocatedSizeUnSync

public long getAllocatedSizeUnSync()

toString

public String toString()
Overrides:
toString in class Object

addAllocationThreshold

public Runnable addAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction,
                                       long threshold,
                                       Runnable action)
Adds an allocation threshold action.

There can be only a single action associated with each unique direction and threshold combination. If an action is already associated with the supplied combination then the action is replaced by the new action and the old action is returned.

Actions are fired on passing through the supplied threshold and are called synchronously with the triggering allocation. This means care must be taken to avoid mutating any map that uses this page source from within the action otherwise deadlocks may result. Exceptions thrown by the action will be caught and logged by the page source and will not be propagated on the allocating thread.

Parameters:
direction - new actions direction
threshold - new actions threshold level
action - fired on breaching the threshold
Returns:
the replaced action or null if no action was present.

removeAllocationThreshold

public Runnable removeAllocationThreshold(UpfrontAllocatingPageSource.ThresholdDirection direction,
                                          long threshold)
Removes an allocation threshold action.

Removes the allocation threshold action for the given level and direction.

Parameters:
direction - registered actions direction
threshold - registered actions threshold level
Returns:
the removed condition or null if no action was present.


Copyright © 2015. All Rights Reserved.