1 /*
2 * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
3 *
4 * UniProton is licensed under Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 * http://license.coscl.org.cn/MulanPSL2
8 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11 * See the Mulan PSL v2 for more details.
12 * Create: 2009-12-22
13 * Description: 双向链表操作的内部头文件
14 */
15 #ifndef PRT_LIST_EXTERNAL_H
16 #define PRT_LIST_EXTERNAL_H
17
18 #include "prt_typedef.h"
19
20 struct TagListObject {
21 struct TagListObject *prev;
22 struct TagListObject *next;
23 };
24
25 #define LIST_OBJECT_INIT(object) { \
26 &(object), &(object) \
27 }
28
29 #define INIT_LIST_OBJECT(object) \
30 do { \
31 (object)->next = (object); \
32 (object)->prev = (object); \
33 } while (0)
34
35 #define LIST_FIRST(object) ((object)->next)
36 #define OS_LIST_FIRST(object) ((object)->next)
37
38 /* list action low level add */
ListLowLevelAdd(struct TagListObject * newNode,struct TagListObject * prev,struct TagListObject * next)39 OS_SEC_ALW_INLINE INLINE void ListLowLevelAdd(struct TagListObject *newNode, struct TagListObject *prev,
40 struct TagListObject *next)
41 {
42 newNode->next = next;
43 newNode->prev = prev;
44 next->prev = newNode;
45 prev->next = newNode;
46 }
47
48 /* list action add */
ListAdd(struct TagListObject * newNode,struct TagListObject * listObject)49 OS_SEC_ALW_INLINE INLINE void ListAdd(struct TagListObject *newNode, struct TagListObject *listObject)
50 {
51 ListLowLevelAdd(newNode, listObject, listObject->next);
52 }
53
54 /* list action tail add */
ListTailAdd(struct TagListObject * newNode,struct TagListObject * listObject)55 OS_SEC_ALW_INLINE INLINE void ListTailAdd(struct TagListObject *newNode, struct TagListObject *listObject)
56 {
57 ListLowLevelAdd(newNode, listObject->prev, listObject);
58 }
59
60 /* list action lowel delete */
ListLowLevelDelete(struct TagListObject * prevNode,struct TagListObject * nextNode)61 OS_SEC_ALW_INLINE INLINE void ListLowLevelDelete(struct TagListObject *prevNode, struct TagListObject *nextNode)
62 {
63 nextNode->prev = prevNode;
64 prevNode->next = nextNode;
65 }
66
67 /* list action delete */
ListDelete(struct TagListObject * node)68 OS_SEC_ALW_INLINE INLINE void ListDelete(struct TagListObject *node)
69 {
70 ListLowLevelDelete(node->prev, node->next);
71
72 node->next = NULL;
73 node->prev = NULL;
74 }
75
76 /* list action empty */
ListEmpty(const struct TagListObject * listObject)77 OS_SEC_ALW_INLINE INLINE bool ListEmpty(const struct TagListObject *listObject)
78 {
79 return (bool)((listObject->next == listObject) && (listObject->prev == listObject));
80 }
81
82 #define OFFSET_OF_FIELD(type, field) ((uintptr_t)((uintptr_t)(&((type *)0x10)->field) - (uintptr_t)0x10))
83
84 #define COMPLEX_OF(ptr, type, field) ((type *)((uintptr_t)(ptr) - OFFSET_OF_FIELD(type, field)))
85
86 /* 根据成员地址得到控制块首地址, ptr成员地址, type控制块结构, field成员名 */
87 #define LIST_COMPONENT(ptrOfList, typeOfList, fieldOfList) COMPLEX_OF(ptrOfList, typeOfList, fieldOfList)
88
89 #define LIST_FOR_EACH(posOfList, listObject, typeOfList, field) \
90 for ((posOfList) = LIST_COMPONENT((listObject)->next, typeOfList, field); &(posOfList)->field != (listObject); \
91 (posOfList) = LIST_COMPONENT((posOfList)->field.next, typeOfList, field))
92
93 #endif /* PRT_LIST_EXTERNAL_H */
94