文档

Unity Engine

Unity Engine

IUnityMemoryManager API 参考

Use the memory manager API in native plug-ins.
阅读时间4 分钟最后更新于 1 个月前

本页面提供
IUnityMemoryManager
接口的 API 参考。

CreateAllocator

声明

UnityAllocator* (UNITY_INTERFACE_API * CreateAllocator)(const char* areaName, const char* objectName);

参数

参数

描述

const char areaName*内存分配器所属大类别的名称。
const char objectName*此特定分配器的名称。

描述

创建一个可以分配内存块的新分配器对象。

DestroyAllocator

声明

void(UNITY_INTERFACE_API * DestroyAllocator)(UnityAllocator * allocator);

参数

参数

描述

UnityAllocator * allocator要删除的分配器。

描述

删除现有的分配器对象。

分配

声明

void* (UNITY_INTERFACE_API * Allocate)(UnityAllocator * allocator, size_t size, size_t align, const char* file, int32_t line);

参数

参数

描述

UnityAllocator * allocator用于分配的分配器。
size_t size要分配的内存量(以字节为单位)。
size_t align最终指针所指向内存地址的对齐方式。
const char file*发起该内存分配调用的源文件路径。在此处使用预定义的宏 FILE。
int32_t line发起该内存分配调用的代码在源文件中所处的行号。在此处使用预定义的宏 LINE。

描述

使用现有分配器分配内存块。此方法返回指向新分配的内存的指针。

取消分配

声明

void(UNITY_INTERFACE_API * Deallocate)(UnityAllocator * allocator, void* ptr, const char* file, int32_t line);

参数

参数

描述

UnityAllocator * allocator用于取消分配的分配器。
void ptr*指向要取消分配的内存的指针。
const char file*发起该内存取消分配调用的源文件路径。在此处使用预定义的宏 FILE。
int32_t line发起该内存取消分配调用的代码在源文件中所处的行号。在此处使用预定义的宏 LINE。

描述

取消分配指定指针指向的内存。此操作不会将指针设置为 NULL。

重新分配

声明

void* (UNITY_INTERFACE_API * Reallocate)(UnityAllocator * allocator, void* ptr, size_t size, size_t align, const char* file, int32_t line);

参数

参数

描述

UnityAllocator * allocator用于重新分配操作的分配器。
void ptr*指向要取消分配的内存的指针。
size_t size要分配的内存量(以字节为单位)。
size_t align最终指针所指向内存地址的对齐方式。
const char file*发起该内存重新分配调用的源文件路径。在此处使用预定义的宏 FILE。
int32_t line发起该内存重新分配调用的代码在源文件中所处的行号。在此处使用预定义的宏 LINE。

描述

重新分配现有指针以指向不同的内存块。

实现示例

以下是
IUnityMemoryManager
接口的实现示例。
#include "IUnityInterface.h"#include "IUnityMemoryManager.h"#include <cstdint>static IUnityMemoryManager* s_MemoryManager = NULL;static UnityAllocator* s_Alloc = NULL;extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces * unityInterfaces){ s_MemoryManager = unityInterfaces->Get<IUnityMemoryManager>(); if (s_MemoryManager == NULL) return; // Create an allocator. This allows you to see the allocation root in the profiler when taking snapshots. Under plug-ins-native - Plugin Backend Allocator // All memory allocated here also goes under kMemNativePlugin s_Alloc = s_MemoryManager->CreateAllocator("plug-ins-native", "Plugin Backend Allocator");}extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload(){ //Free allocator s_MemoryManager->DestroyAllocator(s_Alloc); s_Alloc = NULL; s_MemoryManager = NULL;}void DoMemoryOperations(){ // Allocate 1KB memory void* mem = s_MemoryManager->Allocate(s_Alloc, 1 * 1024, 16, __FILE__, __LINE__); // Reallocate the same pointer with 2KB mem = s_MemManager->Reallocate(s_Alloc, mem, 2 * 1024, 16, __FILE__, __LINE__); // Delete allocated memory s_MemoryManager->Deallocate(s_Alloc, mem, __FILE__, __LINE__);}

其他资源