10e98b08fSopenharmony_ci/*
20e98b08fSopenharmony_ci * Copyright (c) 2020 Huawei Device Co., Ltd.
30e98b08fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40e98b08fSopenharmony_ci * you may not use this file except in compliance with the License.
50e98b08fSopenharmony_ci * You may obtain a copy of the License at
60e98b08fSopenharmony_ci *
70e98b08fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80e98b08fSopenharmony_ci *
90e98b08fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100e98b08fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110e98b08fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120e98b08fSopenharmony_ci * See the License for the specific language governing permissions and
130e98b08fSopenharmony_ci * limitations under the License.
140e98b08fSopenharmony_ci */
150e98b08fSopenharmony_ci
160e98b08fSopenharmony_ci/*
170e98b08fSopenharmony_ci * @defgroup utils_list Doubly linked list
180e98b08fSopenharmony_ci * @ingroup utils
190e98b08fSopenharmony_ci * @attention
200e98b08fSopenharmony_ci * <ul>
210e98b08fSopenharmony_ci * <li>All of the APIs provided in this module are not thread-safe.</li>
220e98b08fSopenharmony_ci * </ul>
230e98b08fSopenharmony_ci */
240e98b08fSopenharmony_ci
250e98b08fSopenharmony_ci#ifndef _UTILS_LIST_H
260e98b08fSopenharmony_ci#define _UTILS_LIST_H
270e98b08fSopenharmony_ci
280e98b08fSopenharmony_ci#include <stdbool.h>
290e98b08fSopenharmony_ci#include "ohos_types.h"
300e98b08fSopenharmony_ci
310e98b08fSopenharmony_ci#ifdef __cplusplus
320e98b08fSopenharmony_ci#if __cplusplus
330e98b08fSopenharmony_ciextern "C" {
340e98b08fSopenharmony_ci#endif /* __cplusplus */
350e98b08fSopenharmony_ci#endif /* __cplusplus */
360e98b08fSopenharmony_ci
370e98b08fSopenharmony_citypedef struct UTILS_DL_LIST {
380e98b08fSopenharmony_ci    struct UTILS_DL_LIST *pstPrev; /* < Current node's pointer to the previous node */
390e98b08fSopenharmony_ci    struct UTILS_DL_LIST *pstNext; /* < Current node's pointer to the next node */
400e98b08fSopenharmony_ci} UTILS_DL_LIST;
410e98b08fSopenharmony_ci
420e98b08fSopenharmony_ci/*
430e98b08fSopenharmony_ci * @ingroup utils_list
440e98b08fSopenharmony_ci *
450e98b08fSopenharmony_ci * @par Description:
460e98b08fSopenharmony_ci * This API is used to initialize a doubly linked list.
470e98b08fSopenharmony_ci * @attention
480e98b08fSopenharmony_ci * <ul>
490e98b08fSopenharmony_ci * <li>The parameter passed in should be ensured to be a legal pointer.</li>
500e98b08fSopenharmony_ci * </ul>
510e98b08fSopenharmony_ci *
520e98b08fSopenharmony_ci * @param list    [IN] Node in a doubly linked list.
530e98b08fSopenharmony_ci *
540e98b08fSopenharmony_ci * @retval None.
550e98b08fSopenharmony_ci * @par Dependency:
560e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
570e98b08fSopenharmony_ci * @see
580e98b08fSopenharmony_ci */
590e98b08fSopenharmony_cistatic inline void UtilsListInit(UTILS_DL_LIST *list)
600e98b08fSopenharmony_ci{
610e98b08fSopenharmony_ci    list->pstNext = list;
620e98b08fSopenharmony_ci    list->pstPrev = list;
630e98b08fSopenharmony_ci}
640e98b08fSopenharmony_ci
650e98b08fSopenharmony_ci/*
660e98b08fSopenharmony_ci * @ingroup utils_list
670e98b08fSopenharmony_ci * @brief Point to the next node pointed to by the current node.
680e98b08fSopenharmony_ci *
690e98b08fSopenharmony_ci * @par Description:
700e98b08fSopenharmony_ci * <ul>
710e98b08fSopenharmony_ci * <li>This API is used to point to the next node pointed to by the current node.</li>
720e98b08fSopenharmony_ci * </ul>
730e98b08fSopenharmony_ci * @attention
740e98b08fSopenharmony_ci * <ul>
750e98b08fSopenharmony_ci * <li>None.</li>
760e98b08fSopenharmony_ci * </ul>
770e98b08fSopenharmony_ci *
780e98b08fSopenharmony_ci * @param object  [IN] Node in the doubly linked list.
790e98b08fSopenharmony_ci *
800e98b08fSopenharmony_ci * @retval None.
810e98b08fSopenharmony_ci * @par Dependency:
820e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
830e98b08fSopenharmony_ci * @see
840e98b08fSopenharmony_ci */
850e98b08fSopenharmony_ci#define UTILS_DL_LIST_FIRST(object) ((object)->pstNext)
860e98b08fSopenharmony_ci
870e98b08fSopenharmony_ci/*
880e98b08fSopenharmony_ci * @ingroup utils_list
890e98b08fSopenharmony_ci * @brief Node is the end of the list.
900e98b08fSopenharmony_ci *
910e98b08fSopenharmony_ci * @par Description:
920e98b08fSopenharmony_ci * <ul>
930e98b08fSopenharmony_ci * <li>This API is used to test node is the end of the list.</li>
940e98b08fSopenharmony_ci * </ul>
950e98b08fSopenharmony_ci * @attention
960e98b08fSopenharmony_ci * <ul>
970e98b08fSopenharmony_ci * <li>None.</li>
980e98b08fSopenharmony_ci * </ul>
990e98b08fSopenharmony_ci *
1000e98b08fSopenharmony_ci * @param object  [IN] Node in the doubly linked list.
1010e98b08fSopenharmony_ci *
1020e98b08fSopenharmony_ci * @retval None.
1030e98b08fSopenharmony_ci * @par Dependency:
1040e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
1050e98b08fSopenharmony_ci * @see
1060e98b08fSopenharmony_ci */
1070e98b08fSopenharmony_ci#define UTILS_DL_LIST_IS_END(list, node) ((list) == (node) ? TRUE : FALSE)
1080e98b08fSopenharmony_ci
1090e98b08fSopenharmony_ci/*
1100e98b08fSopenharmony_ci * @ingroup utils_list
1110e98b08fSopenharmony_ci * @brief Node is on the list.
1120e98b08fSopenharmony_ci *
1130e98b08fSopenharmony_ci * @par Description:
1140e98b08fSopenharmony_ci * <ul>
1150e98b08fSopenharmony_ci * <li>This API is used to test node is on the list.</li>
1160e98b08fSopenharmony_ci * </ul>
1170e98b08fSopenharmony_ci * @attention
1180e98b08fSopenharmony_ci * <ul>
1190e98b08fSopenharmony_ci * <li>None.</li>
1200e98b08fSopenharmony_ci * </ul>
1210e98b08fSopenharmony_ci *
1220e98b08fSopenharmony_ci * @param object  [IN] Node in the doubly linked list.
1230e98b08fSopenharmony_ci *
1240e98b08fSopenharmony_ci * @retval None.
1250e98b08fSopenharmony_ci * @par Dependency:
1260e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
1270e98b08fSopenharmony_ci * @see
1280e98b08fSopenharmony_ci */
1290e98b08fSopenharmony_ci#define UTILS_DL_LIST_IS_ON_QUEUE(node) ((node)->pstPrev != NULL && (node)->pstNext != NULL)
1300e98b08fSopenharmony_ci
1310e98b08fSopenharmony_ci/*
1320e98b08fSopenharmony_ci * @ingroup utils_list
1330e98b08fSopenharmony_ci * @brief Point to the previous node pointed to by the current node.
1340e98b08fSopenharmony_ci *
1350e98b08fSopenharmony_ci * @par Description:
1360e98b08fSopenharmony_ci * <ul>
1370e98b08fSopenharmony_ci * <li>This API is used to point to the previous node pointed to by the current node.</li>
1380e98b08fSopenharmony_ci * </ul>
1390e98b08fSopenharmony_ci * @attention
1400e98b08fSopenharmony_ci * <ul>
1410e98b08fSopenharmony_ci * <li>None.</li>
1420e98b08fSopenharmony_ci * </ul>
1430e98b08fSopenharmony_ci *
1440e98b08fSopenharmony_ci * @param object  [IN] Node in the doubly linked list.
1450e98b08fSopenharmony_ci *
1460e98b08fSopenharmony_ci * @retval None.
1470e98b08fSopenharmony_ci * @par Dependency:
1480e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
1490e98b08fSopenharmony_ci * @see
1500e98b08fSopenharmony_ci */
1510e98b08fSopenharmony_ci#define UTILS_DL_LIST_LAST(object) ((object)->pstPrev)
1520e98b08fSopenharmony_ci
1530e98b08fSopenharmony_ci/*
1540e98b08fSopenharmony_ci * @ingroup utils_list
1550e98b08fSopenharmony_ci * @brief Insert a new node to a doubly linked list.
1560e98b08fSopenharmony_ci *
1570e98b08fSopenharmony_ci * @par Description:
1580e98b08fSopenharmony_ci * This API is used to insert a new node to a doubly linked list.
1590e98b08fSopenharmony_ci * @attention
1600e98b08fSopenharmony_ci * <ul>
1610e98b08fSopenharmony_ci * <li>The parameters passed in should be ensured to be legal pointers.</li>
1620e98b08fSopenharmony_ci * </ul>
1630e98b08fSopenharmony_ci *
1640e98b08fSopenharmony_ci * @param list    [IN] Doubly linked list where the new node is inserted.
1650e98b08fSopenharmony_ci * @param node    [IN] New node to be inserted.
1660e98b08fSopenharmony_ci *
1670e98b08fSopenharmony_ci * @retval None
1680e98b08fSopenharmony_ci * @par Dependency:
1690e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
1700e98b08fSopenharmony_ci * @see UtilsListDelete
1710e98b08fSopenharmony_ci */
1720e98b08fSopenharmony_cistatic inline void UtilsListAdd(UTILS_DL_LIST *list, UTILS_DL_LIST *node)
1730e98b08fSopenharmony_ci{
1740e98b08fSopenharmony_ci    node->pstNext = list->pstNext;
1750e98b08fSopenharmony_ci    node->pstPrev = list;
1760e98b08fSopenharmony_ci    list->pstNext->pstPrev = node;
1770e98b08fSopenharmony_ci    list->pstNext = node;
1780e98b08fSopenharmony_ci}
1790e98b08fSopenharmony_ci
1800e98b08fSopenharmony_ci/*
1810e98b08fSopenharmony_ci * @ingroup utils_list
1820e98b08fSopenharmony_ci * @brief Insert a node to the tail of a doubly linked list.
1830e98b08fSopenharmony_ci *
1840e98b08fSopenharmony_ci * @par Description:
1850e98b08fSopenharmony_ci * This API is used to insert a new node to the tail of a doubly linked list.
1860e98b08fSopenharmony_ci * @attention
1870e98b08fSopenharmony_ci * <ul>
1880e98b08fSopenharmony_ci * <li>The parameters passed in should be ensured to be legal pointers.</li>
1890e98b08fSopenharmony_ci * </ul>
1900e98b08fSopenharmony_ci *
1910e98b08fSopenharmony_ci * @param list     [IN] Doubly linked list where the new node is inserted.
1920e98b08fSopenharmony_ci * @param node     [IN] New node to be inserted.
1930e98b08fSopenharmony_ci *
1940e98b08fSopenharmony_ci * @retval None.
1950e98b08fSopenharmony_ci * @par Dependency:
1960e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
1970e98b08fSopenharmony_ci * @see UtilsListAdd | UtilsListHeadInsert
1980e98b08fSopenharmony_ci */
1990e98b08fSopenharmony_cistatic inline void UtilsListTailInsert(UTILS_DL_LIST *list, UTILS_DL_LIST *node)
2000e98b08fSopenharmony_ci{
2010e98b08fSopenharmony_ci    UtilsListAdd(list->pstPrev, node);
2020e98b08fSopenharmony_ci}
2030e98b08fSopenharmony_ci
2040e98b08fSopenharmony_ci/*
2050e98b08fSopenharmony_ci * @ingroup utils_list
2060e98b08fSopenharmony_ci * @brief Insert a node to the head of a doubly linked list.
2070e98b08fSopenharmony_ci *
2080e98b08fSopenharmony_ci * @par Description:
2090e98b08fSopenharmony_ci * This API is used to insert a new node to the head of a doubly linked list.
2100e98b08fSopenharmony_ci * @attention
2110e98b08fSopenharmony_ci * <ul>
2120e98b08fSopenharmony_ci * <li>The parameters passed in should be ensured to be legal pointers.</li>
2130e98b08fSopenharmony_ci * </ul>
2140e98b08fSopenharmony_ci *
2150e98b08fSopenharmony_ci * @param list     [IN] Doubly linked list where the new node is inserted.
2160e98b08fSopenharmony_ci * @param node     [IN] New node to be inserted.
2170e98b08fSopenharmony_ci *
2180e98b08fSopenharmony_ci * @retval None.
2190e98b08fSopenharmony_ci * @par Dependency:
2200e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
2210e98b08fSopenharmony_ci * @see UtilsListAdd | UtilsListTailInsert
2220e98b08fSopenharmony_ci */
2230e98b08fSopenharmony_cistatic inline void UtilsListHeadInsert(UTILS_DL_LIST *list, UTILS_DL_LIST *node)
2240e98b08fSopenharmony_ci{
2250e98b08fSopenharmony_ci    UtilsListAdd(list, node);
2260e98b08fSopenharmony_ci}
2270e98b08fSopenharmony_ci
2280e98b08fSopenharmony_ci/*
2290e98b08fSopenharmony_ci * @ingroup utils_list
2300e98b08fSopenharmony_ci *
2310e98b08fSopenharmony_ci * @par Description:
2320e98b08fSopenharmony_ci * <ul>
2330e98b08fSopenharmony_ci * <li>This API is used to delete a specified node from a doubly linked list.</li>
2340e98b08fSopenharmony_ci * </ul>
2350e98b08fSopenharmony_ci * @attention
2360e98b08fSopenharmony_ci * <ul>
2370e98b08fSopenharmony_ci * <li>The parameter passed in should be ensured to be a legal pointer.</li>
2380e98b08fSopenharmony_ci * </ul>
2390e98b08fSopenharmony_ci *
2400e98b08fSopenharmony_ci * @param node    [IN] Node to be deleted.
2410e98b08fSopenharmony_ci *
2420e98b08fSopenharmony_ci * @retval None.
2430e98b08fSopenharmony_ci * @par Dependency:
2440e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
2450e98b08fSopenharmony_ci * @see UtilsListAdd
2460e98b08fSopenharmony_ci */
2470e98b08fSopenharmony_cistatic inline void UtilsListDelete(UTILS_DL_LIST *node)
2480e98b08fSopenharmony_ci{
2490e98b08fSopenharmony_ci    node->pstNext->pstPrev = node->pstPrev;
2500e98b08fSopenharmony_ci    node->pstPrev->pstNext = node->pstNext;
2510e98b08fSopenharmony_ci    node->pstNext = NULL;
2520e98b08fSopenharmony_ci    node->pstPrev = NULL;
2530e98b08fSopenharmony_ci}
2540e98b08fSopenharmony_ci
2550e98b08fSopenharmony_ci/*
2560e98b08fSopenharmony_ci * @ingroup utils_list
2570e98b08fSopenharmony_ci * @brief Identify whether a specified doubly linked list is empty.
2580e98b08fSopenharmony_ci *
2590e98b08fSopenharmony_ci * @par Description:
2600e98b08fSopenharmony_ci * <ul>
2610e98b08fSopenharmony_ci * <li>This API is used to return whether a doubly linked list is empty.</li>
2620e98b08fSopenharmony_ci * </ul>
2630e98b08fSopenharmony_ci * @attention
2640e98b08fSopenharmony_ci * <ul>
2650e98b08fSopenharmony_ci * <li>The parameter passed in should be ensured to be a legal pointer.</li>
2660e98b08fSopenharmony_ci * </ul>
2670e98b08fSopenharmony_ci *
2680e98b08fSopenharmony_ci * @param list  [IN] Doubly linked list.
2690e98b08fSopenharmony_ci *
2700e98b08fSopenharmony_ci * @retval TRUE The doubly linked list is empty.
2710e98b08fSopenharmony_ci * @retval FALSE The doubly linked list is not empty.
2720e98b08fSopenharmony_ci * @par Dependency:
2730e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
2740e98b08fSopenharmony_ci * @see
2750e98b08fSopenharmony_ci */
2760e98b08fSopenharmony_cistatic inline bool UtilsListEmpty(UTILS_DL_LIST *list)
2770e98b08fSopenharmony_ci{
2780e98b08fSopenharmony_ci    return (bool)(list->pstNext == list);
2790e98b08fSopenharmony_ci}
2800e98b08fSopenharmony_ci
2810e98b08fSopenharmony_ci/*
2820e98b08fSopenharmony_ci * @ingroup utils_list
2830e98b08fSopenharmony_ci * @brief Insert a new list to a doubly linked list.
2840e98b08fSopenharmony_ci *
2850e98b08fSopenharmony_ci * @par Description:
2860e98b08fSopenharmony_ci * This API is used to insert a new list to a doubly linked list.
2870e98b08fSopenharmony_ci * @attention
2880e98b08fSopenharmony_ci * <ul>
2890e98b08fSopenharmony_ci * <li>The parameters passed in should be ensured to be legal pointers.</li>
2900e98b08fSopenharmony_ci * </ul>
2910e98b08fSopenharmony_ci *
2920e98b08fSopenharmony_ci * @param oldList    [IN] Doubly linked list where the new list is inserted.
2930e98b08fSopenharmony_ci * @param newList    [IN] New list to be inserted.
2940e98b08fSopenharmony_ci *
2950e98b08fSopenharmony_ci * @retval None
2960e98b08fSopenharmony_ci * @par Dependency:
2970e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
2980e98b08fSopenharmony_ci * @see UtilsListDelete
2990e98b08fSopenharmony_ci */
3000e98b08fSopenharmony_cistatic inline void UtilsListAddList(UTILS_DL_LIST *oldList, UTILS_DL_LIST *newList)
3010e98b08fSopenharmony_ci{
3020e98b08fSopenharmony_ci    UTILS_DL_LIST *oldListHead = oldList->pstNext;
3030e98b08fSopenharmony_ci    UTILS_DL_LIST *oldListTail = oldList;
3040e98b08fSopenharmony_ci    UTILS_DL_LIST *newListHead = newList;
3050e98b08fSopenharmony_ci    UTILS_DL_LIST *newListTail = newList->pstPrev;
3060e98b08fSopenharmony_ci
3070e98b08fSopenharmony_ci    oldListTail->pstNext = newListHead;
3080e98b08fSopenharmony_ci    newListHead->pstPrev = oldListTail;
3090e98b08fSopenharmony_ci    oldListHead->pstPrev = newListTail;
3100e98b08fSopenharmony_ci    newListTail->pstNext = oldListHead;
3110e98b08fSopenharmony_ci}
3120e98b08fSopenharmony_ci
3130e98b08fSopenharmony_ci/*
3140e98b08fSopenharmony_ci * @ingroup utils_list
3150e98b08fSopenharmony_ci * @brief Insert a doubly list to the tail of a doubly linked list.
3160e98b08fSopenharmony_ci *
3170e98b08fSopenharmony_ci * @par Description:
3180e98b08fSopenharmony_ci * This API is used to insert a new doubly list to the tail of a doubly linked list.
3190e98b08fSopenharmony_ci * @attention
3200e98b08fSopenharmony_ci * <ul>
3210e98b08fSopenharmony_ci * <li>The parameters passed in should be ensured to be legal pointers.</li>
3220e98b08fSopenharmony_ci * </ul>
3230e98b08fSopenharmony_ci *
3240e98b08fSopenharmony_ci * @param oldList     [IN] Doubly linked list where the new list is inserted.
3250e98b08fSopenharmony_ci * @param newList     [IN] New list to be inserted.
3260e98b08fSopenharmony_ci *
3270e98b08fSopenharmony_ci * @retval None.
3280e98b08fSopenharmony_ci * @par Dependency:
3290e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
3300e98b08fSopenharmony_ci * @see UtilsListAddList | UtilsListHeadInsertList
3310e98b08fSopenharmony_ci */
3320e98b08fSopenharmony_cistatic inline void UtilsListTailInsertList(UTILS_DL_LIST *oldList, UTILS_DL_LIST *newList)
3330e98b08fSopenharmony_ci{
3340e98b08fSopenharmony_ci    UtilsListAddList(oldList->pstPrev, newList);
3350e98b08fSopenharmony_ci}
3360e98b08fSopenharmony_ci
3370e98b08fSopenharmony_ci/*
3380e98b08fSopenharmony_ci * @ingroup utils_list
3390e98b08fSopenharmony_ci * @brief Insert a doubly list to the head of a doubly linked list.
3400e98b08fSopenharmony_ci *
3410e98b08fSopenharmony_ci * @par Description:
3420e98b08fSopenharmony_ci * This API is used to insert a new doubly list to the head of a doubly linked list.
3430e98b08fSopenharmony_ci * @attention
3440e98b08fSopenharmony_ci * <ul>
3450e98b08fSopenharmony_ci * <li>The parameters passed in should be ensured to be legal pointers.</li>
3460e98b08fSopenharmony_ci * </ul>
3470e98b08fSopenharmony_ci *
3480e98b08fSopenharmony_ci * @param oldList     [IN] Doubly linked list where the new list is inserted.
3490e98b08fSopenharmony_ci * @param newList     [IN] New list to be inserted.
3500e98b08fSopenharmony_ci *
3510e98b08fSopenharmony_ci * @retval None.
3520e98b08fSopenharmony_ci * @par Dependency:
3530e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
3540e98b08fSopenharmony_ci * @see UtilsListAddList | UtilsListTailInsertList
3550e98b08fSopenharmony_ci */
3560e98b08fSopenharmony_cistatic inline void UtilsListHeadInsertList(UTILS_DL_LIST *oldList, UTILS_DL_LIST *newList)
3570e98b08fSopenharmony_ci{
3580e98b08fSopenharmony_ci    UtilsListAddList(oldList, newList);
3590e98b08fSopenharmony_ci}
3600e98b08fSopenharmony_ci
3610e98b08fSopenharmony_ci/*
3620e98b08fSopenharmony_ci * @ingroup utils_list
3630e98b08fSopenharmony_ci * @brief Obtain the offset of a field to a structure address.
3640e98b08fSopenharmony_ci *
3650e98b08fSopenharmony_ci * @par  Description:
3660e98b08fSopenharmony_ci * This API is used to obtain the offset of a field to a structure address.
3670e98b08fSopenharmony_ci * @attention
3680e98b08fSopenharmony_ci * <ul>
3690e98b08fSopenharmony_ci * <li>None.</li>
3700e98b08fSopenharmony_ci * </ul>
3710e98b08fSopenharmony_ci *
3720e98b08fSopenharmony_ci * @param type   [IN] Structure name.
3730e98b08fSopenharmony_ci * @param field  [IN] Name of the field of which the offset is to be measured.
3740e98b08fSopenharmony_ci *
3750e98b08fSopenharmony_ci * @retval Offset of the field to the structure address.
3760e98b08fSopenharmony_ci * @par Dependency:
3770e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
3780e98b08fSopenharmony_ci * @see
3790e98b08fSopenharmony_ci */
3800e98b08fSopenharmony_ci#ifndef OFFSET_OF_FIELD
3810e98b08fSopenharmony_ci#define OFFSET_OF_FIELD(type, field) ((unsigned int)&((type *)0)->field)
3820e98b08fSopenharmony_ci#endif
3830e98b08fSopenharmony_ci
3840e98b08fSopenharmony_ci/*
3850e98b08fSopenharmony_ci * @ingroup utils_list
3860e98b08fSopenharmony_ci * @brief Obtain the pointer to a doubly linked list in a structure.
3870e98b08fSopenharmony_ci *
3880e98b08fSopenharmony_ci * @par Description:
3890e98b08fSopenharmony_ci * This API is used to obtain the pointer to a doubly linked list in a structure.
3900e98b08fSopenharmony_ci * @attention
3910e98b08fSopenharmony_ci * <ul>
3920e98b08fSopenharmony_ci * <li>None.</li>
3930e98b08fSopenharmony_ci * </ul>
3940e98b08fSopenharmony_ci *
3950e98b08fSopenharmony_ci * @param type    [IN] Structure name.
3960e98b08fSopenharmony_ci * @param member  [IN] Member name of the doubly linked list in the structure.
3970e98b08fSopenharmony_ci *
3980e98b08fSopenharmony_ci * @retval Pointer to the doubly linked list in the structure.
3990e98b08fSopenharmony_ci * @par Dependency:
4000e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
4010e98b08fSopenharmony_ci * @see
4020e98b08fSopenharmony_ci */
4030e98b08fSopenharmony_ci#define UTILS_OFF_SET_OF(type, member) ((unsigned int)&((type *)0)->member)
4040e98b08fSopenharmony_ci
4050e98b08fSopenharmony_ci/*
4060e98b08fSopenharmony_ci * @ingroup utils_list
4070e98b08fSopenharmony_ci * @brief Obtain the pointer to a structure that contains a doubly linked list.
4080e98b08fSopenharmony_ci *
4090e98b08fSopenharmony_ci * @par Description:
4100e98b08fSopenharmony_ci * This API is used to obtain the pointer to a structure that contains a doubly linked list.
4110e98b08fSopenharmony_ci * <ul>
4120e98b08fSopenharmony_ci * <li>None.</li>
4130e98b08fSopenharmony_ci * </ul>
4140e98b08fSopenharmony_ci * @attention
4150e98b08fSopenharmony_ci * <ul>
4160e98b08fSopenharmony_ci * <li>None.</li>
4170e98b08fSopenharmony_ci * </ul>
4180e98b08fSopenharmony_ci *
4190e98b08fSopenharmony_ci * @param item    [IN] Current node's pointer to the next node.
4200e98b08fSopenharmony_ci * @param type    [IN] Structure name.
4210e98b08fSopenharmony_ci * @param member  [IN] Member name of the doubly linked list in the structure.
4220e98b08fSopenharmony_ci *
4230e98b08fSopenharmony_ci * @retval Pointer to the structure that contains the doubly linked list.
4240e98b08fSopenharmony_ci * @par Dependency:
4250e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
4260e98b08fSopenharmony_ci * @see
4270e98b08fSopenharmony_ci */
4280e98b08fSopenharmony_ci#define UTILS_DL_LIST_ENTRY(item, type, member) \
4290e98b08fSopenharmony_ci    ((type *)(void *)((char *)(item) - UTILS_OFF_SET_OF(type, member)))
4300e98b08fSopenharmony_ci
4310e98b08fSopenharmony_ci/*
4320e98b08fSopenharmony_ci * @ingroup utils_list
4330e98b08fSopenharmony_ci * @brief Iterate over a doubly linked list of given type.
4340e98b08fSopenharmony_ci *
4350e98b08fSopenharmony_ci * @par Description:
4360e98b08fSopenharmony_ci * This API is used to iterate over a doubly linked list of given type.
4370e98b08fSopenharmony_ci * @attention
4380e98b08fSopenharmony_ci * <ul>
4390e98b08fSopenharmony_ci * <li>None.</li>
4400e98b08fSopenharmony_ci * </ul>
4410e98b08fSopenharmony_ci *
4420e98b08fSopenharmony_ci * @param item           [IN] Pointer to the structure that contains the doubly linked list that is to be traversed.
4430e98b08fSopenharmony_ci * @param list           [IN] Pointer to the doubly linked list to be traversed.
4440e98b08fSopenharmony_ci * @param type           [IN] Structure name.
4450e98b08fSopenharmony_ci * @param member         [IN] Member name of the doubly linked list in the structure.
4460e98b08fSopenharmony_ci *
4470e98b08fSopenharmony_ci * @retval None.
4480e98b08fSopenharmony_ci * @par Dependency:
4490e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
4500e98b08fSopenharmony_ci * @see
4510e98b08fSopenharmony_ci */
4520e98b08fSopenharmony_ci#define UTILS_DL_LIST_FOR_EACH_ENTRY(item, list, type, member)             \
4530e98b08fSopenharmony_ci    for (item = UTILS_DL_LIST_ENTRY((list)->pstNext, type, member);        \
4540e98b08fSopenharmony_ci         &(item)->member != (list);                                      \
4550e98b08fSopenharmony_ci         item = UTILS_DL_LIST_ENTRY((item)->member.pstNext, type, member))
4560e98b08fSopenharmony_ci
4570e98b08fSopenharmony_ci/*
4580e98b08fSopenharmony_ci * @ingroup utils_list
4590e98b08fSopenharmony_ci * @brief iterate over a doubly linked list safe against removal of list entry.
4600e98b08fSopenharmony_ci *
4610e98b08fSopenharmony_ci * @par Description:
4620e98b08fSopenharmony_ci * This API is used to iterate over a doubly linked list safe against removal of list entry.
4630e98b08fSopenharmony_ci * @attention
4640e98b08fSopenharmony_ci * <ul>
4650e98b08fSopenharmony_ci * <li>None.</li>
4660e98b08fSopenharmony_ci * </ul>
4670e98b08fSopenharmony_ci *
4680e98b08fSopenharmony_ci * @param item           [IN] Pointer to the structure that contains the doubly linked list that is to be traversed.
4690e98b08fSopenharmony_ci * @param next           [IN] Save the next node.
4700e98b08fSopenharmony_ci * @param list           [IN] Pointer to the doubly linked list to be traversed.
4710e98b08fSopenharmony_ci * @param type           [IN] Structure name.
4720e98b08fSopenharmony_ci * @param member         [IN] Member name of the doubly linked list in the structure.
4730e98b08fSopenharmony_ci *
4740e98b08fSopenharmony_ci * @retval None.
4750e98b08fSopenharmony_ci * @par Dependency:
4760e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
4770e98b08fSopenharmony_ci * @see
4780e98b08fSopenharmony_ci */
4790e98b08fSopenharmony_ci#define UTILS_DL_LIST_FOR_EACH_ENTRY_SAFE(item, next, list, type, member)               \
4800e98b08fSopenharmony_ci    for (item = UTILS_DL_LIST_ENTRY((list)->pstNext, type, member),                     \
4810e98b08fSopenharmony_ci         next = UTILS_DL_LIST_ENTRY((item)->member.pstNext, type, member);              \
4820e98b08fSopenharmony_ci         &(item)->member != (list);                                                   \
4830e98b08fSopenharmony_ci         item = next, next = UTILS_DL_LIST_ENTRY((item)->member.pstNext, type, member))
4840e98b08fSopenharmony_ci
4850e98b08fSopenharmony_ci/*
4860e98b08fSopenharmony_ci * @ingroup utils_list
4870e98b08fSopenharmony_ci * @brief Delete initialize a doubly linked list.
4880e98b08fSopenharmony_ci *
4890e98b08fSopenharmony_ci * @par Description:
4900e98b08fSopenharmony_ci * This API is used to delete initialize a doubly linked list.
4910e98b08fSopenharmony_ci * @attention
4920e98b08fSopenharmony_ci * <ul>
4930e98b08fSopenharmony_ci * <li>The parameter passed in should be ensured to be s legal pointer.</li>
4940e98b08fSopenharmony_ci * </ul>
4950e98b08fSopenharmony_ci *
4960e98b08fSopenharmony_ci * @param list    [IN] Doubly linked list.
4970e98b08fSopenharmony_ci *
4980e98b08fSopenharmony_ci * @retval None.
4990e98b08fSopenharmony_ci * @par Dependency:
5000e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
5010e98b08fSopenharmony_ci * @see
5020e98b08fSopenharmony_ci */
5030e98b08fSopenharmony_cistatic inline void UtilsListDelInit(UTILS_DL_LIST *list)
5040e98b08fSopenharmony_ci{
5050e98b08fSopenharmony_ci    list->pstNext->pstPrev = list->pstPrev;
5060e98b08fSopenharmony_ci    list->pstPrev->pstNext = list->pstNext;
5070e98b08fSopenharmony_ci    UtilsListInit(list);
5080e98b08fSopenharmony_ci}
5090e98b08fSopenharmony_ci
5100e98b08fSopenharmony_ci/*
5110e98b08fSopenharmony_ci * @ingroup utils_list
5120e98b08fSopenharmony_ci * @brief iterate over a doubly linked list.
5130e98b08fSopenharmony_ci *
5140e98b08fSopenharmony_ci * @par Description:
5150e98b08fSopenharmony_ci * This API is used to iterate over a doubly linked list.
5160e98b08fSopenharmony_ci * @attention
5170e98b08fSopenharmony_ci * <ul>
5180e98b08fSopenharmony_ci * <li>None.</li>
5190e98b08fSopenharmony_ci * </ul>
5200e98b08fSopenharmony_ci *
5210e98b08fSopenharmony_ci * @param item           [IN] Pointer to the structure that contains the doubly linked list that is to be traversed.
5220e98b08fSopenharmony_ci * @param list           [IN] Pointer to the doubly linked list to be traversed.
5230e98b08fSopenharmony_ci *
5240e98b08fSopenharmony_ci * @retval None.
5250e98b08fSopenharmony_ci * @par Dependency:
5260e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
5270e98b08fSopenharmony_ci * @see
5280e98b08fSopenharmony_ci */
5290e98b08fSopenharmony_ci#define UTILS_DL_LIST_FOR_EACH(item, list) \
5300e98b08fSopenharmony_ci    for (item = (list)->pstNext;         \
5310e98b08fSopenharmony_ci         (item) != (list);               \
5320e98b08fSopenharmony_ci         item = (item)->pstNext)
5330e98b08fSopenharmony_ci
5340e98b08fSopenharmony_ci/*
5350e98b08fSopenharmony_ci * @ingroup utils_list
5360e98b08fSopenharmony_ci * @brief Iterate over a doubly linked list safe against removal of list entry.
5370e98b08fSopenharmony_ci *
5380e98b08fSopenharmony_ci * @par Description:
5390e98b08fSopenharmony_ci * This API is used to iterate over a doubly linked list safe against removal of list entry.
5400e98b08fSopenharmony_ci * @attention
5410e98b08fSopenharmony_ci * <ul>
5420e98b08fSopenharmony_ci * <li>None.</li>
5430e98b08fSopenharmony_ci * </ul>
5440e98b08fSopenharmony_ci *
5450e98b08fSopenharmony_ci * @param item           [IN] Pointer to the structure that contains the doubly linked list that is to be traversed.
5460e98b08fSopenharmony_ci * @param next           [IN] Save the next node.
5470e98b08fSopenharmony_ci * @param list           [IN] Pointer to the doubly linked list to be traversed.
5480e98b08fSopenharmony_ci *
5490e98b08fSopenharmony_ci * @retval None.
5500e98b08fSopenharmony_ci * @par Dependency:
5510e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
5520e98b08fSopenharmony_ci * @see
5530e98b08fSopenharmony_ci */
5540e98b08fSopenharmony_ci#define UTILS_DL_LIST_FOR_EACH_SAFE(item, next, list)      \
5550e98b08fSopenharmony_ci    for (item = (list)->pstNext, next = (item)->pstNext; \
5560e98b08fSopenharmony_ci         (item) != (list);                               \
5570e98b08fSopenharmony_ci         item = next, next = (item)->pstNext)
5580e98b08fSopenharmony_ci
5590e98b08fSopenharmony_ci/*
5600e98b08fSopenharmony_ci * @ingroup utils_list
5610e98b08fSopenharmony_ci * @brief Initialize a double linked list.
5620e98b08fSopenharmony_ci *
5630e98b08fSopenharmony_ci * @par Description:
5640e98b08fSopenharmony_ci * This API is used to initialize a double linked list.
5650e98b08fSopenharmony_ci * @attention
5660e98b08fSopenharmony_ci * <ul>
5670e98b08fSopenharmony_ci * <li>None.</li>
5680e98b08fSopenharmony_ci * </ul>
5690e98b08fSopenharmony_ci *
5700e98b08fSopenharmony_ci * @param list           [IN] Pointer to the doubly linked list to be traversed.
5710e98b08fSopenharmony_ci *
5720e98b08fSopenharmony_ci * @retval None.
5730e98b08fSopenharmony_ci * @par Dependency:
5740e98b08fSopenharmony_ci * <ul><li>utils_list.h: the header file that contains the API declaration.</li></ul>
5750e98b08fSopenharmony_ci * @see
5760e98b08fSopenharmony_ci */
5770e98b08fSopenharmony_ci#define UTILS_DL_LIST_HEAD(list) UTILS_DL_LIST list = { &(list), &(list) }
5780e98b08fSopenharmony_ci
5790e98b08fSopenharmony_ci#define UTILS_ListPeekHeadType(list, type, element)                    \
5800e98b08fSopenharmony_ci    do {                                                               \
5810e98b08fSopenharmony_ci        type *__t;                                                     \
5820e98b08fSopenharmony_ci        if ((list)->pstNext == list) {                                 \
5830e98b08fSopenharmony_ci            __t = NULL;                                                \
5840e98b08fSopenharmony_ci        } else {                                                       \
5850e98b08fSopenharmony_ci            __t = UTILS_DL_LIST_ENTRY((list)->pstNext, type, element); \
5860e98b08fSopenharmony_ci        }                                                              \
5870e98b08fSopenharmony_ci        __t;                                                           \
5880e98b08fSopenharmony_ci    } while (0)
5890e98b08fSopenharmony_ci
5900e98b08fSopenharmony_ci#define UTILS_ListRemoveHeadType(list, type, element)                  \
5910e98b08fSopenharmony_ci    do {                                                               \
5920e98b08fSopenharmony_ci        type *__t;                                                     \
5930e98b08fSopenharmony_ci        if ((list)->pstNext == list) {                                 \
5940e98b08fSopenharmony_ci            __t = NULL;                                                \
5950e98b08fSopenharmony_ci        } else {                                                       \
5960e98b08fSopenharmony_ci            __t = UTILS_DL_LIST_ENTRY((list)->pstNext, type, element); \
5970e98b08fSopenharmony_ci            UtilsListDelete((list)->pstNext);                         \
5980e98b08fSopenharmony_ci        }                                                              \
5990e98b08fSopenharmony_ci        __t;                                                           \
6000e98b08fSopenharmony_ci    } while (0)
6010e98b08fSopenharmony_ci
6020e98b08fSopenharmony_ci#define UTILS_ListNextType(list, item, type, element)                  \
6030e98b08fSopenharmony_ci    do {                                                               \
6040e98b08fSopenharmony_ci        type *__t;                                                     \
6050e98b08fSopenharmony_ci        if ((item)->pstNext == list) {                                 \
6060e98b08fSopenharmony_ci            __t = NULL;                                                \
6070e98b08fSopenharmony_ci        } else {                                                       \
6080e98b08fSopenharmony_ci            __t = UTILS_DL_LIST_ENTRY((item)->pstNext, type, element); \
6090e98b08fSopenharmony_ci        }                                                              \
6100e98b08fSopenharmony_ci        __t;                                                           \
6110e98b08fSopenharmony_ci    } while (0)
6120e98b08fSopenharmony_ci
6130e98b08fSopenharmony_ci#ifdef __cplusplus
6140e98b08fSopenharmony_ci#if __cplusplus
6150e98b08fSopenharmony_ci}
6160e98b08fSopenharmony_ci#endif /* __cplusplus */
6170e98b08fSopenharmony_ci#endif /* __cplusplus */
6180e98b08fSopenharmony_ci
6190e98b08fSopenharmony_ci#endif /* _UTILS_LIST_H */