/// <summary> /// <para>Initializes a new instance of the <see cref="SafeUnmanagedMemoryStream"/> class.</para> /// </summary> /// <param name="pointer"> /// <para>Pointer to the unmanaged binary data block.</para> /// </param> /// <param name="length"> /// <para>Length of the segment of the block to read.</para> /// </param> /// <param name="cleanup"> /// <para>Action that cleans up block on close/dispose/finalize.</para> /// </param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="pointer"/> is <see langword="null"/>.</para> /// <para>-or-</para> /// <para><paramref name="cleanup"/> is <see langword="null"/>.</para> /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <para><paramref name="length"/> is less than 0.</para> /// </exception> public SafeUnmanagedMemoryStream(byte* pointer, long length, PostAccessUnmanagedMemoryCleanup cleanup) { if (pointer == null) throw new ArgumentNullException("pointer"); if (length < 0) throw new ArgumentOutOfRangeException("length"); if (cleanup == null) throw new ArgumentNullException("cleanup"); _pointer = pointer; _cleanupPointer = pointer; _length = length; _remaining = length; _cleanup = cleanup; }
/// <summary> /// <para>Overriden. Releases the unmanaged resources used by the <see cref="System.IO.UnmanagedMemoryStream"/> and optionally releases the managed resources.</para> /// </summary> /// <param name="disposing"> /// <para>true to release both managed and unmanaged resources; false to release only unmanaged resources.</para> /// </param> protected override void Dispose(bool disposing) { if (_pointer == null) return; try { _cleanup(_cleanupPointer); } finally { _pointer = null; if (disposing) { // don't need to call GC.SuppressFinalize because Stream.Close // and Stream.Dispose do so _cleanup = null; _remaining = 0; } } }
/// <summary> /// <para>Initializes a new instance of the <see cref="SafeUnmanagedMemoryStream"/> class.</para> /// </summary> /// <param name="pointer"> /// <para>Pointer to the unmanaged binary data block.</para> /// </param> /// <param name="length"> /// <para>Length of the segment of the block to read.</para> /// </param> /// <param name="cleanup"> /// <para>Action that cleans up block on close/dispose/finalize.</para> /// </param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="pointer"/> is <see langword="null"/>.</para> /// <para>-or-</para> /// <para><paramref name="cleanup"/> is <see langword="null"/>.</para> /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <para><paramref name="length"/> is less than 0.</para> /// </exception> public SafeUnmanagedMemoryStream(byte *pointer, long length, PostAccessUnmanagedMemoryCleanup cleanup) { if (pointer == null) { throw new ArgumentNullException("pointer"); } if (length < 0) { throw new ArgumentOutOfRangeException("length"); } if (cleanup == null) { throw new ArgumentNullException("cleanup"); } _pointer = pointer; _cleanupPointer = pointer; _length = length; _remaining = length; _cleanup = cleanup; }
/// <summary> /// <para>Overriden. Releases the unmanaged resources used by the <see cref="System.IO.UnmanagedMemoryStream"/> and optionally releases the managed resources.</para> /// </summary> /// <param name="disposing"> /// <para>true to release both managed and unmanaged resources; false to release only unmanaged resources.</para> /// </param> protected override void Dispose(bool disposing) { if (_pointer == null) { return; } try { _cleanup(_cleanupPointer); } finally { _pointer = null; if (disposing) { // don't need to call GC.SuppressFinalize because Stream.Close // and Stream.Dispose do so _cleanup = null; _remaining = 0; } } }