php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
spl_dllist.c
Go to the documentation of this file.
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
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
33static zend_object_handlers spl_handler_SplDoublyLinkedList;
37
38#define SPL_LLIST_RC(elem) Z_EXTRA((elem)->data)
39
40#define SPL_LLIST_DELREF(elem) if (!--SPL_LLIST_RC(elem)) { \
41 efree(elem); \
42}
43
44#define SPL_LLIST_CHECK_DELREF_EX(elem, on_free) if ((elem) && !--SPL_LLIST_RC(elem)) { \
45 efree(elem); \
46 on_free \
47}
48
49#define SPL_LLIST_CHECK_DELREF(elem) SPL_LLIST_CHECK_DELREF_EX(elem, ;)
50
51#define SPL_LLIST_ADDREF(elem) SPL_LLIST_RC(elem)++
52#define SPL_LLIST_CHECK_ADDREF(elem) if (elem) SPL_LLIST_RC(elem)++
53
54#ifdef accept
55#undef accept
56#endif
57
63
69
72
86
87/* define an overloaded iterator structure */
94
95static inline spl_dllist_object *spl_dllist_from_obj(zend_object *obj) /* {{{ */ {
96 return (spl_dllist_object*)((char*)(obj) - XtOffsetOf(spl_dllist_object, std));
97}
98/* }}} */
99
100#define Z_SPLDLLIST_P(zv) spl_dllist_from_obj(Z_OBJ_P((zv)))
101
102static spl_ptr_llist *spl_ptr_llist_init(void) /* {{{ */
103{
104 spl_ptr_llist *llist = emalloc(sizeof(spl_ptr_llist));
105
106 llist->head = NULL;
107 llist->tail = NULL;
108 llist->count = 0;
109
110 return llist;
111}
112/* }}} */
113
114static zend_long spl_ptr_llist_count(spl_ptr_llist *llist) /* {{{ */
115{
116 return (zend_long)llist->count;
117}
118/* }}} */
119
120static void spl_ptr_llist_destroy(spl_ptr_llist *llist) /* {{{ */
121{
123
124 while (current) {
125 next = current->next;
126 zval_ptr_dtor(&current->data);
128 current = next;
129 }
130
131 efree(llist);
132}
133/* }}} */
134
135static spl_ptr_llist_element *spl_ptr_llist_offset(spl_ptr_llist *llist, zend_long offset, int backward) /* {{{ */
136{
137
139 int pos = 0;
140
141 if (backward) {
142 current = llist->tail;
143 } else {
144 current = llist->head;
145 }
146
147 while (current && pos < offset) {
148 pos++;
149 if (backward) {
150 current = current->prev;
151 } else {
152 current = current->next;
153 }
154 }
155
156 return current;
157}
158/* }}} */
159
160static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */
161{
163
164 elem->prev = NULL;
165 elem->next = llist->head;
166 ZVAL_COPY(&elem->data, data);
167 SPL_LLIST_RC(elem) = 1;
168
169 if (llist->head) {
170 llist->head->prev = elem;
171 } else {
172 llist->tail = elem;
173 }
174
175 llist->head = elem;
176 llist->count++;
177}
178/* }}} */
179
180static void spl_ptr_llist_push(spl_ptr_llist *llist, zval *data) /* {{{ */
181{
183
184 elem->prev = llist->tail;
185 elem->next = NULL;
186 ZVAL_COPY(&elem->data, data);
187 SPL_LLIST_RC(elem) = 1;
188
189 if (llist->tail) {
190 llist->tail->next = elem;
191 } else {
192 llist->head = elem;
193 }
194
195 llist->tail = elem;
196 llist->count++;
197}
198/* }}} */
199
200static void spl_ptr_llist_pop(spl_ptr_llist *llist, zval *ret) /* {{{ */
201{
203
204 if (tail == NULL) {
206 return;
207 }
208
209 if (tail->prev) {
210 tail->prev->next = NULL;
211 } else {
212 llist->head = NULL;
213 }
214
215 llist->tail = tail->prev;
216 llist->count--;
217 ZVAL_COPY_VALUE(ret, &tail->data);
218 ZVAL_UNDEF(&tail->data);
219
220 tail->prev = NULL;
221
223}
224/* }}} */
225
226static zval *spl_ptr_llist_last(spl_ptr_llist *llist) /* {{{ */
227{
229
230 if (tail == NULL) {
231 return NULL;
232 } else {
233 return &tail->data;
234 }
235}
236/* }}} */
237
238static zval *spl_ptr_llist_first(spl_ptr_llist *llist) /* {{{ */
239{
241
242 if (head == NULL) {
243 return NULL;
244 } else {
245 return &head->data;
246 }
247}
248/* }}} */
249
250static void spl_ptr_llist_shift(spl_ptr_llist *llist, zval *ret) /* {{{ */
251{
253
254 if (head == NULL) {
256 return;
257 }
258
259 if (head->next) {
260 head->next->prev = NULL;
261 } else {
262 llist->tail = NULL;
263 }
264
265 llist->head = head->next;
266 llist->count--;
267 ZVAL_COPY_VALUE(ret, &head->data);
268 ZVAL_UNDEF(&head->data);
269
270 head->next = NULL;
271
273}
274/* }}} */
275
276static void spl_ptr_llist_copy(spl_ptr_llist *from, spl_ptr_llist *to) /* {{{ */
277{
279
280 while (current) {
281 next = current->next;
282 spl_ptr_llist_push(to, &current->data);
283 current = next;
284 }
285
286}
287/* }}} */
288
289/* }}} */
290
291static void spl_dllist_object_free_storage(zend_object *object) /* {{{ */
292{
293 spl_dllist_object *intern = spl_dllist_from_obj(object);
294 zval tmp;
295
296 zend_object_std_dtor(&intern->std);
297
298 if (intern->llist) {
299 while (intern->llist->count > 0) {
300 spl_ptr_llist_pop(intern->llist, &tmp);
301 zval_ptr_dtor(&tmp);
302 }
303 spl_ptr_llist_destroy(intern->llist);
304 }
306}
307/* }}} */
308
309static zend_object *spl_dllist_object_new_ex(zend_class_entry *class_type, zend_object *orig, int clone_orig) /* {{{ */
310{
311 spl_dllist_object *intern;
312 zend_class_entry *parent = class_type;
313 int inherited = 0;
314
315 intern = zend_object_alloc(sizeof(spl_dllist_object), parent);
316
317 zend_object_std_init(&intern->std, class_type);
318 object_properties_init(&intern->std, class_type);
319
320 intern->flags = 0;
321 intern->traverse_position = 0;
322
323 if (orig) {
324 spl_dllist_object *other = spl_dllist_from_obj(orig);
325 intern->ce_get_iterator = other->ce_get_iterator;
326
327 if (clone_orig) {
328 intern->llist = spl_ptr_llist_init();
329 spl_ptr_llist_copy(other->llist, intern->llist);
330 intern->traverse_pointer = intern->llist->head;
332 } else {
333 intern->llist = other->llist;
334 intern->traverse_pointer = intern->llist->head;
336 }
337
338 intern->flags = other->flags;
339 } else {
340 intern->llist = spl_ptr_llist_init();
341 intern->traverse_pointer = intern->llist->head;
343 }
344
345 while (parent) {
346 if (parent == spl_ce_SplStack) {
348 } else if (parent == spl_ce_SplQueue) {
349 intern->flags |= SPL_DLLIST_IT_FIX;
350 }
351
352 if (parent == spl_ce_SplDoublyLinkedList) {
353 break;
354 }
355
356 parent = parent->parent;
357 inherited = 1;
358 }
359
360 ZEND_ASSERT(parent);
361
362 if (inherited) {
363 intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
364 if (intern->fptr_offset_get->common.scope == parent) {
365 intern->fptr_offset_get = NULL;
366 }
367 intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
368 if (intern->fptr_offset_set->common.scope == parent) {
369 intern->fptr_offset_set = NULL;
370 }
371 intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
372 if (intern->fptr_offset_has->common.scope == parent) {
373 intern->fptr_offset_has = NULL;
374 }
375 intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);
376 if (intern->fptr_offset_del->common.scope == parent) {
377 intern->fptr_offset_del = NULL;
378 }
379 /* Find count() method */
380 intern->fptr_count = zend_hash_find_ptr(&class_type->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
381 if (intern->fptr_count->common.scope == parent) {
382 intern->fptr_count = NULL;
383 }
384 }
385
386 return &intern->std;
387}
388/* }}} */
389
390static zend_object *spl_dllist_object_new(zend_class_entry *class_type) /* {{{ */
391{
392 return spl_dllist_object_new_ex(class_type, NULL, 0);
393}
394/* }}} */
395
396static zend_object *spl_dllist_object_clone(zend_object *old_object) /* {{{ */
397{
398 zend_object *new_object = spl_dllist_object_new_ex(old_object->ce, old_object, 1);
399
400 zend_objects_clone_members(new_object, old_object);
401
402 return new_object;
403}
404/* }}} */
405
406static zend_result spl_dllist_object_count_elements(zend_object *object, zend_long *count) /* {{{ */
407{
408 spl_dllist_object *intern = spl_dllist_from_obj(object);
409
410 if (intern->fptr_count) {
411 zval rv;
412 zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
413 if (!Z_ISUNDEF(rv)) {
414 *count = zval_get_long(&rv);
416 return SUCCESS;
417 }
418 *count = 0;
419 return FAILURE;
420 }
421
422 *count = spl_ptr_llist_count(intern->llist);
423 return SUCCESS;
424}
425/* }}} */
426
427static inline HashTable* spl_dllist_object_get_debug_info(zend_object *obj) /* {{{{ */
428{
429 spl_dllist_object *intern = spl_dllist_from_obj(obj);
431 zval tmp, dllist_array;
432 HashTable *debug_info;
433 HashTable *properties = zend_std_get_properties_ex(&intern->std);
434
435 /* +2 As we are adding 2 additional key-entries */
436 debug_info = zend_new_array(zend_hash_num_elements(properties) + 2);
437 zend_hash_copy(debug_info, properties, (copy_ctor_func_t) zval_add_ref);
438
439 ZVAL_LONG(&tmp, intern->flags);
440 spl_set_private_debug_info_property(spl_ce_SplDoublyLinkedList, "flags", strlen("flags"), debug_info, &tmp);
441
442 array_init(&dllist_array);
443
444 zend_ulong index = 0;
445 while (current) {
447
448 add_index_zval(&dllist_array, index, &current->data);
449 if (Z_REFCOUNTED(current->data)) {
450 Z_ADDREF(current->data);
451 }
452 index++;
453
454 current = next;
455 }
456
457 spl_set_private_debug_info_property(spl_ce_SplDoublyLinkedList, "dllist", strlen("dllist"), debug_info, &dllist_array);
458
459 return debug_info;
460}
461/* }}}} */
462
463static HashTable *spl_dllist_object_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
464{
465 spl_dllist_object *intern = spl_dllist_from_obj(obj);
468
469 while (current) {
470 zend_get_gc_buffer_add_zval(gc_buffer, &current->data);
471 current = current->next;
472 }
473
474 zend_get_gc_buffer_use(gc_buffer, gc_data, gc_data_count);
475 return zend_std_get_properties(obj);
476}
477/* }}} */
478
479/* {{{ Push $value on the SplDoublyLinkedList */
481{
482 zval *value;
483 spl_dllist_object *intern;
484
487 }
488
489 intern = Z_SPLDLLIST_P(ZEND_THIS);
490 spl_ptr_llist_push(intern->llist, value);
491}
492/* }}} */
493
494/* {{{ Unshift $value on the SplDoublyLinkedList */
496{
497 zval *value;
498 spl_dllist_object *intern;
499
502 }
503
504 intern = Z_SPLDLLIST_P(ZEND_THIS);
505 spl_ptr_llist_unshift(intern->llist, value);
506}
507/* }}} */
508
509/* {{{ Pop an element out of the SplDoublyLinkedList */
511{
512 spl_dllist_object *intern;
513
516 }
517
518 intern = Z_SPLDLLIST_P(ZEND_THIS);
519 spl_ptr_llist_pop(intern->llist, return_value);
520
522 zend_throw_exception(spl_ce_RuntimeException, "Can't pop from an empty datastructure", 0);
524 }
525}
526/* }}} */
527
528/* {{{ Shift an element out of the SplDoublyLinkedList */
530{
531 spl_dllist_object *intern;
532
535 }
536
537 intern = Z_SPLDLLIST_P(ZEND_THIS);
538 spl_ptr_llist_shift(intern->llist, return_value);
539
541 zend_throw_exception(spl_ce_RuntimeException, "Can't shift from an empty datastructure", 0);
543 }
544}
545/* }}} */
546
547/* {{{ Peek at the top element of the SplDoublyLinkedList */
549{
550 zval *value;
551 spl_dllist_object *intern;
552
555 }
556
557 intern = Z_SPLDLLIST_P(ZEND_THIS);
558 value = spl_ptr_llist_last(intern->llist);
559
560 if (value == NULL || Z_ISUNDEF_P(value)) {
561 zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0);
563 }
564
566}
567/* }}} */
568
569/* {{{ Peek at the bottom element of the SplDoublyLinkedList */
571{
572 zval *value;
573 spl_dllist_object *intern;
574
577 }
578
579 intern = Z_SPLDLLIST_P(ZEND_THIS);
580 value = spl_ptr_llist_first(intern->llist);
581
582 if (value == NULL || Z_ISUNDEF_P(value)) {
583 zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0);
585 }
586
588}
589/* }}} */
590
591/* {{{ Return the number of elements in the datastructure. */
593{
596
599 }
600
601 count = spl_ptr_llist_count(intern->llist);
603}
604/* }}} */
605
606/* {{{ Return true if the SplDoublyLinkedList is empty. */
608{
610
613 }
614
615 spl_dllist_object_count_elements(Z_OBJ_P(ZEND_THIS), &count);
616 RETURN_BOOL(count == 0);
617}
618/* }}} */
619
620/* {{{ Set the mode of iteration */
622{
624 spl_dllist_object *intern;
625
628 }
629
630 intern = Z_SPLDLLIST_P(ZEND_THIS);
631
632 if ((intern->flags & SPL_DLLIST_IT_FIX)
633 && (intern->flags & SPL_DLLIST_IT_LIFO) != (value & SPL_DLLIST_IT_LIFO)) {
634 zend_throw_exception(spl_ce_RuntimeException, "Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen", 0);
636 }
637
638 intern->flags = (value & SPL_DLLIST_IT_MASK) | (intern->flags & SPL_DLLIST_IT_FIX);
639
640 RETURN_LONG(intern->flags);
641}
642/* }}} */
643
644/* {{{ Return the mode of iteration */
646{
647 spl_dllist_object *intern;
648
651 }
652
653 intern = Z_SPLDLLIST_P(ZEND_THIS);
654
655 RETURN_LONG(intern->flags);
656}
657/* }}} */
658
659/* {{{ Returns whether the requested $index exists. */
661{
662 spl_dllist_object *intern;
663 zend_long index;
664
665 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
667 }
668
669 intern = Z_SPLDLLIST_P(ZEND_THIS);
670
671 RETURN_BOOL(index >= 0 && index < intern->llist->count);
672} /* }}} */
673
674/* {{{ Returns the value at the specified $index. */
676{
677 zend_long index;
678 spl_dllist_object *intern;
679 spl_ptr_llist_element *element;
680
681 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
683 }
684
685 intern = Z_SPLDLLIST_P(ZEND_THIS);
686
687 if (index < 0 || index >= intern->llist->count) {
688 zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range");
690 }
691
692 element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
693 if (element == NULL) {
694 zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset");
696 }
697
698 RETURN_COPY_DEREF(&element->data);
699} /* }}} */
700
701/* {{{ Sets the value at the specified $index to $newval. */
703{
704 zend_long index;
705 bool index_is_null = 1;
706 zval *value;
707 spl_dllist_object *intern;
708
709 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l!z", &index, &index_is_null, &value) == FAILURE) {
711 }
712
713 intern = Z_SPLDLLIST_P(ZEND_THIS);
714
715 if (index_is_null) {
716 /* $obj[] = ... */
717 spl_ptr_llist_push(intern->llist, value);
718 } else {
719 /* $obj[$foo] = ... */
720 spl_ptr_llist_element *element;
721
722 if (index < 0 || index >= intern->llist->count) {
723 zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range");
725 }
726
727 element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
728
729 if (element != NULL) {
730 /* the element is replaced, delref the old one as in
731 * SplDoublyLinkedList::pop() */
733 ZVAL_COPY_VALUE(&garbage, &element->data);
734 ZVAL_COPY(&element->data, value);
736 } else {
738 zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset");
740 }
741 }
742} /* }}} */
743
744/* {{{ Unsets the value at the specified $index. */
746{
747 zend_long index;
748 spl_dllist_object *intern;
749 spl_ptr_llist_element *element;
750 spl_ptr_llist *llist;
751
752 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
754 }
755
756 intern = Z_SPLDLLIST_P(ZEND_THIS);
757 llist = intern->llist;
758
759 if (index < 0 || index >= intern->llist->count) {
760 zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range");
762 }
763
764 element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
765
766 if (element != NULL) {
767 /* connect the neightbors */
768 if (element->prev) {
769 element->prev->next = element->next;
770 }
771
772 if (element->next) {
773 element->next->prev = element->prev;
774 }
775
776 /* take care of head/tail */
777 if (element == llist->head) {
778 llist->head = element->next;
779 }
780
781 if (element == llist->tail) {
782 llist->tail = element->prev;
783 }
784
785 /* finally, delete the element */
786 llist->count--;
787
788 if (intern->traverse_pointer == element) {
789 SPL_LLIST_DELREF(element);
790 intern->traverse_pointer = NULL;
791 }
792
793 zval_ptr_dtor(&element->data);
794 ZVAL_UNDEF(&element->data);
795
796 SPL_LLIST_DELREF(element);
797 } else {
798 zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset");
800 }
801} /* }}} */
802
803static void spl_dllist_it_dtor(zend_object_iterator *iter) /* {{{ */
804{
805 spl_dllist_it *iterator = (spl_dllist_it *)iter;
806
808
809 zval_ptr_dtor(&iterator->intern.data);
810}
811/* }}} */
812
813static void spl_dllist_it_helper_rewind(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */
814{
815 SPL_LLIST_CHECK_DELREF(*traverse_pointer_ptr);
816
818 *traverse_position_ptr = llist->count-1;
819 *traverse_pointer_ptr = llist->tail;
820 } else {
821 *traverse_position_ptr = 0;
822 *traverse_pointer_ptr = llist->head;
823 }
824
825 SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
826}
827/* }}} */
828
829static void spl_dllist_it_helper_move_forward(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */
830{
831 if (*traverse_pointer_ptr) {
832 spl_ptr_llist_element *old = *traverse_pointer_ptr;
833
835 *traverse_pointer_ptr = old->prev;
836 (*traverse_position_ptr)--;
837
839 zval prev;
840 spl_ptr_llist_pop(llist, &prev);
841
843 }
844 } else {
845 *traverse_pointer_ptr = old->next;
846
848 zval prev;
849 spl_ptr_llist_shift(llist, &prev);
850
852 } else {
853 (*traverse_position_ptr)++;
854 }
855 }
856
857 SPL_LLIST_DELREF(old);
858 SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
859 }
860}
861/* }}} */
862
863static void spl_dllist_it_rewind(zend_object_iterator *iter) /* {{{ */
864{
865 spl_dllist_it *iterator = (spl_dllist_it *)iter;
866 spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
867 spl_ptr_llist *llist = object->llist;
868
869 spl_dllist_it_helper_rewind(&iterator->traverse_pointer, &iterator->traverse_position, llist, iterator->flags);
870}
871/* }}} */
872
873static zend_result spl_dllist_it_valid(zend_object_iterator *iter) /* {{{ */
874{
875 spl_dllist_it *iterator = (spl_dllist_it *)iter;
876 spl_ptr_llist_element *element = iterator->traverse_pointer;
877
878 return (element != NULL ? SUCCESS : FAILURE);
879}
880/* }}} */
881
882static zval *spl_dllist_it_get_current_data(zend_object_iterator *iter) /* {{{ */
883{
884 spl_dllist_it *iterator = (spl_dllist_it *)iter;
885 spl_ptr_llist_element *element = iterator->traverse_pointer;
886
887 if (element == NULL || Z_ISUNDEF(element->data)) {
888 return NULL;
889 }
890
891 return &element->data;
892}
893/* }}} */
894
895static void spl_dllist_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
896{
897 spl_dllist_it *iterator = (spl_dllist_it *)iter;
898
899 ZVAL_LONG(key, iterator->traverse_position);
900}
901/* }}} */
902
903static void spl_dllist_it_move_forward(zend_object_iterator *iter) /* {{{ */
904{
905 spl_dllist_it *iterator = (spl_dllist_it *)iter;
906 spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
907
908 spl_dllist_it_helper_move_forward(&iterator->traverse_pointer, &iterator->traverse_position, object->llist, iterator->flags);
909}
910/* }}} */
911
912/* {{{ Return current array key */
923/* }}} */
924
925/* {{{ Move to next entry */
927{
929
932 }
933
934 spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags ^ SPL_DLLIST_IT_LIFO);
935}
936/* }}} */
937
938/* {{{ Move to next entry */
940{
942
945 }
946
947 spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
948}
949/* }}} */
950
951/* {{{ Check whether the datastructure contains more entries */
962/* }}} */
963
964/* {{{ Rewind the datastructure back to the start */
966{
968
971 }
972
973 spl_dllist_it_helper_rewind(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
974}
975/* }}} */
976
977/* {{{ Return current datastructure entry */
979{
981 spl_ptr_llist_element *element = intern->traverse_pointer;
982
985 }
986
987 if (element == NULL || Z_ISUNDEF(element->data)) {
988 RETURN_NULL();
989 } else {
990 RETURN_COPY_DEREF(&element->data);
991 }
992}
993/* }}} */
994
995/* {{{ Serializes storage */
997{
999 smart_str buf = {0};
1001 zval flags;
1003
1005 RETURN_THROWS();
1006 }
1007
1009
1010 /* flags */
1011 ZVAL_LONG(&flags, intern->flags);
1013
1014 /* elements */
1015 while (current) {
1016 smart_str_appendc(&buf, ':');
1017 next = current->next;
1018
1020
1022
1024
1025 current = next;
1026 }
1027
1028 /* done */
1030
1031 RETURN_STR(smart_str_extract(&buf));
1032} /* }}} */
1033
1034/* {{{ Unserializes storage */
1036{
1038 zval *flags, *elem;
1039 char *buf;
1040 size_t buf_len;
1041 const unsigned char *p, *s;
1043
1044 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1045 RETURN_THROWS();
1046 }
1047
1048 if (buf_len == 0) {
1049 return;
1050 }
1051
1052 while (intern->llist->count > 0) {
1053 zval tmp;
1054 spl_ptr_llist_pop(intern->llist, &tmp);
1055 zval_ptr_dtor(&tmp);
1056 }
1057
1058 s = p = (const unsigned char*)buf;
1060
1061 /* flags */
1063 if (!php_var_unserialize(flags, &p, s + buf_len, &var_hash) || Z_TYPE_P(flags) != IS_LONG) {
1064 goto error;
1065 }
1066
1067 intern->flags = (int)Z_LVAL_P(flags);
1068
1069 /* elements */
1070 while(*p == ':') {
1071 ++p;
1072 elem = var_tmp_var(&var_hash);
1073 if (!php_var_unserialize(elem, &p, s + buf_len, &var_hash)) {
1074 goto error;
1075 }
1076 var_push_dtor(&var_hash, elem);
1077
1078 spl_ptr_llist_push(intern->llist, elem);
1079 }
1080
1081 if (*p != '\0') {
1082 goto error;
1083 }
1084
1086 return;
1087
1088error:
1090 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len);
1091 RETURN_THROWS();
1092
1093} /* }}} */
1094
1095/* {{{ */
1097{
1100 zval tmp;
1101
1103 RETURN_THROWS();
1104 }
1105
1107
1108 /* flags */
1109 ZVAL_LONG(&tmp, intern->flags);
1111
1112 /* elements */
1113 array_init_size(&tmp, intern->llist->count);
1114 while (current) {
1116 Z_TRY_ADDREF(current->data);
1117 current = current->next;
1118 }
1120
1121 /* members */
1123 zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1125} /* }}} */
1126
1127/* {{{ */
1130 HashTable *data;
1131 zval *flags_zv, *storage_zv, *members_zv, *elem;
1132
1134 RETURN_THROWS();
1135 }
1136
1137 flags_zv = zend_hash_index_find(data, 0);
1138 storage_zv = zend_hash_index_find(data, 1);
1139 members_zv = zend_hash_index_find(data, 2);
1140 if (!flags_zv || !storage_zv || !members_zv ||
1141 Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(storage_zv) != IS_ARRAY ||
1142 Z_TYPE_P(members_zv) != IS_ARRAY) {
1144 "Incomplete or ill-typed serialization data", 0);
1145 RETURN_THROWS();
1146 }
1147
1148 intern->flags = (int) Z_LVAL_P(flags_zv);
1149
1150 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), elem) {
1151 spl_ptr_llist_push(intern->llist, elem);
1153
1154 object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1155} /* }}} */
1156
1157/* {{{ Inserts a new entry before the specified $index consisting of $newval. */
1159{
1160 zval *value;
1161 spl_dllist_object *intern;
1162 spl_ptr_llist_element *element;
1163 zend_long index;
1164
1165 if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz", &index, &value) == FAILURE) {
1166 RETURN_THROWS();
1167 }
1168
1169 intern = Z_SPLDLLIST_P(ZEND_THIS);
1170
1171 if (index < 0 || index > intern->llist->count) {
1172 zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range");
1173 RETURN_THROWS();
1174 }
1175
1176 if (index == intern->llist->count) {
1177 /* If index is the last entry+1 then we do a push because we're not inserting before any entry */
1178 spl_ptr_llist_push(intern->llist, value);
1179 } else {
1180 /* Create the new element we want to insert */
1182
1183 /* Get the element we want to insert before */
1184 element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
1185 ZEND_ASSERT(element != NULL);
1186
1187 ZVAL_COPY(&elem->data, value);
1188 SPL_LLIST_RC(elem) = 1;
1189 /* connect to the neighbours */
1190 elem->next = element;
1191 elem->prev = element->prev;
1192
1193 /* connect the neighbours to this new element */
1194 if (elem->prev == NULL) {
1195 intern->llist->head = elem;
1196 } else {
1197 element->prev->next = elem;
1198 }
1199 element->prev = elem;
1200
1201 intern->llist->count++;
1202 }
1203} /* }}} */
1204
1205/* {{{ */
1207{
1209 RETURN_THROWS();
1210 }
1211
1212 RETURN_ARR(spl_dllist_object_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1213} /* }}} */
1214
1215/* {{{ iterator handler table */
1216static const zend_object_iterator_funcs spl_dllist_it_funcs = {
1217 spl_dllist_it_dtor,
1218 spl_dllist_it_valid,
1219 spl_dllist_it_get_current_data,
1220 spl_dllist_it_get_current_key,
1221 spl_dllist_it_move_forward,
1222 spl_dllist_it_rewind,
1223 NULL,
1224 NULL, /* get_gc */
1225}; /* }}} */
1226
1227static zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1228{
1229 spl_dllist_object *dllist_object = Z_SPLDLLIST_P(object);
1230
1231 if (by_ref) {
1232 zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1233 return NULL;
1234 }
1235
1236 spl_dllist_it *iterator = emalloc(sizeof(spl_dllist_it));
1237
1238 zend_iterator_init(&iterator->intern);
1239
1240 ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
1241 iterator->intern.funcs = &spl_dllist_it_funcs;
1242 iterator->traverse_position = dllist_object->traverse_position;
1243 iterator->traverse_pointer = dllist_object->traverse_pointer;
1244 iterator->flags = dllist_object->flags & SPL_DLLIST_IT_MASK;
1245
1247
1248 return &iterator->intern;
1249}
1250/* }}} */
1251
1252PHP_MINIT_FUNCTION(spl_dllist) /* {{{ */
1253{
1254 spl_ce_SplDoublyLinkedList = register_class_SplDoublyLinkedList(
1256 );
1257 spl_ce_SplDoublyLinkedList->create_object = spl_dllist_object_new;
1258 spl_ce_SplDoublyLinkedList->default_object_handlers = &spl_handler_SplDoublyLinkedList;
1259 spl_ce_SplDoublyLinkedList->get_iterator = spl_dllist_get_iterator;
1260
1261 memcpy(&spl_handler_SplDoublyLinkedList, &std_object_handlers, sizeof(zend_object_handlers));
1262
1263 spl_handler_SplDoublyLinkedList.offset = XtOffsetOf(spl_dllist_object, std);
1264 spl_handler_SplDoublyLinkedList.clone_obj = spl_dllist_object_clone;
1265 spl_handler_SplDoublyLinkedList.count_elements = spl_dllist_object_count_elements;
1266 spl_handler_SplDoublyLinkedList.get_gc = spl_dllist_object_get_gc;
1267 spl_handler_SplDoublyLinkedList.free_obj = spl_dllist_object_free_storage;
1268
1269 spl_ce_SplQueue = register_class_SplQueue(spl_ce_SplDoublyLinkedList);
1270 spl_ce_SplQueue->create_object = spl_dllist_object_new;
1271 spl_ce_SplQueue->get_iterator = spl_dllist_get_iterator;
1272
1273 spl_ce_SplStack = register_class_SplStack(spl_ce_SplDoublyLinkedList);
1274 spl_ce_SplStack->create_object = spl_dllist_object_new;
1275 spl_ce_SplStack->get_iterator = spl_dllist_get_iterator;
1276
1277 return SUCCESS;
1278}
1279/* }}} */
rewind($stream)
prev(array|object &$array)
count(Countable|array $value, int $mode=COUNT_NORMAL)
char s[4]
Definition cdf.c:77
error($message)
Definition ext_skel.php:22
memcpy(ptr1, ptr2, size)
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
zend_long offset
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
#define next(ls)
Definition minilua.c:2661
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_METHOD
Definition php.h:365
#define PHPAPI
Definition php.h:71
#define add(i, ts)
unsigned const char * pos
Definition php_ffi.h:52
struct php_pcntl_pending_signal * head
Definition php_pcntl.h:47
struct php_pcntl_pending_signal * tail
Definition php_pcntl.h:47
unsigned char key[REFLECTION_KEY_LEN]
#define PHP_VAR_UNSERIALIZE_DESTROY(d)
Definition php_var.h:59
struct php_unserialize_data * php_unserialize_data_t
Definition php_var.h:32
struct php_serialize_data * php_serialize_data_t
Definition php_var.h:31
#define PHP_VAR_UNSERIALIZE_INIT(d)
Definition php_var.h:56
PHPAPI zval * var_tmp_var(php_unserialize_data_t *var_hashx)
PHPAPI int php_var_unserialize(zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash)
PHPAPI void var_push_dtor(php_unserialize_data_t *var_hash, zval *val)
#define PHP_VAR_SERIALIZE_INIT(d)
Definition php_var.h:50
PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data)
Definition var.c:1319
#define PHP_VAR_SERIALIZE_DESTROY(d)
Definition php_var.h:53
zend_constant * data
original_stack top
zval * current
Definition session.c:1024
zval rv
Definition session.c:1024
p
Definition session.c:1105
php_unserialize_data_t var_hash
Definition session.c:964
struct _spl_dllist_object spl_dllist_object
Definition spl_dllist.c:70
struct _spl_dllist_it spl_dllist_it
Definition spl_dllist.c:71
#define SPL_LLIST_CHECK_DELREF_EX(elem, on_free)
Definition spl_dllist.c:44
#define Z_SPLDLLIST_P(zv)
Definition spl_dllist.c:100
struct _spl_ptr_llist spl_ptr_llist
#define SPL_LLIST_RC(elem)
Definition spl_dllist.c:38
PHPAPI zend_class_entry * spl_ce_SplStack
Definition spl_dllist.c:36
struct _spl_ptr_llist_element spl_ptr_llist_element
#define SPL_LLIST_DELREF(elem)
Definition spl_dllist.c:40
PHPAPI zend_class_entry * spl_ce_SplDoublyLinkedList
Definition spl_dllist.c:34
PHPAPI zend_class_entry * spl_ce_SplQueue
Definition spl_dllist.c:35
#define SPL_LLIST_CHECK_DELREF(elem)
Definition spl_dllist.c:49
#define SPL_LLIST_CHECK_ADDREF(elem)
Definition spl_dllist.c:52
#define SPL_DLLIST_IT_FIX
Definition spl_dllist.h:27
#define SPL_DLLIST_IT_LIFO
Definition spl_dllist.h:25
#define SPL_DLLIST_IT_DELETE
Definition spl_dllist.h:24
#define SPL_DLLIST_IT_MASK
Definition spl_dllist.h:26
PHPAPI zend_class_entry * spl_ce_RuntimeException
PHPAPI zend_class_entry * spl_ce_UnexpectedValueException
PHPAPI zend_class_entry * spl_ce_OutOfRangeException
void spl_set_private_debug_info_property(const zend_class_entry *ce, const char *property, size_t property_len, HashTable *debug_info, zval *value)
zend_object_iterator intern
Definition spl_dllist.c:89
int traverse_position
Definition spl_dllist.c:91
spl_ptr_llist_element * traverse_pointer
Definition spl_dllist.c:90
zend_object std
Definition spl_dllist.c:84
zend_function * fptr_offset_del
Definition spl_dllist.c:81
zend_function * fptr_offset_set
Definition spl_dllist.c:79
zend_class_entry * ce_get_iterator
Definition spl_dllist.c:83
zend_function * fptr_offset_get
Definition spl_dllist.c:78
spl_ptr_llist * llist
Definition spl_dllist.c:74
zend_function * fptr_count
Definition spl_dllist.c:82
spl_ptr_llist_element * traverse_pointer
Definition spl_dllist.c:75
zend_function * fptr_offset_has
Definition spl_dllist.c:80
struct _spl_ptr_llist_element * prev
Definition spl_dllist.c:59
struct _spl_ptr_llist_element * next
Definition spl_dllist.c:60
spl_ptr_llist_element * tail
Definition spl_dllist.c:66
spl_ptr_llist_element * head
Definition spl_dllist.c:65
HashTable function_table
Definition zend.h:163
const zend_object_iterator_funcs * funcs
zend_class_entry * ce
Definition zend_types.h:560
zend_class_entry * scope
struct _zend_function::@236135173067030250234125302313220025134003177336 common
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format,...)
Definition zend_API.c:413
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
Definition zend_API.c:1300
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
Definition zend_API.c:1688
ZEND_API void object_properties_load(zend_object *object, HashTable *properties)
Definition zend_API.c:1728
#define RETURN_COPY_DEREF(zv)
Definition zend_API.h:1056
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_NULL()
Definition zend_API.h:1036
#define array_init_size(arg, size)
Definition zend_API.h:538
#define RETURN_ARR(r)
Definition zend_API.h:1050
#define zend_parse_parameters_none()
Definition zend_API.h:353
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_BOOL(b)
Definition zend_API.h:1035
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETURN_STR(s)
Definition zend_API.h:1039
#define ZEND_THIS
Definition zend_API.h:523
#define array_init(arg)
Definition zend_API.h:537
#define efree(ptr)
Definition zend_alloc.h:155
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
strlen(string $string)
ZEND_API ZEND_COLD zend_object * zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code)
ZEND_API ZEND_COLD zend_object * zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format,...)
union _zend_function zend_function
ZEND_API zend_get_gc_buffer * zend_get_gc_buffer_create(void)
Definition zend_gc.c:2130
ZEND_API HashTable *ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, bool always_duplicate)
Definition zend_hash.c:3387
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
Definition zend_hash.c:1224
ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor)
Definition zend_hash.c:2240
ZEND_API zval *ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h)
Definition zend_hash.c:2701
#define zend_new_array(size)
Definition zend_hash.h:338
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
#define ZEND_HASH_FOREACH_VAL(ht, _val)
Definition zend_hash.h:1102
ZEND_API zend_class_entry * zend_ce_countable
ZEND_API zend_class_entry * zend_ce_iterator
ZEND_API zend_class_entry * zend_ce_serializable
ZEND_API zend_class_entry * zend_ce_arrayaccess
ZEND_API void zend_iterator_init(zend_object_iterator *iter)
struct _zend_object_iterator zend_object_iterator
struct _zend_object_iterator_funcs zend_object_iterator_funcs
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
ZEND_API HashTable * zend_std_get_properties(zend_object *zobj)
ZEND_API const zend_object_handlers std_object_handlers
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
#define XtOffsetOf(s_type, field)
#define ZEND_ASSERT(c)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define ZSTR_KNOWN(idx)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define ZVAL_UNDEF(z)
#define Z_ISUNDEF_P(zval_p)
Definition zend_types.h:957
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
#define ZVAL_LONG(z, l)
struct _zend_array HashTable
Definition zend_types.h:386
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define IS_ARRAY
Definition zend_types.h:607
#define Z_ISUNDEF(zval)
Definition zend_types.h:956
#define Z_REFCOUNTED(zval)
Definition zend_types.h:917
@ FAILURE
Definition zend_types.h:61
#define Z_TRY_ADDREF(z)
#define Z_ADDREF(z)
#define IS_LONG
Definition zend_types.h:604
#define ZVAL_ARR(z, a)
void(* copy_ctor_func_t)(zval *pElement)
Definition zend_types.h:108
#define ZVAL_COPY(z, v)
#define ZVAL_OBJ_COPY(z, o)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
struct _zend_object_handlers zend_object_handlers
Definition zend_types.h:88
#define Z_ARRVAL(zval)
Definition zend_types.h:986
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
#define ZVAL_COPY_VALUE(z, v)
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
ZEND_API void zval_add_ref(zval *p)
zval * return_value
object
zend_refcounted * garbage
zval * ret
value