• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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.
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            i++;
49        }
50        return ret;
51    }
52
53    // Declare the parameters of the extended function for modifying the object.
54    struct AppendParaData{
55        int newPara;
56    };
57
58    // Declare the extended function for modifying the object.
59    bool AddFunc(void* data, size_t size, void* param){
60        bool ret = true;
61        int *oriDatap = (int*) data;
62        AppendParaData* apData = (AppendParaData*)param;
63        *oriDatap += apData->newPara;
64        return ret;
65    }
66    ```
672. Create a **PurgeableMemory** object.
68    ```c++
69    // Define the memory data size to 4 MB.
70    #define DATASIZE (4 * 1024 * 1024)
71
72    // Declare the parameters of the constructor.
73    struct ParaData pdata = {1,2};
74
75    // Create a PurgeableMemory object.
76    OH_PurgeableMemory* pPurgmem = OH_PurgeableMemory_Create(DATASIZE, FactorialFunc, &pdata);
77    ```
78
793. Perform a read operation on the **PurgeableMemory** object.
80    ```c++
81    // Define an object type based on the service requirements.
82    class ReqObj;
83
84    // Begin a read operation on the object.
85    OH_PurgeableMemory_BeginRead(pPurgmem);
86
87    // Obtain the object size.
88    size_t size = OH_PurgeableMemory_ContentSize(pPurgmem);
89
90    // Obtain the object content.
91    ReqObj* pReqObj = (ReqObj*) OH_PurgeableMemory_GetContent(pPurgmem);
92
93    // End the read operation on the object.
94    OH_PurgeableMemory_EndRead(pPurgmem);
95    ```
96
974. Perform a write operation on the **PurgeableMemory** object.
98    ```c++
99     // Define an object type based on the service requirements.
100    class ReqObj;
101
102    // Begin a write operation on the object.
103    OH_PurgeableMemory_BeginWrite(pPurgmem);
104
105    // Obtain the object data.
106    ReqObj* pReqObj = (ReqObj*) OH_PurgeableMemory_GetContent(pPurgmem);
107
108    // Declare the parameters of the extended constructor.
109    struct AppendParaData apdata = {1};
110
111    // Update the rules for recreating the object.
112    OH_PurgeableMemory_AppendModify(pPurgmem, AddFunc, &apdata);
113
114    // Stop writing data to the object.
115    OH_PurgeableMemory_EndWrite(pPurgmem);
116    ```
117
1185. Destroy the **PurgeableMemory** object.
119    ```c++
120    // Destroy the object.
121    OH_PurgeableMemory_Destroy(pPurgmem);
122    ```
123