1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* tools.c: Various functions needed here and there. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
* |
24 |
* When you update these functions make sure you update the ones in tools.h |
25 |
* as well!!! |
26 |
*/ |
27 |
|
28 |
#include "stdinc.h" |
29 |
#include "tools.h" |
30 |
|
31 |
|
32 |
#ifndef NDEBUG |
33 |
/* |
34 |
* frob some memory. debugging time. |
35 |
* -- adrian |
36 |
*/ |
37 |
void |
38 |
mem_frob(void *data, int len) |
39 |
{ |
40 |
/* correct for Intel only! little endian */ |
41 |
unsigned char b[4] = { 0xef, 0xbe, 0xad, 0xde }; |
42 |
int i; |
43 |
char *cdata = data; |
44 |
for (i = 0; i < len; i++) |
45 |
{ |
46 |
*cdata = b[i % 4]; |
47 |
cdata++; |
48 |
} |
49 |
} |
50 |
#endif |
51 |
|
52 |
/* |
53 |
* dlink_ routines are stolen from squid, except for dlinkAddBefore, |
54 |
* which is mine. |
55 |
* -- adrian |
56 |
*/ |
57 |
void |
58 |
dlinkAdd(void *data, dlink_node * m, dlink_list * list) |
59 |
{ |
60 |
m->data = data; |
61 |
m->prev = NULL; |
62 |
m->next = list->head; |
63 |
|
64 |
/* Assumption: If list->tail != NULL, list->head != NULL */ |
65 |
if (list->head != NULL) |
66 |
list->head->prev = m; |
67 |
else /* if (list->tail == NULL) */ |
68 |
list->tail = m; |
69 |
|
70 |
list->head = m; |
71 |
list->length++; |
72 |
} |
73 |
|
74 |
void |
75 |
dlinkAddBefore(dlink_node *b, void *data, dlink_node *m, dlink_list *list) |
76 |
{ |
77 |
/* Shortcut - if its the first one, call dlinkAdd only */ |
78 |
if (b == list->head) |
79 |
{ |
80 |
dlinkAdd(data, m, list); |
81 |
} |
82 |
else |
83 |
{ |
84 |
m->data = data; |
85 |
b->prev->next = m; |
86 |
m->prev = b->prev; |
87 |
b->prev = m; |
88 |
m->next = b; |
89 |
list->length++; |
90 |
} |
91 |
} |
92 |
|
93 |
void |
94 |
dlinkAddTail(void *data, dlink_node *m, dlink_list *list) |
95 |
{ |
96 |
m->data = data; |
97 |
m->next = NULL; |
98 |
m->prev = list->tail; |
99 |
/* Assumption: If list->tail != NULL, list->head != NULL */ |
100 |
if (list->tail != NULL) |
101 |
list->tail->next = m; |
102 |
else /* if (list->head == NULL) */ |
103 |
list->head = m; |
104 |
|
105 |
list->tail = m; |
106 |
list->length++; |
107 |
} |
108 |
|
109 |
/* Execution profiles show that this function is called the most |
110 |
* often of all non-spontaneous functions. So it had better be |
111 |
* efficient. */ |
112 |
void |
113 |
dlinkDelete(dlink_node *m, dlink_list *list) |
114 |
{ |
115 |
/* Assumption: If m->next == NULL, then list->tail == m |
116 |
* and: If m->prev == NULL, then list->head == m |
117 |
*/ |
118 |
if (m->next) |
119 |
m->next->prev = m->prev; |
120 |
else { |
121 |
assert(list->tail == m); |
122 |
list->tail = m->prev; |
123 |
} |
124 |
if (m->prev) |
125 |
m->prev->next = m->next; |
126 |
else { |
127 |
assert(list->head == m); |
128 |
list->head = m->next; |
129 |
} |
130 |
|
131 |
/* Set this to NULL does matter */ |
132 |
m->next = m->prev = NULL; |
133 |
list->length--; |
134 |
} |
135 |
|
136 |
|
137 |
/* |
138 |
* dlinkFind |
139 |
* inputs - list to search |
140 |
* - data |
141 |
* output - pointer to link or NULL if not found |
142 |
* side effects - Look for ptr in the linked listed pointed to by link. |
143 |
*/ |
144 |
dlink_node * |
145 |
dlinkFind(dlink_list *list, void *data) |
146 |
{ |
147 |
dlink_node *ptr; |
148 |
|
149 |
DLINK_FOREACH(ptr, list->head) |
150 |
{ |
151 |
if (ptr->data == data) |
152 |
return(ptr); |
153 |
} |
154 |
|
155 |
return(NULL); |
156 |
} |
157 |
|
158 |
void |
159 |
dlinkMoveList(dlink_list *from, dlink_list *to) |
160 |
{ |
161 |
/* There are three cases */ |
162 |
/* case one, nothing in from list */ |
163 |
if(from->head == NULL) |
164 |
return; |
165 |
|
166 |
/* case two, nothing in to list */ |
167 |
/* actually if to->head is NULL and to->tail isn't, thats a bug */ |
168 |
|
169 |
if(to->head == NULL) |
170 |
{ |
171 |
to->head = from->head; |
172 |
to->tail = from->tail; |
173 |
from->head = from->tail = NULL; |
174 |
to->length = from->length; |
175 |
from->length = 0; |
176 |
return; |
177 |
} |
178 |
|
179 |
/* third case play with the links */ |
180 |
|
181 |
from->tail->next = to->head; |
182 |
from->head->prev = to->head->prev; |
183 |
to->head->prev = from->tail; |
184 |
to->head = from->head; |
185 |
from->head = from->tail = NULL; |
186 |
to->length += from->length; |
187 |
from->length = 0; |
188 |
/* I think I got that right */ |
189 |
} |
190 |
|
191 |
dlink_node * |
192 |
dlinkFindDelete(dlink_list *list, void *data) |
193 |
{ |
194 |
dlink_node *m; |
195 |
|
196 |
DLINK_FOREACH(m, list->head) |
197 |
{ |
198 |
if (m->data == data) |
199 |
{ |
200 |
if (m->next) |
201 |
m->next->prev = m->prev; |
202 |
else |
203 |
{ |
204 |
assert(list->tail == m); |
205 |
list->tail = m->prev; |
206 |
} |
207 |
if (m->prev) |
208 |
m->prev->next = m->next; |
209 |
else |
210 |
{ |
211 |
assert(list->head == m); |
212 |
list->head = m->next; |
213 |
} |
214 |
/* Set this to NULL does matter */ |
215 |
m->next = m->prev = NULL; |
216 |
list->length--; |
217 |
|
218 |
return(m); |
219 |
} |
220 |
} |
221 |
|
222 |
return(NULL); |
223 |
} |