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 _LOS_DLINK_MEM_H 33 #define _LOS_DLINK_MEM_H 34 35 #ifdef __cplusplus 36 #if __cplusplus 37 extern "C" { 38 #endif /* __cplusplus */ 39 #endif /* __cplusplus */ 40 41 #if (OS_SYS_MEM_CHECK == 1) 42 #define LOS_DLNK_ENABLE_ALLOC_CHECK 43 #endif 44 45 typedef VOID (*MALLOC_HOOK)(VOID); 46 47 extern MALLOC_HOOK g_mallocHook; 48 49 /* * 50 * @ingroup los_dlinkmem 51 * Memory pool information structure 52 */ 53 typedef struct { 54 void *pPoolAddr; /* *<Starting address of a memory pool */ 55 UINT32 uwPoolSize; /* *<Memory pool size */ 56 } LOS_DLNK_POOL_INFO; 57 58 /* * 59 * @ingroup los_dlinkmem 60 * Memory linked list node structure 61 */ 62 typedef struct tagLOS_DLNK_NODE { 63 LOS_DL_LIST stFreeNodeInfo; /* *<Free memory node */ 64 struct tagLOS_DLNK_NODE *pstPreNode; /* *<Pointer to the previous memory node */ 65 UINT32 uwSizeAndFlag; /* *<Size and flag of the current node (the highest bit represents a flag, and the rest bits 66 specify the size) */ 67 } LOS_DLNK_NODE; 68 69 /* * 70 * @ingroup los_dlinkmem 71 * @brief Initialize dynamic memory. 72 * 73 * @par Description: 74 * <ul> 75 * <li>This API is used to initialize the dynamic memory of a doubly linked list.</li> 76 * </ul> 77 * @attention 78 * <ul> 79 * <li>Call this API when dynamic memory needs to be initialized during the startup of HuaweiLite OS.</li> 80 * </ul> 81 * 82 * @param pool [IN] Starting address of memory. 83 * @param size [IN] Memory size. 84 * 85 * @retval #OS_ERROR �C1: The dynamic memory fails to be initialized. 86 * @retval #LOS_OK 0: The dynamic memory is successfully initialized. 87 * @par Dependency: 88 * <ul> 89 * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 90 * </ul> 91 * @see None. 92 */ 93 extern UINT32 LOS_DLnkInitMem(VOID *pool, UINT32 size); 94 95 /* * 96 * @ingroup los_dlinkmem 97 * @brief Allocate memory. 98 * 99 * @par Description: 100 * <ul> 101 * <li>This API is used to allocate memory of which the size is specified by size.</li> 102 * </ul> 103 * @attention 104 * <ul> 105 * <li>After calling this API, ensure that the returned memory address is not null in case that a null pointer will be 106 accessed later.</li> 107 108 * </ul> 109 * 110 * @param pool [IN] Starting address of memory. 111 * @param size [IN] Size of the memory to be allocated (unit: byte). 112 * 113 * @retval Address of the allocated memory. The memory is successfully allocated. 114 * @retval NULL. The memory fails to be allocated. 115 * @par Dependency: 116 * <ul> 117 * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 118 * </ul> 119 * @see LOS_DLnkFreeMem 120 */ 121 extern void *LOS_DLnkAllocMem(VOID *pool, UINT32 size); 122 123 /* * 124 * @ingroup los_dlinkmem 125 * @brief Free memory. 126 * 127 * @par Description: 128 * <ul> 129 * <li>This API is used to free the allocated memory.</li> 130 * </ul> 131 * @attention 132 * <ul> 133 * <li>The memory fails to be freed if it has been already freed.</li> 134 135 * </ul> 136 * 137 * @param pool [IN] Starting address of memory. 138 * @param mem [IN] Address of the memory to be freed. 139 * 140 * @retval #LOS_NOK 1: The memory fails to be freed. 141 * @retval #LOS_OK 0: The memory is successfully freed. 142 * @par Dependency: 143 * <ul> 144 * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 145 * </ul> 146 * @see LOS_DLnkAllocMem 147 */ 148 extern UINT32 LOS_DLnkFreeMem(VOID *pool, VOID *mem); 149 150 /* * 151 * @ingroup los_dlinkmem 152 * @brief Re-allocate memory. 153 * 154 * @par Description: 155 * <ul> 156 * <li>This API is used to re-allocate memory if the memory allocated before is insufficient. Data in the original memory 157 will be copied to the re-allocated memory, after which the original memory will be freed.</li> 158 159 * </ul> 160 * @attention 161 * <ul> 162 * <li>Before calling this API, check whether the return value of this API is null.</li> 163 * <li>If the passed-in address of the original memory is not null, and the passed-in size of the new memory block is 0, 164 calling this API frees the original memory.</li> 165 * <li>If the passed-in address of the original memory is null, calling this API allocates a new memory block.</li> 166 * <li>If the new memory block fails to be allocated, the original one will not be released.</li> 167 * </ul> 168 * 169 * @param pool [IN] Starting address of memory. 170 * @param ptr [IN] Address of the re-allocated memory. 171 * @param size [IN] Size of the memory to be allocated. 172 * 173 * @retval Address of the re-allocated memory. The memory is successfully re-allocated. 174 * @retval NULL. The memory fails to be re-allocated. 175 * @par Dependency: 176 * <ul> 177 * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 178 * </ul> 179 * @see LOS_DLnkAllocMem 180 */ 181 extern void *LOS_DLnkReAllocMem(VOID *pool, VOID *ptr, UINT32 size); 182 183 /* * 184 * @ingroup los_dlinkmem 185 * @brief Allocate aligned memory. 186 * 187 * @par Description: 188 * <ul> 189 * <li>This API is used to allocate memory blocks of specified size and of which the starting addresses are aligned on a 190 * specified boundary.</li> 191 * </ul> 192 * @attention 193 * <ul> 194 * <li>The alignment parameter value must be a power of 2 with the minimum value being 4.</li> 195 * </ul> 196 * 197 * @param pool [IN] Starting address of memory. 198 * @param size [IN] Size of the memory to be allocated. 199 * @param boundary[IN] Boundary on which the memory is aligned. 200 * 201 * @retval Starting address of the allocated aligned memory. The memory is successfully allocated. 202 * @retval The memory fails to be allocated. 203 * @par Dependency: 204 * <ul> 205 * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 206 * </ul> 207 * @see None. 208 */ 209 extern void *LOS_DlnkAllocMemAlign(void *pool, UINT32 size, UINT32 boundary); 210 211 extern UINT32 LOS_DLnkCheckMem(void *pool); 212 extern UINT32 LOS_DLnkGetTotalMemUsed(void *pool); 213 extern UINT32 LOS_DLnkGetMemFreeBlks(void *pool); 214 extern UINT32 LOS_DLnkGetMemUsedBlks(void *pool); 215 extern UINT32 LOS_DLnkGetMemTskId(void *ptr); 216 217 #ifdef OS_MEM_CHECK_DEBUG 218 219 /* 220 * memcheck error code: the stack have not inited 221 * Value: 0x02000100 222 * Solution: do memcheck must after stack mem init 223 */ 224 #define OS_ERRNO_MEMCHECK_NOT_INIT LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x0) 225 /* 226 * memcheck error code: the pPtr is NULL 227 * Value: 0x02000101 228 * Solution: don't give a NULL parameter 229 */ 230 #define OS_ERRNO_MEMCHECK_PARA_NULL LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x1) 231 /* 232 * memcheck error code: the pPtr addr not in the suit range 233 * Value: 0x02000102 234 * Solution: check pPtr and comfirm it included by stack 235 */ 236 #define OS_ERRNO_MEMCHECK_OUTSIDE LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x2) 237 /* 238 * memcheck error code: can't find the ctrl node 239 * Value: 0x02000103 240 * Solution: confirm the pPtr if this node has been freed or has not been alloced 241 */ 242 #define OS_ERRNO_MEMCHECK_NO_HEAD LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x3) 243 /* 244 * memcheck error code: the para level is wrong 245 * Value: 0x02000104 246 * Solution: checkout the memcheck level by the func mCheck_Level" 247 */ 248 #define OS_ERRNO_MEMCHECK_WRONG_LEVEL LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x4) 249 /* 250 * memcheck error code: memcheck func not open 251 * Value: 0x02000105 252 * Solution: enable memcheck by the func "OS_SetMemCheck_Level" 253 */ 254 #define OS_ERRNO_MEMCHECK_DISABLED LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x5) 255 256 #define LOS_MEM_CHECK_LEVEL_LO 0 257 #define LOS_MEM_CHECK_LEVEL_HI 1 258 #define LOS_MEM_CHECK_DISABLE 0xff 259 #define LOS_MEM_CHECK_LEVEL_D OS_ERRNO_MEMCHECK_DISABLED 260 261 extern UINT32 LOS_CheckMemSize(VOID *pool, VOID *ptr, UINT32 *totalSize, UINT32 *availSize); 262 extern UINT32 LOS_SetMemCheck_Level(UINT8 level); 263 extern UINT8 LOS_GetMemCheck_Level(VOID); 264 #endif 265 266 #ifdef __cplusplus 267 #if __cplusplus 268 } 269 #endif /* __cplusplus */ 270 #endif /* __cplusplus */ 271 272 #endif /* _LOS_DLINK_MEM_H */ 273