| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2014 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 its 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 |
/* Assumption: If m->next == NULL, then list->tail == m |
| 136 |
* and: If m->prev == NULL, then list->head == m |
| 137 |
*/ |
| 138 |
if (m->next) |
| 139 |
m->next->prev = m->prev; |
| 140 |
else |
| 141 |
{ |
| 142 |
assert(list->tail == m); |
| 143 |
list->tail = m->prev; |
| 144 |
} |
| 145 |
|
| 146 |
if (m->prev) |
| 147 |
m->prev->next = m->next; |
| 148 |
else |
| 149 |
{ |
| 150 |
assert(list->head == m); |
| 151 |
list->head = m->next; |
| 152 |
} |
| 153 |
|
| 154 |
/* Set this to NULL does matter */ |
| 155 |
m->next = m->prev = NULL; |
| 156 |
list->length--; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* dlinkFind |
| 161 |
* inputs - list to search |
| 162 |
* - data |
| 163 |
* output - pointer to link or NULL if not found |
| 164 |
* side effects - Look for ptr in the linked listed pointed to by link. |
| 165 |
*/ |
| 166 |
dlink_node * |
| 167 |
dlinkFind(dlink_list *list, const void *data) |
| 168 |
{ |
| 169 |
dlink_node *node = NULL; |
| 170 |
|
| 171 |
DLINK_FOREACH(node, list->head) |
| 172 |
if (node->data == data) |
| 173 |
return node; |
| 174 |
|
| 175 |
return NULL; |
| 176 |
} |
| 177 |
|
| 178 |
void |
| 179 |
dlinkMoveList(dlink_list *from, dlink_list *to) |
| 180 |
{ |
| 181 |
/* There are three cases */ |
| 182 |
/* case one, nothing in from list */ |
| 183 |
if (from->head == NULL) |
| 184 |
return; |
| 185 |
|
| 186 |
/* case two, nothing in to list */ |
| 187 |
/* actually if to->head is NULL and to->tail isn't, thats a bug */ |
| 188 |
if (to->head == NULL) |
| 189 |
{ |
| 190 |
to->head = from->head; |
| 191 |
to->tail = from->tail; |
| 192 |
from->head = from->tail = NULL; |
| 193 |
to->length = from->length; |
| 194 |
from->length = 0; |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
/* third case play with the links */ |
| 199 |
from->tail->next = to->head; |
| 200 |
from->head->prev = to->head->prev; |
| 201 |
to->head->prev = from->tail; |
| 202 |
to->head = from->head; |
| 203 |
from->head = from->tail = NULL; |
| 204 |
to->length += from->length; |
| 205 |
from->length = 0; |
| 206 |
/* I think I got that right */ |
| 207 |
} |
| 208 |
|
| 209 |
void |
| 210 |
dlink_move_node(dlink_node *m, dlink_list *list_del, dlink_list *list_add) |
| 211 |
{ |
| 212 |
/* Assumption: If m->next == NULL, then list_del->tail == m |
| 213 |
* and: If m->prev == NULL, then list_del->head == m |
| 214 |
*/ |
| 215 |
if (m->next) |
| 216 |
m->next->prev = m->prev; |
| 217 |
else |
| 218 |
{ |
| 219 |
assert(list_del->tail == m); |
| 220 |
list_del->tail = m->prev; |
| 221 |
} |
| 222 |
|
| 223 |
if (m->prev) |
| 224 |
m->prev->next = m->next; |
| 225 |
else |
| 226 |
{ |
| 227 |
assert(list_del->head == m); |
| 228 |
list_del->head = m->next; |
| 229 |
} |
| 230 |
|
| 231 |
/* Set this to NULL does matter */ |
| 232 |
m->prev = NULL; |
| 233 |
m->next = list_add->head; |
| 234 |
|
| 235 |
/* Assumption: If list_add->tail != NULL, list_add->head != NULL */ |
| 236 |
if (list_add->head != NULL) |
| 237 |
list_add->head->prev = m; |
| 238 |
else if (list_add->tail == NULL) |
| 239 |
list_add->tail = m; |
| 240 |
|
| 241 |
list_add->head = m; |
| 242 |
list_add->length++; |
| 243 |
list_del->length--; |
| 244 |
} |
| 245 |
|
| 246 |
dlink_node * |
| 247 |
dlinkFindDelete(dlink_list *list, void *data) |
| 248 |
{ |
| 249 |
dlink_node *m = NULL; |
| 250 |
|
| 251 |
DLINK_FOREACH(m, list->head) |
| 252 |
{ |
| 253 |
if (m->data != data) |
| 254 |
continue; |
| 255 |
|
| 256 |
if (m->next) |
| 257 |
m->next->prev = m->prev; |
| 258 |
else |
| 259 |
{ |
| 260 |
assert(list->tail == m); |
| 261 |
list->tail = m->prev; |
| 262 |
} |
| 263 |
|
| 264 |
if (m->prev) |
| 265 |
m->prev->next = m->next; |
| 266 |
else |
| 267 |
{ |
| 268 |
assert(list->head == m); |
| 269 |
list->head = m->next; |
| 270 |
} |
| 271 |
|
| 272 |
/* Set this to NULL does matter */ |
| 273 |
m->next = m->prev = NULL; |
| 274 |
list->length--; |
| 275 |
|
| 276 |
return m; |
| 277 |
} |
| 278 |
|
| 279 |
return NULL; |
| 280 |
} |