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
Afficher le fichier Open project: GridProtectionAlliance/openHistorian Class Usage Examples

Méthodes publiques

Свойство Type Description
PageMask int
PageShiftBits int
PageSize int

Méthodes publiques

Méthode 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

Méthode 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 méthode

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.
Résultat void

Dispose() public méthode

Releases all the resources used by the MemoryPool object.
public Dispose ( ) : void
Résultat void

MemoryPool() public méthode

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.
Résultat System

ReleasePage() public méthode

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
Résultat void

ReleasePages() public méthode

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

SetMaximumBufferSize() public méthode

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

SetTargetUtilizationLevel() public méthode

Changes the utilization level
public SetTargetUtilizationLevel ( TargetUtilizationLevels utilizationLevel ) : void
utilizationLevel TargetUtilizationLevels
Résultat 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
Résultat 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
Résultat int

PageSize public_oe property

Each page will be exactly this size (Based on RAM)
public int PageSize
Résultat int