public static bool TryLoadNativeMethods() { var nativeLibraryManager = NativeMethods.NativeLibraryManager; if (nativeLibraryManager != null) { try { Zero = nativeLibraryManager.GetMethod <ZeroDelegate>("BytesOperations_Zero"); Copy = nativeLibraryManager.GetMethod <CopyDelegate>("BytesOperations_Copy"); Equals = nativeLibraryManager.GetMethod <EqualsDelegate>("BytesOperations_Equals"); Compare = nativeLibraryManager.GetMethod <CompareDelegate>("BytesOperations_Compare"); And = nativeLibraryManager.GetMethod <BitwiseOperationDelegate>("BytesOperations_And"); Or = nativeLibraryManager.GetMethod <BitwiseOperationDelegate>("BytesOperations_Or"); Xor = nativeLibraryManager.GetMethod <BitwiseOperationDelegate>("BytesOperations_Xor"); return(true); } catch (Exception e) { _logger.Error(e); } } return(false); }
public static void LoadPureUnsafeMethods() { Zero = PureUnsafe.Zero; Copy = PureUnsafe.Copy; Equals = PureUnsafe.Equals; Compare = PureUnsafe.Compare; And = PureUnsafe.And; Or = PureUnsafe.Or; Xor = PureUnsafe.Xor; }
internal static void LoadPureUnsafeMethods() { _zero = PureUnsafeMethods.Zero; _copy = PureUnsafeMethods.Copy; _equals = PureUnsafeMethods.Equals; _compare = PureUnsafeMethods.Compare; _and = PureUnsafeMethods.And; _or = PureUnsafeMethods.Or; _xor = PureUnsafeMethods.Xor; }
internal static void LoadNativeMethods() { _nativeLibraryManager?.Dispose(); try { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (RuntimeInformation.ProcessArchitecture == Architecture.X64) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Omnix.Base.win-x64.dll"); } else { throw new NotSupportedException(); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { if (RuntimeInformation.ProcessArchitecture == Architecture.X64) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Omnix.Base.linux-x64.so"); } else { throw new NotSupportedException(); } } else { throw new NotSupportedException(); } _zero = _nativeLibraryManager.GetMethod <ZeroDelegate>("zero"); _copy = _nativeLibraryManager.GetMethod <CopyDelegate>("copy"); _equals = _nativeLibraryManager.GetMethod <EqualsDelegate>("equals"); _compare = _nativeLibraryManager.GetMethod <CompareDelegate>("compare"); _and = _nativeLibraryManager.GetMethod <BitwiseOperationDelegate>("math_and"); _or = _nativeLibraryManager.GetMethod <BitwiseOperationDelegate>("math_or"); _xor = _nativeLibraryManager.GetMethod <BitwiseOperationDelegate>("math_xor"); } catch (Exception) { _nativeLibraryManager?.Dispose(); _nativeLibraryManager = null; throw; } }
static Unsafe() { try { #if Windows if (System.Environment.Is64BitProcess) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_x64.dll"); } else { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_x86.dll"); } #endif #if Unix if (System.Environment.Is64BitProcess) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_x64.so"); } else { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_x86.so"); } #endif _copy = _nativeLibraryManager.GetMethod<CopyDelegate>("copy"); _equals = _nativeLibraryManager.GetMethod<EqualsDelegate>("equals"); _compare = _nativeLibraryManager.GetMethod<CompareDelegate>("compare"); _and = _nativeLibraryManager.GetMethod<BitwiseOperationDelegate>("math_and"); _or = _nativeLibraryManager.GetMethod<BitwiseOperationDelegate>("math_or"); _xor = _nativeLibraryManager.GetMethod<BitwiseOperationDelegate>("math_xor"); } catch (Exception e) { Log.Warning(e); } }
private static void BitwiseOperation(BitwiseOperationDelegate bitwiseOperation, byte[] source1, int source1Index, byte[] source2, int source2Index, byte[] destination, int destinationIndex, int length) { if (source1 == null) throw new ArgumentNullException(nameof(source1)); if (source2 == null) throw new ArgumentNullException(nameof(source2)); if (destination == null) throw new ArgumentNullException(nameof(destination)); if (0 > (source1.Length - source1Index)) throw new ArgumentOutOfRangeException(nameof(source1Index)); if (0 > (source2.Length - source2Index)) throw new ArgumentOutOfRangeException(nameof(source2Index)); if (0 > (destination.Length - destinationIndex)) throw new ArgumentOutOfRangeException(nameof(destinationIndex)); if (length > (source1.Length - source1Index)) throw new ArgumentOutOfRangeException(nameof(length)); if (length > (source2.Length - source2Index)) throw new ArgumentOutOfRangeException(nameof(length)); if (length > (destination.Length - destinationIndex)) throw new ArgumentOutOfRangeException(nameof(length)); fixed (byte* p_x = source1, p_y = source2) { byte* t_x = p_x + source1Index, t_y = p_y + source2Index; fixed (byte* p_buffer = destination) { byte* t_buffer = p_buffer + destinationIndex; bitwiseOperation(t_x, t_y, t_buffer, length); } } }
private static void BitwiseOperation(BitwiseOperationDelegate bitwiseOperation, byte[] source1, byte[] source2, byte[] destination) { if (source1 == null) throw new ArgumentNullException(nameof(source1)); if (source2 == null) throw new ArgumentNullException(nameof(source2)); if (destination == null) throw new ArgumentNullException(nameof(destination)); // Zero { int targetRange = Math.Max(source1.Length, source2.Length); if (destination.Length > targetRange) { Unsafe.Zero(destination, targetRange, destination.Length - targetRange); } } if (source1.Length > source2.Length && destination.Length > source2.Length) { Unsafe.Copy(source1, source2.Length, destination, source2.Length, Math.Min(source1.Length, destination.Length) - source2.Length); } else if (source2.Length > source1.Length && destination.Length > source1.Length) { Unsafe.Copy(source2, source1.Length, destination, source1.Length, Math.Min(source2.Length, destination.Length) - source1.Length); } int length = Math.Min(Math.Min(source1.Length, source2.Length), destination.Length); fixed (byte* p_x = source1, p_y = source2) { fixed (byte* p_buffer = destination) { bitwiseOperation(p_x, p_y, p_buffer, length); } } }