This pages provides the API reference for the IUnityMemoryManager
interface.
UnityAllocator* (UNITY_INTERFACE_API * CreateAllocator)(const char* areaName, const char* objectName);
参数 | 描述 |
---|---|
const char* areaName | The name for the broad category for this allocator. |
const char* objectName | The name for this specific allocator. |
Creates a new Allocator object which can allocate blocks of memory.
void(UNITY_INTERFACE_API * DestroyAllocator)(UnityAllocator * allocator);
参数 | 描述 |
---|---|
UnityAllocator * allocator | The allocator to delete. |
Deletes an existing Allocator object.
void* (UNITY_INTERFACE_API * Allocate)(UnityAllocator * allocator, size_t size, size_t align, const char* file, int32_t line);
参数 | 描述 |
---|---|
UnityAllocator * allocator | The allocator to use for allocation. |
size_t size | How much memory to allocate, in bytes. |
size_t align | The alignment of the memory address for the resulting pointer. |
const char* file | The path to the source file where the call to make this allocation came from. Use the predefined macro FILE here. |
int32_t line | The line number in the source file where the call to make this allocation came from. Use the predefined macro LINE here. |
Allocates a block of memory using an existing allocator. This method returns a pointer to the newly allocated memory.
void(UNITY_INTERFACE_API * Deallocate)(UnityAllocator * allocator, void* ptr, const char* file, int32_t line);
参数 | 描述 |
---|---|
UnityAllocator * allocator | The allocator to use for deallocation. |
void* ptr | The pointer to the memory to be deallocated. |
const char* file | The path to the source file where the call to make this deallocation came from. Use the predefined macro FILE here. |
int32_t line | The line number in the source file where the call to make this deallocation came from. Use the predefined macro LINE here. |
Deallocates the memory that the specified pointer points to. This doesn’t set the pointer to NULL.
void* (UNITY_INTERFACE_API * Reallocate)(UnityAllocator * allocator, void* ptr, size_t size, size_t align, const char* file, int32_t line);
参数 | 描述 |
---|---|
UnityAllocator * allocator | The allocator to use for the reallocation operation. |
void* ptr | The pointer to the memory to be deallocated. |
size_t size | How much memory to allocate, in bytes. |
size_t align | The alignment of the memory address for the resulting pointer. |
const char* file | The path to the source file where the call to make this reallocation came from. Use the predefined macro FILE here. |
int32_t line | The line number in the source file where the call to make this reallocation came from. Use the predefined macro LINE here. |
Reallocates an existing pointer to point to a different block of memory.
Below is an example implementation of the IUnityMemoryManager
interface.
#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 NativePlugins - Plugin Backend Allocator
// All memory allocated here also goes under kMemNativePlugin
s_Alloc = s_MemoryManager->CreateAllocator("NativePlugins", "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__);
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.