aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/Common/NativeAllocator.cs
blob: 93c48adda648648d39413826525a7125a44bda11 (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);
        }
    }
}