Class Utilities
Utility class.
public static class Utilities
- Inheritance
-
objectUtilities
Methods
AllocateClearedMemory(int, byte, int)
Allocate an aligned memory buffer and clear it with a specified value (0 by defaault).
public static nint AllocateClearedMemory(int sizeInBytes, byte clearValue = 0, int align = 16)
Parameters
sizeInBytesintSize of the buffer to allocate.
clearValuebyteDefault value used to clear the buffer.
alignintAlignment, 16 bytes by default.
Returns
- nint
A pointer to a buffer aligned.
Remarks
To free this buffer, call FreeMemory(nint)
AllocateMemory(int, int)
Allocate an aligned memory buffer.
public static nint AllocateMemory(int sizeInBytes, int align = 16)
Parameters
sizeInBytesintSize of the buffer to allocate.
alignintAlignment, a positive value which is a power of 2. 16 bytes by default.
Returns
- nint
A pointer to a buffer aligned.
Remarks
To free this buffer, call FreeMemory(nint)
Clear(void*, uint)
Zero out memory at the address provided
public static void Clear(void* startAddress, uint byteCount)
Parameters
Compare<TKey, TValue>(SortedList<TKey, TValue>, SortedList<TKey, TValue>)
Compares two collection, element by elements.
public static bool Compare<TKey, TValue>(SortedList<TKey, TValue> first, SortedList<TKey, TValue> second)
Parameters
firstSortedList<TKey, TValue>The collection to compare from.
secondSortedList<TKey, TValue>The colllection to compare to.
Returns
- bool
True if lists are identical (but not necessarily in the same order). False otherwise.
Type Parameters
TKeyTValue
Remarks
Concrete SortedList is favored over interface to avoid enumerator object allocation.
Compare<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>)
Compares two collection, element by elements.
public static bool Compare<TKey, TValue>(IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
Parameters
firstIDictionary<TKey, TValue>The collection to compare from.
secondIDictionary<TKey, TValue>The colllection to compare to.
Returns
- bool
True if lists are identical (but no necessarely of the same time). False otherwise.
Type Parameters
TKeyTValue
ConvertRawToTimestamp(long)
public static TimeSpan ConvertRawToTimestamp(long delta)
Parameters
deltalongThe delta.
Returns
CopyWithAlignmentFallback(ref byte, ref readonly byte, uint)
Copies bytes from the source address to the destination address.
public static void CopyWithAlignmentFallback(ref byte destination, ref readonly byte source, uint byteCount)
Parameters
destinationbyteThe managed pointer corresponding to the destination address to copy to.
sourcebyteThe managed pointer corresponding to the source address to copy from.
byteCountuintThe number of bytes to copy.
Remarks
Some of the architecture dotnet runs on do not support arbitrary unaligned reads or writes, use this instead of other memcopy if you aren't sure whether the pointers you passed in are aligned.
CopyWithAlignmentFallback(void*, void*, uint)
Copies bytes from the source address to the destination address.
public static void CopyWithAlignmentFallback(void* destination, void* source, uint byteCount)
Parameters
destinationvoid*The managed pointer corresponding to the destination address to copy to.
sourcevoid*The managed pointer corresponding to the source address to copy from.
byteCountuintThe number of bytes to copy.
Remarks
Some of the architecture dotnet runs on do not support arbitrary unaligned reads or writes, use this instead of other memcopy if you aren't sure whether the pointers you passed in are aligned.
Dispose<T>(ref T?)
If non-null, disposes the specified object and set it to null, otherwise do nothing.
public static void Dispose<T>(ref T? disposable) where T : class, IDisposable
Parameters
disposableTThe disposable.
Type Parameters
T
FreeMemory(nint)
Free an aligned memory buffer.
public static void FreeMemory(nint alignedBuffer)
Parameters
alignedBuffernint
Remarks
The buffer must have been allocated with AllocateMemory(int, int)
GetHashCode(IDictionary)
Computes a hashcode for a dictionary.
public static int GetHashCode(IDictionary dict)
Parameters
dictIDictionary
Returns
- int
Hashcode for the list.
GetHashCode(IEnumerable)
Computes a hashcode for an enumeration
public static int GetHashCode(IEnumerable it)
Parameters
itIEnumerableAn enumerator.
Returns
- int
Hashcode for the list.
GetHashCode(IEnumerator)
Computes a hashcode for an enumeration
public static int GetHashCode(IEnumerator it)
Parameters
itIEnumeratorAn enumerator.
Returns
- int
Hashcode for the list.
IsMemoryAligned(nint, int)
Determines whether the specified memory pointer is aligned in memory.
public static bool IsMemoryAligned(nint memoryPtr, int align = 16)
Parameters
Returns
- bool
trueif the specified memory pointer is aligned in memory; otherwise,false.
IterateTree<T>(T, Func<T, IEnumerable<T>>)
Linq assisted full tree iteration and collection in a single line. Warning, could be slow.
public static IEnumerable<T> IterateTree<T>(T root, Func<T, IEnumerable<T>> childrenF)
Parameters
rootTThe root item
childrenFFunc<T, IEnumerable<T>>The function to retrieve a child
Returns
- IEnumerable<T>
Type Parameters
TThe type to iterate.
ReadStream(Stream)
Read stream to a byte[] buffer
[Obsolete("Allocates. Read into the destination.")]
public static byte[] ReadStream(Stream stream)
Parameters
streamStreaminput stream
Returns
- byte[]
a byte[] buffer
Swap<T>(ref T, ref T)
Swaps the value between two references.
public static void Swap<T>(ref T left, ref T right)
Parameters
leftTThe left value.
rightTThe right value.
Type Parameters
TType of a data to swap.