IUnityMemoryManager API 参考
在原生插件中使用内存管理员 API。
阅读时间5 分钟最后更新于 6 天前
本页面提供接口的 API 参考。
IUnityMemoryManagerCreateAllocator
声明
UnityAllocator* (UNITY_INTERFACE_API * CreateAllocator)(const char* areaName, const char* objectName);参数
参数 | 描述 |
|---|---|
| const char areaName* | 此分配器的Category的Name。 |
| const char objectName* | 此指定的分配器的Name。 |
描述
创建一个可以分配内存的新建配器Object。
DestroyAllocator
声明
void(UNITY_INTERFACE_API * DestroyAllocator)(UnityAllocator * allocator);参数
参数 | 描述 |
|---|---|
| UnityAllocator * 分配器 | 要删除的分配器。 |
描述
删除现有的分配器Object。
分配
声明
void* (UNITY_INTERFACE_API * Allocate)(UnityAllocator * allocator, size_t size, size_t align, const char* file, int32_t line);参数
参数 | 描述 |
|---|---|
| UnityAllocator * 分配器 | 使用于分配的分配器。 |
| size_t size | 要分配的内存(以字节为单位)。 |
| size_t align | 生成的指针的内存地址的 Alignment。 |
| const char 文件* | 用于进行此分配的调用的音频源文件的路径。在此处使用预定义的宏_文件_。 |
| int32_t 线 | 音频源文件中用于make此分配的调用的数量。在此处使用预定义的宏_线_。 |
描述
使用现有分配器分内存此方法返回指向新分配的内存的指针。
取消分配
声明
void(UNITY_INTERFACE_API * Deallocate)(UnityAllocator * allocator, void* ptr, const char* file, int32_t line);参数
参数 | 描述 |
|---|---|
| UnityAllocator * 分配器 | 使用于取消分配的分配器。 |
| void ptr* | 指向要取消分配的内存的指针。 |
| const char 文件* | make此取消分配的调用来自的音频源文件的路径。在此处使用预定义的宏_文件_。 |
| int32_t 线 | 音频源文件中的数量,用于make此取消分配的调用来自该文件。在此处使用预定义的宏_线_。 |
描述
取消分配指定指针指向的内存。此操作不会将指针设置为 null。
重新分配
声明
void* (UNITY_INTERFACE_API * Reallocate)(UnityAllocator * allocator, void* ptr, size_t size, size_t align, const char* file, int32_t line);参数
参数 | 描述 |
|---|---|
| UnityAllocator * 分配器 | 使用于重新分配操作的分配器。 |
| void ptr* | 指向要取消分配的内存的指针。 |
| size_t size | 要分配的内存(以字节为单位)。 |
| size_t align | 生成的指针的内存地址的 Alignment。 |
| const char 文件* | make此重新分配的调用来自的音频源文件的路径。在此处使用预定义的宏_文件_。 |
| int32_t 线 | 音频源文件中用于make此重新分配的调用的数量。在此处使用预定义的宏_线_。 |
描述
重新分配现有指针以指向不同的内存代码块。
实现示例
以下是接口的实现示例。
IUnityMemoryManager#include "IUnityInterface.h"#include "IUnityMemoryManager.h"#include <cstdint>static IUnityMemoryManager* s_MemoryManager = NULL;static UnityAllocator* s_Alloc = NULL;extern "C" void Unity_INTERFACE_导出 Unity_INTERFACE_API UnityPluginLoad(IUnityInterfaces * unityInterfaces){ s_MemoryManager = unityInterfaces->Get<IUnityMemoryManager>(); if (s_MemoryManager == NULL) 返回; // Create an allocator.这允许您在拍摄快照When在性性能分析器中查看分分配。在 Plug-ins-原生 - 插件后端分配器下 // All memory allocated here also goes under kMemNativePlugin s_Alloc = s_MemoryManager->CreateAllocator("plug-ins-native", "Plugin Backend Allocator");}extern "C" void Unity_INTERFACE_导出 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, __文件__, __线__);}