C# Класс 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.
Наследование: IDisposable
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
PageMask int
PageShiftBits int
PageSize int

Открытые методы

Метод Описание
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

Приватные методы

Метод Описание
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.

Описание методов

AllocatePage() публичный Метод

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.
Результат void

Dispose() публичный Метод

Releases all the resources used by the MemoryPool object.
public Dispose ( ) : void
Результат void

MemoryPool() публичный Метод

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.
Результат System

ReleasePage() публичный Метод

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
Результат void

ReleasePages() публичный Метод

Releases all of the supplied pages
public ReleasePages ( IEnumerable pageIndexes ) : void
pageIndexes IEnumerable A collection of pages.
Результат void

SetMaximumBufferSize() публичный Метод

Changes the allowable buffer size
public SetMaximumBufferSize ( long value ) : long
value long the number of bytes to set.
Результат long

SetTargetUtilizationLevel() публичный Метод

Changes the utilization level
public SetTargetUtilizationLevel ( TargetUtilizationLevels utilizationLevel ) : void
utilizationLevel TargetUtilizationLevels
Результат void

Описание свойств

PageMask публичное свойство

Provides a mask that the user can apply that can be used to get the offset position of a page.
public int PageMask
Результат int

PageShiftBits публичное свойство

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
Результат int

PageSize публичное свойство

Each page will be exactly this size (Based on RAM)
public int PageSize
Результат int