1 /* ---------------------------------------------------------------------------- 2 * Copyright (c) Huawei Technologies Co., Ltd. 2013-2015. All rights reserved. 3 * Description: LiteOS memory Module Implementation 4 * Redistribution and use in source and binary forms, with or without modification, 5 * are permitted provided that the following conditions are met: 6 * 1. Redistributions of source code must retain the above copyright notice, this list of 7 * conditions and the following disclaimer. 8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 9 * of conditions and the following disclaimer in the documentation and/or other materials 10 * provided with the distribution. 11 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 12 * to endorse or promote products derived from this software without specific prior written 13 * permission. 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * --------------------------------------------------------------------------- */ 26 27 #ifndef _LOS_MEMORY_PRI_H 28 #define _LOS_MEMORY_PRI_H 29 30 #include "los_memory.h" 31 #include "los_memstat_pri.h" 32 33 #ifdef __cplusplus 34 #if __cplusplus 35 extern "C" { 36 #endif /* __cplusplus */ 37 #endif /* __cplusplus */ 38 39 #define OS_MEM_ENABLE_MEM_STATISTICS 40 41 #ifdef OS_MEM_ENABLE_MEM_STATISTICS 42 #define OS_MEM_ADD_USED(size, taskID) OsTaskMemUsedInc(size, taskID) 43 #define OS_MEM_REDUCE_USED(size, taskID) OsTaskMemUsedDec(size, taskID) 44 #else 45 #define OS_MEM_ADD_USED(size, taskID) 46 #define OS_MEM_REDUCE_USED(size, taskID) 47 #endif 48 49 /** 50 * @ingroup los_memory 51 * Memory linked list node structure 52 */ 53 typedef struct tagLosMemDynNode { 54 LOS_DL_LIST freeNodeInfo; /**< Free memory node */ 55 struct tagLosMemDynNode *preNode; /**< Pointer to the previous memory node */ 56 UINT32 sizeAndFlag; /**< Size and flag of the current node(the highest bit 57 represents a flag, and the rest bits specify the size) */ 58 } LosMemDynNode; 59 60 typedef VOID (*MemNodeHook)(LosMemDynNode *tmpNode); 61 extern MemNodeHook g_memAllocFinishHook; 62 extern MemNodeHook g_memFreeHook; 63 64 #ifdef LOSCFG_DEBUG_KASAN 65 extern LITE_OS_RAM_SECTION void Asan_malloc_set(LosMemDynNode *allocNode); 66 extern LITE_OS_RAM_SECTION void Asan_free_set(LosMemDynNode *node); 67 #endif 68 69 #define IS_ALIGNED(value, alignSize) ((((UINT32)(value)) & ((UINT32)((alignSize) - 1))) == 0) 70 #define OS_MEM_TASKID_SET(node, ID) \ 71 do { \ 72 UINTPTR tmp = (UINTPTR)(((LosMemDynNode *)(node))->freeNodeInfo.pstNext); \ 73 tmp = tmp & 0xffff0000; \ 74 tmp |= (ID); \ 75 ((LosMemDynNode *)(node))->freeNodeInfo.pstNext = (LOS_DL_LIST *)(tmp); \ 76 } while (0) 77 78 #define OS_MEM_TASKID_GET(node) ((UINT32)(UINTPTR)(((LosMemDynNode *)(node))->freeNodeInfo.pstNext) & 0xffff) 79 #define OS_MEM_ALIGN(p, alignSize) (((unsigned int)(p) + (alignSize) - 1) & ~((alignSize) - 1)) 80 #define OS_MEM_NODE_HEAD_SIZE sizeof(LosMemDynNode) 81 #define OS_MEM_MIN_POOL_SIZE (OS_DLNK_HEAD_SIZE + (2 * OS_MEM_NODE_HEAD_SIZE) + sizeof(LosMemPoolInfo)) 82 #define OS_MEM_ALIGN_SIZE 4 83 #define OS_MEM_NODE_USED_FLAG 0x80000000 84 #define OS_MEM_NODE_ALIGNED_FLAG 0x40000000 85 #define OS_EXC_ERR_NODE_RANGE 0x40 86 #define OS_MEM_NODE_ALIGN_SIZE 64 87 #define OS_MEM_NODE_DATA_SIZE 4 88 89 #define OS_MEM_NODE_GET_ALIGNED_FLAG(sizeAndFlag) ((sizeAndFlag) & OS_MEM_NODE_ALIGNED_FLAG) 90 #define OS_MEM_NODE_SET_ALIGNED_FLAG(sizeAndFlag) ((sizeAndFlag) = ((sizeAndFlag) | OS_MEM_NODE_ALIGNED_FLAG)) 91 #define OS_MEM_NODE_GET_ALIGNED_GAPSIZE(sizeAndFlag) ((sizeAndFlag) & (~OS_MEM_NODE_ALIGNED_FLAG)) 92 #define OS_MEM_NODE_GET_USED_FLAG(sizeAndFlag) ((sizeAndFlag) & OS_MEM_NODE_USED_FLAG) 93 #define OS_MEM_NODE_SET_USED_FLAG(sizeAndFlag) ((sizeAndFlag) = ((sizeAndFlag) | OS_MEM_NODE_USED_FLAG)) 94 #define OS_MEM_NODE_GET_SIZE(sizeAndFlag) ((sizeAndFlag) & (~OS_MEM_NODE_USED_FLAG)) 95 #define OS_MEM_IS_NODE_NEXT_EXIST(node, poolInfo) (((UINT32)(node) + (node)->sizeAndFlag) < \ 96 ((UINT32)(poolInfo) + (poolInfo)->uwPoolSize)) 97 #define OS_MEM_HEAD(pool, size) OS_DLNK_HEAD(OS_MEM_HEAD_ADDR(pool), size) 98 #define OS_MEM_HEAD_ADDR(pool) ((VOID *)((UINT32)(UINTPTR)(pool) + sizeof(LosMemPoolInfo))) 99 #define OS_MEM_NEXT_NODE(node) ((LosMemDynNode *)((UINT8 *)(node) + OS_MEM_NODE_GET_SIZE((node)->sizeAndFlag))) 100 #define OS_MEM_FIRST_NODE(pool) ((LosMemDynNode *)((UINT8 *)OS_MEM_HEAD_ADDR(pool) + OS_DLNK_HEAD_SIZE)) 101 #define OS_MEM_END_NODE(pool, size) ((LosMemDynNode *)(((UINT8 *)(pool) + (size)) - OS_MEM_NODE_HEAD_SIZE)) 102 #define OS_MEM_MIDDLE_ADDR_OPEN_END(startAddr, middleAddr, endAddr) \ 103 (((UINT8 *)(startAddr) <= (UINT8 *)(middleAddr)) && ((UINT8 *)(middleAddr) < (UINT8 *)(endAddr))) 104 #define OS_MEM_MIDDLE_ADDR(startAddr, middleAddr, endAddr) (((UINT8 *)(startAddr) <= (UINT8 *)(middleAddr)) && \ 105 ((UINT8 *)(middleAddr) <= (UINT8 *)(endAddr))) 106 #define OS_MEM_SET_MAGIC(value) (value) = (LOS_DL_LIST *)(UINTPTR)((UINT32)(UINTPTR)(&(value)) ^ 0xffffffff) 107 #define OS_MEM_MAGIC_VALID(value) ((((UINT32)(UINTPTR)(value)) ^ ((UINT32)(UINTPTR)(&(value)))) == 0xffffffff) 108 109 /** 110 * @ingroup los_memory 111 * Memory pool information structure 112 */ 113 typedef struct tagLosMemPoolInfo { 114 VOID *poolAddr; /**< Starting address of a memory pool */ 115 UINT32 poolSize; /**< Memory pool size */ 116 UINT32 poolFailCount; /**< Memory pool malloc failed times */ 117 #if defined(LOSCFG_MEM_WATERLINE) && (LOSCFG_MEM_WATERLINE == YES) 118 UINT32 poolWaterLine; /**< Maximum usage size in a memory pool */ 119 UINT32 poolCurUsedSize; /**< Current usage size in a memory pool */ 120 #endif 121 } LosMemPoolInfo; 122 123 /* 124 * memcheck error code: the stack have not inited 125 * Value: 0x02000100 126 * Solution: do memcheck must after stack mem init 127 */ 128 #define OS_ERRNO_MEMCHECK_NOT_INIT LOS_ERRNO_OS_ERROR(LOS_MOD_MEM, 0x0) 129 130 /* 131 * memcheck error code: the pPtr is NULL 132 * Value: 0x02000101 133 * Solution: don't give a NULL parameter 134 */ 135 #define OS_ERRNO_MEMCHECK_PARA_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_MEM, 0x1) 136 137 /* 138 * memcheck error code: the pPtr addr not in the suit range 139 * Value: 0x02000102 140 * Solution: check pPtr and comfirm it included by stack 141 */ 142 #define OS_ERRNO_MEMCHECK_OUTSIDE LOS_ERRNO_OS_ERROR(LOS_MOD_MEM, 0x2) 143 144 /* 145 * memcheck error code: can't find the ctrl node 146 * Value: 0x02000103 147 * Solution: confirm the pPtr if this node has been freed or has not been alloced 148 */ 149 #define OS_ERRNO_MEMCHECK_NO_HEAD LOS_ERRNO_OS_ERROR(LOS_MOD_MEM, 0x3) 150 151 /* 152 * memcheck error code: the para level is wrong 153 * Value: 0x02000104 154 * Solution: checkout the memcheck level by the func "OS_GetMemCheck_Level" 155 */ 156 #define OS_ERRNO_MEMCHECK_WRONG_LEVEL LOS_ERRNO_OS_ERROR(LOS_MOD_MEM, 0x4) 157 158 /* 159 * memcheck error code: memcheck func not open 160 * Value: 0x02000105 161 * Solution: enable memcheck by the func "OS_SetMemCheck_Level" 162 */ 163 #define OS_ERRNO_MEMCHECK_DISABLED LOS_ERRNO_OS_ERROR(LOS_MOD_MEM, 0x5) 164 165 /* 166 * memcheck error code: memcheck err in LOS_MemIntegrityCheck 167 * Value: 0x02000106 168 * Solution: memcheck error number used for err handle 169 */ 170 #define OS_ERRNO_MEMCHECK_ERR LOS_ERRNO_OS_ERROR(LOS_MOD_MEM, 0x6) 171 172 typedef UINT32 (*OsMemAllocCheckFunc)(VOID *pool); 173 extern OsMemAllocCheckFunc g_memAllocCheckHook; 174 175 typedef VOID (*OsMemFreeCheckFunc)(const VOID *pool, const LosMemDynNode *node); 176 extern OsMemFreeCheckFunc g_memFreeCheckHook; 177 178 /** 179 * @ingroup los_memory 180 * @brief Initialization the memory system. 181 * 182 * @par Description: 183 * <ul> 184 * <li>This API is used to initialization the memory system.</li> 185 * </ul> 186 * @attention 187 * <ul> 188 * <li>None.</li> 189 * </ul> 190 * 191 * @param None. 192 * 193 * @retval UINT32 Initialization result. 194 * @par Dependency: 195 * <ul><li>los_memory_pri.h: the header file that contains the API declaration.</li></ul> 196 * @see None. 197 */ 198 extern UINT32 OsMemSystemInit(VOID); 199 200 /** 201 * @ingroup los_memory 202 * @brief Check memory when free node. 203 * 204 * @par Description: 205 * <ul> 206 * <li>This API is used to check memory when free node.</li> 207 * </ul> 208 * @attention 209 * <ul> 210 * <li>None.</li> 211 * </ul> 212 * 213 * @param pool [IN] A pointer pointed to the memory pool. 214 * @param node [IN] A pointer pointed to the free node. 215 * 216 * @retval None. 217 * @par Dependency: 218 * <ul><li>los_memory_pri.h: the header file that contains the API declaration.</li></ul> 219 * @see None. 220 */ 221 extern VOID OsMemFreeNodeCheck(const VOID *pool, const LosMemDynNode *node); 222 extern UINT32 OsMemPoolInit(VOID); 223 224 #ifdef __cplusplus 225 #if __cplusplus 226 } 227 #endif /* __cplusplus */ 228 #endif /* __cplusplus */ 229 230 #endif /* _LOS_MEMORY_PRI_H */ 231