# IUnityMemoryManager API リファレンス

> Use the memory manager API in native plug-ins.

このページは `IUnityMemoryManager` インターフェースの API リファレンスを提供します。

## CreateAllocator

### 宣言

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

### パラメーター

| **パラメーター**                  | **説明**                 |
| --------------------------- | ---------------------- |
| **const char* areaName*\*   | このアロケーターの大きなカテゴリの名前です。 |
| **const char* objectName*\* | この特定のアロケーターの名前です。      |

### 説明

メモリブロックを割り当てることができる Allocator オブジェクトを新しく作成します。

## DestroyAllocator

### 宣言

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

### パラメーター

| **パラメーター**                      | **説明**        |
| ------------------------------- | ------------- |
| **UnityAllocator \* allocator** | 削除するアロケーターです。 |

### 説明

既存の Allocator オブジェクトを削除します。

## Allocate

### 宣言

`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* を使用します。 |

### 説明

既存のアロケーターを使用してメモリのブロックを割り当てます。このメソッドは、新しく割り当てられたメモリへのポインターを返します。

## Deallocate

### 宣言

`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 には設定しません。

## Reallocate

### 宣言

`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` インターフェースの実装例です。

```lang-csharp
#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__);
}
```

## 追加リソース

* [低レベルのネイティブプラグインレンダリング拡張](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-rendering-extensions.md)
* [低レベルのネイティブプラグインのシェーダーコンパイラーへのアクセス](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-shader-compiler-access.md)
* [低レベルのネイティブプラグイン用メモリマネージャー API](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-memory-manager-api.md)
