Table of Contents

Class PooledSegment<T>

Namespace
CryptoHives.Foundation.Memory.Buffers
Assembly
CryptoHives.Foundation.Memory.dll

An ISegmentOwner<T> that rents a buffer from Shared and returns it automatically on dispose.

public sealed class PooledSegment<T> : ISegmentOwner<T>, IDisposable

Type Parameters

T
Inheritance
PooledSegment<T>
Implements
Inherited Members

Remarks

Implemented as a sealed class so the backing array can never be disposed more than once by an unintended struct copy.

Properties

this[int]

Gets or sets element i relative to Offset inside Array.

public T this[int i] { get; set; }

Parameters

i int

Zero-based index within the current segment.

Property Value

T

Segment

Gets the current ArraySegment<T> view of the underlying array.

public ArraySegment<T> Segment { get; }

Property Value

ArraySegment<T>

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Rent(int, bool)

Rents a buffer with at least minimumLength elements from Shared and returns it wrapped in a PooledSegment<T>.

public static ISegmentOwner<T> Rent(int minimumLength, bool clearArray = false)

Parameters

minimumLength int

The minimum number of elements required.

clearArray bool

Whether to zero the buffer as it returns to the pool. Pass true when the segment holds key material or anything else that should not be legible to the next renter.

Returns

ISegmentOwner<T>

An ISegmentOwner<T> whose Segment has Count equal to minimumLength.

Remarks

A rented buffer arrives holding whatever the previous tenant left in it, and by default goes back the same way. clearArray zeroes it on the way back, so what this buys is that the next renter cannot read the data — it does nothing about the window during which the data was live, and nothing about copies taken elsewhere.

The whole array is zeroed, not just the segment window, so a TrySetSegment(int, int) call cannot leave part of it legible. The cost is a pass over the buffer on dispose, proportional to its length.

TrySetSegment(int, int)

Sets the offset and the length of the ArraySegment<T>.

public bool TrySetSegment(int offset, int length)

Parameters

offset int

The new zero-based offset into the underlying array.

length int

The number of elements to expose starting at offset.

Returns

bool

true if the segment was updated; false if the underlying array is null or too small to satisfy the requested range.