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