1 /* 2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12 * of conditions and the following disclaimer in the documentation and/or other materials 13 * provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 16 * to endorse or promote products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef IT_LOS_MEM_H 33 #define IT_LOS_MEM_H 34 35 #ifdef __cplusplus 36 #if __cplusplus 37 extern "C" { 38 #endif /* __cplusplus */ 39 #endif /* __cplusplus */ 40 41 #include "osTest.h" 42 #include "los_memory.h" 43 #include "los_config.h" 44 #include "iCunit.h" 45 #include "math.h" 46 47 #define OS_MEM_WATERLINE 1 48 49 #define LOS_DLNK_NODE_HEAD_SIZE 0 50 #define MIN_DLNK_POOL_SIZE 0 51 52 #ifdef OS_MEM_WATERLINE 53 #define TEST_MEM_MINIUSE (LOS_DLNK_HEAD_SIZE + LOS_DLNK_NODE_HEAD_SIZE + sizeof(LOS_MEM_POOL_INFO)) 54 #endif 55 56 #define TEST_MEM_SIZE 0x2000 57 58 #define IS_ALIGNED_SIZE(value, alignSize) (0 == ((UINT32)(value) & (UINT32)((alignSize) - 1))) 59 60 #define TEST_POOL_SIZE (1 * 8 * 1024) 61 62 #define RANDOM(x) (rand() % (x)) 63 64 #define LOS_INIT_MEM LOS_MemInit 65 #define LOS_ALLOC_MEM LOS_MemAlloc 66 #define LOS_REALLOC_MEM LOS_MemRealloc 67 #define LOS_FREE_MEM LOS_MemFree 68 69 /* Supposing a Second Level Index: SLI = 3. */ 70 #define OS_MEM_SLI 3 71 /* Giving 1 free list for each small bucket: 4, 8, 12, up to 124. */ 72 #define OS_MEM_SMALL_BUCKET_COUNT 31 73 #define OS_MEM_SMALL_BUCKET_MAX_SIZE 128 74 /* Giving OS_MEM_FREE_LIST_NUM free lists for each large bucket. */ 75 #define OS_MEM_LARGE_BUCKET_COUNT 24 76 #define OS_MEM_FREE_LIST_NUM (1 << OS_MEM_SLI) 77 /* OS_MEM_SMALL_BUCKET_MAX_SIZE to the power of 2 is 7. */ 78 #define OS_MEM_LARGE_START_BUCKET 7 79 80 /* The count of free list. */ 81 #define OS_MEM_FREE_LIST_COUNT (OS_MEM_SMALL_BUCKET_COUNT + (OS_MEM_LARGE_BUCKET_COUNT << OS_MEM_SLI)) 82 /* The bitmap is used to indicate whether the free list is empty, 1: not empty, 0: empty. */ 83 #define OS_MEM_BITMAP_WORDS ((OS_MEM_FREE_LIST_COUNT >> 5) + 1) 84 85 struct TestMemNodeHead { 86 #if (LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK == 1) 87 UINT32 magic; 88 #endif 89 #if (LOSCFG_MEM_LEAKCHECK == 1) 90 UINTPTR linkReg[LOSCFG_MEM_RECORD_LR_CNT]; 91 #endif 92 union { 93 struct TestMemNodeHead *prev; /* The prev is used for current node points to the previous node */ 94 struct TestMemNodeHead *next; /* The next is used for sentinel node points to the expand node */ 95 } ptr; 96 #if (LOSCFG_MEM_FREE_BY_TASKID == 1) 97 UINT32 taskID : 6; 98 UINT32 sizeAndFlag : 26; 99 #else 100 UINT32 sizeAndFlag; 101 #endif 102 }; 103 104 struct TestMemUsedNodeHead { 105 struct TestMemNodeHead header; 106 }; 107 108 struct TestMemFreeNodeHead { 109 struct TestMemNodeHead header; 110 struct TestMemFreeNodeHead *prev; 111 struct TestMemFreeNodeHead *next; 112 }; 113 114 struct TestMemPoolInfo { 115 VOID *pool; 116 UINT32 totalSize; 117 UINT32 attr; 118 #if (LOSCFG_MEM_WATERLINE == 1) 119 UINT32 waterLine; /* Maximum usage size in a memory pool */ 120 UINT32 curUsedSize; /* Current usage size in a memory pool */ 121 #endif 122 }; 123 124 struct TestMemPoolHead { 125 struct TestMemPoolInfo info; 126 UINT32 freeListBitmap[OS_MEM_BITMAP_WORDS]; 127 struct TestMemFreeNodeHead *freeList[OS_MEM_FREE_LIST_COUNT]; 128 #if (LOSCFG_MEM_MUL_POOL == 1) 129 VOID *nextPool; 130 #endif 131 }; 132 133 #define LOS_MEM_NODE_HEAD_SIZE sizeof(struct TestMemUsedNodeHead) 134 #define MIN_MEM_POOL_SIZE (LOS_MEM_NODE_HEAD_SIZE + sizeof(struct TestMemPoolHead)) 135 #define LOS_MEM_POOL_SIZE sizeof(struct TestMemPoolHead) 136 137 extern void *g_memPool; 138 extern void *g_testPool; 139 140 extern void MemStart(void); 141 extern void MemEnd(void); 142 extern void MemInit(void); 143 extern void MemFree(void); 144 extern UINT32 MemGetFreeSize(void *pool); 145 extern UINT32 CalPow(UINT32 exp); 146 147 VOID ItLosMem001(void); 148 VOID ItLosMem002(void); 149 VOID ItLosMem003(void); 150 VOID ItLosMem004(void); 151 VOID ItLosMem005(void); 152 VOID ItLosMem006(void); 153 VOID ItLosMem007(void); 154 VOID ItLosMem008(void); 155 VOID ItLosMem009(void); 156 VOID ItLosMem010(void); 157 VOID ItLosMem011(void); 158 VOID ItLosMem012(void); 159 VOID ItLosMem013(void); 160 VOID ItLosMem014(void); 161 VOID ItLosMem015(void); 162 VOID ItLosMem016(void); 163 VOID ItLosMem017(void); 164 VOID ItLosMem018(void); 165 VOID ItLosMem019(void); 166 VOID ItLosMem020(void); 167 VOID ItLosMem021(void); 168 VOID ItLosMem022(void); 169 VOID ItLosMem023(void); 170 VOID ItLosMem024(void); 171 VOID ItLosMem025(void); 172 VOID ItLosMem026(void); 173 VOID ItLosMem027(void); 174 VOID ItLosMem028(void); 175 VOID ItLosMem029(void); 176 VOID ItLosMem030(void); 177 VOID ItLosMem031(void); 178 VOID ItLosMem032(void); 179 VOID ItLosMem033(void); 180 VOID ItLosMem035(void); 181 VOID ItLosMem036(void); 182 VOID ItLosMem037(void); 183 VOID ItLosMem038(void); 184 VOID ItLosMem039(void); 185 VOID ItLosMem040(void); 186 VOID ItLosMem041(void); 187 VOID ItLosMem042(void); 188 VOID ItLosMem043(void); 189 VOID ItLosMem044(void); 190 VOID ItLosMem045(void); 191 VOID ItLosMem046(void); 192 VOID ItLosMem047(void); 193 VOID ItLosMem058(void); 194 VOID ItLosMem063(void); 195 VOID ItLosMem064(void); 196 VOID ItLosMem065(void); 197 VOID ItLosTick001(void); 198 199 #ifdef __cplusplus 200 #if __cplusplus 201 } 202 #endif /* __cplusplus */ 203 #endif /* __cplusplus */ 204 205 #endif /* IT_LOS_MEM_H */ 206