namespace System.Runtime.InteropServices; /// /// This class contains methods that are mainly used to manage native memory. /// internal static unsafe class NativeMemory { /// /// Allocates a block of memory of the specified size, in bytes. /// /// The size, in bytes, of the block to allocate. /// A pointer to the allocated block of memory. /// Thrown when allocating of memory failed. /// /// This method allows to be 0 and will return a valid pointer that should not be dereferenced and that should be passed to free to avoid memory leaks. /// This method is a thin wrapper over the C malloc API. /// public static void* Alloc(nuint byteCount) { return (void*)Marshal.AllocHGlobal(checked((int)byteCount)); } /// /// Frees a block of memory. /// /// A pointer to the block of memory that should be freed. /// /// This method does nothing if is . /// This method is a thin wrapper over the C free API. /// public static void Free(void* ptr) { Marshal.FreeHGlobal((IntPtr)ptr); } }