NativeAllocator.cs 615 B

123456789101112131415161718192021222324252627
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace ARMeilleure.Common
  4. {
  5. unsafe sealed class NativeAllocator : Allocator
  6. {
  7. public static NativeAllocator Instance { get; } = new();
  8. public override void* Allocate(ulong size)
  9. {
  10. void* result = (void*)Marshal.AllocHGlobal((IntPtr)size);
  11. if (result == null)
  12. {
  13. throw new OutOfMemoryException();
  14. }
  15. return result;
  16. }
  17. public override void Free(void* block)
  18. {
  19. Marshal.FreeHGlobal((IntPtr)block);
  20. }
  21. }
  22. }