xref: /PHP-8.3/Zend/zend_llist.c (revision bf1cfc07)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    +----------------------------------------------------------------------+
18 */
19 
20 #include "zend.h"
21 #include "zend_llist.h"
22 #include "zend_sort.h"
23 
zend_llist_init(zend_llist * l,size_t size,llist_dtor_func_t dtor,unsigned char persistent)24 ZEND_API void zend_llist_init(zend_llist *l, size_t size, llist_dtor_func_t dtor, unsigned char persistent)
25 {
26 	l->head  = NULL;
27 	l->tail  = NULL;
28 	l->count = 0;
29 	l->size  = size;
30 	l->dtor  = dtor;
31 	l->persistent = persistent;
32 }
33 
zend_llist_add_element(zend_llist * l,const void * element)34 ZEND_API void zend_llist_add_element(zend_llist *l, const void *element)
35 {
36 	zend_llist_element *tmp = pemalloc(sizeof(zend_llist_element)+l->size-1, l->persistent);
37 
38 	tmp->prev = l->tail;
39 	tmp->next = NULL;
40 	if (l->tail) {
41 		l->tail->next = tmp;
42 	} else {
43 		l->head = tmp;
44 	}
45 	l->tail = tmp;
46 	memcpy(tmp->data, element, l->size);
47 
48 	++l->count;
49 }
50 
51 
zend_llist_prepend_element(zend_llist * l,const void * element)52 ZEND_API void zend_llist_prepend_element(zend_llist *l, const void *element)
53 {
54 	zend_llist_element *tmp = pemalloc(sizeof(zend_llist_element)+l->size-1, l->persistent);
55 
56 	tmp->next = l->head;
57 	tmp->prev = NULL;
58 	if (l->head) {
59 		l->head->prev = tmp;
60 	} else {
61 		l->tail = tmp;
62 	}
63 	l->head = tmp;
64 	memcpy(tmp->data, element, l->size);
65 
66 	++l->count;
67 }
68 
69 
70 #define DEL_LLIST_ELEMENT(current, l) \
71 			if ((current)->prev) {\
72 				(current)->prev->next = (current)->next;\
73 			} else {\
74 				(l)->head = (current)->next;\
75 			}\
76 			if ((current)->next) {\
77 				(current)->next->prev = (current)->prev;\
78 			} else {\
79 				(l)->tail = (current)->prev;\
80 			}\
81 			if ((l)->dtor) {\
82 				(l)->dtor((current)->data);\
83 			}\
84 			pefree((current), (l)->persistent);\
85 			--l->count;
86 
87 
zend_llist_del_element(zend_llist * l,void * element,int (* compare)(void * element1,void * element2))88 ZEND_API void zend_llist_del_element(zend_llist *l, void *element, int (*compare)(void *element1, void *element2))
89 {
90 	zend_llist_element *current=l->head;
91 
92 	while (current) {
93 		if (compare(current->data, element)) {
94 			DEL_LLIST_ELEMENT(current, l);
95 			break;
96 		}
97 		current = current->next;
98 	}
99 }
100 
101 
zend_llist_destroy(zend_llist * l)102 ZEND_API void zend_llist_destroy(zend_llist *l)
103 {
104 	zend_llist_element *current=l->head, *next;
105 
106 	while (current) {
107 		next = current->next;
108 		if (l->dtor) {
109 			l->dtor(current->data);
110 		}
111 		pefree(current, l->persistent);
112 		current = next;
113 	}
114 
115 	l->head  = NULL;
116 	l->tail  = NULL;
117 	l->count = 0;
118 }
119 
120 
zend_llist_clean(zend_llist * l)121 ZEND_API void zend_llist_clean(zend_llist *l)
122 {
123 	zend_llist_destroy(l);
124 	l->head = l->tail = NULL;
125 }
126 
127 
zend_llist_remove_tail(zend_llist * l)128 ZEND_API void zend_llist_remove_tail(zend_llist *l)
129 {
130 	zend_llist_element *old_tail = l->tail;
131 	if (!old_tail) {
132 		return;
133 	}
134 
135 	if (old_tail->prev) {
136 		old_tail->prev->next = NULL;
137 	} else {
138 		l->head = NULL;
139 	}
140 
141 	l->tail = old_tail->prev;
142 	--l->count;
143 
144 	if (l->dtor) {
145 		l->dtor(old_tail->data);
146 	}
147 	pefree(old_tail, l->persistent);
148 }
149 
150 
zend_llist_copy(zend_llist * dst,zend_llist * src)151 ZEND_API void zend_llist_copy(zend_llist *dst, zend_llist *src)
152 {
153 	zend_llist_element *ptr;
154 
155 	zend_llist_init(dst, src->size, src->dtor, src->persistent);
156 	ptr = src->head;
157 	while (ptr) {
158 		zend_llist_add_element(dst, ptr->data);
159 		ptr = ptr->next;
160 	}
161 }
162 
163 
zend_llist_apply_with_del(zend_llist * l,int (* func)(void * data))164 ZEND_API void zend_llist_apply_with_del(zend_llist *l, int (*func)(void *data))
165 {
166 	zend_llist_element *element, *next;
167 
168 	element=l->head;
169 	while (element) {
170 		next = element->next;
171 		if (func(element->data)) {
172 			DEL_LLIST_ELEMENT(element, l);
173 		}
174 		element = next;
175 	}
176 }
177 
178 
zend_llist_apply(zend_llist * l,llist_apply_func_t func)179 ZEND_API void zend_llist_apply(zend_llist *l, llist_apply_func_t func)
180 {
181 	zend_llist_element *element;
182 
183 	for (element=l->head; element; element=element->next) {
184 		func(element->data);
185 	}
186 }
187 
zend_llist_swap(zend_llist_element ** p,zend_llist_element ** q)188 static void zend_llist_swap(zend_llist_element **p, zend_llist_element **q)
189 {
190 	zend_llist_element *t;
191 	t = *p;
192 	*p = *q;
193 	*q = t;
194 }
195 
zend_llist_sort(zend_llist * l,llist_compare_func_t comp_func)196 ZEND_API void zend_llist_sort(zend_llist *l, llist_compare_func_t comp_func)
197 {
198 	size_t i;
199 
200 	zend_llist_element **elements;
201 	zend_llist_element *element, **ptr;
202 
203 	if (l->count == 0) {
204 		return;
205 	}
206 
207 	elements = (zend_llist_element **) emalloc(l->count * sizeof(zend_llist_element *));
208 
209 	ptr = &elements[0];
210 
211 	for (element=l->head; element; element=element->next) {
212 		*ptr++ = element;
213 	}
214 
215 	zend_sort(elements, l->count, sizeof(zend_llist_element *),
216 			(compare_func_t) comp_func, (swap_func_t) zend_llist_swap);
217 
218 	l->head = elements[0];
219 	elements[0]->prev = NULL;
220 
221 	for (i = 1; i < l->count; i++) {
222 		elements[i]->prev = elements[i-1];
223 		elements[i-1]->next = elements[i];
224 	}
225 	elements[i-1]->next = NULL;
226 	l->tail = elements[i-1];
227 	efree(elements);
228 }
229 
230 
zend_llist_apply_with_argument(zend_llist * l,llist_apply_with_arg_func_t func,void * arg)231 ZEND_API void zend_llist_apply_with_argument(zend_llist *l, llist_apply_with_arg_func_t func, void *arg)
232 {
233 	zend_llist_element *element;
234 
235 	for (element=l->head; element; element=element->next) {
236 		func(element->data, arg);
237 	}
238 }
239 
240 
zend_llist_apply_with_arguments(zend_llist * l,llist_apply_with_args_func_t func,int num_args,...)241 ZEND_API void zend_llist_apply_with_arguments(zend_llist *l, llist_apply_with_args_func_t func, int num_args, ...)
242 {
243 	zend_llist_element *element;
244 	va_list args;
245 
246 	va_start(args, num_args);
247 	for (element=l->head; element; element=element->next) {
248 		func(element->data, num_args, args);
249 	}
250 	va_end(args);
251 }
252 
253 
zend_llist_count(zend_llist * l)254 ZEND_API size_t zend_llist_count(zend_llist *l)
255 {
256 	return l->count;
257 }
258 
259 
zend_llist_get_first_ex(zend_llist * l,zend_llist_position * pos)260 ZEND_API void *zend_llist_get_first_ex(zend_llist *l, zend_llist_position *pos)
261 {
262 	zend_llist_position *current = pos ? pos : &l->traverse_ptr;
263 
264 	*current = l->head;
265 	if (*current) {
266 		return (*current)->data;
267 	} else {
268 		return NULL;
269 	}
270 }
271 
272 
zend_llist_get_last_ex(zend_llist * l,zend_llist_position * pos)273 ZEND_API void *zend_llist_get_last_ex(zend_llist *l, zend_llist_position *pos)
274 {
275 	zend_llist_position *current = pos ? pos : &l->traverse_ptr;
276 
277 	*current = l->tail;
278 	if (*current) {
279 		return (*current)->data;
280 	} else {
281 		return NULL;
282 	}
283 }
284 
285 
zend_llist_get_next_ex(zend_llist * l,zend_llist_position * pos)286 ZEND_API void *zend_llist_get_next_ex(zend_llist *l, zend_llist_position *pos)
287 {
288 	zend_llist_position *current = pos ? pos : &l->traverse_ptr;
289 
290 	if (*current) {
291 		*current = (*current)->next;
292 		if (*current) {
293 			return (*current)->data;
294 		}
295 	}
296 	return NULL;
297 }
298 
299 
zend_llist_get_prev_ex(zend_llist * l,zend_llist_position * pos)300 ZEND_API void *zend_llist_get_prev_ex(zend_llist *l, zend_llist_position *pos)
301 {
302 	zend_llist_position *current = pos ? pos : &l->traverse_ptr;
303 
304 	if (*current) {
305 		*current = (*current)->prev;
306 		if (*current) {
307 			return (*current)->data;
308 		}
309 	}
310 	return NULL;
311 }
312