C# Class GSF.IO.Unmanaged.MemoryPool

This class allocates and pools unmanaged memory. Designed to be internally thread safe.
Be careful how this class is referenced. Deadlocks can occur when registering to event RequestCollection and when calling AllocatePage. See comments for these methods for considerations.
Inheritance: IDisposable
显示文件 Open project: GridProtectionAlliance/openHistorian Class Usage Examples

Public Properties

Property Type Description
PageMask int
PageShiftBits int
PageSize int

Public Methods

Method Description
AllocatePage ( int &index, IntPtr &addressPointer ) : void

Requests a page from the buffered pool. If there is not a free one available, method will block and request a collection of unused pages by raising RequestCollection event.

IMPORTANT NOTICE: Be careful when calling this method as the calling thread will block if no memory is available to have a background collection to occur. There is a possiblity for a deadlock if calling this method from within a lock. The page allocated will not be initialized, so assume that the data is garbage.

Dispose ( ) : void

Releases all the resources used by the MemoryPool object.

MemoryPool ( int pageSize = 64*1024, long maximumBufferSize = -1, TargetUtilizationLevels utilizationLevel = TargetUtilizationLevels.Low ) : System

Creates a new MemoryPool.

ReleasePage ( int pageIndex ) : void

Releases the page back to the buffer pool for reallocation.

The page released will not be initialized. Releasing a page is on the honor system. Rereferencing a released page will most certainly cause unexpected crashing or data corruption or any other unexplained behavior.

ReleasePages ( IEnumerable pageIndexes ) : void

Releases all of the supplied pages

SetMaximumBufferSize ( long value ) : long

Changes the allowable buffer size

SetTargetUtilizationLevel ( TargetUtilizationLevels utilizationLevel ) : void

Changes the utilization level

Private Methods

Method Description
CalculateStopShrinkingLimit ( long size ) : long

Calculates at what point a collection cycle will cease prematurely.

CalculateThresholds ( long maximumBufferSize, TargetUtilizationLevels levels ) : void
GetCollectionLevelBasedOnSize ( long size ) : int

Gets the number of collection rounds base on the size.

GetCollectionLevelString ( int iterations ) : string
RemoveDeadEvents ( ) : void

Searches the collection events and removes any events that have been collected by the garbage collector.

RequestMoreFreeBlocks ( ) : void

Determines whether to allocate more memory or to do a collection cycle on the existing pool.

Method Details

AllocatePage() public method

Requests a page from the buffered pool. If there is not a free one available, method will block and request a collection of unused pages by raising RequestCollection event.
IMPORTANT NOTICE: Be careful when calling this method as the calling thread will block if no memory is available to have a background collection to occur. There is a possiblity for a deadlock if calling this method from within a lock. The page allocated will not be initialized, so assume that the data is garbage.
public AllocatePage ( int &index, IntPtr &addressPointer ) : void
index int the index id of the page that was allocated
addressPointer System.IntPtr outputs a address that can be used /// to access this memory address. You cannot call release with this parameter. /// Use the returned index to release pages.
return void

Dispose() public method

Releases all the resources used by the MemoryPool object.
public Dispose ( ) : void
return void

MemoryPool() public method

Creates a new MemoryPool.
public MemoryPool ( int pageSize = 64*1024, long maximumBufferSize = -1, TargetUtilizationLevels utilizationLevel = TargetUtilizationLevels.Low ) : System
pageSize int The desired page size. Must be between 4KB and 256KB
maximumBufferSize long The desired maximum size of the allocation. Note: could be less if there is not enough system memory.
utilizationLevel TargetUtilizationLevels Specifies the desired utilization level of the allocated space.
return System

ReleasePage() public method

Releases the page back to the buffer pool for reallocation.
The page released will not be initialized. Releasing a page is on the honor system. Rereferencing a released page will most certainly cause unexpected crashing or data corruption or any other unexplained behavior.
public ReleasePage ( int pageIndex ) : void
pageIndex int A value of zero or less will return silently
return void

ReleasePages() public method

Releases all of the supplied pages
public ReleasePages ( IEnumerable pageIndexes ) : void
pageIndexes IEnumerable A collection of pages.
return void

SetMaximumBufferSize() public method

Changes the allowable buffer size
public SetMaximumBufferSize ( long value ) : long
value long the number of bytes to set.
return long

SetTargetUtilizationLevel() public method

Changes the utilization level
public SetTargetUtilizationLevel ( TargetUtilizationLevels utilizationLevel ) : void
utilizationLevel TargetUtilizationLevels
return void

Property Details

PageMask public_oe property

Provides a mask that the user can apply that can be used to get the offset position of a page.
public int PageMask
return int

PageShiftBits public_oe property

Gets the number of bits that must be shifted to calculate an index of a position. This is not the same as a page index that is returned by the allocate functions.
public int PageShiftBits
return int

PageSize public_oe property

Each page will be exactly this size (Based on RAM)
public int PageSize
return int