Version: 6000.3
语言: 中文
用于内存管理的原生插件 API
用于分析的本机插件 API

IUnityMemoryManager API 参考

此页面提供了IUnityMemoryManager接口。

创建分配器

声明

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

参数

参数 描述
const char* areaName 此分配器的广泛类别的名称。
const char* 对象名称 此特定分配器的名称。

描述

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

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 * 分配器 用于分配的分配器。
size_t尺寸 要分配多少内存,以字节为单位。
size_t对齐 生成指针的内存地址对齐方式。
const char* 文件 源文件的路径,用于进行此分配的调用来自该文件。在此处使用预定义的宏 FILE
int32_t线 源文件中进行此分配的调用来自的行号。在此处使用预定义的宏 LINE

描述

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

解除分配

声明

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

参数

参数 描述
UnityAllocator * 分配器 用于解除分配的分配器。
无效* PTR 指向要解除分配的内存的指针。
const char* 文件 源文件的路径,用于进行此取消分配的调用来自该文件。在此处使用预定义的宏 FILE
int32_t线 源文件中用于进行此取消分配的调用的行号。在此处使用预定义的宏 LINE

描述

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

重新分配

声明

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

参数

参数 描述
UnityAllocator * 分配器 用于重新分配作的分配器。
无效* PTR 指向要解除分配的内存的指针。
size_t尺寸 要分配多少内存,以字节为单位。
size_t对齐 生成指针的内存地址对齐方式。
const char* 文件 源文件的路径,用于进行此重新分配的调用来自该文件。在此处使用预定义的宏 FILE
int32_t线 源文件中进行此重新分配的调用的行号。在此处使用预定义的宏 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__);
}

其他资源

用于内存管理的原生插件 API
用于分析的本机插件 API