php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_llist.c
Go to the documentation of this file.
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
25{
26 l->head = NULL;
27 l->tail = NULL;
28 l->count = 0;
29 l->size = size;
30 l->dtor = dtor;
32}
33
34ZEND_API void zend_llist_add_element(zend_llist *l, const void *element)
35{
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
52ZEND_API void zend_llist_prepend_element(zend_llist *l, const void *element)
53{
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
88ZEND_API void zend_llist_del_element(zend_llist *l, void *element, int (*compare)(void *element1, void *element2))
89{
91
92 while (current) {
93 if (compare(current->data, element)) {
95 break;
96 }
97 current = current->next;
98 }
99}
100
101
103{
105
106 while (current) {
107 next = current->next;
108 if (l->dtor) {
109 l->dtor(current->data);
110 }
112 current = next;
113 }
114
115 l->head = NULL;
116 l->tail = NULL;
117 l->count = 0;
118}
119
120
122{
124 l->head = l->tail = NULL;
125}
126
127
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
152{
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
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
180{
181 zend_llist_element *element;
182
183 for (element=l->head; element; element=element->next) {
184 func(element->data);
185 }
186}
187
188static void zend_llist_swap(zend_llist_element **p, zend_llist_element **q)
189{
191 t = *p;
192 *p = *q;
193 *q = t;
194}
195
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
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
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
255{
256 return l->count;
257}
258
259
261{
263
264 *current = l->head;
265 if (*current) {
266 return (*current)->data;
267 } else {
268 return NULL;
269 }
270}
271
272
274{
276
277 *current = l->tail;
278 if (*current) {
279 return (*current)->data;
280 } else {
281 return NULL;
282 }
283}
284
285
287{
289
290 if (*current) {
291 *current = (*current)->next;
292 if (*current) {
293 return (*current)->data;
294 }
295 }
296 return NULL;
297}
298
299
301{
303
304 if (*current) {
305 *current = (*current)->prev;
306 if (*current) {
307 return (*current)->data;
308 }
309 }
310 return NULL;
311}
new_type size
Definition ffi.c:4365
void * ptr
Definition ffi.c:3814
memcpy(ptr1, ptr2, size)
zval * arg
Definition ffi.c:3975
ffi persistent
Definition ffi.c:3633
#define NULL
Definition gdcache.h:45
#define next(ls)
Definition minilua.c:2661
unsigned const char * pos
Definition php_ffi.h:52
zend_constant * data
zval * current
Definition session.c:1024
p
Definition session.c:1105
struct _zend_llist_element * prev
Definition zend_llist.h:27
struct _zend_llist_element * next
Definition zend_llist.h:26
size_t size
Definition zend_llist.h:41
zend_llist_element * traverse_ptr
Definition zend_llist.h:44
unsigned char persistent
Definition zend_llist.h:43
zend_llist_element * head
Definition zend_llist.h:38
size_t count
Definition zend_llist.h:40
zend_llist_element * tail
Definition zend_llist.h:39
llist_dtor_func_t dtor
Definition zend_llist.h:42
#define efree(ptr)
Definition zend_alloc.h:155
#define pefree(ptr, persistent)
Definition zend_alloc.h:191
#define pemalloc(size, persistent)
Definition zend_alloc.h:189
#define emalloc(size)
Definition zend_alloc.h:151
uint32_t num_args
execute_data func
zval * args
#define ZEND_API
ZEND_API void zend_llist_destroy(zend_llist *l)
Definition zend_llist.c:102
ZEND_API size_t zend_llist_count(zend_llist *l)
Definition zend_llist.c:254
ZEND_API void zend_llist_prepend_element(zend_llist *l, const void *element)
Definition zend_llist.c:52
#define DEL_LLIST_ELEMENT(current, l)
Definition zend_llist.c:70
ZEND_API void zend_llist_copy(zend_llist *dst, zend_llist *src)
Definition zend_llist.c:151
ZEND_API void zend_llist_del_element(zend_llist *l, void *element, int(*compare)(void *element1, void *element2))
Definition zend_llist.c:88
ZEND_API void zend_llist_add_element(zend_llist *l, const void *element)
Definition zend_llist.c:34
ZEND_API void zend_llist_init(zend_llist *l, size_t size, llist_dtor_func_t dtor, unsigned char persistent)
Definition zend_llist.c:24
ZEND_API void zend_llist_clean(zend_llist *l)
Definition zend_llist.c:121
ZEND_API void * zend_llist_get_prev_ex(zend_llist *l, zend_llist_position *pos)
Definition zend_llist.c:300
ZEND_API void zend_llist_apply_with_del(zend_llist *l, int(*func)(void *data))
Definition zend_llist.c:164
ZEND_API void * zend_llist_get_next_ex(zend_llist *l, zend_llist_position *pos)
Definition zend_llist.c:286
ZEND_API void zend_llist_apply_with_arguments(zend_llist *l, llist_apply_with_args_func_t func, int num_args,...)
Definition zend_llist.c:241
ZEND_API void zend_llist_remove_tail(zend_llist *l)
Definition zend_llist.c:128
ZEND_API void zend_llist_sort(zend_llist *l, llist_compare_func_t comp_func)
Definition zend_llist.c:196
ZEND_API void zend_llist_apply(zend_llist *l, llist_apply_func_t func)
Definition zend_llist.c:179
ZEND_API void zend_llist_apply_with_argument(zend_llist *l, llist_apply_with_arg_func_t func, void *arg)
Definition zend_llist.c:231
ZEND_API void * zend_llist_get_first_ex(zend_llist *l, zend_llist_position *pos)
Definition zend_llist.c:260
ZEND_API void * zend_llist_get_last_ex(zend_llist *l, zend_llist_position *pos)
Definition zend_llist.c:273
int(* llist_compare_func_t)(const zend_llist_element **, const zend_llist_element **)
Definition zend_llist.h:32
struct _zend_llist_element zend_llist_element
zend_llist_element * zend_llist_position
Definition zend_llist.h:47
void(* llist_apply_with_args_func_t)(void *data, int num_args, va_list args)
Definition zend_llist.h:33
void(* llist_dtor_func_t)(void *)
Definition zend_llist.h:31
void(* llist_apply_with_arg_func_t)(void *data, void *arg)
Definition zend_llist.h:34
void(* llist_apply_func_t)(void *)
Definition zend_llist.h:35
struct _zend_llist zend_llist
ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
Definition zend_sort.c:248
int(* compare_func_t)(const void *, const void *)
Definition zend_types.h:104
void(* swap_func_t)(void *, void *)
Definition zend_types.h:105