13d8536b4Sopenharmony_ci/* 23d8536b4Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 33d8536b4Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 43d8536b4Sopenharmony_ci * 53d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 63d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met: 73d8536b4Sopenharmony_ci * 83d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 93d8536b4Sopenharmony_ci * conditions and the following disclaimer. 103d8536b4Sopenharmony_ci * 113d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 123d8536b4Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 133d8536b4Sopenharmony_ci * provided with the distribution. 143d8536b4Sopenharmony_ci * 153d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 163d8536b4Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 173d8536b4Sopenharmony_ci * permission. 183d8536b4Sopenharmony_ci * 193d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 203d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 213d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 223d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 233d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 243d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 253d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 263d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 273d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 283d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 293d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 303d8536b4Sopenharmony_ci */ 313d8536b4Sopenharmony_ci 323d8536b4Sopenharmony_ci#ifndef _LOS_DLINK_MEM_H 333d8536b4Sopenharmony_ci#define _LOS_DLINK_MEM_H 343d8536b4Sopenharmony_ci 353d8536b4Sopenharmony_ci#ifdef __cplusplus 363d8536b4Sopenharmony_ci#if __cplusplus 373d8536b4Sopenharmony_ciextern "C" { 383d8536b4Sopenharmony_ci#endif /* __cplusplus */ 393d8536b4Sopenharmony_ci#endif /* __cplusplus */ 403d8536b4Sopenharmony_ci 413d8536b4Sopenharmony_ci#if (OS_SYS_MEM_CHECK == 1) 423d8536b4Sopenharmony_ci#define LOS_DLNK_ENABLE_ALLOC_CHECK 433d8536b4Sopenharmony_ci#endif 443d8536b4Sopenharmony_ci 453d8536b4Sopenharmony_citypedef VOID (*MALLOC_HOOK)(VOID); 463d8536b4Sopenharmony_ci 473d8536b4Sopenharmony_ciextern MALLOC_HOOK g_mallocHook; 483d8536b4Sopenharmony_ci 493d8536b4Sopenharmony_ci/* * 503d8536b4Sopenharmony_ci * @ingroup los_dlinkmem 513d8536b4Sopenharmony_ci * Memory pool information structure 523d8536b4Sopenharmony_ci */ 533d8536b4Sopenharmony_citypedef struct { 543d8536b4Sopenharmony_ci void *pPoolAddr; /* *<Starting address of a memory pool */ 553d8536b4Sopenharmony_ci UINT32 uwPoolSize; /* *<Memory pool size */ 563d8536b4Sopenharmony_ci} LOS_DLNK_POOL_INFO; 573d8536b4Sopenharmony_ci 583d8536b4Sopenharmony_ci/* * 593d8536b4Sopenharmony_ci * @ingroup los_dlinkmem 603d8536b4Sopenharmony_ci * Memory linked list node structure 613d8536b4Sopenharmony_ci */ 623d8536b4Sopenharmony_citypedef struct tagLOS_DLNK_NODE { 633d8536b4Sopenharmony_ci LOS_DL_LIST stFreeNodeInfo; /* *<Free memory node */ 643d8536b4Sopenharmony_ci struct tagLOS_DLNK_NODE *pstPreNode; /* *<Pointer to the previous memory node */ 653d8536b4Sopenharmony_ci UINT32 uwSizeAndFlag; /* *<Size and flag of the current node (the highest bit represents a flag, and the rest bits 663d8536b4Sopenharmony_ci specify the size) */ 673d8536b4Sopenharmony_ci} LOS_DLNK_NODE; 683d8536b4Sopenharmony_ci 693d8536b4Sopenharmony_ci/* * 703d8536b4Sopenharmony_ci * @ingroup los_dlinkmem 713d8536b4Sopenharmony_ci * @brief Initialize dynamic memory. 723d8536b4Sopenharmony_ci * 733d8536b4Sopenharmony_ci * @par Description: 743d8536b4Sopenharmony_ci * <ul> 753d8536b4Sopenharmony_ci * <li>This API is used to initialize the dynamic memory of a doubly linked list.</li> 763d8536b4Sopenharmony_ci * </ul> 773d8536b4Sopenharmony_ci * @attention 783d8536b4Sopenharmony_ci * <ul> 793d8536b4Sopenharmony_ci * <li>Call this API when dynamic memory needs to be initialized during the startup of HuaweiLite OS.</li> 803d8536b4Sopenharmony_ci * </ul> 813d8536b4Sopenharmony_ci * 823d8536b4Sopenharmony_ci * @param pool [IN] Starting address of memory. 833d8536b4Sopenharmony_ci * @param size [IN] Memory size. 843d8536b4Sopenharmony_ci * 853d8536b4Sopenharmony_ci * @retval #OS_ERROR �C1: The dynamic memory fails to be initialized. 863d8536b4Sopenharmony_ci * @retval #LOS_OK 0: The dynamic memory is successfully initialized. 873d8536b4Sopenharmony_ci * @par Dependency: 883d8536b4Sopenharmony_ci * <ul> 893d8536b4Sopenharmony_ci * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 903d8536b4Sopenharmony_ci * </ul> 913d8536b4Sopenharmony_ci * @see None. 923d8536b4Sopenharmony_ci */ 933d8536b4Sopenharmony_ciextern UINT32 LOS_DLnkInitMem(VOID *pool, UINT32 size); 943d8536b4Sopenharmony_ci 953d8536b4Sopenharmony_ci/* * 963d8536b4Sopenharmony_ci * @ingroup los_dlinkmem 973d8536b4Sopenharmony_ci * @brief Allocate memory. 983d8536b4Sopenharmony_ci * 993d8536b4Sopenharmony_ci * @par Description: 1003d8536b4Sopenharmony_ci * <ul> 1013d8536b4Sopenharmony_ci * <li>This API is used to allocate memory of which the size is specified by size.</li> 1023d8536b4Sopenharmony_ci * </ul> 1033d8536b4Sopenharmony_ci * @attention 1043d8536b4Sopenharmony_ci * <ul> 1053d8536b4Sopenharmony_ci * <li>After calling this API, ensure that the returned memory address is not null in case that a null pointer will be 1063d8536b4Sopenharmony_ci accessed later.</li> 1073d8536b4Sopenharmony_ci 1083d8536b4Sopenharmony_ci * </ul> 1093d8536b4Sopenharmony_ci * 1103d8536b4Sopenharmony_ci * @param pool [IN] Starting address of memory. 1113d8536b4Sopenharmony_ci * @param size [IN] Size of the memory to be allocated (unit: byte). 1123d8536b4Sopenharmony_ci * 1133d8536b4Sopenharmony_ci * @retval Address of the allocated memory. The memory is successfully allocated. 1143d8536b4Sopenharmony_ci * @retval NULL. The memory fails to be allocated. 1153d8536b4Sopenharmony_ci * @par Dependency: 1163d8536b4Sopenharmony_ci * <ul> 1173d8536b4Sopenharmony_ci * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 1183d8536b4Sopenharmony_ci * </ul> 1193d8536b4Sopenharmony_ci * @see LOS_DLnkFreeMem 1203d8536b4Sopenharmony_ci */ 1213d8536b4Sopenharmony_ciextern void *LOS_DLnkAllocMem(VOID *pool, UINT32 size); 1223d8536b4Sopenharmony_ci 1233d8536b4Sopenharmony_ci/* * 1243d8536b4Sopenharmony_ci * @ingroup los_dlinkmem 1253d8536b4Sopenharmony_ci * @brief Free memory. 1263d8536b4Sopenharmony_ci * 1273d8536b4Sopenharmony_ci * @par Description: 1283d8536b4Sopenharmony_ci * <ul> 1293d8536b4Sopenharmony_ci * <li>This API is used to free the allocated memory.</li> 1303d8536b4Sopenharmony_ci * </ul> 1313d8536b4Sopenharmony_ci * @attention 1323d8536b4Sopenharmony_ci * <ul> 1333d8536b4Sopenharmony_ci * <li>The memory fails to be freed if it has been already freed.</li> 1343d8536b4Sopenharmony_ci 1353d8536b4Sopenharmony_ci * </ul> 1363d8536b4Sopenharmony_ci * 1373d8536b4Sopenharmony_ci * @param pool [IN] Starting address of memory. 1383d8536b4Sopenharmony_ci * @param mem [IN] Address of the memory to be freed. 1393d8536b4Sopenharmony_ci * 1403d8536b4Sopenharmony_ci * @retval #LOS_NOK 1: The memory fails to be freed. 1413d8536b4Sopenharmony_ci * @retval #LOS_OK 0: The memory is successfully freed. 1423d8536b4Sopenharmony_ci * @par Dependency: 1433d8536b4Sopenharmony_ci * <ul> 1443d8536b4Sopenharmony_ci * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 1453d8536b4Sopenharmony_ci * </ul> 1463d8536b4Sopenharmony_ci * @see LOS_DLnkAllocMem 1473d8536b4Sopenharmony_ci */ 1483d8536b4Sopenharmony_ciextern UINT32 LOS_DLnkFreeMem(VOID *pool, VOID *mem); 1493d8536b4Sopenharmony_ci 1503d8536b4Sopenharmony_ci/* * 1513d8536b4Sopenharmony_ci * @ingroup los_dlinkmem 1523d8536b4Sopenharmony_ci * @brief Re-allocate memory. 1533d8536b4Sopenharmony_ci * 1543d8536b4Sopenharmony_ci * @par Description: 1553d8536b4Sopenharmony_ci * <ul> 1563d8536b4Sopenharmony_ci * <li>This API is used to re-allocate memory if the memory allocated before is insufficient. Data in the original memory 1573d8536b4Sopenharmony_ci will be copied to the re-allocated memory, after which the original memory will be freed.</li> 1583d8536b4Sopenharmony_ci 1593d8536b4Sopenharmony_ci * </ul> 1603d8536b4Sopenharmony_ci * @attention 1613d8536b4Sopenharmony_ci * <ul> 1623d8536b4Sopenharmony_ci * <li>Before calling this API, check whether the return value of this API is null.</li> 1633d8536b4Sopenharmony_ci * <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, 1643d8536b4Sopenharmony_ci calling this API frees the original memory.</li> 1653d8536b4Sopenharmony_ci * <li>If the passed-in address of the original memory is null, calling this API allocates a new memory block.</li> 1663d8536b4Sopenharmony_ci * <li>If the new memory block fails to be allocated, the original one will not be released.</li> 1673d8536b4Sopenharmony_ci * </ul> 1683d8536b4Sopenharmony_ci * 1693d8536b4Sopenharmony_ci * @param pool [IN] Starting address of memory. 1703d8536b4Sopenharmony_ci * @param ptr [IN] Address of the re-allocated memory. 1713d8536b4Sopenharmony_ci * @param size [IN] Size of the memory to be allocated. 1723d8536b4Sopenharmony_ci * 1733d8536b4Sopenharmony_ci * @retval Address of the re-allocated memory. The memory is successfully re-allocated. 1743d8536b4Sopenharmony_ci * @retval NULL. The memory fails to be re-allocated. 1753d8536b4Sopenharmony_ci * @par Dependency: 1763d8536b4Sopenharmony_ci * <ul> 1773d8536b4Sopenharmony_ci * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 1783d8536b4Sopenharmony_ci * </ul> 1793d8536b4Sopenharmony_ci * @see LOS_DLnkAllocMem 1803d8536b4Sopenharmony_ci */ 1813d8536b4Sopenharmony_ciextern void *LOS_DLnkReAllocMem(VOID *pool, VOID *ptr, UINT32 size); 1823d8536b4Sopenharmony_ci 1833d8536b4Sopenharmony_ci/* * 1843d8536b4Sopenharmony_ci * @ingroup los_dlinkmem 1853d8536b4Sopenharmony_ci * @brief Allocate aligned memory. 1863d8536b4Sopenharmony_ci * 1873d8536b4Sopenharmony_ci * @par Description: 1883d8536b4Sopenharmony_ci * <ul> 1893d8536b4Sopenharmony_ci * <li>This API is used to allocate memory blocks of specified size and of which the starting addresses are aligned on a 1903d8536b4Sopenharmony_ci * specified boundary.</li> 1913d8536b4Sopenharmony_ci * </ul> 1923d8536b4Sopenharmony_ci * @attention 1933d8536b4Sopenharmony_ci * <ul> 1943d8536b4Sopenharmony_ci * <li>The alignment parameter value must be a power of 2 with the minimum value being 4.</li> 1953d8536b4Sopenharmony_ci * </ul> 1963d8536b4Sopenharmony_ci * 1973d8536b4Sopenharmony_ci * @param pool [IN] Starting address of memory. 1983d8536b4Sopenharmony_ci * @param size [IN] Size of the memory to be allocated. 1993d8536b4Sopenharmony_ci * @param boundary[IN] Boundary on which the memory is aligned. 2003d8536b4Sopenharmony_ci * 2013d8536b4Sopenharmony_ci * @retval Starting address of the allocated aligned memory. The memory is successfully allocated. 2023d8536b4Sopenharmony_ci * @retval The memory fails to be allocated. 2033d8536b4Sopenharmony_ci * @par Dependency: 2043d8536b4Sopenharmony_ci * <ul> 2053d8536b4Sopenharmony_ci * <li>los_dlinkmem.h: the header file that contains the API declaration.</li> 2063d8536b4Sopenharmony_ci * </ul> 2073d8536b4Sopenharmony_ci * @see None. 2083d8536b4Sopenharmony_ci */ 2093d8536b4Sopenharmony_ciextern void *LOS_DlnkAllocMemAlign(void *pool, UINT32 size, UINT32 boundary); 2103d8536b4Sopenharmony_ci 2113d8536b4Sopenharmony_ciextern UINT32 LOS_DLnkCheckMem(void *pool); 2123d8536b4Sopenharmony_ciextern UINT32 LOS_DLnkGetTotalMemUsed(void *pool); 2133d8536b4Sopenharmony_ciextern UINT32 LOS_DLnkGetMemFreeBlks(void *pool); 2143d8536b4Sopenharmony_ciextern UINT32 LOS_DLnkGetMemUsedBlks(void *pool); 2153d8536b4Sopenharmony_ciextern UINT32 LOS_DLnkGetMemTskId(void *ptr); 2163d8536b4Sopenharmony_ci 2173d8536b4Sopenharmony_ci#ifdef OS_MEM_CHECK_DEBUG 2183d8536b4Sopenharmony_ci 2193d8536b4Sopenharmony_ci/* 2203d8536b4Sopenharmony_ci * memcheck error code: the stack have not inited 2213d8536b4Sopenharmony_ci * Value: 0x02000100 2223d8536b4Sopenharmony_ci * Solution: do memcheck must after stack mem init 2233d8536b4Sopenharmony_ci */ 2243d8536b4Sopenharmony_ci#define OS_ERRNO_MEMCHECK_NOT_INIT LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x0) 2253d8536b4Sopenharmony_ci/* 2263d8536b4Sopenharmony_ci * memcheck error code: the pPtr is NULL 2273d8536b4Sopenharmony_ci * Value: 0x02000101 2283d8536b4Sopenharmony_ci * Solution: don't give a NULL parameter 2293d8536b4Sopenharmony_ci */ 2303d8536b4Sopenharmony_ci#define OS_ERRNO_MEMCHECK_PARA_NULL LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x1) 2313d8536b4Sopenharmony_ci/* 2323d8536b4Sopenharmony_ci * memcheck error code: the pPtr addr not in the suit range 2333d8536b4Sopenharmony_ci * Value: 0x02000102 2343d8536b4Sopenharmony_ci * Solution: check pPtr and comfirm it included by stack 2353d8536b4Sopenharmony_ci */ 2363d8536b4Sopenharmony_ci#define OS_ERRNO_MEMCHECK_OUTSIDE LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x2) 2373d8536b4Sopenharmony_ci/* 2383d8536b4Sopenharmony_ci * memcheck error code: can't find the ctrl node 2393d8536b4Sopenharmony_ci * Value: 0x02000103 2403d8536b4Sopenharmony_ci * Solution: confirm the pPtr if this node has been freed or has not been alloced 2413d8536b4Sopenharmony_ci */ 2423d8536b4Sopenharmony_ci#define OS_ERRNO_MEMCHECK_NO_HEAD LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x3) 2433d8536b4Sopenharmony_ci/* 2443d8536b4Sopenharmony_ci * memcheck error code: the para level is wrong 2453d8536b4Sopenharmony_ci * Value: 0x02000104 2463d8536b4Sopenharmony_ci * Solution: checkout the memcheck level by the func mCheck_Level" 2473d8536b4Sopenharmony_ci */ 2483d8536b4Sopenharmony_ci#define OS_ERRNO_MEMCHECK_WRONG_LEVEL LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x4) 2493d8536b4Sopenharmony_ci/* 2503d8536b4Sopenharmony_ci * memcheck error code: memcheck func not open 2513d8536b4Sopenharmony_ci * Value: 0x02000105 2523d8536b4Sopenharmony_ci * Solution: enable memcheck by the func "OS_SetMemCheck_Level" 2533d8536b4Sopenharmony_ci */ 2543d8536b4Sopenharmony_ci#define OS_ERRNO_MEMCHECK_DISABLED LOS_ERRNO_OS_ERROR(OS_MOD_MEM, 0x5) 2553d8536b4Sopenharmony_ci 2563d8536b4Sopenharmony_ci#define LOS_MEM_CHECK_LEVEL_LO 0 2573d8536b4Sopenharmony_ci#define LOS_MEM_CHECK_LEVEL_HI 1 2583d8536b4Sopenharmony_ci#define LOS_MEM_CHECK_DISABLE 0xff 2593d8536b4Sopenharmony_ci#define LOS_MEM_CHECK_LEVEL_D OS_ERRNO_MEMCHECK_DISABLED 2603d8536b4Sopenharmony_ci 2613d8536b4Sopenharmony_ciextern UINT32 LOS_CheckMemSize(VOID *pool, VOID *ptr, UINT32 *totalSize, UINT32 *availSize); 2623d8536b4Sopenharmony_ciextern UINT32 LOS_SetMemCheck_Level(UINT8 level); 2633d8536b4Sopenharmony_ciextern UINT8 LOS_GetMemCheck_Level(VOID); 2643d8536b4Sopenharmony_ci#endif 2653d8536b4Sopenharmony_ci 2663d8536b4Sopenharmony_ci#ifdef __cplusplus 2673d8536b4Sopenharmony_ci#if __cplusplus 2683d8536b4Sopenharmony_ci} 2693d8536b4Sopenharmony_ci#endif /* __cplusplus */ 2703d8536b4Sopenharmony_ci#endif /* __cplusplus */ 2713d8536b4Sopenharmony_ci 2723d8536b4Sopenharmony_ci#endif /* _LOS_DLINK_MEM_H */ 273