1# Purgeable Memory Development 2 3## When to Use 4 5You can use the native purgeable memory APIs to apply for and release purgeable memory. 6 7 8The following scenarios are common for native purgeable memory development: 9 10* Apply for a **PurgeableMemory** object and write data to the object. 11* Release the **PurgeableMemory** object when it is no longer required. 12 13## Available APIs 14 15| API| Description| 16| -------- | -------- | 17| 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.| 18| bool OH_PurgeableMemory_Destroy(OH_PurgeableMemory \*purgObj) | Destroys a **PurgeableMemory** object.| 19| bool OH_PurgeableMemory_BeginRead(OH_PurgeableMemory \*purgObj) | Begins a read operation on a **PurgeableMemory** object.| 20| 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.| 21|bool OH_PurgeableMemory_BeginWrite(OH_PurgeableMemory \*purgObj) | Begins a write operation on a **PurgeableMemory** object.| 22|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.| 23|void \*OH_PurgeableMemory_GetContent(OH_PurgeableMemory \*purgObj)|Obtains the memory data of a **PurgeableMemory** object.| 24|size_t OH_PurgeableMemory_ContentSize(OH_PurgeableMemory \*purgObj)|Obtains the memory data size of a **PurgeableMemory** object.| 25|bool OH_PurgeableMemory_AppendModify(OH_PurgeableMemory \*purgObj, OH_PurgeableMemory_ModifyFunc func, void \*funcPara)|Adds a function for modifying a **PurgeableMemory** object.| 26 27 28## How to Develop 29 30The following steps describe 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. 31 321. Declare the rules for creating a **PurgeableMemory** object. 33 ```c++ 34 // Declare the parameters of the constructor. 35 struct ParaData{ 36 int start; 37 int end; 38 }; 39 40 // Declare a function for modifying the object. 41 bool FactorialFunc(void* data, size_t size, void* param){ 42 bool ret = true; 43 ParaData *pdata = (ParaData*) param; 44 int* oriData = (int*)data; 45 int i = pdata->start; 46 while(i<pdata->end){ 47 *oriData *= i; 48 } 49 return ret; 50 } 51 52 // Declare the parameters of the extended function for modifying the object. 53 struct AppendParaData{ 54 int newPara; 55 }; 56 57 // Declare the extended function for modifying the object. 58 bool AddFunc(void* data, size_t size, void* param){ 59 bool ret = true; 60 int *oriDatap = (int*) data; 61 AppendParaData* apData = (AppendParaData*)param; 62 *oriDatap += apData->newPara; 63 return ret; 64 } 65 ``` 662. Create a **PurgeableMemory** object. 67 ```c++ 68 // Define the memory data size to 4 MB. 69 #define DATASIZE (4 * 1024 * 1024) 70 71 // Declare the parameters of the constructor. 72 struct ParaData pdata = {1,2}; 73 74 // Create a PurgeableMemory object. 75 OH_PurgeableMemory* pPurgmem = OH_PurgeableMemory_Create(DATASIZE, FactorialFunc, &pdata); 76 ``` 77 783. Perform a read operation on the **PurgeableMemory** object. 79 ```c++ 80 // Define an object type based on the service requirements. 81 class ReqObj; 82 83 // Begin a read operation on the object. 84 OH_PurgeableMemory_BeginRead(pPurgmem); 85 86 // Obtain the object size. 87 size_t size = OH_PurgeableMemory_ContentSize(pPurgmem); 88 89 // Obtain the object content. 90 ReqObj* pReqObj = (ReqObj*) OH_PurgeableMemory_GetContent(pPurgmem); 91 92 // End a read operation on the object. 93 OH_PurgeableMemory_EndRead(pPurgmem); 94 ``` 95 964. Perform a write operation on the **PurgeableMemory** object. 97 ```c++ 98 // Define an object type based on the service requirements. 99 class ReqObj; 100 101 // Begin a write operation on the object. 102 OH_PurgeableMemory_BeginWrite(pPurgmem); 103 104 // Obtain the object data. 105 ReqObj* pReqObj = (ReqObj*) OH_PurgeableMemory_GetContent(pPurgmem); 106 107 // Declare the parameters of the extended constructor. 108 struct AppendParaData apdata = {1}; 109 110 // Update the rules for recreating the object. 111 OH_PurgeableMemory_AppendModify(pPurgmem, AddFunc, &apdata); 112 113 // Stop writing data to the object. 114 OH_PurgeableMemory_EndWrite(pPurgmem); 115 ``` 116 1175. Destroy the **PurgeableMemory** object. 118 ```c++ 119 // Destroy the object. 120 OH_PurgeableMemory_Destroy(pPurgmem); 121 ``` 122