forked from xdebug/xdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdebug_llist.c
189 lines (158 loc) · 4.06 KB
/
xdebug_llist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2018 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.01 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| https://xdebug.org/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| derick@xdebug.org so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Sterling Hughes <sterling@php.net> |
+----------------------------------------------------------------------+
*/
#include <stdlib.h>
#include <string.h>
#include "xdebug_llist.h"
xdebug_llist *xdebug_llist_alloc(xdebug_llist_dtor dtor)
{
xdebug_llist *l;
l = malloc(sizeof(xdebug_llist));
xdebug_llist_init(l, dtor);
return l;
}
void xdebug_llist_init(xdebug_llist *l, xdebug_llist_dtor dtor)
{
l->size = 0;
l->dtor = dtor;
l->head = NULL;
l->tail = NULL;
}
int xdebug_llist_insert_next(xdebug_llist *l, xdebug_llist_element *e, const void *p)
{
xdebug_llist_element *ne;
if (!e) {
e = XDEBUG_LLIST_TAIL(l);
}
ne = (xdebug_llist_element *) malloc(sizeof(xdebug_llist_element));
ne->ptr = (void *) p;
if (l->size == 0) {
l->head = ne;
l->head->prev = NULL;
l->head->next = NULL;
l->tail = ne;
} else {
ne->next = e->next;
ne->prev = e;
if (e->next) {
e->next->prev = ne;
} else {
l->tail = ne;
}
e->next = ne;
}
++l->size;
return 1;
}
int xdebug_llist_insert_prev(xdebug_llist *l, xdebug_llist_element *e, const void *p)
{
xdebug_llist_element *ne;
if (!e) {
e = XDEBUG_LLIST_HEAD(l);
}
ne = (xdebug_llist_element *) malloc(sizeof(xdebug_llist_element));
ne->ptr = (void *) p;
if (l->size == 0) {
l->head = ne;
l->head->prev = NULL;
l->head->next = NULL;
l->tail = ne;
} else {
ne->next = e;
ne->prev = e->prev;
if (e->prev)
e->prev->next = ne;
else
l->head = ne;
e->prev = ne;
}
++l->size;
return 0;
}
int xdebug_llist_remove(xdebug_llist *l, xdebug_llist_element *e, void *user)
{
if (e == NULL || l->size == 0)
return 0;
if (e == l->head) {
l->head = e->next;
if (l->head == NULL)
l->tail = NULL;
else
e->next->prev = NULL;
} else {
e->prev->next = e->next;
if (!e->next)
l->tail = e->prev;
else
e->next->prev = e->prev;
}
if (l->dtor) {
l->dtor(user, e->ptr);
}
free(e);
--l->size;
return 0;
}
int xdebug_llist_remove_next(xdebug_llist *l, xdebug_llist_element *e, void *user)
{
return xdebug_llist_remove(l, e->next, user);
}
int xdebug_llist_remove_prev(xdebug_llist *l, xdebug_llist_element *e, void *user)
{
return xdebug_llist_remove(l, e->prev, user);
}
xdebug_llist_element *xdebug_llist_jump(xdebug_llist *l, int where, int pos)
{
xdebug_llist_element *e=NULL;
int i;
if (where == LIST_HEAD) {
e = XDEBUG_LLIST_HEAD(l);
for (i = 0; i < pos; ++i) {
e = XDEBUG_LLIST_NEXT(e);
}
}
else if (where == LIST_TAIL) {
e = XDEBUG_LLIST_TAIL(l);
for (i = 0; i < pos; ++i) {
e = XDEBUG_LLIST_PREV(e);
}
}
return e;
}
size_t xdebug_llist_count(xdebug_llist *l)
{
return l->size;
}
void xdebug_llist_empty(xdebug_llist *l, void *user)
{
while (xdebug_llist_count(l) > 0 && XDEBUG_LLIST_TAIL(l)) {
xdebug_llist_remove(l, XDEBUG_LLIST_TAIL(l), user);
}
}
void xdebug_llist_destroy(xdebug_llist *l, void *user)
{
xdebug_llist_empty(l, user);
free (l);
}
/*
* Local Variables:
* c-basic-offset: 4
* tab-width: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/