Match C++ managed memory alloc with C# for scripting

This commit is contained in:
Wojtek Figat
2023-02-27 22:56:15 +01:00
parent 6f9f40192e
commit 499df770cc
4 changed files with 21 additions and 6 deletions

View File

@@ -1098,6 +1098,18 @@ namespace FlaxEngine
nativeLibraryPaths[moduleName] = modulePath; 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) internal static void* NativeAlloc(int byteCount)
{ {
return NativeMemory.AlignedAlloc((UIntPtr)byteCount, 16); return NativeMemory.AlignedAlloc((UIntPtr)byteCount, 16);

View File

@@ -129,14 +129,18 @@ void CoreCLR::RegisterNativeLibrary(const char* moduleName, const char* modulePa
CoreCLR::CallStaticMethod<void, const char*, const char*>(RegisterNativeLibraryPtr, moduleName, modulePath); CoreCLR::CallStaticMethod<void, const char*, const char*>(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<void*, int32>(AllocMemoryPtr, size);
} }
void CoreCLR::Free(void* ptr) void CoreCLR::Free(void* ptr)
{ {
Platform::Free(ptr); if (!ptr)
return;
static void* FreeMemoryPtr = CoreCLR::GetStaticMethodPointer(TEXT("FreeMemory"));
CoreCLR::CallStaticMethod<void, void*>(FreeMemoryPtr, ptr);
} }
#endif #endif

View File

@@ -49,7 +49,7 @@ public:
static void RegisterNativeLibrary(const char* moduleName, const char* modulePath); static void RegisterNativeLibrary(const char* moduleName, const char* modulePath);
static const char* GetClassFullname(void* klass); static const char* GetClassFullname(void* klass);
static void* Allocate(int size); static void* Allocate(int32 size);
static void Free(void* ptr); static void Free(void* ptr);
static MGCHandle NewGCHandle(void* obj, bool pinned); static MGCHandle NewGCHandle(void* obj, bool pinned);
static MGCHandle NewGCHandleWeakref(void* obj, bool track_resurrection); static MGCHandle NewGCHandleWeakref(void* obj, bool track_resurrection);

View File

@@ -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) MONO_API void mono_free(void* ptr)
{ {
if (ptr != nullptr) CoreCLR::Free(ptr);
CoreCLR::Free(ptr);
} }
/* /*