1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2000-2018 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file list.c |
23 |
* \brief Maintains doubly-linked lists. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "mempool.h" |
30 |
|
31 |
|
32 |
static mp_pool_t *dnode_pool; |
33 |
|
34 |
|
35 |
/* init_dlink_nodes() |
36 |
* |
37 |
* inputs - NONE |
38 |
* output - NONE |
39 |
* side effects - initializes the dnode BlockHeap |
40 |
*/ |
41 |
void |
42 |
init_dlink_nodes(void) |
43 |
{ |
44 |
dnode_pool = mp_pool_new(sizeof(dlink_node), MP_CHUNK_SIZE_DNODE); |
45 |
} |
46 |
|
47 |
/* make_dlink_node() |
48 |
* |
49 |
* inputs - NONE |
50 |
* output - pointer to new dlink_node |
51 |
* side effects - NONE |
52 |
*/ |
53 |
dlink_node * |
54 |
make_dlink_node(void) |
55 |
{ |
56 |
dlink_node *node = mp_pool_get(dnode_pool); |
57 |
|
58 |
return node; |
59 |
} |
60 |
|
61 |
/* free_dlink_node() |
62 |
* |
63 |
* inputs - pointer to dlink_node |
64 |
* output - NONE |
65 |
* side effects - free given dlink_node |
66 |
*/ |
67 |
void |
68 |
free_dlink_node(dlink_node *node) |
69 |
{ |
70 |
mp_pool_release(node); |
71 |
} |
72 |
|
73 |
/* |
74 |
* dlink_ routines are stolen from squid, except for dlinkAddBefore, |
75 |
* which is mine. |
76 |
* -- adrian |
77 |
*/ |
78 |
void |
79 |
dlinkAdd(void *data, dlink_node *m, dlink_list *list) |
80 |
{ |
81 |
m->data = data; |
82 |
m->prev = NULL; |
83 |
m->next = list->head; |
84 |
|
85 |
/* Assumption: If list->tail != NULL, list->head != NULL */ |
86 |
if (list->head) |
87 |
list->head->prev = m; |
88 |
else if (list->tail == NULL) |
89 |
list->tail = m; |
90 |
|
91 |
list->head = m; |
92 |
list->length++; |
93 |
} |
94 |
|
95 |
void |
96 |
dlinkAddBefore(dlink_node *b, void *data, dlink_node *m, dlink_list *list) |
97 |
{ |
98 |
/* Shortcut - if it's the first one, call dlinkAdd only */ |
99 |
if (b == list->head) |
100 |
dlinkAdd(data, m, list); |
101 |
else |
102 |
{ |
103 |
m->data = data; |
104 |
b->prev->next = m; |
105 |
m->prev = b->prev; |
106 |
b->prev = m; |
107 |
m->next = b; |
108 |
list->length++; |
109 |
} |
110 |
} |
111 |
|
112 |
void |
113 |
dlinkAddTail(void *data, dlink_node *m, dlink_list *list) |
114 |
{ |
115 |
m->data = data; |
116 |
m->next = NULL; |
117 |
m->prev = list->tail; |
118 |
|
119 |
/* Assumption: If list->tail != NULL, list->head != NULL */ |
120 |
if (list->tail) |
121 |
list->tail->next = m; |
122 |
else if (list->head == NULL) |
123 |
list->head = m; |
124 |
|
125 |
list->tail = m; |
126 |
list->length++; |
127 |
} |
128 |
|
129 |
/* Execution profiles show that this function is called the most |
130 |
* often of all non-spontaneous functions. So it had better be |
131 |
* efficient. */ |
132 |
void |
133 |
dlinkDelete(dlink_node *m, dlink_list *list) |
134 |
{ |
135 |
assert(list->length > 0); |
136 |
|
137 |
/* Assumption: If m->next == NULL, then list->tail == m |
138 |
* and: If m->prev == NULL, then list->head == m |
139 |
*/ |
140 |
if (m->next) |
141 |
m->next->prev = m->prev; |
142 |
else |
143 |
{ |
144 |
assert(list->tail == m); |
145 |
list->tail = m->prev; |
146 |
} |
147 |
|
148 |
if (m->prev) |
149 |
m->prev->next = m->next; |
150 |
else |
151 |
{ |
152 |
assert(list->head == m); |
153 |
list->head = m->next; |
154 |
} |
155 |
|
156 |
/* Set this to NULL does matter */ |
157 |
m->data = NULL; |
158 |
m->next = NULL; |
159 |
m->prev = NULL; |
160 |
|
161 |
list->length--; |
162 |
} |
163 |
|
164 |
/* |
165 |
* dlinkFind |
166 |
* inputs - list to search |
167 |
* - data |
168 |
* output - pointer to link or NULL if not found |
169 |
* side effects - Look for ptr in the linked listed pointed to by link. |
170 |
*/ |
171 |
dlink_node * |
172 |
dlinkFind(dlink_list *list, const void *data) |
173 |
{ |
174 |
dlink_node *node; |
175 |
|
176 |
DLINK_FOREACH(node, list->head) |
177 |
if (node->data == data) |
178 |
return node; |
179 |
|
180 |
return NULL; |
181 |
} |
182 |
|
183 |
void |
184 |
dlinkMoveList(dlink_list *from, dlink_list *to) |
185 |
{ |
186 |
/* There are three cases */ |
187 |
/* case one, nothing in from list */ |
188 |
if (from->head == NULL) |
189 |
return; |
190 |
|
191 |
/* case two, nothing in to list */ |
192 |
/* actually if to->head is NULL and to->tail isn't, that's a bug */ |
193 |
if (to->head == NULL) |
194 |
{ |
195 |
to->head = from->head; |
196 |
to->tail = from->tail; |
197 |
from->head = from->tail = NULL; |
198 |
to->length = from->length; |
199 |
from->length = 0; |
200 |
return; |
201 |
} |
202 |
|
203 |
/* third case play with the links */ |
204 |
from->tail->next = to->head; |
205 |
from->head->prev = to->head->prev; |
206 |
to->head->prev = from->tail; |
207 |
to->head = from->head; |
208 |
from->head = from->tail = NULL; |
209 |
to->length += from->length; |
210 |
from->length = 0; |
211 |
/* I think I got that right */ |
212 |
} |
213 |
|
214 |
void |
215 |
dlink_move_node(dlink_node *m, dlink_list *list_del, dlink_list *list_add) |
216 |
{ |
217 |
/* Assumption: If m->next == NULL, then list_del->tail == m |
218 |
* and: If m->prev == NULL, then list_del->head == m |
219 |
*/ |
220 |
if (m->next) |
221 |
m->next->prev = m->prev; |
222 |
else |
223 |
{ |
224 |
assert(list_del->tail == m); |
225 |
list_del->tail = m->prev; |
226 |
} |
227 |
|
228 |
if (m->prev) |
229 |
m->prev->next = m->next; |
230 |
else |
231 |
{ |
232 |
assert(list_del->head == m); |
233 |
list_del->head = m->next; |
234 |
} |
235 |
|
236 |
/* Set this to NULL does matter */ |
237 |
m->prev = NULL; |
238 |
m->next = list_add->head; |
239 |
|
240 |
/* Assumption: If list_add->tail != NULL, list_add->head != NULL */ |
241 |
if (list_add->head) |
242 |
list_add->head->prev = m; |
243 |
else if (list_add->tail == NULL) |
244 |
list_add->tail = m; |
245 |
|
246 |
list_add->head = m; |
247 |
list_add->length++; |
248 |
list_del->length--; |
249 |
} |
250 |
|
251 |
dlink_node * |
252 |
dlinkFindDelete(dlink_list *list, void *data) |
253 |
{ |
254 |
dlink_node *m; |
255 |
|
256 |
DLINK_FOREACH(m, list->head) |
257 |
{ |
258 |
if (m->data == data) |
259 |
{ |
260 |
dlinkDelete(m, list); |
261 |
return m; |
262 |
} |
263 |
} |
264 |
|
265 |
return NULL; |
266 |
} |