1e41f4b71Sopenharmony_ci# Purgeable Memory Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciOpenHarmony provides Purgeable Memory management, which enables you to use related APIs to create **PurgeableMemory** objects to manage purgeable memory. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciYou can use the native APIs to apply for and release purgeable memory. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciThe following scenarios are common for the development of purgeable memory management: 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci* Apply for a **PurgeableMemory** object and write data to the object. 14e41f4b71Sopenharmony_ci* Release the **PurgeableMemory** object when it is no longer required. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Available APIs 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci| API| Description| 19e41f4b71Sopenharmony_ci| -------- | -------- | 20e41f4b71Sopenharmony_ci| OH_PurgeableMemory \*OH_PurgeableMemory_Create(size_t size, OH_PurgeableMemory_ModifyFunc func, void \*funcPara) | Creates a **PurgeableMemory** object. A new **PurgeableMemory** object is generated each time this API is called.| 21e41f4b71Sopenharmony_ci| bool OH_PurgeableMemory_Destroy(OH_PurgeableMemory \*purgObj) | Destroys a **PurgeableMemory** object.| 22e41f4b71Sopenharmony_ci| bool OH_PurgeableMemory_BeginRead(OH_PurgeableMemory \*purgObj) | Begins a read operation on a **PurgeableMemory** object.| 23e41f4b71Sopenharmony_ci| void OH_PurgeableMemory_EndRead(OH_PurgeableMemory \*purgObj) | Ends a read operation on a **PurgeableMemory** object and decreases the reference count of the object by 1. When the reference count reaches 0, the object can be reclaimed by the system.| 24e41f4b71Sopenharmony_ci|bool OH_PurgeableMemory_BeginWrite(OH_PurgeableMemory \*purgObj) | Begins a write operation on a **PurgeableMemory** object.| 25e41f4b71Sopenharmony_ci|void OH_PurgeableMemory_EndWrite(OH_PurgeableMemory \*purgObj)|Ends a write operation on a **PurgeableMemory** object and decreases the reference count of the object by 1. When the reference count reaches 0, the object can be reclaimed by the system.| 26e41f4b71Sopenharmony_ci|void \*OH_PurgeableMemory_GetContent(OH_PurgeableMemory \*purgObj)|Obtains the memory data of a **PurgeableMemory** object.| 27e41f4b71Sopenharmony_ci|size_t OH_PurgeableMemory_ContentSize(OH_PurgeableMemory \*purgObj)|Obtains the memory data size of a **PurgeableMemory** object.| 28e41f4b71Sopenharmony_ci|bool OH_PurgeableMemory_AppendModify(OH_PurgeableMemory \*purgObj, OH_PurgeableMemory_ModifyFunc func, void \*funcPara)|Adds a function for modifying a **PurgeableMemory** object.| 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## How to Develop 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciThe following walks you through on how to use the native purgeable memory APIs to apply for a **PurgeableMemory** object, write data to the object, and read data from the object. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci1. Declare the rules for creating a **PurgeableMemory** object. 36e41f4b71Sopenharmony_ci ```c++ 37e41f4b71Sopenharmony_ci // Declare the parameters of the constructor. 38e41f4b71Sopenharmony_ci struct ParaData{ 39e41f4b71Sopenharmony_ci int start; 40e41f4b71Sopenharmony_ci int end; 41e41f4b71Sopenharmony_ci }; 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci // Declare a function for modifying the object. 44e41f4b71Sopenharmony_ci bool FactorialFunc(void* data, size_t size, void* param){ 45e41f4b71Sopenharmony_ci bool ret = true; 46e41f4b71Sopenharmony_ci ParaData *pdata = (ParaData*) param; 47e41f4b71Sopenharmony_ci int* oriData = (int*)data; 48e41f4b71Sopenharmony_ci int i = pdata->start; 49e41f4b71Sopenharmony_ci while(i<pdata->end){ 50e41f4b71Sopenharmony_ci *oriData *= i; 51e41f4b71Sopenharmony_ci i++; 52e41f4b71Sopenharmony_ci } 53e41f4b71Sopenharmony_ci return ret; 54e41f4b71Sopenharmony_ci } 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci // Declare the parameters of the extended function for modifying the object. 57e41f4b71Sopenharmony_ci struct AppendParaData{ 58e41f4b71Sopenharmony_ci int newPara; 59e41f4b71Sopenharmony_ci }; 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci // Declare the extended function for modifying the object. 62e41f4b71Sopenharmony_ci bool AddFunc(void* data, size_t size, void* param){ 63e41f4b71Sopenharmony_ci bool ret = true; 64e41f4b71Sopenharmony_ci int *oriDatap = (int*) data; 65e41f4b71Sopenharmony_ci AppendParaData* apData = (AppendParaData*)param; 66e41f4b71Sopenharmony_ci *oriDatap += apData->newPara; 67e41f4b71Sopenharmony_ci return ret; 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci ``` 70e41f4b71Sopenharmony_ci2. Create a **PurgeableMemory** object. 71e41f4b71Sopenharmony_ci ```c++ 72e41f4b71Sopenharmony_ci // Define the memory data size to 4 MB. 73e41f4b71Sopenharmony_ci #define DATASIZE (4 * 1024 * 1024) 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci // Declare the parameters of the constructor. 76e41f4b71Sopenharmony_ci struct ParaData pdata = {1,2}; 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci // Create a PurgeableMemory object. 79e41f4b71Sopenharmony_ci OH_PurgeableMemory* pPurgmem = OH_PurgeableMemory_Create(DATASIZE, FactorialFunc, &pdata); 80e41f4b71Sopenharmony_ci ``` 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci3. Perform a read operation on the **PurgeableMemory** object. 83e41f4b71Sopenharmony_ci ```c++ 84e41f4b71Sopenharmony_ci // Define an object type based on the service requirements. 85e41f4b71Sopenharmony_ci class ReqObj; 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci // Begin a read operation on the object. 88e41f4b71Sopenharmony_ci if(OH_PurgeableMemory_BeginRead(pPurgmem)) { 89e41f4b71Sopenharmony_ci // Obtain the object size. 90e41f4b71Sopenharmony_ci size_t size = OH_PurgeableMemory_ContentSize(pPurgmem); 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci // Obtain the object content. 93e41f4b71Sopenharmony_ci ReqObj* pReqObj = (ReqObj*) OH_PurgeableMemory_GetContent(pPurgmem); 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci // End the read operation on the object. 96e41f4b71Sopenharmony_ci OH_PurgeableMemory_EndRead(pPurgmem); 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci ``` 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci4. Perform a write operation on the **PurgeableMemory** object. 101e41f4b71Sopenharmony_ci ```c++ 102e41f4b71Sopenharmony_ci // Define an object type based on the service requirements. 103e41f4b71Sopenharmony_ci class ReqObj; 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci // Begin a write operation on the object. 106e41f4b71Sopenharmony_ci if(OH_PurgeableMemory_BeginWrite(pPurgmem)) { 107e41f4b71Sopenharmony_ci // Obtain the object data. 108e41f4b71Sopenharmony_ci ReqObj* pReqObj = (ReqObj*) OH_PurgeableMemory_GetContent(pPurgmem); 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci // Declare the parameters of the extended constructor. 111e41f4b71Sopenharmony_ci struct AppendParaData apdata = {1}; 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci // Update the rules for recreating the object. 114e41f4b71Sopenharmony_ci OH_PurgeableMemory_AppendModify(pPurgmem, AddFunc, &apdata); 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci // Stop writing data to the object. 117e41f4b71Sopenharmony_ci OH_PurgeableMemory_EndWrite(pPurgmem); 118e41f4b71Sopenharmony_ci } 119e41f4b71Sopenharmony_ci ``` 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci5. Destroy the **PurgeableMemory** object. 122e41f4b71Sopenharmony_ci ```c++ 123e41f4b71Sopenharmony_ci // Destroy the object. 124e41f4b71Sopenharmony_ci OH_PurgeableMemory_Destroy(pPurgmem); 125e41f4b71Sopenharmony_ci // Set a null pointer to prevent UAF. 126e41f4b71Sopenharmony_ci pPurgmem = nullptr; 127e41f4b71Sopenharmony_ci ``` 128