From 499df770cc06d51380821e39392d7a28300d006b Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 27 Feb 2023 22:56:15 +0100 Subject: [PATCH] Match C++ managed memory alloc with C# for scripting --- Source/Engine/Engine/NativeInterop.cs | 12 ++++++++++++ Source/Engine/Scripting/DotNet/CoreCLR.cpp | 10 +++++++--- Source/Engine/Scripting/DotNet/CoreCLR.h | 2 +- Source/Engine/Scripting/DotNet/MonoApi.cpp | 3 +-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Source/Engine/Engine/NativeInterop.cs b/Source/Engine/Engine/NativeInterop.cs index 77445e8e5..51ec8c55f 100644 --- a/Source/Engine/Engine/NativeInterop.cs +++ b/Source/Engine/Engine/NativeInterop.cs @@ -1098,6 +1098,18 @@ namespace FlaxEngine nativeLibraryPaths[moduleName] = modulePath; } + [UnmanagedCallersOnly] + internal static void* AllocMemory(int size) + { + return NativeMemory.AlignedAlloc((UIntPtr)size, 16); + } + + [UnmanagedCallersOnly] + internal static void FreeMemory(void* ptr) + { + NativeMemory.AlignedFree(ptr); + } + internal static void* NativeAlloc(int byteCount) { return NativeMemory.AlignedAlloc((UIntPtr)byteCount, 16); diff --git a/Source/Engine/Scripting/DotNet/CoreCLR.cpp b/Source/Engine/Scripting/DotNet/CoreCLR.cpp index e0e0a768d..da5b74860 100644 --- a/Source/Engine/Scripting/DotNet/CoreCLR.cpp +++ b/Source/Engine/Scripting/DotNet/CoreCLR.cpp @@ -129,14 +129,18 @@ void CoreCLR::RegisterNativeLibrary(const char* moduleName, const char* modulePa CoreCLR::CallStaticMethod(RegisterNativeLibraryPtr, moduleName, modulePath); } -void* CoreCLR::Allocate(int size) +void* CoreCLR::Allocate(int32 size) { - return Platform::Allocate(size, 16); + static void* AllocMemoryPtr = CoreCLR::GetStaticMethodPointer(TEXT("AllocMemory")); + return CoreCLR::CallStaticMethod(AllocMemoryPtr, size); } void CoreCLR::Free(void* ptr) { - Platform::Free(ptr); + if (!ptr) + return; + static void* FreeMemoryPtr = CoreCLR::GetStaticMethodPointer(TEXT("FreeMemory")); + CoreCLR::CallStaticMethod(FreeMemoryPtr, ptr); } #endif diff --git a/Source/Engine/Scripting/DotNet/CoreCLR.h b/Source/Engine/Scripting/DotNet/CoreCLR.h index 60ad1742e..2bd15e15b 100644 --- a/Source/Engine/Scripting/DotNet/CoreCLR.h +++ b/Source/Engine/Scripting/DotNet/CoreCLR.h @@ -49,7 +49,7 @@ public: static void RegisterNativeLibrary(const char* moduleName, const char* modulePath); static const char* GetClassFullname(void* klass); - static void* Allocate(int size); + static void* Allocate(int32 size); static void Free(void* ptr); static MGCHandle NewGCHandle(void* obj, bool pinned); static MGCHandle NewGCHandleWeakref(void* obj, bool track_resurrection); diff --git a/Source/Engine/Scripting/DotNet/MonoApi.cpp b/Source/Engine/Scripting/DotNet/MonoApi.cpp index 53d1b851e..9e1e6d85b 100644 --- a/Source/Engine/Scripting/DotNet/MonoApi.cpp +++ b/Source/Engine/Scripting/DotNet/MonoApi.cpp @@ -1450,8 +1450,7 @@ MONO_API MONO_RT_EXTERNAL_ONLY MonoMethod* mono_class_get_method_from_name(MonoC MONO_API void mono_free(void* ptr) { - if (ptr != nullptr) - CoreCLR::Free(ptr); + CoreCLR::Free(ptr); } /*