/src/php-src/ext/spl/spl_dllist.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | https://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Etienne Kneuss <colder@php.net> | |
14 | | +----------------------------------------------------------------------+ |
15 | | */ |
16 | | |
17 | | #ifdef HAVE_CONFIG_H |
18 | | # include "config.h" |
19 | | #endif |
20 | | |
21 | | #include "php.h" |
22 | | #include "zend_interfaces.h" |
23 | | #include "zend_exceptions.h" |
24 | | #include "zend_hash.h" |
25 | | |
26 | | #include "ext/standard/php_var.h" |
27 | | #include "zend_smart_str.h" |
28 | | #include "spl_dllist.h" |
29 | | #include "spl_dllist_arginfo.h" |
30 | | #include "spl_exceptions.h" |
31 | | #include "spl_functions.h" /* For spl_set_private_debug_info_property() */ |
32 | | |
33 | | static zend_object_handlers spl_handler_SplDoublyLinkedList; |
34 | | PHPAPI zend_class_entry *spl_ce_SplDoublyLinkedList; |
35 | | PHPAPI zend_class_entry *spl_ce_SplQueue; |
36 | | PHPAPI zend_class_entry *spl_ce_SplStack; |
37 | | |
38 | 72 | #define SPL_LLIST_RC(elem) Z_EXTRA((elem)->data) |
39 | | |
40 | 36 | #define SPL_LLIST_DELREF(elem) if (!--SPL_LLIST_RC(elem)) { \ |
41 | 36 | efree(elem); \ |
42 | 36 | } |
43 | | |
44 | 664 | #define SPL_LLIST_CHECK_DELREF_EX(elem, on_free) if ((elem) && !--SPL_LLIST_RC(elem)) { \ |
45 | 0 | efree(elem); \ |
46 | 0 | on_free \ |
47 | 0 | } |
48 | | |
49 | 664 | #define SPL_LLIST_CHECK_DELREF(elem) SPL_LLIST_CHECK_DELREF_EX(elem, ;) |
50 | | |
51 | | #define SPL_LLIST_ADDREF(elem) SPL_LLIST_RC(elem)++ |
52 | 664 | #define SPL_LLIST_CHECK_ADDREF(elem) if (elem) SPL_LLIST_RC(elem)++ |
53 | | |
54 | | #ifdef accept |
55 | | #undef accept |
56 | | #endif |
57 | | |
58 | | typedef struct _spl_ptr_llist_element { |
59 | | struct _spl_ptr_llist_element *prev; |
60 | | struct _spl_ptr_llist_element *next; |
61 | | zval data; |
62 | | } spl_ptr_llist_element; |
63 | | |
64 | | typedef struct _spl_ptr_llist { |
65 | | spl_ptr_llist_element *head; |
66 | | spl_ptr_llist_element *tail; |
67 | | int count; |
68 | | } spl_ptr_llist; |
69 | | |
70 | | typedef struct _spl_dllist_object spl_dllist_object; |
71 | | typedef struct _spl_dllist_it spl_dllist_it; |
72 | | |
73 | | struct _spl_dllist_object { |
74 | | spl_ptr_llist *llist; |
75 | | spl_ptr_llist_element *traverse_pointer; |
76 | | int traverse_position; |
77 | | int flags; |
78 | | zend_function *fptr_offset_get; |
79 | | zend_function *fptr_offset_set; |
80 | | zend_function *fptr_offset_has; |
81 | | zend_function *fptr_offset_del; |
82 | | zend_function *fptr_count; |
83 | | zend_class_entry *ce_get_iterator; |
84 | | zend_object std; |
85 | | }; |
86 | | |
87 | | /* define an overloaded iterator structure */ |
88 | | struct _spl_dllist_it { |
89 | | zend_object_iterator intern; |
90 | | spl_ptr_llist_element *traverse_pointer; |
91 | | int traverse_position; |
92 | | int flags; |
93 | | }; |
94 | | |
95 | 895 | static inline spl_dllist_object *spl_dllist_from_obj(zend_object *obj) /* {{{ */ { |
96 | 895 | return (spl_dllist_object*)((char*)(obj) - XtOffsetOf(spl_dllist_object, std)); |
97 | 895 | } |
98 | | /* }}} */ |
99 | | |
100 | 53 | #define Z_SPLDLLIST_P(zv) spl_dllist_from_obj(Z_OBJ_P((zv))) |
101 | | |
102 | | static spl_ptr_llist *spl_ptr_llist_init(void) /* {{{ */ |
103 | 664 | { |
104 | 664 | spl_ptr_llist *llist = emalloc(sizeof(spl_ptr_llist)); |
105 | | |
106 | 664 | llist->head = NULL; |
107 | 664 | llist->tail = NULL; |
108 | 664 | llist->count = 0; |
109 | | |
110 | 664 | return llist; |
111 | 664 | } |
112 | | /* }}} */ |
113 | | |
114 | | static zend_long spl_ptr_llist_count(spl_ptr_llist *llist) /* {{{ */ |
115 | 0 | { |
116 | 0 | return (zend_long)llist->count; |
117 | 0 | } |
118 | | /* }}} */ |
119 | | |
120 | | static void spl_ptr_llist_destroy(spl_ptr_llist *llist) /* {{{ */ |
121 | 664 | { |
122 | 664 | spl_ptr_llist_element *current = llist->head, *next; |
123 | | |
124 | 664 | while (current) { |
125 | 0 | next = current->next; |
126 | 0 | zval_ptr_dtor(¤t->data); |
127 | 0 | SPL_LLIST_DELREF(current); |
128 | 0 | current = next; |
129 | 0 | } |
130 | | |
131 | 664 | efree(llist); |
132 | 664 | } |
133 | | /* }}} */ |
134 | | |
135 | | static spl_ptr_llist_element *spl_ptr_llist_offset(spl_ptr_llist *llist, zend_long offset, int backward) /* {{{ */ |
136 | 0 | { |
137 | |
|
138 | 0 | spl_ptr_llist_element *current; |
139 | 0 | int pos = 0; |
140 | |
|
141 | 0 | if (backward) { |
142 | 0 | current = llist->tail; |
143 | 0 | } else { |
144 | 0 | current = llist->head; |
145 | 0 | } |
146 | |
|
147 | 0 | while (current && pos < offset) { |
148 | 0 | pos++; |
149 | 0 | if (backward) { |
150 | 0 | current = current->prev; |
151 | 0 | } else { |
152 | 0 | current = current->next; |
153 | 0 | } |
154 | 0 | } |
155 | |
|
156 | 0 | return current; |
157 | 0 | } |
158 | | /* }}} */ |
159 | | |
160 | | static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */ |
161 | 0 | { |
162 | 0 | spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element)); |
163 | |
|
164 | 0 | elem->prev = NULL; |
165 | 0 | elem->next = llist->head; |
166 | 0 | ZVAL_COPY(&elem->data, data); |
167 | 0 | SPL_LLIST_RC(elem) = 1; |
168 | |
|
169 | 0 | if (llist->head) { |
170 | 0 | llist->head->prev = elem; |
171 | 0 | } else { |
172 | 0 | llist->tail = elem; |
173 | 0 | } |
174 | |
|
175 | 0 | llist->head = elem; |
176 | 0 | llist->count++; |
177 | 0 | } |
178 | | /* }}} */ |
179 | | |
180 | | static void spl_ptr_llist_push(spl_ptr_llist *llist, zval *data) /* {{{ */ |
181 | 36 | { |
182 | 36 | spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element)); |
183 | | |
184 | 36 | elem->prev = llist->tail; |
185 | 36 | elem->next = NULL; |
186 | 36 | ZVAL_COPY(&elem->data, data); |
187 | 36 | SPL_LLIST_RC(elem) = 1; |
188 | | |
189 | 36 | if (llist->tail) { |
190 | 13 | llist->tail->next = elem; |
191 | 23 | } else { |
192 | 23 | llist->head = elem; |
193 | 23 | } |
194 | | |
195 | 36 | llist->tail = elem; |
196 | 36 | llist->count++; |
197 | 36 | } |
198 | | /* }}} */ |
199 | | |
200 | | static void spl_ptr_llist_pop(spl_ptr_llist *llist, zval *ret) /* {{{ */ |
201 | 36 | { |
202 | 36 | spl_ptr_llist_element *tail = llist->tail; |
203 | | |
204 | 36 | if (tail == NULL) { |
205 | 0 | ZVAL_UNDEF(ret); |
206 | 0 | return; |
207 | 0 | } |
208 | | |
209 | 36 | if (tail->prev) { |
210 | 13 | tail->prev->next = NULL; |
211 | 23 | } else { |
212 | 23 | llist->head = NULL; |
213 | 23 | } |
214 | | |
215 | 36 | llist->tail = tail->prev; |
216 | 36 | llist->count--; |
217 | 36 | ZVAL_COPY_VALUE(ret, &tail->data); |
218 | 36 | ZVAL_UNDEF(&tail->data); |
219 | | |
220 | 36 | tail->prev = NULL; |
221 | | |
222 | 36 | SPL_LLIST_DELREF(tail); |
223 | 36 | } |
224 | | /* }}} */ |
225 | | |
226 | | static zval *spl_ptr_llist_last(spl_ptr_llist *llist) /* {{{ */ |
227 | 0 | { |
228 | 0 | spl_ptr_llist_element *tail = llist->tail; |
229 | |
|
230 | 0 | if (tail == NULL) { |
231 | 0 | return NULL; |
232 | 0 | } else { |
233 | 0 | return &tail->data; |
234 | 0 | } |
235 | 0 | } |
236 | | /* }}} */ |
237 | | |
238 | | static zval *spl_ptr_llist_first(spl_ptr_llist *llist) /* {{{ */ |
239 | 0 | { |
240 | 0 | spl_ptr_llist_element *head = llist->head; |
241 | |
|
242 | 0 | if (head == NULL) { |
243 | 0 | return NULL; |
244 | 0 | } else { |
245 | 0 | return &head->data; |
246 | 0 | } |
247 | 0 | } |
248 | | /* }}} */ |
249 | | |
250 | | static void spl_ptr_llist_shift(spl_ptr_llist *llist, zval *ret) /* {{{ */ |
251 | 0 | { |
252 | 0 | spl_ptr_llist_element *head = llist->head; |
253 | |
|
254 | 0 | if (head == NULL) { |
255 | 0 | ZVAL_UNDEF(ret); |
256 | 0 | return; |
257 | 0 | } |
258 | | |
259 | 0 | if (head->next) { |
260 | 0 | head->next->prev = NULL; |
261 | 0 | } else { |
262 | 0 | llist->tail = NULL; |
263 | 0 | } |
264 | |
|
265 | 0 | llist->head = head->next; |
266 | 0 | llist->count--; |
267 | 0 | ZVAL_COPY_VALUE(ret, &head->data); |
268 | 0 | ZVAL_UNDEF(&head->data); |
269 | |
|
270 | 0 | head->next = NULL; |
271 | |
|
272 | 0 | SPL_LLIST_DELREF(head); |
273 | 0 | } |
274 | | /* }}} */ |
275 | | |
276 | | static void spl_ptr_llist_copy(spl_ptr_llist *from, spl_ptr_llist *to) /* {{{ */ |
277 | 0 | { |
278 | 0 | spl_ptr_llist_element *current = from->head, *next; |
279 | |
|
280 | 0 | while (current) { |
281 | 0 | next = current->next; |
282 | 0 | spl_ptr_llist_push(to, ¤t->data); |
283 | 0 | current = next; |
284 | 0 | } |
285 | |
|
286 | 0 | } |
287 | | /* }}} */ |
288 | | |
289 | | /* }}} */ |
290 | | |
291 | | static void spl_dllist_object_free_storage(zend_object *object) /* {{{ */ |
292 | 664 | { |
293 | 664 | spl_dllist_object *intern = spl_dllist_from_obj(object); |
294 | 664 | zval tmp; |
295 | | |
296 | 664 | zend_object_std_dtor(&intern->std); |
297 | | |
298 | 664 | if (intern->llist) { |
299 | 700 | while (intern->llist->count > 0) { |
300 | 36 | spl_ptr_llist_pop(intern->llist, &tmp); |
301 | 36 | zval_ptr_dtor(&tmp); |
302 | 36 | } |
303 | 664 | spl_ptr_llist_destroy(intern->llist); |
304 | 664 | } |
305 | 664 | SPL_LLIST_CHECK_DELREF(intern->traverse_pointer); |
306 | 664 | } |
307 | | /* }}} */ |
308 | | |
309 | | static zend_object *spl_dllist_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig) /* {{{ */ |
310 | 664 | { |
311 | 664 | spl_dllist_object *intern; |
312 | 664 | zend_class_entry *parent = class_type; |
313 | 664 | int inherited = 0; |
314 | | |
315 | 664 | intern = zend_object_alloc(sizeof(spl_dllist_object), parent); |
316 | | |
317 | 664 | zend_object_std_init(&intern->std, class_type); |
318 | 664 | object_properties_init(&intern->std, class_type); |
319 | | |
320 | 664 | intern->flags = 0; |
321 | 664 | intern->traverse_position = 0; |
322 | | |
323 | 664 | if (orig) { |
324 | 0 | spl_dllist_object *other = spl_dllist_from_obj(orig); |
325 | 0 | intern->ce_get_iterator = other->ce_get_iterator; |
326 | |
|
327 | 0 | if (clone_orig) { |
328 | 0 | intern->llist = spl_ptr_llist_init(); |
329 | 0 | spl_ptr_llist_copy(other->llist, intern->llist); |
330 | 0 | intern->traverse_pointer = intern->llist->head; |
331 | 0 | SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer); |
332 | 0 | } else { |
333 | 0 | intern->llist = other->llist; |
334 | 0 | intern->traverse_pointer = intern->llist->head; |
335 | 0 | SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer); |
336 | 0 | } |
337 | |
|
338 | 0 | intern->flags = other->flags; |
339 | 664 | } else { |
340 | 664 | intern->llist = spl_ptr_llist_init(); |
341 | 664 | intern->traverse_pointer = intern->llist->head; |
342 | 664 | SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer); |
343 | 664 | } |
344 | | |
345 | 1.28k | while (parent) { |
346 | 1.28k | if (parent == spl_ce_SplStack) { |
347 | 612 | intern->flags |= (SPL_DLLIST_IT_FIX | SPL_DLLIST_IT_LIFO); |
348 | 671 | } else if (parent == spl_ce_SplQueue) { |
349 | 7 | intern->flags |= SPL_DLLIST_IT_FIX; |
350 | 7 | } |
351 | | |
352 | 1.28k | if (parent == spl_ce_SplDoublyLinkedList) { |
353 | 664 | break; |
354 | 664 | } |
355 | | |
356 | 619 | parent = parent->parent; |
357 | 619 | inherited = 1; |
358 | 619 | } |
359 | | |
360 | 664 | ZEND_ASSERT(parent); |
361 | | |
362 | 664 | if (inherited) { |
363 | 619 | intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1); |
364 | 619 | if (intern->fptr_offset_get->common.scope == parent) { |
365 | 619 | intern->fptr_offset_get = NULL; |
366 | 619 | } |
367 | 619 | intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1); |
368 | 619 | if (intern->fptr_offset_set->common.scope == parent) { |
369 | 619 | intern->fptr_offset_set = NULL; |
370 | 619 | } |
371 | 619 | intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1); |
372 | 619 | if (intern->fptr_offset_has->common.scope == parent) { |
373 | 619 | intern->fptr_offset_has = NULL; |
374 | 619 | } |
375 | 619 | intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1); |
376 | 619 | if (intern->fptr_offset_del->common.scope == parent) { |
377 | 619 | intern->fptr_offset_del = NULL; |
378 | 619 | } |
379 | | /* Find count() method */ |
380 | 619 | intern->fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT)); |
381 | 619 | if (intern->fptr_count->common.scope == parent) { |
382 | 619 | intern->fptr_count = NULL; |
383 | 619 | } |
384 | 619 | } |
385 | | |
386 | 664 | return &intern->std; |
387 | 664 | } |
388 | | /* }}} */ |
389 | | |
390 | | static zend_object *spl_dllist_object_new(zend_class_entry *class_type) /* {{{ */ |
391 | 664 | { |
392 | 664 | return spl_dllist_object_new_ex(class_type, NULL, 0); |
393 | 664 | } |
394 | | /* }}} */ |
395 | | |
396 | | static zend_object *spl_dllist_object_clone(zend_object *old_object) /* {{{ */ |
397 | 0 | { |
398 | 0 | zend_object *new_object = spl_dllist_object_new_ex(old_object->ce, old_object, 1); |
399 | |
|
400 | 0 | zend_objects_clone_members(new_object, old_object); |
401 | |
|
402 | 0 | return new_object; |
403 | 0 | } |
404 | | /* }}} */ |
405 | | |
406 | | static zend_result spl_dllist_object_count_elements(zend_object *object, zend_long *count) /* {{{ */ |
407 | 0 | { |
408 | 0 | spl_dllist_object *intern = spl_dllist_from_obj(object); |
409 | |
|
410 | 0 | if (intern->fptr_count) { |
411 | 0 | zval rv; |
412 | 0 | zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv); |
413 | 0 | if (!Z_ISUNDEF(rv)) { |
414 | 0 | *count = zval_get_long(&rv); |
415 | 0 | zval_ptr_dtor(&rv); |
416 | 0 | return SUCCESS; |
417 | 0 | } |
418 | 0 | *count = 0; |
419 | 0 | return FAILURE; |
420 | 0 | } |
421 | | |
422 | 0 | *count = spl_ptr_llist_count(intern->llist); |
423 | 0 | return SUCCESS; |
424 | 0 | } |
425 | | /* }}} */ |
426 | | |
427 | | static inline HashTable* spl_dllist_object_get_debug_info(zend_object *obj) /* {{{{ */ |
428 | 10 | { |
429 | 10 | spl_dllist_object *intern = spl_dllist_from_obj(obj); |
430 | 10 | spl_ptr_llist_element *current = intern->llist->head; |
431 | 10 | zval tmp, dllist_array; |
432 | 10 | HashTable *debug_info; |
433 | 10 | HashTable *properties = zend_std_get_properties_ex(&intern->std); |
434 | | |
435 | | /* +2 As we are adding 2 additional key-entries */ |
436 | 10 | debug_info = zend_new_array(zend_hash_num_elements(properties) + 2); |
437 | 10 | zend_hash_copy(debug_info, properties, (copy_ctor_func_t) zval_add_ref); |
438 | | |
439 | 10 | ZVAL_LONG(&tmp, intern->flags); |
440 | 10 | spl_set_private_debug_info_property(spl_ce_SplDoublyLinkedList, "flags", strlen("flags"), debug_info, &tmp); |
441 | | |
442 | 10 | array_init(&dllist_array); |
443 | | |
444 | 10 | zend_ulong index = 0; |
445 | 30 | while (current) { |
446 | 20 | spl_ptr_llist_element *next = current->next; |
447 | | |
448 | 20 | add_index_zval(&dllist_array, index, ¤t->data); |
449 | 20 | if (Z_REFCOUNTED(current->data)) { |
450 | 20 | Z_ADDREF(current->data); |
451 | 20 | } |
452 | 20 | index++; |
453 | | |
454 | 20 | current = next; |
455 | 20 | } |
456 | | |
457 | 10 | spl_set_private_debug_info_property(spl_ce_SplDoublyLinkedList, "dllist", strlen("dllist"), debug_info, &dllist_array); |
458 | | |
459 | 10 | return debug_info; |
460 | 10 | } |
461 | | /* }}}} */ |
462 | | |
463 | | static HashTable *spl_dllist_object_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */ |
464 | 168 | { |
465 | 168 | spl_dllist_object *intern = spl_dllist_from_obj(obj); |
466 | 168 | zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create(); |
467 | 168 | spl_ptr_llist_element *current = intern->llist->head; |
468 | | |
469 | 273 | while (current) { |
470 | 105 | zend_get_gc_buffer_add_zval(gc_buffer, ¤t->data); |
471 | 105 | current = current->next; |
472 | 105 | } |
473 | | |
474 | 168 | zend_get_gc_buffer_use(gc_buffer, gc_data, gc_data_count); |
475 | 168 | return zend_std_get_properties(obj); |
476 | 168 | } |
477 | | /* }}} */ |
478 | | |
479 | | /* {{{ Push $value on the SplDoublyLinkedList */ |
480 | | PHP_METHOD(SplDoublyLinkedList, push) |
481 | 0 | { |
482 | 0 | zval *value; |
483 | 0 | spl_dllist_object *intern; |
484 | |
|
485 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) { |
486 | 0 | RETURN_THROWS(); |
487 | 0 | } |
488 | | |
489 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
490 | 0 | spl_ptr_llist_push(intern->llist, value); |
491 | 0 | } |
492 | | /* }}} */ |
493 | | |
494 | | /* {{{ Unshift $value on the SplDoublyLinkedList */ |
495 | | PHP_METHOD(SplDoublyLinkedList, unshift) |
496 | 0 | { |
497 | 0 | zval *value; |
498 | 0 | spl_dllist_object *intern; |
499 | |
|
500 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) { |
501 | 0 | RETURN_THROWS(); |
502 | 0 | } |
503 | | |
504 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
505 | 0 | spl_ptr_llist_unshift(intern->llist, value); |
506 | 0 | } |
507 | | /* }}} */ |
508 | | |
509 | | /* {{{ Pop an element out of the SplDoublyLinkedList */ |
510 | | PHP_METHOD(SplDoublyLinkedList, pop) |
511 | 0 | { |
512 | 0 | spl_dllist_object *intern; |
513 | |
|
514 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
515 | | |
516 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
517 | 0 | spl_ptr_llist_pop(intern->llist, return_value); |
518 | |
|
519 | 0 | if (Z_ISUNDEF_P(return_value)) { |
520 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Can't pop from an empty datastructure", 0); |
521 | 0 | RETURN_THROWS(); |
522 | 0 | } |
523 | 0 | } |
524 | | /* }}} */ |
525 | | |
526 | | /* {{{ Shift an element out of the SplDoublyLinkedList */ |
527 | | PHP_METHOD(SplDoublyLinkedList, shift) |
528 | 0 | { |
529 | 0 | spl_dllist_object *intern; |
530 | |
|
531 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
532 | | |
533 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
534 | 0 | spl_ptr_llist_shift(intern->llist, return_value); |
535 | |
|
536 | 0 | if (Z_ISUNDEF_P(return_value)) { |
537 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Can't shift from an empty datastructure", 0); |
538 | 0 | RETURN_THROWS(); |
539 | 0 | } |
540 | 0 | } |
541 | | /* }}} */ |
542 | | |
543 | | /* {{{ Peek at the top element of the SplDoublyLinkedList */ |
544 | | PHP_METHOD(SplDoublyLinkedList, top) |
545 | 0 | { |
546 | 0 | zval *value; |
547 | 0 | spl_dllist_object *intern; |
548 | |
|
549 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
550 | | |
551 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
552 | 0 | value = spl_ptr_llist_last(intern->llist); |
553 | |
|
554 | 0 | if (value == NULL || Z_ISUNDEF_P(value)) { |
555 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0); |
556 | 0 | RETURN_THROWS(); |
557 | 0 | } |
558 | | |
559 | 0 | RETURN_COPY_DEREF(value); |
560 | 0 | } |
561 | | /* }}} */ |
562 | | |
563 | | /* {{{ Peek at the bottom element of the SplDoublyLinkedList */ |
564 | | PHP_METHOD(SplDoublyLinkedList, bottom) |
565 | 0 | { |
566 | 0 | zval *value; |
567 | 0 | spl_dllist_object *intern; |
568 | |
|
569 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
570 | | |
571 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
572 | 0 | value = spl_ptr_llist_first(intern->llist); |
573 | |
|
574 | 0 | if (value == NULL || Z_ISUNDEF_P(value)) { |
575 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0); |
576 | 0 | RETURN_THROWS(); |
577 | 0 | } |
578 | | |
579 | 0 | RETURN_COPY_DEREF(value); |
580 | 0 | } |
581 | | /* }}} */ |
582 | | |
583 | | /* {{{ Return the number of elements in the datastructure. */ |
584 | | PHP_METHOD(SplDoublyLinkedList, count) |
585 | 0 | { |
586 | 0 | zend_long count; |
587 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
588 | |
|
589 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
590 | | |
591 | 0 | count = spl_ptr_llist_count(intern->llist); |
592 | 0 | RETURN_LONG(count); |
593 | 0 | } |
594 | | /* }}} */ |
595 | | |
596 | | /* {{{ Return true if the SplDoublyLinkedList is empty. */ |
597 | | PHP_METHOD(SplDoublyLinkedList, isEmpty) |
598 | 0 | { |
599 | 0 | zend_long count; |
600 | |
|
601 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
602 | | |
603 | 0 | spl_dllist_object_count_elements(Z_OBJ_P(ZEND_THIS), &count); |
604 | 0 | RETURN_BOOL(count == 0); |
605 | 0 | } |
606 | | /* }}} */ |
607 | | |
608 | | /* {{{ Set the mode of iteration */ |
609 | | PHP_METHOD(SplDoublyLinkedList, setIteratorMode) |
610 | 0 | { |
611 | 0 | zend_long value; |
612 | 0 | spl_dllist_object *intern; |
613 | |
|
614 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &value) == FAILURE) { |
615 | 0 | RETURN_THROWS(); |
616 | 0 | } |
617 | | |
618 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
619 | |
|
620 | 0 | if ((intern->flags & SPL_DLLIST_IT_FIX) |
621 | 0 | && (intern->flags & SPL_DLLIST_IT_LIFO) != (value & SPL_DLLIST_IT_LIFO)) { |
622 | 0 | zend_throw_exception(spl_ce_RuntimeException, "Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen", 0); |
623 | 0 | RETURN_THROWS(); |
624 | 0 | } |
625 | | |
626 | 0 | intern->flags = (value & SPL_DLLIST_IT_MASK) | (intern->flags & SPL_DLLIST_IT_FIX); |
627 | |
|
628 | 0 | RETURN_LONG(intern->flags); |
629 | 0 | } |
630 | | /* }}} */ |
631 | | |
632 | | /* {{{ Return the mode of iteration */ |
633 | | PHP_METHOD(SplDoublyLinkedList, getIteratorMode) |
634 | 0 | { |
635 | 0 | spl_dllist_object *intern; |
636 | |
|
637 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
638 | | |
639 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
640 | |
|
641 | 0 | RETURN_LONG(intern->flags); |
642 | 0 | } |
643 | | /* }}} */ |
644 | | |
645 | | /* {{{ Returns whether the requested $index exists. */ |
646 | | PHP_METHOD(SplDoublyLinkedList, offsetExists) |
647 | 0 | { |
648 | 0 | spl_dllist_object *intern; |
649 | 0 | zend_long index; |
650 | |
|
651 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) { |
652 | 0 | RETURN_THROWS(); |
653 | 0 | } |
654 | | |
655 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
656 | |
|
657 | 0 | RETURN_BOOL(index >= 0 && index < intern->llist->count); |
658 | 0 | } /* }}} */ |
659 | | |
660 | | /* {{{ Returns the value at the specified $index. */ |
661 | | PHP_METHOD(SplDoublyLinkedList, offsetGet) |
662 | 0 | { |
663 | 0 | zend_long index; |
664 | 0 | spl_dllist_object *intern; |
665 | 0 | spl_ptr_llist_element *element; |
666 | |
|
667 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) { |
668 | 0 | RETURN_THROWS(); |
669 | 0 | } |
670 | | |
671 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
672 | |
|
673 | 0 | if (index < 0 || index >= intern->llist->count) { |
674 | 0 | zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range"); |
675 | 0 | RETURN_THROWS(); |
676 | 0 | } |
677 | | |
678 | 0 | element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO); |
679 | 0 | if (element == NULL) { |
680 | 0 | zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset"); |
681 | 0 | RETURN_THROWS(); |
682 | 0 | } |
683 | | |
684 | 0 | RETURN_COPY_DEREF(&element->data); |
685 | 0 | } /* }}} */ |
686 | | |
687 | | /* {{{ Sets the value at the specified $index to $newval. */ |
688 | | PHP_METHOD(SplDoublyLinkedList, offsetSet) |
689 | 0 | { |
690 | 0 | zend_long index; |
691 | 0 | bool index_is_null = true; |
692 | 0 | zval *value; |
693 | 0 | spl_dllist_object *intern; |
694 | |
|
695 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l!z", &index, &index_is_null, &value) == FAILURE) { |
696 | 0 | RETURN_THROWS(); |
697 | 0 | } |
698 | | |
699 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
700 | |
|
701 | 0 | if (index_is_null) { |
702 | | /* $obj[] = ... */ |
703 | 0 | spl_ptr_llist_push(intern->llist, value); |
704 | 0 | } else { |
705 | | /* $obj[$foo] = ... */ |
706 | 0 | spl_ptr_llist_element *element; |
707 | |
|
708 | 0 | if (index < 0 || index >= intern->llist->count) { |
709 | 0 | zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range"); |
710 | 0 | RETURN_THROWS(); |
711 | 0 | } |
712 | | |
713 | 0 | element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO); |
714 | |
|
715 | 0 | if (element != NULL) { |
716 | | /* the element is replaced, delref the old one as in |
717 | | * SplDoublyLinkedList::pop() */ |
718 | 0 | zval garbage; |
719 | 0 | ZVAL_COPY_VALUE(&garbage, &element->data); |
720 | 0 | ZVAL_COPY(&element->data, value); |
721 | 0 | zval_ptr_dtor(&garbage); |
722 | 0 | } else { |
723 | 0 | zval_ptr_dtor(value); |
724 | 0 | zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset"); |
725 | 0 | RETURN_THROWS(); |
726 | 0 | } |
727 | 0 | } |
728 | 0 | } /* }}} */ |
729 | | |
730 | | /* {{{ Unsets the value at the specified $index. */ |
731 | | PHP_METHOD(SplDoublyLinkedList, offsetUnset) |
732 | 0 | { |
733 | 0 | zend_long index; |
734 | 0 | spl_dllist_object *intern; |
735 | 0 | spl_ptr_llist_element *element; |
736 | 0 | spl_ptr_llist *llist; |
737 | |
|
738 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) { |
739 | 0 | RETURN_THROWS(); |
740 | 0 | } |
741 | | |
742 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
743 | 0 | llist = intern->llist; |
744 | |
|
745 | 0 | if (index < 0 || index >= intern->llist->count) { |
746 | 0 | zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range"); |
747 | 0 | RETURN_THROWS(); |
748 | 0 | } |
749 | | |
750 | 0 | element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO); |
751 | |
|
752 | 0 | if (element != NULL) { |
753 | | /* connect the neighbors */ |
754 | 0 | if (element->prev) { |
755 | 0 | element->prev->next = element->next; |
756 | 0 | } |
757 | |
|
758 | 0 | if (element->next) { |
759 | 0 | element->next->prev = element->prev; |
760 | 0 | } |
761 | | |
762 | | /* take care of head/tail */ |
763 | 0 | if (element == llist->head) { |
764 | 0 | llist->head = element->next; |
765 | 0 | } |
766 | |
|
767 | 0 | if (element == llist->tail) { |
768 | 0 | llist->tail = element->prev; |
769 | 0 | } |
770 | | |
771 | | /* finally, delete the element */ |
772 | 0 | llist->count--; |
773 | |
|
774 | 0 | if (intern->traverse_pointer == element) { |
775 | 0 | SPL_LLIST_DELREF(element); |
776 | 0 | intern->traverse_pointer = NULL; |
777 | 0 | } |
778 | |
|
779 | 0 | zval_ptr_dtor(&element->data); |
780 | 0 | ZVAL_UNDEF(&element->data); |
781 | |
|
782 | 0 | SPL_LLIST_DELREF(element); |
783 | 0 | } else { |
784 | 0 | zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset"); |
785 | 0 | RETURN_THROWS(); |
786 | 0 | } |
787 | 0 | } /* }}} */ |
788 | | |
789 | | static void spl_dllist_it_dtor(zend_object_iterator *iter) /* {{{ */ |
790 | 0 | { |
791 | 0 | spl_dllist_it *iterator = (spl_dllist_it *)iter; |
792 | |
|
793 | 0 | SPL_LLIST_CHECK_DELREF(iterator->traverse_pointer); |
794 | |
|
795 | 0 | zval_ptr_dtor(&iterator->intern.data); |
796 | 0 | } |
797 | | /* }}} */ |
798 | | |
799 | | static void spl_dllist_it_helper_rewind(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */ |
800 | 0 | { |
801 | 0 | SPL_LLIST_CHECK_DELREF(*traverse_pointer_ptr); |
802 | |
|
803 | 0 | if (flags & SPL_DLLIST_IT_LIFO) { |
804 | 0 | *traverse_position_ptr = llist->count-1; |
805 | 0 | *traverse_pointer_ptr = llist->tail; |
806 | 0 | } else { |
807 | 0 | *traverse_position_ptr = 0; |
808 | 0 | *traverse_pointer_ptr = llist->head; |
809 | 0 | } |
810 | |
|
811 | 0 | SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr); |
812 | 0 | } |
813 | | /* }}} */ |
814 | | |
815 | | static void spl_dllist_it_helper_move_forward(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */ |
816 | 0 | { |
817 | 0 | if (*traverse_pointer_ptr) { |
818 | 0 | spl_ptr_llist_element *old = *traverse_pointer_ptr; |
819 | |
|
820 | 0 | if (flags & SPL_DLLIST_IT_LIFO) { |
821 | 0 | *traverse_pointer_ptr = old->prev; |
822 | 0 | (*traverse_position_ptr)--; |
823 | |
|
824 | 0 | if (flags & SPL_DLLIST_IT_DELETE) { |
825 | 0 | zval prev; |
826 | 0 | spl_ptr_llist_pop(llist, &prev); |
827 | |
|
828 | 0 | zval_ptr_dtor(&prev); |
829 | 0 | } |
830 | 0 | } else { |
831 | 0 | *traverse_pointer_ptr = old->next; |
832 | |
|
833 | 0 | if (flags & SPL_DLLIST_IT_DELETE) { |
834 | 0 | zval prev; |
835 | 0 | spl_ptr_llist_shift(llist, &prev); |
836 | |
|
837 | 0 | zval_ptr_dtor(&prev); |
838 | 0 | } else { |
839 | 0 | (*traverse_position_ptr)++; |
840 | 0 | } |
841 | 0 | } |
842 | |
|
843 | 0 | SPL_LLIST_DELREF(old); |
844 | 0 | SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr); |
845 | 0 | } |
846 | 0 | } |
847 | | /* }}} */ |
848 | | |
849 | | static void spl_dllist_it_rewind(zend_object_iterator *iter) /* {{{ */ |
850 | 0 | { |
851 | 0 | spl_dllist_it *iterator = (spl_dllist_it *)iter; |
852 | 0 | spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data); |
853 | 0 | spl_ptr_llist *llist = object->llist; |
854 | |
|
855 | 0 | spl_dllist_it_helper_rewind(&iterator->traverse_pointer, &iterator->traverse_position, llist, iterator->flags); |
856 | 0 | } |
857 | | /* }}} */ |
858 | | |
859 | | static zend_result spl_dllist_it_valid(zend_object_iterator *iter) /* {{{ */ |
860 | 0 | { |
861 | 0 | spl_dllist_it *iterator = (spl_dllist_it *)iter; |
862 | 0 | spl_ptr_llist_element *element = iterator->traverse_pointer; |
863 | |
|
864 | 0 | return (element != NULL ? SUCCESS : FAILURE); |
865 | 0 | } |
866 | | /* }}} */ |
867 | | |
868 | | static zval *spl_dllist_it_get_current_data(zend_object_iterator *iter) /* {{{ */ |
869 | 0 | { |
870 | 0 | spl_dllist_it *iterator = (spl_dllist_it *)iter; |
871 | 0 | spl_ptr_llist_element *element = iterator->traverse_pointer; |
872 | |
|
873 | 0 | if (element == NULL || Z_ISUNDEF(element->data)) { |
874 | 0 | return NULL; |
875 | 0 | } |
876 | | |
877 | 0 | return &element->data; |
878 | 0 | } |
879 | | /* }}} */ |
880 | | |
881 | | static void spl_dllist_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */ |
882 | 0 | { |
883 | 0 | spl_dllist_it *iterator = (spl_dllist_it *)iter; |
884 | |
|
885 | 0 | ZVAL_LONG(key, iterator->traverse_position); |
886 | 0 | } |
887 | | /* }}} */ |
888 | | |
889 | | static void spl_dllist_it_move_forward(zend_object_iterator *iter) /* {{{ */ |
890 | 0 | { |
891 | 0 | spl_dllist_it *iterator = (spl_dllist_it *)iter; |
892 | 0 | spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data); |
893 | |
|
894 | 0 | spl_dllist_it_helper_move_forward(&iterator->traverse_pointer, &iterator->traverse_position, object->llist, iterator->flags); |
895 | 0 | } |
896 | | /* }}} */ |
897 | | |
898 | | /* {{{ Return current array key */ |
899 | | PHP_METHOD(SplDoublyLinkedList, key) |
900 | 0 | { |
901 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
902 | |
|
903 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
904 | | |
905 | 0 | RETURN_LONG(intern->traverse_position); |
906 | 0 | } |
907 | | /* }}} */ |
908 | | |
909 | | /* {{{ Move to next entry */ |
910 | | PHP_METHOD(SplDoublyLinkedList, prev) |
911 | 0 | { |
912 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
913 | |
|
914 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
915 | | |
916 | 0 | spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags ^ SPL_DLLIST_IT_LIFO); |
917 | 0 | } |
918 | | /* }}} */ |
919 | | |
920 | | /* {{{ Move to next entry */ |
921 | | PHP_METHOD(SplDoublyLinkedList, next) |
922 | 0 | { |
923 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
924 | |
|
925 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
926 | | |
927 | 0 | spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags); |
928 | 0 | } |
929 | | /* }}} */ |
930 | | |
931 | | /* {{{ Check whether the datastructure contains more entries */ |
932 | | PHP_METHOD(SplDoublyLinkedList, valid) |
933 | 0 | { |
934 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
935 | |
|
936 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
937 | | |
938 | 0 | RETURN_BOOL(intern->traverse_pointer != NULL); |
939 | 0 | } |
940 | | /* }}} */ |
941 | | |
942 | | /* {{{ Rewind the datastructure back to the start */ |
943 | | PHP_METHOD(SplDoublyLinkedList, rewind) |
944 | 0 | { |
945 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
946 | |
|
947 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
948 | | |
949 | 0 | spl_dllist_it_helper_rewind(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags); |
950 | 0 | } |
951 | | /* }}} */ |
952 | | |
953 | | /* {{{ Return current datastructure entry */ |
954 | | PHP_METHOD(SplDoublyLinkedList, current) |
955 | 0 | { |
956 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
957 | 0 | spl_ptr_llist_element *element = intern->traverse_pointer; |
958 | |
|
959 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
960 | | |
961 | 0 | if (element == NULL || Z_ISUNDEF(element->data)) { |
962 | 0 | RETURN_NULL(); |
963 | 0 | } else { |
964 | 0 | RETURN_COPY_DEREF(&element->data); |
965 | 0 | } |
966 | 0 | } |
967 | | /* }}} */ |
968 | | |
969 | | /* {{{ Serializes storage */ |
970 | | PHP_METHOD(SplDoublyLinkedList, serialize) |
971 | 0 | { |
972 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
973 | 0 | smart_str buf = {0}; |
974 | 0 | spl_ptr_llist_element *current = intern->llist->head, *next; |
975 | 0 | zval flags; |
976 | 0 | php_serialize_data_t var_hash; |
977 | |
|
978 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
979 | | |
980 | 0 | PHP_VAR_SERIALIZE_INIT(var_hash); |
981 | | |
982 | | /* flags */ |
983 | 0 | ZVAL_LONG(&flags, intern->flags); |
984 | 0 | php_var_serialize(&buf, &flags, &var_hash); |
985 | | |
986 | | /* elements */ |
987 | 0 | while (current) { |
988 | 0 | smart_str_appendc(&buf, ':'); |
989 | 0 | next = current->next; |
990 | |
|
991 | 0 | SPL_LLIST_CHECK_ADDREF(next); |
992 | |
|
993 | 0 | php_var_serialize(&buf, ¤t->data, &var_hash); |
994 | |
|
995 | 0 | SPL_LLIST_CHECK_DELREF_EX(next, break;); |
996 | |
|
997 | 0 | current = next; |
998 | 0 | } |
999 | | |
1000 | | /* done */ |
1001 | 0 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
1002 | |
|
1003 | 0 | RETURN_STR(smart_str_extract(&buf)); |
1004 | 0 | } /* }}} */ |
1005 | | |
1006 | | /* {{{ Unserializes storage */ |
1007 | | PHP_METHOD(SplDoublyLinkedList, unserialize) |
1008 | 36 | { |
1009 | 36 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
1010 | 36 | zval *flags, *elem; |
1011 | 36 | char *buf; |
1012 | 36 | size_t buf_len; |
1013 | 36 | const unsigned char *p, *s; |
1014 | 36 | php_unserialize_data_t var_hash; |
1015 | | |
1016 | 36 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) { |
1017 | 0 | RETURN_THROWS(); |
1018 | 0 | } |
1019 | | |
1020 | 36 | if (buf_len == 0) { |
1021 | 3 | return; |
1022 | 3 | } |
1023 | | |
1024 | 33 | while (intern->llist->count > 0) { |
1025 | 0 | zval tmp; |
1026 | 0 | spl_ptr_llist_pop(intern->llist, &tmp); |
1027 | 0 | zval_ptr_dtor(&tmp); |
1028 | 0 | } |
1029 | | |
1030 | 33 | s = p = (const unsigned char*)buf; |
1031 | 33 | PHP_VAR_UNSERIALIZE_INIT(var_hash); |
1032 | | |
1033 | | /* flags */ |
1034 | 33 | flags = var_tmp_var(&var_hash); |
1035 | 33 | if (!php_var_unserialize(flags, &p, s + buf_len, &var_hash) || Z_TYPE_P(flags) != IS_LONG) { |
1036 | 4 | goto error; |
1037 | 4 | } |
1038 | | |
1039 | 29 | intern->flags = (int)Z_LVAL_P(flags); |
1040 | | |
1041 | | /* elements */ |
1042 | 65 | while(*p == ':') { |
1043 | 45 | ++p; |
1044 | 45 | elem = var_tmp_var(&var_hash); |
1045 | 45 | if (!php_var_unserialize(elem, &p, s + buf_len, &var_hash)) { |
1046 | 9 | goto error; |
1047 | 9 | } |
1048 | 36 | var_push_dtor(&var_hash, elem); |
1049 | | |
1050 | 36 | spl_ptr_llist_push(intern->llist, elem); |
1051 | 36 | } |
1052 | | |
1053 | 20 | if (*p != '\0') { |
1054 | 7 | goto error; |
1055 | 7 | } |
1056 | | |
1057 | 13 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
1058 | 13 | return; |
1059 | | |
1060 | 20 | error: |
1061 | 20 | PHP_VAR_UNSERIALIZE_DESTROY(var_hash); |
1062 | 20 | zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len); |
1063 | 20 | RETURN_THROWS(); |
1064 | | |
1065 | 20 | } /* }}} */ |
1066 | | |
1067 | | /* {{{ */ |
1068 | | PHP_METHOD(SplDoublyLinkedList, __serialize) |
1069 | 0 | { |
1070 | 0 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
1071 | 0 | spl_ptr_llist_element *current = intern->llist->head; |
1072 | 0 | zval tmp; |
1073 | |
|
1074 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
1075 | | |
1076 | 0 | array_init(return_value); |
1077 | | |
1078 | | /* flags */ |
1079 | 0 | ZVAL_LONG(&tmp, intern->flags); |
1080 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); |
1081 | | |
1082 | | /* elements */ |
1083 | 0 | array_init_size(&tmp, intern->llist->count); |
1084 | 0 | while (current) { |
1085 | 0 | zend_hash_next_index_insert(Z_ARRVAL(tmp), ¤t->data); |
1086 | 0 | Z_TRY_ADDREF(current->data); |
1087 | 0 | current = current->next; |
1088 | 0 | } |
1089 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); |
1090 | | |
1091 | | /* members */ |
1092 | 0 | ZVAL_ARR(&tmp, zend_proptable_to_symtable( |
1093 | 0 | zend_std_get_properties(&intern->std), /* always_duplicate */ 1)); |
1094 | 0 | zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); |
1095 | 0 | } /* }}} */ |
1096 | | |
1097 | | /* {{{ */ |
1098 | 17 | PHP_METHOD(SplDoublyLinkedList, __unserialize) { |
1099 | 17 | spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS); |
1100 | 17 | HashTable *data; |
1101 | 17 | zval *flags_zv, *storage_zv, *members_zv, *elem; |
1102 | | |
1103 | 17 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) { |
1104 | 0 | RETURN_THROWS(); |
1105 | 0 | } |
1106 | | |
1107 | 17 | flags_zv = zend_hash_index_find(data, 0); |
1108 | 17 | storage_zv = zend_hash_index_find(data, 1); |
1109 | 17 | members_zv = zend_hash_index_find(data, 2); |
1110 | 17 | if (!flags_zv || !storage_zv || !members_zv || |
1111 | 0 | Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(storage_zv) != IS_ARRAY || |
1112 | 17 | Z_TYPE_P(members_zv) != IS_ARRAY) { |
1113 | 17 | zend_throw_exception(spl_ce_UnexpectedValueException, |
1114 | 17 | "Incomplete or ill-typed serialization data", 0); |
1115 | 17 | RETURN_THROWS(); |
1116 | 17 | } |
1117 | | |
1118 | 0 | intern->flags = (int) Z_LVAL_P(flags_zv); |
1119 | |
|
1120 | 0 | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), elem) { |
1121 | 0 | spl_ptr_llist_push(intern->llist, elem); |
1122 | 0 | } ZEND_HASH_FOREACH_END(); |
1123 | |
|
1124 | 0 | object_properties_load(&intern->std, Z_ARRVAL_P(members_zv)); |
1125 | 0 | } /* }}} */ |
1126 | | |
1127 | | /* {{{ Inserts a new entry before the specified $index consisting of $newval. */ |
1128 | | PHP_METHOD(SplDoublyLinkedList, add) |
1129 | 0 | { |
1130 | 0 | zval *value; |
1131 | 0 | spl_dllist_object *intern; |
1132 | 0 | spl_ptr_llist_element *element; |
1133 | 0 | zend_long index; |
1134 | |
|
1135 | 0 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz", &index, &value) == FAILURE) { |
1136 | 0 | RETURN_THROWS(); |
1137 | 0 | } |
1138 | | |
1139 | 0 | intern = Z_SPLDLLIST_P(ZEND_THIS); |
1140 | |
|
1141 | 0 | if (index < 0 || index > intern->llist->count) { |
1142 | 0 | zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range"); |
1143 | 0 | RETURN_THROWS(); |
1144 | 0 | } |
1145 | | |
1146 | 0 | if (index == intern->llist->count) { |
1147 | | /* If index is the last entry+1 then we do a push because we're not inserting before any entry */ |
1148 | 0 | spl_ptr_llist_push(intern->llist, value); |
1149 | 0 | } else { |
1150 | | /* Create the new element we want to insert */ |
1151 | 0 | spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element)); |
1152 | | |
1153 | | /* Get the element we want to insert before */ |
1154 | 0 | element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO); |
1155 | 0 | ZEND_ASSERT(element != NULL); |
1156 | |
|
1157 | 0 | ZVAL_COPY(&elem->data, value); |
1158 | 0 | SPL_LLIST_RC(elem) = 1; |
1159 | | /* connect to the neighbours */ |
1160 | 0 | elem->next = element; |
1161 | 0 | elem->prev = element->prev; |
1162 | | |
1163 | | /* connect the neighbours to this new element */ |
1164 | 0 | if (elem->prev == NULL) { |
1165 | 0 | intern->llist->head = elem; |
1166 | 0 | } else { |
1167 | 0 | element->prev->next = elem; |
1168 | 0 | } |
1169 | 0 | element->prev = elem; |
1170 | |
|
1171 | 0 | intern->llist->count++; |
1172 | 0 | } |
1173 | 0 | } /* }}} */ |
1174 | | |
1175 | | /* {{{ */ |
1176 | | PHP_METHOD(SplDoublyLinkedList, __debugInfo) |
1177 | 10 | { |
1178 | 10 | ZEND_PARSE_PARAMETERS_NONE(); |
1179 | | |
1180 | 10 | RETURN_ARR(spl_dllist_object_get_debug_info(Z_OBJ_P(ZEND_THIS))); |
1181 | 10 | } /* }}} */ |
1182 | | |
1183 | | /* {{{ iterator handler table */ |
1184 | | static const zend_object_iterator_funcs spl_dllist_it_funcs = { |
1185 | | spl_dllist_it_dtor, |
1186 | | spl_dllist_it_valid, |
1187 | | spl_dllist_it_get_current_data, |
1188 | | spl_dllist_it_get_current_key, |
1189 | | spl_dllist_it_move_forward, |
1190 | | spl_dllist_it_rewind, |
1191 | | NULL, |
1192 | | NULL, /* get_gc */ |
1193 | | }; /* }}} */ |
1194 | | |
1195 | | static zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */ |
1196 | 0 | { |
1197 | 0 | spl_dllist_object *dllist_object = Z_SPLDLLIST_P(object); |
1198 | |
|
1199 | 0 | if (by_ref) { |
1200 | 0 | zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); |
1201 | 0 | return NULL; |
1202 | 0 | } |
1203 | | |
1204 | 0 | spl_dllist_it *iterator = emalloc(sizeof(spl_dllist_it)); |
1205 | |
|
1206 | 0 | zend_iterator_init(&iterator->intern); |
1207 | |
|
1208 | 0 | ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object)); |
1209 | 0 | iterator->intern.funcs = &spl_dllist_it_funcs; |
1210 | 0 | iterator->traverse_position = dllist_object->traverse_position; |
1211 | 0 | iterator->traverse_pointer = dllist_object->traverse_pointer; |
1212 | 0 | iterator->flags = dllist_object->flags & SPL_DLLIST_IT_MASK; |
1213 | |
|
1214 | 0 | SPL_LLIST_CHECK_ADDREF(iterator->traverse_pointer); |
1215 | |
|
1216 | 0 | return &iterator->intern; |
1217 | 0 | } |
1218 | | /* }}} */ |
1219 | | |
1220 | | PHP_MINIT_FUNCTION(spl_dllist) /* {{{ */ |
1221 | 16 | { |
1222 | 16 | spl_ce_SplDoublyLinkedList = register_class_SplDoublyLinkedList( |
1223 | 16 | zend_ce_iterator, zend_ce_countable, zend_ce_arrayaccess, zend_ce_serializable |
1224 | 16 | ); |
1225 | 16 | spl_ce_SplDoublyLinkedList->create_object = spl_dllist_object_new; |
1226 | 16 | spl_ce_SplDoublyLinkedList->default_object_handlers = &spl_handler_SplDoublyLinkedList; |
1227 | 16 | spl_ce_SplDoublyLinkedList->get_iterator = spl_dllist_get_iterator; |
1228 | | |
1229 | 16 | memcpy(&spl_handler_SplDoublyLinkedList, &std_object_handlers, sizeof(zend_object_handlers)); |
1230 | | |
1231 | 16 | spl_handler_SplDoublyLinkedList.offset = XtOffsetOf(spl_dllist_object, std); |
1232 | 16 | spl_handler_SplDoublyLinkedList.clone_obj = spl_dllist_object_clone; |
1233 | 16 | spl_handler_SplDoublyLinkedList.count_elements = spl_dllist_object_count_elements; |
1234 | 16 | spl_handler_SplDoublyLinkedList.get_gc = spl_dllist_object_get_gc; |
1235 | 16 | spl_handler_SplDoublyLinkedList.free_obj = spl_dllist_object_free_storage; |
1236 | | |
1237 | 16 | spl_ce_SplQueue = register_class_SplQueue(spl_ce_SplDoublyLinkedList); |
1238 | 16 | spl_ce_SplQueue->create_object = spl_dllist_object_new; |
1239 | 16 | spl_ce_SplQueue->get_iterator = spl_dllist_get_iterator; |
1240 | | |
1241 | 16 | spl_ce_SplStack = register_class_SplStack(spl_ce_SplDoublyLinkedList); |
1242 | 16 | spl_ce_SplStack->create_object = spl_dllist_object_new; |
1243 | 16 | spl_ce_SplStack->get_iterator = spl_dllist_get_iterator; |
1244 | | |
1245 | 16 | return SUCCESS; |
1246 | 16 | } |
1247 | | /* }}} */ |