mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Experimental GPU overclock
This commit is contained in:
262
app/NvAPIWrapper/Native/Helpers/Structures/ValueTypeArray.cs
Normal file
262
app/NvAPIWrapper/Native/Helpers/Structures/ValueTypeArray.cs
Normal file
@@ -0,0 +1,262 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using NvAPIWrapper.Native.Interfaces;
|
||||
|
||||
namespace NvAPIWrapper.Native.Helpers.Structures
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct ValueTypeArray : IDisposable, IHandle, IEquatable<ValueTypeArray>
|
||||
{
|
||||
// ReSharper disable once ConvertToAutoProperty
|
||||
public IntPtr MemoryAddress { get; }
|
||||
|
||||
public static ValueTypeArray Null
|
||||
{
|
||||
get => new ValueTypeArray();
|
||||
}
|
||||
|
||||
public bool IsNull
|
||||
{
|
||||
get => MemoryAddress == IntPtr.Zero;
|
||||
}
|
||||
|
||||
public ValueTypeArray(IntPtr memoryAddress)
|
||||
{
|
||||
MemoryAddress = memoryAddress;
|
||||
}
|
||||
|
||||
public static ValueTypeArray FromArray(IEnumerable<object> list)
|
||||
{
|
||||
var array = list.ToArray();
|
||||
|
||||
if (array.Length > 0)
|
||||
{
|
||||
if (array[0] == null || !array[0].GetType().IsValueType)
|
||||
{
|
||||
throw new ArgumentException("Only Value Types are acceptable.", nameof(list));
|
||||
}
|
||||
|
||||
var type = array[0].GetType();
|
||||
|
||||
if (array.Any(item => item.GetType() != type))
|
||||
{
|
||||
throw new ArgumentException("Array should not hold objects of multiple types.", nameof(list));
|
||||
}
|
||||
|
||||
return FromArray(array, type);
|
||||
}
|
||||
|
||||
return Null;
|
||||
}
|
||||
|
||||
|
||||
// ReSharper disable once ExcessiveIndentation
|
||||
// ReSharper disable once MethodTooLong
|
||||
public static ValueTypeArray FromArray(IEnumerable<object> list, Type type)
|
||||
{
|
||||
var array = list.ToArray();
|
||||
|
||||
if (array.Length > 0)
|
||||
{
|
||||
var typeSize = Marshal.SizeOf(type);
|
||||
var memoryAddress = Marshal.AllocHGlobal(array.Length * typeSize);
|
||||
|
||||
if (memoryAddress != IntPtr.Zero)
|
||||
{
|
||||
var result = new ValueTypeArray(memoryAddress);
|
||||
|
||||
foreach (var item in array)
|
||||
{
|
||||
if (type == typeof(int))
|
||||
{
|
||||
Marshal.WriteInt32(memoryAddress, (int) item);
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
Marshal.WriteInt32(memoryAddress, (int) (uint) item);
|
||||
}
|
||||
else if (type == typeof(short))
|
||||
{
|
||||
Marshal.WriteInt16(memoryAddress, (short) item);
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
Marshal.WriteInt16(memoryAddress, (short) (ushort) item);
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
Marshal.WriteInt64(memoryAddress, (long) item);
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
Marshal.WriteInt64(memoryAddress, (long) (ulong) item);
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
Marshal.WriteByte(memoryAddress, (byte) item);
|
||||
}
|
||||
else if (type == typeof(IntPtr))
|
||||
{
|
||||
Marshal.WriteIntPtr(memoryAddress, (IntPtr) item);
|
||||
}
|
||||
else
|
||||
{
|
||||
Marshal.StructureToPtr(item, memoryAddress, false);
|
||||
}
|
||||
|
||||
memoryAddress += typeSize;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return Null;
|
||||
}
|
||||
|
||||
public bool Equals(ValueTypeArray other)
|
||||
{
|
||||
return MemoryAddress.Equals(other.MemoryAddress);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return obj is ValueTypeArray array && Equals(array);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return MemoryAddress.GetHashCode();
|
||||
}
|
||||
|
||||
public static bool operator ==(ValueTypeArray left, ValueTypeArray right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(ValueTypeArray left, ValueTypeArray right)
|
||||
{
|
||||
return !left.Equals(right);
|
||||
}
|
||||
|
||||
public static ValueTypeArray FromArray<T>(T[] array) where T : struct
|
||||
{
|
||||
return FromArray(array.Cast<object>());
|
||||
}
|
||||
|
||||
public T[] ToArray<T>(int count) where T : struct
|
||||
{
|
||||
return ToArray<T>(count, typeof(T));
|
||||
}
|
||||
|
||||
public T[] ToArray<T>(int count, Type type)
|
||||
{
|
||||
return ToArray<T>(0, count, type);
|
||||
}
|
||||
|
||||
public T[] ToArray<T>(int start, int count) where T : struct
|
||||
{
|
||||
return ToArray<T>(start, count, typeof(T)).ToArray();
|
||||
}
|
||||
|
||||
public T[] ToArray<T>(int start, int count, Type type)
|
||||
{
|
||||
if (IsNull)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return AsEnumerable<T>(start, count, type).ToArray();
|
||||
}
|
||||
|
||||
public IEnumerable<T> AsEnumerable<T>(int count) where T : struct
|
||||
{
|
||||
return AsEnumerable<T>(count, typeof(T));
|
||||
}
|
||||
|
||||
|
||||
public IEnumerable<T> AsEnumerable<T>(int count, Type type)
|
||||
{
|
||||
return AsEnumerable<T>(0, count, type);
|
||||
}
|
||||
|
||||
public IEnumerable<T> AsEnumerable<T>(int start, int count) where T : struct
|
||||
{
|
||||
return AsEnumerable<T>(start, count, typeof(T));
|
||||
}
|
||||
|
||||
// ReSharper disable once ExcessiveIndentation
|
||||
// ReSharper disable once MethodTooLong
|
||||
public IEnumerable<T> AsEnumerable<T>(int start, int count, Type type)
|
||||
{
|
||||
if (!IsNull)
|
||||
{
|
||||
if (!type.IsValueType)
|
||||
{
|
||||
throw new ArgumentException("Only Value Types are acceptable.", nameof(type));
|
||||
}
|
||||
|
||||
var typeSize = Marshal.SizeOf(type);
|
||||
var address = MemoryAddress + start * typeSize;
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
if (type == typeof(int))
|
||||
{
|
||||
yield return (T) (object) Marshal.ReadInt32(address);
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
yield return (T) (object) (uint) Marshal.ReadInt32(address);
|
||||
}
|
||||
else if (type == typeof(short))
|
||||
{
|
||||
yield return (T) (object) Marshal.ReadInt16(address);
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
yield return (T) (object) (ushort) Marshal.ReadInt16(address);
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
yield return (T) (object) Marshal.ReadInt64(address);
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
yield return (T) (object) (ulong) Marshal.ReadInt64(address);
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
yield return (T) (object) Marshal.ReadByte(address);
|
||||
}
|
||||
else if (type == typeof(IntPtr))
|
||||
{
|
||||
yield return (T) (object) Marshal.ReadIntPtr(address);
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return (T) Marshal.PtrToStructure(address, type);
|
||||
}
|
||||
|
||||
address += typeSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!IsNull)
|
||||
{
|
||||
Marshal.FreeHGlobal(MemoryAddress);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
128
app/NvAPIWrapper/Native/Helpers/Structures/ValueTypeArray`1.cs
Normal file
128
app/NvAPIWrapper/Native/Helpers/Structures/ValueTypeArray`1.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using NvAPIWrapper.Native.Interfaces;
|
||||
|
||||
namespace NvAPIWrapper.Native.Helpers.Structures
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct ValueTypeArray<T> : IDisposable, IHandle, IEquatable<ValueTypeArray<T>> where T : struct
|
||||
{
|
||||
private ValueTypeArray underlyingArray;
|
||||
|
||||
public IntPtr MemoryAddress
|
||||
{
|
||||
get => underlyingArray.MemoryAddress;
|
||||
}
|
||||
|
||||
public static ValueTypeArray<T> Null
|
||||
{
|
||||
get => new ValueTypeArray<T>();
|
||||
}
|
||||
|
||||
public bool IsNull
|
||||
{
|
||||
get => underlyingArray.IsNull;
|
||||
}
|
||||
|
||||
public ValueTypeArray(IntPtr memoryAddress)
|
||||
{
|
||||
underlyingArray = new ValueTypeArray(memoryAddress);
|
||||
}
|
||||
|
||||
private ValueTypeArray(ValueTypeArray underlyingArray)
|
||||
{
|
||||
this.underlyingArray = underlyingArray;
|
||||
}
|
||||
|
||||
public static ValueTypeArray<T> FromArray(T[] array)
|
||||
{
|
||||
return new ValueTypeArray<T>(ValueTypeArray.FromArray(array));
|
||||
}
|
||||
|
||||
public static ValueTypeArray<T> FromArray(IEnumerable<T> list, Type type)
|
||||
{
|
||||
return new ValueTypeArray<T>(ValueTypeArray.FromArray(list.Cast<object>(), type));
|
||||
}
|
||||
|
||||
public bool Equals(ValueTypeArray<T> other)
|
||||
{
|
||||
return underlyingArray.Equals(other.underlyingArray);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return obj is ValueTypeArray<T> array && Equals(array);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
// ReSharper disable once NonReadonlyMemberInGetHashCode
|
||||
return underlyingArray.GetHashCode();
|
||||
}
|
||||
|
||||
public static bool operator ==(ValueTypeArray<T> left, ValueTypeArray<T> right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(ValueTypeArray<T> left, ValueTypeArray<T> right)
|
||||
{
|
||||
return !left.Equals(right);
|
||||
}
|
||||
|
||||
public T[] ToArray(int count)
|
||||
{
|
||||
return underlyingArray.ToArray<T>(count, typeof(T));
|
||||
}
|
||||
|
||||
public T[] ToArray(int count, Type type)
|
||||
{
|
||||
return underlyingArray.ToArray<T>(0, count, type);
|
||||
}
|
||||
|
||||
public T[] ToArray(int start, int count)
|
||||
{
|
||||
return underlyingArray.ToArray<T>(start, count, typeof(T)).ToArray();
|
||||
}
|
||||
|
||||
public T[] ToArray(int start, int count, Type type)
|
||||
{
|
||||
return underlyingArray.ToArray<T>(start, count, type);
|
||||
}
|
||||
|
||||
public IEnumerable<T> AsEnumerable(int count)
|
||||
{
|
||||
return underlyingArray.AsEnumerable<T>(count, typeof(T));
|
||||
}
|
||||
|
||||
public IEnumerable<T> AsEnumerable(int count, Type type)
|
||||
{
|
||||
return underlyingArray.AsEnumerable<T>(0, count, type);
|
||||
}
|
||||
|
||||
public IEnumerable<T> AsEnumerable(int start, int count)
|
||||
{
|
||||
return underlyingArray.AsEnumerable<T>(start, count, typeof(T));
|
||||
}
|
||||
|
||||
public IEnumerable<T> AsEnumerable(int start, int count, Type type)
|
||||
{
|
||||
return underlyingArray.AsEnumerable<T>(start, count, type);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!IsNull)
|
||||
{
|
||||
underlyingArray.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
116
app/NvAPIWrapper/Native/Helpers/Structures/ValueTypeReference.cs
Normal file
116
app/NvAPIWrapper/Native/Helpers/Structures/ValueTypeReference.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using NvAPIWrapper.Native.Interfaces;
|
||||
|
||||
namespace NvAPIWrapper.Native.Helpers.Structures
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct ValueTypeReference : IDisposable, IHandle, IEquatable<ValueTypeReference>
|
||||
{
|
||||
// ReSharper disable once ConvertToAutoProperty
|
||||
public IntPtr MemoryAddress { get; }
|
||||
|
||||
public static ValueTypeReference Null
|
||||
{
|
||||
get => new ValueTypeReference();
|
||||
}
|
||||
|
||||
public bool IsNull
|
||||
{
|
||||
get => MemoryAddress == IntPtr.Zero;
|
||||
}
|
||||
|
||||
public ValueTypeReference(IntPtr memoryAddress)
|
||||
{
|
||||
MemoryAddress = memoryAddress;
|
||||
}
|
||||
|
||||
public static ValueTypeReference FromValueType<T>(T valueType) where T : struct
|
||||
{
|
||||
return FromValueType(valueType, typeof(T));
|
||||
}
|
||||
|
||||
public static ValueTypeReference FromValueType(object valueType, Type type)
|
||||
{
|
||||
if (!type.IsValueType)
|
||||
{
|
||||
throw new ArgumentException("Only Value Types are acceptable.", nameof(type));
|
||||
}
|
||||
|
||||
var memoryAddress = Marshal.AllocHGlobal(Marshal.SizeOf(type));
|
||||
|
||||
if (memoryAddress != IntPtr.Zero)
|
||||
{
|
||||
var result = new ValueTypeReference(memoryAddress);
|
||||
Marshal.StructureToPtr(valueType, memoryAddress, false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return Null;
|
||||
}
|
||||
|
||||
public bool Equals(ValueTypeReference other)
|
||||
{
|
||||
return MemoryAddress.Equals(other.MemoryAddress);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return obj is ValueTypeReference reference && Equals(reference);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return MemoryAddress.GetHashCode();
|
||||
}
|
||||
|
||||
public static bool operator ==(ValueTypeReference left, ValueTypeReference right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(ValueTypeReference left, ValueTypeReference right)
|
||||
{
|
||||
return !left.Equals(right);
|
||||
}
|
||||
|
||||
public T ToValueType<T>(Type type)
|
||||
{
|
||||
if (MemoryAddress == IntPtr.Zero)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
|
||||
if (!type.IsValueType)
|
||||
{
|
||||
throw new ArgumentException("Only Value Types are acceptable.", nameof(type));
|
||||
}
|
||||
|
||||
return (T) Marshal.PtrToStructure(MemoryAddress, type);
|
||||
}
|
||||
|
||||
public T? ToValueType<T>() where T : struct
|
||||
{
|
||||
if (IsNull)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return ToValueType<T>(typeof(T));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!IsNull)
|
||||
{
|
||||
Marshal.FreeHGlobal(MemoryAddress);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using NvAPIWrapper.Native.Interfaces;
|
||||
|
||||
namespace NvAPIWrapper.Native.Helpers.Structures
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct ValueTypeReference<T> : IDisposable, IHandle, IEquatable<ValueTypeReference<T>> where T : struct
|
||||
{
|
||||
private ValueTypeReference underlyingReference;
|
||||
|
||||
public IntPtr MemoryAddress
|
||||
{
|
||||
get => underlyingReference.MemoryAddress;
|
||||
}
|
||||
|
||||
public static ValueTypeReference<T> Null
|
||||
{
|
||||
get => new ValueTypeReference<T>();
|
||||
}
|
||||
|
||||
public bool IsNull
|
||||
{
|
||||
get => underlyingReference.IsNull;
|
||||
}
|
||||
|
||||
public ValueTypeReference(IntPtr memoryAddress)
|
||||
{
|
||||
underlyingReference = new ValueTypeReference(memoryAddress);
|
||||
}
|
||||
|
||||
private ValueTypeReference(ValueTypeReference underlyingReference)
|
||||
{
|
||||
this.underlyingReference = underlyingReference;
|
||||
}
|
||||
|
||||
public static ValueTypeReference<T> FromValueType(T valueType)
|
||||
{
|
||||
return new ValueTypeReference<T>(ValueTypeReference.FromValueType(valueType));
|
||||
}
|
||||
|
||||
public static ValueTypeReference<T> FromValueType(object valueType, Type type)
|
||||
{
|
||||
return new ValueTypeReference<T>(ValueTypeReference.FromValueType(valueType, type));
|
||||
}
|
||||
|
||||
public bool Equals(ValueTypeReference<T> other)
|
||||
{
|
||||
return underlyingReference.Equals(other.underlyingReference);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return obj is ValueTypeReference<T> reference && Equals(reference);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
// ReSharper disable once NonReadonlyMemberInGetHashCode
|
||||
return underlyingReference.GetHashCode();
|
||||
}
|
||||
|
||||
public static bool operator ==(ValueTypeReference<T> left, ValueTypeReference<T> right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(ValueTypeReference<T> left, ValueTypeReference<T> right)
|
||||
{
|
||||
return !left.Equals(right);
|
||||
}
|
||||
|
||||
public T ToValueType(Type type)
|
||||
{
|
||||
return underlyingReference.ToValueType<T>(type);
|
||||
}
|
||||
|
||||
public T? ToValueType()
|
||||
{
|
||||
return underlyingReference.ToValueType<T>();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!IsNull)
|
||||
{
|
||||
underlyingReference.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user