|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: ExtendedSystemObjects.Helper |
| 4 | + * FILE: ExtendedSystemObjects.Helper/UnmanagedMemoryHelper.cs |
| 5 | + * PURPOSE: Provides helper methods for low-level unmanaged memory operations. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Runtime.CompilerServices; |
| 11 | +using System.Runtime.InteropServices; |
| 12 | + |
| 13 | +namespace ExtendedSystemObjects.Helper |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Provides helper methods for allocating, reallocating, and clearing unmanaged memory blocks. |
| 17 | + /// Designed for use with value types (unmanaged types) only. |
| 18 | + /// </summary> |
| 19 | + internal static class UnmanagedMemoryHelper |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// Allocates a block of unmanaged memory large enough to hold the specified number of elements of type <typeparamref name="T"/>. |
| 23 | + /// </summary> |
| 24 | + /// <typeparam name="T">The unmanaged value type to allocate memory for.</typeparam> |
| 25 | + /// <param name="count">The number of elements to allocate.</param> |
| 26 | + /// <returns>A pointer to the allocated unmanaged memory.</returns> |
| 27 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 28 | + internal static IntPtr Allocate<T>(int count) where T : unmanaged |
| 29 | + { |
| 30 | + unsafe |
| 31 | + { |
| 32 | + return Marshal.AllocHGlobal(sizeof(T) * count); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Reallocates an existing block of unmanaged memory to hold a new number of elements of type <typeparamref name="T"/>. |
| 38 | + /// </summary> |
| 39 | + /// <typeparam name="T">The unmanaged value type used in the memory block.</typeparam> |
| 40 | + /// <param name="ptr">A pointer to the existing unmanaged memory block.</param> |
| 41 | + /// <param name="newCount">The new number of elements to accommodate.</param> |
| 42 | + /// <returns>A pointer to the newly reallocated unmanaged memory block.</returns> |
| 43 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 44 | + internal static IntPtr Reallocate<T>(IntPtr ptr, int newCount) where T : unmanaged |
| 45 | + { |
| 46 | + unsafe |
| 47 | + { |
| 48 | + return Marshal.ReAllocHGlobal(ptr, (IntPtr)(sizeof(T) * newCount)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Clears a block of unmanaged memory by setting its contents to zero. |
| 54 | + /// </summary> |
| 55 | + /// <typeparam name="T">The unmanaged value type used in the memory block.</typeparam> |
| 56 | + /// <param name="buffer">A pointer to the unmanaged memory block.</param> |
| 57 | + /// <param name="count">The number of elements to clear.</param> |
| 58 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 59 | + internal static void Clear<T>(IntPtr buffer, int count) where T : unmanaged |
| 60 | + { |
| 61 | + unsafe |
| 62 | + { |
| 63 | + Span<T> span = new(buffer.ToPointer(), count); |
| 64 | + span.Clear(); // Equivalent to memset 0 |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Shifts the right. Adding data at index. |
| 70 | + /// </summary> |
| 71 | + /// <typeparam name="T"></typeparam> |
| 72 | + /// <param name="ptr">The PTR.</param> |
| 73 | + /// <param name="index">The index.</param> |
| 74 | + /// <param name="count">The count.</param> |
| 75 | + /// <param name="length">The length.</param> |
| 76 | + /// <param name="capacity">The capacity.</param> |
| 77 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 78 | + public static unsafe void ShiftRight<T>(T* ptr, int index, int count, int length, int capacity) where T : unmanaged |
| 79 | + { |
| 80 | + int elementsToShift = length - index; |
| 81 | + if (elementsToShift <= 0) return; |
| 82 | + |
| 83 | + Buffer.MemoryCopy( |
| 84 | + source: ptr + index, |
| 85 | + destination: ptr + index + count, |
| 86 | + destinationSizeInBytes: (capacity - index - count) * sizeof(T), |
| 87 | + sourceBytesToCopy: elementsToShift * sizeof(T)); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Shifts the left. Delete Element at index |
| 92 | + /// </summary> |
| 93 | + /// <typeparam name="T"></typeparam> |
| 94 | + /// <param name="ptr">The PTR.</param> |
| 95 | + /// <param name="index">The index.</param> |
| 96 | + /// <param name="count">The count.</param> |
| 97 | + /// <param name="length">The length.</param> |
| 98 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 99 | + public static unsafe void ShiftLeft<T>(T* ptr, int index, int count, int length) where T : unmanaged |
| 100 | + { |
| 101 | + int elementsToShift = length - (index + count); |
| 102 | + if (elementsToShift <= 0) return; |
| 103 | + |
| 104 | + Buffer.MemoryCopy( |
| 105 | + source: ptr + index + count, |
| 106 | + destination: ptr + index, |
| 107 | + destinationSizeInBytes: elementsToShift * sizeof(T), |
| 108 | + sourceBytesToCopy: elementsToShift * sizeof(T)); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments