Skip to content

Commit

Permalink
removed usages of UnsafeCoerce
Browse files Browse the repository at this point in the history
  • Loading branch information
krauthaufen committed May 21, 2024
1 parent 9871a42 commit 7d0ff88
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 20 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +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
116 changes: 98 additions & 18 deletions src/Aardvark.Base/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
Expand Down Expand Up @@ -2360,11 +2361,29 @@ public static byte[] ComputeMD5Hash(this byte[] data)
/// Computes the MD5 hash of the data array.
/// </summary>
/// <returns>128bit/16byte data hash</returns>
public static byte[] ComputeMD5Hash(this Array data)
public static unsafe byte[] ComputeMD5Hash(this Array data)
{
byte[] hash = null;
data.UnsafeCoercedApply<byte>(array => hash = array.ComputeMD5Hash());
return hash;
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
var ptr = gc.AddrOfPinnedObject();
byte[] hash = null;
using (var md5 = SHA1.Create())
{
using (var s = new UnmanagedMemoryStream((byte*)ptr, data.Length, data.Length,
FileAccess.Read))
{
hash = md5.ComputeHash(s);
}
}

Array.Resize(ref hash, 16);
return hash;
}
finally
{
gc.Free();
}
}

/// <summary>
Expand All @@ -2390,11 +2409,27 @@ public static byte[] ComputeSHA1Hash(this byte[] data)
/// Computes the SHA1 hash of the data array.
/// </summary>
/// <returns>160bit/20byte data hash</returns>
public static byte[] ComputeSHA1Hash(this Array data)
public static unsafe byte[] ComputeSHA1Hash(this Array data)
{
byte[] hash = null;
data.UnsafeCoercedApply<byte>(array => hash = array.ComputeSHA1Hash());
return hash;
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
var ptr = gc.AddrOfPinnedObject();
byte[] hash = null;
using (var sha1 = SHA1.Create())
{
using (var s = new UnmanagedMemoryStream((byte*)ptr, data.Length, data.Length,
FileAccess.Read))
{
hash = sha1.ComputeHash(s);
}
}
return hash;
}
finally
{
gc.Free();
}
}

/// <summary>
Expand All @@ -2420,11 +2455,27 @@ public static byte[] ComputeSHA256Hash(this byte[] data)
/// Computes the SHA256 hash of the data array.
/// </summary>
/// <returns>256bit/32byte data hash</returns>
public static byte[] ComputeSHA256Hash(this Array data)
public static unsafe byte[] ComputeSHA256Hash(this Array data)
{
byte[] hash = null;
data.UnsafeCoercedApply<byte>(array => hash = array.ComputeSHA256Hash());
return hash;
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
var ptr = gc.AddrOfPinnedObject();
byte[] hash = null;
using (var sha256 = SHA256.Create())
{
using (var s = new UnmanagedMemoryStream((byte*)ptr, data.Length, data.Length,
FileAccess.Read))
{
hash = sha256.ComputeHash(s);
}
}
return hash;
}
finally
{
gc.Free();
}
}

/// <summary>
Expand All @@ -2450,11 +2501,27 @@ public static byte[] ComputeSHA512Hash(this byte[] data)
/// Computes the SHA512 hash of the data array.
/// </summary>
/// <returns>512bit/64byte data hash</returns>
public static byte[] ComputeSHA512Hash(this Array data)
public static unsafe byte[] ComputeSHA512Hash(this Array data)
{
byte[] hash = null;
data.UnsafeCoercedApply<byte>(array => hash = array.ComputeSHA512Hash());
return hash;
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
var ptr = gc.AddrOfPinnedObject();
byte[] hash = null;
using (var sha512 = SHA512.Create())
{
using (var s = new UnmanagedMemoryStream((byte*)ptr, data.Length, data.Length,
FileAccess.Read))
{
hash = sha512.ComputeHash(s);
}
}
return hash;
}
finally
{
gc.Free();
}
}

/// <summary>
Expand All @@ -2479,10 +2546,23 @@ public static uint ComputeAdler32Checksum(this byte[] data)
/// <summary>
/// Computes a checksum of the data array using the Adler-32 algorithm (<see cref="Adler32"/>).
/// </summary>
public static uint ComputeAdler32Checksum(this Array data)
public static unsafe uint ComputeAdler32Checksum(this Array data)
{
var a = new Adler32();
data.UnsafeCoercedApply<byte>(array => a.Update(array));
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
var ptr = gc.AddrOfPinnedObject();
using (var s = new UnmanagedMemoryStream((byte*)ptr.ToPointer(), data.Length, data.Length,
FileAccess.Read))
{
a.Update(s);
}
}
finally
{
gc.Free();
}
return a.Checksum;
}

Expand Down
9 changes: 8 additions & 1 deletion src/Aardvark.Base/Extensions/UnsafeCoerce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace Aardvark.Base
public static class ArrayUnsafeCoerceExtensions
{
#region UnsafeCoerce


[Obsolete("breaks net8.0+")]
public static IntPtr GetTypeIdUncached<T>()
where T : struct
{
Expand All @@ -19,6 +20,7 @@ public static IntPtr GetTypeIdUncached<T>()

private static FastConcurrentDict<Type, IntPtr> s_typeIds = new FastConcurrentDict<Type, IntPtr>();

[Obsolete("breaks net8.0+")]
private static IntPtr GetTypeId<T>()
where T : struct
{
Expand All @@ -30,6 +32,7 @@ private static IntPtr GetTypeId<T>()
return typeId;
}

[Obsolete("breaks net8.0+")]
internal static int GetCLRSize(Type t)
{
// TODO: somehow make use of sizeof operator -> requires compile time type -> cannot use ILGenerator in .net standard
Expand All @@ -38,6 +41,7 @@ internal static int GetCLRSize(Type t)
return Marshal.SizeOf(t);
}

[Obsolete("breaks net8.0+")]
internal static TR[] UnsafeCoerce<TR>(this Array input, IntPtr targetId)
where TR : struct
{
Expand All @@ -63,12 +67,14 @@ internal static TR[] UnsafeCoerce<TR>(this Array input, IntPtr targetId)
/// Both types must be structs and you may cause memory leaks when the array-byte-sizes are not multiple of each other
/// WARNING: destroys the original array
/// </summary>
[Obsolete("breaks net8.0+")]
public static TR[] UnsafeCoerce<TR>(this Array input)
where TR : struct
{
return UnsafeCoerce<TR>(input, GetTypeId<TR>());
}

[Obsolete("breaks net8.0+")]
internal static void UnsafeCoercedApply<TR>(this Array input, Action<TR[]> action, IntPtr targetId)
where TR : struct
{
Expand All @@ -95,6 +101,7 @@ internal static void UnsafeCoercedApply<TR>(this Array input, Action<TR[]> actio
gcHandle.Free();
}

[Obsolete("breaks net8.0+")]
public static void UnsafeCoercedApply<TR>(this Array input, Action<TR[]> action)
where TR : struct
{
Expand Down
6 changes: 5 additions & 1 deletion src/Aardvark.Base/Random/ForcedRandomSeries.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System.Runtime.InteropServices;
using System;

namespace Aardvark.Base
{
Expand Down Expand Up @@ -28,7 +30,9 @@ public static V2i[] ReadSeries(string frsSqFile)
if (matrixSize * matrixSize * 8 != bytes.Length)
throw new InvalidDataException("Forced Random series data has invalid length.");

return bytes.UnsafeCoerce<V2i>();
var dst = new V2i[bytes.Length / 8];
bytes.AsSpan().CopyTo(MemoryMarshal.AsBytes(dst.AsSpan()));
return dst;
}

/// <summary>
Expand Down

0 comments on commit 7d0ff88

Please sign in to comment.