1 /*
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 // XXX: This file is made of large pieces of src/tools.cc
10 // with only a few minor modifications. TODO: redesign or delete.
11
12 #include "squid.h"
13 #include "dlink.h"
14 #include <iostream>
15
16 void
xassert(const char * msg,const char * file,int line)17 xassert(const char *msg, const char *file, int line)
18 {
19 std::cout << "Assertion failed: (" << msg << ") at " << file << ":" << line << std::endl;
20 exit (1);
21 }
22
23 void
dlinkAdd(void * data,dlink_node * m,dlink_list * list)24 dlinkAdd(void *data, dlink_node * m, dlink_list * list)
25 {
26 m->data = data;
27 m->prev = NULL;
28 m->next = list->head;
29
30 if (list->head)
31 list->head->prev = m;
32
33 list->head = m;
34
35 if (list->tail == NULL)
36 list->tail = m;
37 }
38
39 void
dlinkAddAfter(void * data,dlink_node * m,dlink_node * n,dlink_list * list)40 dlinkAddAfter(void *data, dlink_node * m, dlink_node * n, dlink_list * list)
41 {
42 m->data = data;
43 m->prev = n;
44 m->next = n->next;
45
46 if (n->next)
47 n->next->prev = m;
48 else {
49 assert(list->tail == n);
50 list->tail = m;
51 }
52
53 n->next = m;
54 }
55
56 void
dlinkAddTail(void * data,dlink_node * m,dlink_list * list)57 dlinkAddTail(void *data, dlink_node * m, dlink_list * list)
58 {
59 m->data = data;
60 m->next = NULL;
61 m->prev = list->tail;
62
63 if (list->tail)
64 list->tail->next = m;
65
66 list->tail = m;
67
68 if (list->head == NULL)
69 list->head = m;
70 }
71
72 void
dlinkDelete(dlink_node * m,dlink_list * list)73 dlinkDelete(dlink_node * m, dlink_list * list)
74 {
75 if (m->next)
76 m->next->prev = m->prev;
77
78 if (m->prev)
79 m->prev->next = m->next;
80
81 if (m == list->head)
82 list->head = m->next;
83
84 if (m == list->tail)
85 list->tail = m->prev;
86
87 m->next = m->prev = NULL;
88 }
89
90