/
t_list.h
116 lines (98 loc) · 6.79 KB
/
t_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/****************************************************************************
* Copyright 2017 Gorgon Meducer (Email:embedded_zhuoran@hotmail.com) *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
****************************************************************************/
#ifndef _USE_TEMPLATE_LIST_H_
#define _USE_TEMPLATE_LIST_H_
/*============================ INCLUDES ======================================*/
/*============================ MACROS ========================================*/
#define END_DEF_LIST(__NAME)
#define END_DEF_SAFE_LIST(__NAME)
/*============================ MACROFIED FUNCTIONS ===========================*/
#define __LIST_STACK_PUSH(__P_TOP, __P_NODE) \
do { \
((__single_list_node_t *)(__P_NODE))->ptNext = \
(__single_list_node_t *)(__P_TOP); \
(*(__single_list_node_t **)&(__P_TOP)) = \
(__single_list_node_t *)(__P_NODE); \
} while(0)
#define LIST_STACK_PUSH(__P_TOP, __P_NODE) \
__LIST_STACK_PUSH((__P_TOP), (__P_NODE))
#define LIST_INSERT_AFTER(__P_TARGET, __P_NODE) \
__LIST_STACK_PUSH((__P_TARGET), (__P_NODE))
#define __LIST_STACK_POP(__P_TOP, __P_NODE) \
do { \
(*(__single_list_node_t **)&(__P_NODE)) = \
(__single_list_node_t *)(__P_TOP); \
(*(__single_list_node_t **)&(__P_TOP)) = \
((__single_list_node_t *)(__P_NODE))->ptNext; \
((__single_list_node_t *)(__P_NODE))->ptNext = NULL; \
} while(0)
#define LIST_STACK_POP(__P_TOP, __P_NODE) __LIST_STACK_POP((__P_TOP), (__P_NODE))
#define LIST_REMOVE_AFTER(__P_TARGET, __P_NODE) \
LIST_STACK_POP((__P_TARGET), (__P_NODE))
#define __LIST_QUEUE_ENQUEUE(__HEAD, __TAIL, __ITEM) \
do { \
if (NULL == (__TAIL)) { \
(*((__single_list_node_t **)&(__TAIL))) = \
(__single_list_node_t *)(__ITEM); \
((__single_list_node_t *)(__ITEM))->ptNext = NULL; \
(*((__single_list_node_t **)&(__HEAD))) = \
(__single_list_node_t *)(__ITEM); \
} else { \
((__single_list_node_t *)(__TAIL))->ptNext = \
(__single_list_node_t *)(__ITEM); \
((__single_list_node_t *)(__ITEM))->ptNext = NULL; \
(*(__single_list_node_t **)&(__TAIL)) = \
(__single_list_node_t *)(__ITEM); \
} \
} while(0)
#define LIST_QUEUE_ENQUEUE(__HEAD, __TAIL, __ITEM) \
__LIST_QUEUE_ENQUEUE((__HEAD), (__TAIL), (__ITEM))
#define __LIST_QUEUE_DEQUEUE(__HEAD, __TAIL, __ITEM) \
do { \
(*(__single_list_node_t **)&(__ITEM)) = \
(__single_list_node_t *)(__HEAD); \
if (NULL != (__HEAD)) { \
(*(__single_list_node_t **)&(__HEAD)) = \
((__single_list_node_t *)(__HEAD))->ptNext; \
if (NULL == (__HEAD)) { \
(__TAIL) = NULL; \
} \
} \
} while(0)
#define LIST_QUEUE_DEQUEUE(__HEAD, __TAIL, __ITEM) \
__LIST_QUEUE_DEQUEUE((__HEAD), (__TAIL), (__ITEM))
#define __LIST_QUEUE_REMOVE(__HEAD, __TAIL, __ITEM) \
do { \
__single_list_node_t **ppList = (__single_list_node_t **)&(__HEAD); \
if (list_find(ppList, (__single_list_node_t *)(__ITEM))) { \
LIST_QUEUE_DEQUEUE((*ppList), (__TAIL), (__ITEM)); \
} \
} while(0)
#define LIST_QUEUE_REMOVE(__HEAD, __TAIL, __ITEM) \
__LIST_QUEUE_REMOVE((__HEAD), (__TAIL), (__ITEM))
/*============================ TYPES =========================================*/
def_structure( __single_list_node_t )
__single_list_node_t *ptNext;
end_def_structure( __single_list_note_t )
def_structure( __dual_list_node_t )
__dual_list_node_t *ptNext;
__dual_list_node_t *ptPrevious;
end_def_structure( __dual_list_note_t )
/*============================ GLOBAL VARIABLES ==============================*/
/*============================ PROTOTYPES ====================================*/
extern bool list_find(__single_list_node_t **ppList, __single_list_node_t *ptItem);
#endif