Added FillMemory methods and exposed Zero method as public on SafeAllocatedMemoryHandle

pull/38/head
David Hall 2019-01-25 11:09:07 -05:00
parent 3bed0225b4
commit a6bd30dd03
1 changed files with 12 additions and 8 deletions

View File

@ -133,15 +133,19 @@ namespace Vanara.InteropServices
public abstract int Size { get; set; }
/// <summary>Zero out all allocated memory.</summary>
protected virtual void Zero()
public virtual void Zero() => Fill(0, Size);
/// <summary>Fills the allocated memory with a specific byte value.</summary>
/// <param name="value">The byte value.</param>
public virtual void Fill(byte value) => Fill(value, Size);
/// <summary>Fills the allocated memory with a specific byte value.</summary>
/// <param name="value">The byte value.</param>
/// <param name="length">The number of bytes in the block of memory to be filled.</param>
public virtual void Fill(byte value, int length)
{
if (handle == IntPtr.Zero || Size <= 0) return;
// Write multiples of 8 bytes first
for (var ofs = 0; ofs < Size / 8; ofs++)
Marshal.WriteInt64(handle, ofs * 8, 0);
// Write remaining bytes
for (var ofs = Size - (Size % 8); ofs < Size; ofs++)
Marshal.WriteByte(handle, ofs, 0);
if (length > Size) throw new ArgumentOutOfRangeException(nameof(length));
Vanara.Extensions.InteropExtensions.FillMemory(handle, value, length);
}
}