1 /******************************************************************************
2 *
3 * Copyright (C) 2009-2018 Realtek Corporation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 /******************************************************************************
19 *
20 * Module Name:
21 * bt_list.c
22 *
23 * Abstract:
24 * To implement list data structure
25 *
26 * Major Change History:
27 * When Who What
28 * --------------------------------------------------------------
29 * 2010-06-04 W.Bi Created
30 *
31 * Notes:
32 *
33 ******************************************************************************/
34 #include "bt_list.h"
35
36 //****************************************************************************
37 // Structure
38 //****************************************************************************
39
40 //****************************************************************************
41 // FUNCTION
42 //****************************************************************************
43 // Initialize a list with its header
ListInitializeHeader(PRT_LIST_HEAD ListHead)44 void ListInitializeHeader(PRT_LIST_HEAD ListHead)
45 {
46 ListHead->Next = ListHead;
47 ListHead->Prev = ListHead;
48 }
49
50 /**
51 Tell whether the list is empty
52 \param [IN] ListHead <RT_LIST_ENTRY> : List header of which to be test
53 */
ListIsEmpty(PRT_LIST_HEAD ListHead)54 unsigned char ListIsEmpty(PRT_LIST_HEAD ListHead)
55 {
56 return ListHead->Next == ListHead;
57 }
58
59 /*
60 Insert a new entry between two known consecutive entries.
61 This is only for internal list manipulation where we know the prev&next entries already
62 @New : New element to be added
63 @Prev: previous element in the list
64 @Next: Next element in the list
65 */
ListAdd(PRT_LIST_ENTRY New,PRT_LIST_ENTRY Prev,PRT_LIST_ENTRY Next)66 void ListAdd(PRT_LIST_ENTRY New,PRT_LIST_ENTRY Prev,PRT_LIST_ENTRY Next)
67 {
68 Next->Prev = New;
69 New->Next = Next;
70 New->Prev = Prev;
71 Prev->Next = New;
72 }
73 /**
74 Add a new entry to the list.
75 Insert a new entry after the specified head. This is good for implementing stacks.
76 \param [IN] ListNew <RT_LIST_ENTRY> : new entry to be added
77 \param [IN OUT] ListHead <RT_LIST_ENTRY> : List header after which to add new entry
78 */
ListAddToHead(PRT_LIST_ENTRY ListNew,PRT_LIST_HEAD ListHead)79 void ListAddToHead(PRT_LIST_ENTRY ListNew,PRT_LIST_HEAD ListHead)
80 {
81 ListAdd(ListNew, ListHead, ListHead->Next);
82 }
83
84 /**
85 Add a new entry to the list.
86 Insert a new entry before the specified head. This is good for implementing queues.
87 \param [IN] ListNew <RT_LIST_ENTRY> : new entry to be added
88 \param [IN OUT] ListHead <RT_LIST_ENTRY> : List header before which to add new entry
89 */
ListAddToTail(PRT_LIST_ENTRY ListNew,PRT_LIST_HEAD ListHead)90 void ListAddToTail(PRT_LIST_ENTRY ListNew,PRT_LIST_HEAD ListHead)
91 {
92 ListAdd(ListNew, ListHead->Prev, ListHead);
93 }
94
ListGetTop(PRT_LIST_HEAD ListHead)95 RT_LIST_ENTRY *ListGetTop(PRT_LIST_HEAD ListHead)
96 {
97
98 if (ListIsEmpty(ListHead))
99 return 0;
100
101 return ListHead->Next;
102 }
103
ListGetTail(PRT_LIST_HEAD ListHead)104 RT_LIST_ENTRY *ListGetTail(PRT_LIST_HEAD ListHead)
105 {
106 if (ListIsEmpty(ListHead))
107 return 0;
108
109 return ListHead->Prev;
110 }
111 /**
112 Delete entry from the list
113 Note: ListIsEmpty() on this list entry would not return true, since its state is undefined
114 \param [IN] ListToDelete <RT_LIST_ENTRY> : list entry to be deleted
115 */
ListDeleteNode(PRT_LIST_ENTRY ListToDelete)116 void ListDeleteNode(PRT_LIST_ENTRY ListToDelete)
117 {
118 // if (ListToDelete->Next != NULL && ListToDelete->Prev != NULL)
119 {
120 ListToDelete->Next->Prev = ListToDelete->Prev;
121 ListToDelete->Prev->Next = ListToDelete->Next;
122 ListToDelete->Next = ListToDelete->Prev = ListToDelete;
123 }
124 }
125