Skip to content

Commit

Permalink
removed Marshal.SizeOf calls in ArrayExtensions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
krauthaufen committed May 21, 2024
1 parent 7d0ff88 commit d09693c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 5.2.30
* removed UnsafeCoerce usages and several other net6.0+ fixes

### 5.2.29
* Fixed color parsing to be independent of the current culture (regression in 5.2.27)
* Added more value variants for Dictionary, Dict, and SymbolDict functions
Expand Down
58 changes: 38 additions & 20 deletions src/Aardvark.Base/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -2271,9 +2272,8 @@ public static void CopyTo(this Array input, int offset, int length, IntPtr targe

try
{
var type = input.GetType().GetElementType();
var typeSize = Marshal.SizeOf(type);
var dataSize = length * typeSize;
var dataSize = Buffer.ByteLength(input);
var typeSize = dataSize / input.Length;

unsafe
{
Expand Down Expand Up @@ -2304,9 +2304,8 @@ public static void CopyTo(this IntPtr input, Array target, int offset, int lengt

try
{
var type = target.GetType().GetElementType();
var typeSize = Marshal.SizeOf(type);
var dataSize = length * typeSize;
var dataSize = Buffer.ByteLength(target);
var typeSize = dataSize / target.Length;

unsafe
{
Expand Down Expand Up @@ -2363,14 +2362,18 @@ public static byte[] ComputeMD5Hash(this byte[] data)
/// <returns>128bit/16byte data hash</returns>
public static unsafe byte[] ComputeMD5Hash(this Array data)
{
if (data == null) return null;

var gc = GCHandle.Alloc(data, GCHandleType.Pinned);

var byteLength = Buffer.ByteLength(data);
try
{
var ptr = gc.AddrOfPinnedObject();
byte[] hash = null;
using (var md5 = SHA1.Create())
{
using (var s = new UnmanagedMemoryStream((byte*)ptr, data.Length, data.Length,
using (var s = new UnmanagedMemoryStream((byte*)ptr, byteLength, byteLength,
FileAccess.Read))
{
hash = md5.ComputeHash(s);
Expand Down Expand Up @@ -2411,14 +2414,17 @@ public static byte[] ComputeSHA1Hash(this byte[] data)
/// <returns>160bit/20byte data hash</returns>
public static unsafe byte[] ComputeSHA1Hash(this Array data)
{
if (data == null) return null;

var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
var byteLength = Buffer.ByteLength(data);
try
{
var ptr = gc.AddrOfPinnedObject();
byte[] hash = null;
using (var sha1 = SHA1.Create())
{
using (var s = new UnmanagedMemoryStream((byte*)ptr, data.Length, data.Length,
using (var s = new UnmanagedMemoryStream((byte*)ptr, byteLength, byteLength,
FileAccess.Read))
{
hash = sha1.ComputeHash(s);
Expand Down Expand Up @@ -2457,14 +2463,17 @@ public static byte[] ComputeSHA256Hash(this byte[] data)
/// <returns>256bit/32byte data hash</returns>
public static unsafe byte[] ComputeSHA256Hash(this Array data)
{
if (data == null) return null;

var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
var byteLength = Buffer.ByteLength(data);
try
{
var ptr = gc.AddrOfPinnedObject();
byte[] hash = null;
using (var sha256 = SHA256.Create())
{
using (var s = new UnmanagedMemoryStream((byte*)ptr, data.Length, data.Length,
using (var s = new UnmanagedMemoryStream((byte*)ptr, byteLength, byteLength,
FileAccess.Read))
{
hash = sha256.ComputeHash(s);
Expand Down Expand Up @@ -2503,14 +2512,17 @@ public static byte[] ComputeSHA512Hash(this byte[] data)
/// <returns>512bit/64byte data hash</returns>
public static unsafe byte[] ComputeSHA512Hash(this Array data)
{
if (data == null) return null;

var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
var byteLength = Buffer.ByteLength(data);
try
{
var ptr = gc.AddrOfPinnedObject();
byte[] hash = null;
using (var sha512 = SHA512.Create())
{
using (var s = new UnmanagedMemoryStream((byte*)ptr, data.Length, data.Length,
using (var s = new UnmanagedMemoryStream((byte*)ptr, byteLength, byteLength,
FileAccess.Read))
{
hash = sha512.ComputeHash(s);
Expand Down Expand Up @@ -2549,20 +2561,26 @@ public static uint ComputeAdler32Checksum(this byte[] data)
public static unsafe uint ComputeAdler32Checksum(this Array data)
{
var a = new Adler32();
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try

if (data != null)
{
var ptr = gc.AddrOfPinnedObject();
using (var s = new UnmanagedMemoryStream((byte*)ptr.ToPointer(), data.Length, data.Length,
FileAccess.Read))
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
var byteLength = Buffer.ByteLength(data);
try
{
a.Update(s);
var ptr = gc.AddrOfPinnedObject();
using (var s = new UnmanagedMemoryStream((byte*)ptr.ToPointer(), byteLength, byteLength,
FileAccess.Read))
{
a.Update(s);
}
}
finally
{
gc.Free();
}
}
finally
{
gc.Free();
}

return a.Checksum;
}

Expand Down

0 comments on commit d09693c

Please sign in to comment.