1# Static Memory<a name="EN-US_TOPIC_0000001078876516"></a> 2 3- [Working Principles](#section165473517522) 4- [Development Guidelines](#section57511620165218) 5 - [When to Use](#section215474911529) 6 - [Available APIs](#section79231214539) 7 - [How to Develop](#section1388511316548) 8 - [Development Example](#section17801515105519) 9 - [Verification](#section11818154112319) 10 11 12## Working Principles<a name="section165473517522"></a> 13 14The static memory is a static array. The block size in the static memory pool is set during initialization and cannot be changed after initialization. 15 16The static memory pool consists of a control block **LOS\_MEMBOX\_INFO** and several memory blocks **LOS\_MEMBOX\_NODE** of the same size. The control block is located at the head of the memory pool and used for memory block management. It contains the memory block size \(**uwBlkSize**\), number of memory blocks \(**uwBlkNum**\), number of allocated memory blocks \(**uwBlkCnt**\), and free list \(**stFreeList**\). Memory is allocated and released by block size. Each memory block contains the pointer **pstNext** that points to the next memory block. 17 18**Figure 1** Static memory<a name="fig1081017327364"></a> 19 20 21## Development Guidelines<a name="section57511620165218"></a> 22 23### When to Use<a name="section215474911529"></a> 24 25Use static memory allocation to obtain memory blocks of the fixed size. When the memory is no longer required, release the static memory. 26 27### Available APIs<a name="section79231214539"></a> 28 29The following table describes APIs available for OpenHarmony LiteOS-M static memory management. For more details about the APIs, see the API reference. 30 31**Table 1** APIs of the static memory module 32 33<a name="table1415203765610"></a> 34<table><thead align="left"><tr id="row134151837125611"><th class="cellrowborder" valign="top" width="16.19161916191619%" id="mcps1.2.4.1.1"><p id="p16415637105612"><a name="p16415637105612"></a><a name="p16415637105612"></a>Category</p> 35</th> 36<th class="cellrowborder" valign="top" width="22.472247224722473%" id="mcps1.2.4.1.2"><p id="p11415163718562"><a name="p11415163718562"></a><a name="p11415163718562"></a>API</p> 37</th> 38<th class="cellrowborder" valign="top" width="61.33613361336133%" id="mcps1.2.4.1.3"><p id="p1641533755612"><a name="p1641533755612"></a><a name="p1641533755612"></a>Description</p> 39</th> 40</tr> 41</thead> 42<tbody><tr id="row0415737175610"><td class="cellrowborder" valign="top" width="16.19161916191619%" headers="mcps1.2.4.1.1 "><p id="p2990613114416"><a name="p2990613114416"></a><a name="p2990613114416"></a>Initializing the static memory pool</p> 43</td> 44<td class="cellrowborder" valign="top" width="22.472247224722473%" headers="mcps1.2.4.1.2 "><p id="p169901113194416"><a name="p169901113194416"></a><a name="p169901113194416"></a>LOS_MemboxInit</p> 45</td> 46<td class="cellrowborder" valign="top" width="61.33613361336133%" headers="mcps1.2.4.1.3 "><p id="p5990113174414"><a name="p5990113174414"></a><a name="p5990113174414"></a>Initialize a static memory pool and sets the start address, total size, and size of each memory block based on the input parameters.</p> 47</td> 48</tr> 49<tr id="row1841519376561"><td class="cellrowborder" valign="top" width="16.19161916191619%" headers="mcps1.2.4.1.1 "><p id="p9367164904413"><a name="p9367164904413"></a><a name="p9367164904413"></a>Clearing static memory blocks</p> 50</td> 51<td class="cellrowborder" valign="top" width="22.472247224722473%" headers="mcps1.2.4.1.2 "><p id="p103671549144411"><a name="p103671549144411"></a><a name="p103671549144411"></a>LOS_MemboxClr</p> 52</td> 53<td class="cellrowborder" valign="top" width="61.33613361336133%" headers="mcps1.2.4.1.3 "><p id="p18367184916448"><a name="p18367184916448"></a><a name="p18367184916448"></a>Clears the memory blocks allocated from the static memory pool.</p> 54</td> 55</tr> 56<tr id="row1187514443616"><td class="cellrowborder" rowspan="2" valign="top" width="16.19161916191619%" headers="mcps1.2.4.1.1 "><p id="p64541711458"><a name="p64541711458"></a><a name="p64541711458"></a>Allocating or releasing static memory</p> 57</td> 58<td class="cellrowborder" valign="top" width="22.472247224722473%" headers="mcps1.2.4.1.2 "><p id="p84547710452"><a name="p84547710452"></a><a name="p84547710452"></a>LOS_MemboxAlloc</p> 59</td> 60<td class="cellrowborder" valign="top" width="61.33613361336133%" headers="mcps1.2.4.1.3 "><p id="p5454177164520"><a name="p5454177164520"></a><a name="p5454177164520"></a>Allocates a memory block from a specified static memory pool.</p> 61</td> 62</tr> 63<tr id="row1745415527441"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p645457204512"><a name="p645457204512"></a><a name="p645457204512"></a>LOS_MemboxFree</p> 64</td> 65<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p545415704514"><a name="p545415704514"></a><a name="p545415704514"></a>Releases a memory block allocated from the static memory pool.</p> 66</td> 67</tr> 68<tr id="row19101718144518"><td class="cellrowborder" rowspan="2" valign="top" width="16.19161916191619%" headers="mcps1.2.4.1.1 "><p id="p15927427144615"><a name="p15927427144615"></a><a name="p15927427144615"></a>Obtaining or printing static memory pool information</p> 69</td> 70<td class="cellrowborder" valign="top" width="22.472247224722473%" headers="mcps1.2.4.1.2 "><p id="p792715277464"><a name="p792715277464"></a><a name="p792715277464"></a>LOS_MemboxStatisticsGet</p> 71</td> 72<td class="cellrowborder" valign="top" width="61.33613361336133%" headers="mcps1.2.4.1.3 "><p id="p139271327114620"><a name="p139271327114620"></a><a name="p139271327114620"></a>Obtains information about a specified static memory pool, including the total number of memory blocks in the memory pool, number of allocated memory blocks, and size of each memory block.</p> 73</td> 74</tr> 75<tr id="row1346314166464"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p8927102754619"><a name="p8927102754619"></a><a name="p8927102754619"></a>LOS_ShowBox</p> 76</td> 77<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p49274279460"><a name="p49274279460"></a><a name="p49274279460"></a>Prints information about all nodes in a specified static memory pool (the print level is <strong id="b1937798153019"><a name="b1937798153019"></a><a name="b1937798153019"></a>LOS_INFO_LEVEL</strong>). The information includes the start address of the memory pool, memory block size, total number of memory blocks, start address of each idle memory block, and start addresses of all memory blocks.</p> 78</td> 79</tr> 80</tbody> 81</table> 82 83> **NOTE:** 84>The number of memory blocks in the memory pool after initialization is not equal to the total memory size divided by the memory block size. The reason is the control block of the memory pool and the control header of each memory block have memory overheads. When setting the total memory size, you need to consider these factors. 85 86### How to Develop<a name="section1388511316548"></a> 87 88The typical development process of static memory is as follows: 89 901. Plan a memory space as the static memory pool. 912. Call the **LOS\_MemboxInit** API to initialize the static memory pool. 92 93 During initialization, the memory space specified by the input parameter is divided into multiple blocks \(the number of blocks depends on the total static memory size and the block size\). Insert all memory blocks to the free list, and place the control header at the beginning of the memory. 94 953. Call the **LOS\_MemboxAlloc** API to allocate static memory. 96 97 The system allocates the first free memory block from the free list and returns the start address of this memory block. 98 994. Call the **LOS\_MemboxClr** API. 100 101 Clear the memory block corresponding to the address contained in the input parameter. 102 1035. Call the **LOS\_MemboxFree** API. 104 105 Add the memory block to the free list. 106 107 108### Development Example<a name="section17801515105519"></a> 109 110The example below implements the following: 111 1121. Initialize a static memory pool. 1132. Allocate a memory block from the static memory pool. 1143. Store a piece of data in a memory block. 1154. Print the data in the memory block. 1165. Clear the data in the memory block. 1176. Release the memory block. 118 119 The sample code is as follows: 120 121 122``` 123#include "los_membox.h" 124 125VOID Example_StaticMem(VOID) 126{ 127 UINT32 *mem = NULL; 128 UINT32 blkSize = 10; 129 UINT32 boxSize = 100; 130 UINT32 boxMem[1000]; 131 UINT32 ret; 132 133 /* Initialize the memory pool.*/ 134 ret = LOS_MemboxInit(&boxMem[0], boxSize, blkSize); 135 if(ret != LOS_OK) { 136 printf("Membox init failed!\n"); 137 return; 138 } else { 139 printf("Membox init success!\n"); 140 } 141 142 /* Request a memory block.*/ 143 mem = (UINT32 *)LOS_MemboxAlloc(boxMem); 144 if (NULL == mem) { 145 printf("Mem alloc failed!\n"); 146 return; 147 } 148 printf("Mem alloc success!\n"); 149 150 /* Assign a value.*/ 151 *mem = 828; 152 printf("*mem = %d\n", *mem); 153 154 /* Clear data in the memory block. */ 155 LOS_MemboxClr(boxMem, mem); 156 printf("Mem clear success \n *mem = %d\n", *mem); 157 158 /* Release the memory.*/ 159 ret = LOS_MemboxFree(boxMem, mem); 160 if (LOS_OK == ret) { 161 printf("Mem free success!\n"); 162 } else { 163 printf("Mem free failed!\n"); 164 } 165 166 return; 167} 168``` 169 170### Verification<a name="section11818154112319"></a> 171 172The output is as follows: 173 174``` 175Membox init success! 176Mem alloc success! 177*mem = 828 178Mem clear success 179*mem = 0 180Mem free success! 181``` 182 183