Editor/Embedded/ZstdSharp/Unsafe/Methods.Allocations.cs

Part of an embedded ZstdSharp unsafe helper for editor code. Provides wrappers for custom memory allocation and free, calling either provided function pointers in ZSTD_customMem or falling back to internal malloc/free via UnsafeHelper.

Native InteropReflection
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using static HumanoidRetargeter.EditorTools.Embedded.ZstdSharp.UnsafeHelper;

namespace HumanoidRetargeter.EditorTools.Embedded.ZstdSharp.Unsafe;

    public static unsafe partial class Methods
    {
        /* custom memory allocation functions */
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static void* ZSTD_customMalloc(nuint size, ZSTD_customMem customMem)
        {
            if (customMem.customAlloc != null)
                return ((delegate* managed<void*, nuint, void*>)customMem.customAlloc)(customMem.opaque, size);
            return malloc(size);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
        {
            if (ptr != null)
            {
                if (customMem.customFree != null)
                    ((delegate* managed<void*, void*, void>)customMem.customFree)(customMem.opaque, ptr);
                else
                    free(ptr);
            }
        }
    }