aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/Common/NativeAllocator.cs
blob: 71c04a9b13017cc15a185aaf99305478c271c7a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Runtime.InteropServices;

namespace ARMeilleure.Common
{
    unsafe sealed class NativeAllocator : Allocator
    {
        public static NativeAllocator Instance { get; } = new();

        public override void* Allocate(ulong size)
        {
            void* result = (void*)Marshal.AllocHGlobal((IntPtr)size);

            if (result == null)
            {
                throw new OutOfMemoryException();
            }

            return result;
        }

        public override void Free(void* block)
        {
            Marshal.FreeHGlobal((IntPtr)block);
        }
    }
}