xref: /php-src/ext/spl/spl_dllist.c (revision 80941042)
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 
431 	if (!intern->std.properties) {
432 		rebuild_object_properties(&intern->std);
433 	}
434 
435 	/* +2 As we are adding 2 additional key-entries */
436 	debug_info = zend_new_array(zend_hash_num_elements(intern->std.properties) + 2);
437 	zend_hash_copy(debug_info, intern->std.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) {
446 		spl_ptr_llist_element *next = current->next;
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 
spl_dllist_object_get_gc(zend_object * obj,zval ** gc_data,int * gc_data_count)463 static 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);
466 	zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
467 	spl_ptr_llist_element *current = intern->llist->head;
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 */
PHP_METHOD(SplDoublyLinkedList,push)480 PHP_METHOD(SplDoublyLinkedList, push)
481 {
482 	zval *value;
483 	spl_dllist_object *intern;
484 
485 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
486 		RETURN_THROWS();
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 */
PHP_METHOD(SplDoublyLinkedList,unshift)495 PHP_METHOD(SplDoublyLinkedList, unshift)
496 {
497 	zval *value;
498 	spl_dllist_object *intern;
499 
500 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
501 		RETURN_THROWS();
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 */
PHP_METHOD(SplDoublyLinkedList,pop)510 PHP_METHOD(SplDoublyLinkedList, pop)
511 {
512 	spl_dllist_object *intern;
513 
514 	if (zend_parse_parameters_none() == FAILURE) {
515 		RETURN_THROWS();
516 	}
517 
518 	intern = Z_SPLDLLIST_P(ZEND_THIS);
519 	spl_ptr_llist_pop(intern->llist, return_value);
520 
521 	if (Z_ISUNDEF_P(return_value)) {
522 		zend_throw_exception(spl_ce_RuntimeException, "Can't pop from an empty datastructure", 0);
523 		RETURN_THROWS();
524 	}
525 }
526 /* }}} */
527 
528 /* {{{ Shift an element out of the SplDoublyLinkedList */
PHP_METHOD(SplDoublyLinkedList,shift)529 PHP_METHOD(SplDoublyLinkedList, shift)
530 {
531 	spl_dllist_object *intern;
532 
533 	if (zend_parse_parameters_none() == FAILURE) {
534 		RETURN_THROWS();
535 	}
536 
537 	intern = Z_SPLDLLIST_P(ZEND_THIS);
538 	spl_ptr_llist_shift(intern->llist, return_value);
539 
540 	if (Z_ISUNDEF_P(return_value)) {
541 		zend_throw_exception(spl_ce_RuntimeException, "Can't shift from an empty datastructure", 0);
542 		RETURN_THROWS();
543 	}
544 }
545 /* }}} */
546 
547 /* {{{ Peek at the top element of the SplDoublyLinkedList */
PHP_METHOD(SplDoublyLinkedList,top)548 PHP_METHOD(SplDoublyLinkedList, top)
549 {
550 	zval *value;
551 	spl_dllist_object *intern;
552 
553 	if (zend_parse_parameters_none() == FAILURE) {
554 		RETURN_THROWS();
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);
562 		RETURN_THROWS();
563 	}
564 
565 	RETURN_COPY_DEREF(value);
566 }
567 /* }}} */
568 
569 /* {{{ Peek at the bottom element of the SplDoublyLinkedList */
PHP_METHOD(SplDoublyLinkedList,bottom)570 PHP_METHOD(SplDoublyLinkedList, bottom)
571 {
572 	zval *value;
573 	spl_dllist_object *intern;
574 
575 	if (zend_parse_parameters_none() == FAILURE) {
576 		RETURN_THROWS();
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);
584 		RETURN_THROWS();
585 	}
586 
587 	RETURN_COPY_DEREF(value);
588 }
589 /* }}} */
590 
591 /* {{{ Return the number of elements in the datastructure. */
PHP_METHOD(SplDoublyLinkedList,count)592 PHP_METHOD(SplDoublyLinkedList, count)
593 {
594 	zend_long count;
595 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
596 
597 	if (zend_parse_parameters_none() == FAILURE) {
598 		RETURN_THROWS();
599 	}
600 
601 	count = spl_ptr_llist_count(intern->llist);
602 	RETURN_LONG(count);
603 }
604 /* }}} */
605 
606 /* {{{ Return true if the SplDoublyLinkedList is empty. */
PHP_METHOD(SplDoublyLinkedList,isEmpty)607 PHP_METHOD(SplDoublyLinkedList, isEmpty)
608 {
609 	zend_long count;
610 
611 	if (zend_parse_parameters_none() == FAILURE) {
612 		RETURN_THROWS();
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 */
PHP_METHOD(SplDoublyLinkedList,setIteratorMode)621 PHP_METHOD(SplDoublyLinkedList, setIteratorMode)
622 {
623 	zend_long value;
624 	spl_dllist_object *intern;
625 
626 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &value) == FAILURE) {
627 		RETURN_THROWS();
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);
635 		RETURN_THROWS();
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 */
PHP_METHOD(SplDoublyLinkedList,getIteratorMode)645 PHP_METHOD(SplDoublyLinkedList, getIteratorMode)
646 {
647 	spl_dllist_object *intern;
648 
649 	if (zend_parse_parameters_none() == FAILURE) {
650 		RETURN_THROWS();
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. */
PHP_METHOD(SplDoublyLinkedList,offsetExists)660 PHP_METHOD(SplDoublyLinkedList, offsetExists)
661 {
662 	spl_dllist_object *intern;
663 	zend_long index;
664 
665 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
666 		RETURN_THROWS();
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. */
PHP_METHOD(SplDoublyLinkedList,offsetGet)675 PHP_METHOD(SplDoublyLinkedList, offsetGet)
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) {
682 		RETURN_THROWS();
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");
689 		RETURN_THROWS();
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");
695 		RETURN_THROWS();
696 	}
697 
698 	RETURN_COPY_DEREF(&element->data);
699 } /* }}} */
700 
701 /* {{{ Sets the value at the specified $index to $newval. */
PHP_METHOD(SplDoublyLinkedList,offsetSet)702 PHP_METHOD(SplDoublyLinkedList, offsetSet)
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) {
710 		RETURN_THROWS();
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");
724 			RETURN_THROWS();
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() */
732 			zval_ptr_dtor(&element->data);
733 			ZVAL_COPY(&element->data, value);
734 		} else {
735 			zval_ptr_dtor(value);
736 			zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset");
737 			RETURN_THROWS();
738 		}
739 	}
740 } /* }}} */
741 
742 /* {{{ Unsets the value at the specified $index. */
PHP_METHOD(SplDoublyLinkedList,offsetUnset)743 PHP_METHOD(SplDoublyLinkedList, offsetUnset)
744 {
745 	zend_long             index;
746 	spl_dllist_object     *intern;
747 	spl_ptr_llist_element *element;
748 	spl_ptr_llist         *llist;
749 
750 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
751 		RETURN_THROWS();
752 	}
753 
754 	intern = Z_SPLDLLIST_P(ZEND_THIS);
755 	llist  = intern->llist;
756 
757 	if (index < 0 || index >= intern->llist->count) {
758 		zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range");
759 		RETURN_THROWS();
760 	}
761 
762 	element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
763 
764 	if (element != NULL) {
765 		/* connect the neightbors */
766 		if (element->prev) {
767 			element->prev->next = element->next;
768 		}
769 
770 		if (element->next) {
771 			element->next->prev = element->prev;
772 		}
773 
774 		/* take care of head/tail */
775 		if (element == llist->head) {
776 			llist->head = element->next;
777 		}
778 
779 		if (element == llist->tail) {
780 			llist->tail = element->prev;
781 		}
782 
783 		/* finally, delete the element */
784 		llist->count--;
785 
786 		if (intern->traverse_pointer == element) {
787 			SPL_LLIST_DELREF(element);
788 			intern->traverse_pointer = NULL;
789 		}
790 
791 		zval_ptr_dtor(&element->data);
792 		ZVAL_UNDEF(&element->data);
793 
794 		SPL_LLIST_DELREF(element);
795 	} else {
796 		zend_argument_error(spl_ce_OutOfRangeException, 1, "is an invalid offset");
797 		RETURN_THROWS();
798 	}
799 } /* }}} */
800 
spl_dllist_it_dtor(zend_object_iterator * iter)801 static void spl_dllist_it_dtor(zend_object_iterator *iter) /* {{{ */
802 {
803 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
804 
805 	SPL_LLIST_CHECK_DELREF(iterator->traverse_pointer);
806 
807 	zval_ptr_dtor(&iterator->intern.data);
808 }
809 /* }}} */
810 
spl_dllist_it_helper_rewind(spl_ptr_llist_element ** traverse_pointer_ptr,int * traverse_position_ptr,spl_ptr_llist * llist,int flags)811 static void spl_dllist_it_helper_rewind(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */
812 {
813 	SPL_LLIST_CHECK_DELREF(*traverse_pointer_ptr);
814 
815 	if (flags & SPL_DLLIST_IT_LIFO) {
816 		*traverse_position_ptr = llist->count-1;
817 		*traverse_pointer_ptr  = llist->tail;
818 	} else {
819 		*traverse_position_ptr = 0;
820 		*traverse_pointer_ptr  = llist->head;
821 	}
822 
823 	SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
824 }
825 /* }}} */
826 
spl_dllist_it_helper_move_forward(spl_ptr_llist_element ** traverse_pointer_ptr,int * traverse_position_ptr,spl_ptr_llist * llist,int flags)827 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) /* {{{ */
828 {
829 	if (*traverse_pointer_ptr) {
830 		spl_ptr_llist_element *old = *traverse_pointer_ptr;
831 
832 		if (flags & SPL_DLLIST_IT_LIFO) {
833 			*traverse_pointer_ptr = old->prev;
834 			(*traverse_position_ptr)--;
835 
836 			if (flags & SPL_DLLIST_IT_DELETE) {
837 				zval prev;
838 				spl_ptr_llist_pop(llist, &prev);
839 
840 				zval_ptr_dtor(&prev);
841 			}
842 		} else {
843 			*traverse_pointer_ptr = old->next;
844 
845 			if (flags & SPL_DLLIST_IT_DELETE) {
846 				zval prev;
847 				spl_ptr_llist_shift(llist, &prev);
848 
849 				zval_ptr_dtor(&prev);
850 			} else {
851 				(*traverse_position_ptr)++;
852 			}
853 		}
854 
855 		SPL_LLIST_DELREF(old);
856 		SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
857 	}
858 }
859 /* }}} */
860 
spl_dllist_it_rewind(zend_object_iterator * iter)861 static void spl_dllist_it_rewind(zend_object_iterator *iter) /* {{{ */
862 {
863 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
864 	spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
865 	spl_ptr_llist *llist = object->llist;
866 
867 	spl_dllist_it_helper_rewind(&iterator->traverse_pointer, &iterator->traverse_position, llist, iterator->flags);
868 }
869 /* }}} */
870 
spl_dllist_it_valid(zend_object_iterator * iter)871 static zend_result spl_dllist_it_valid(zend_object_iterator *iter) /* {{{ */
872 {
873 	spl_dllist_it         *iterator = (spl_dllist_it *)iter;
874 	spl_ptr_llist_element *element  = iterator->traverse_pointer;
875 
876 	return (element != NULL ? SUCCESS : FAILURE);
877 }
878 /* }}} */
879 
spl_dllist_it_get_current_data(zend_object_iterator * iter)880 static zval *spl_dllist_it_get_current_data(zend_object_iterator *iter) /* {{{ */
881 {
882 	spl_dllist_it         *iterator = (spl_dllist_it *)iter;
883 	spl_ptr_llist_element *element  = iterator->traverse_pointer;
884 
885 	if (element == NULL || Z_ISUNDEF(element->data)) {
886 		return NULL;
887 	}
888 
889 	return &element->data;
890 }
891 /* }}} */
892 
spl_dllist_it_get_current_key(zend_object_iterator * iter,zval * key)893 static void spl_dllist_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
894 {
895 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
896 
897 	ZVAL_LONG(key, iterator->traverse_position);
898 }
899 /* }}} */
900 
spl_dllist_it_move_forward(zend_object_iterator * iter)901 static void spl_dllist_it_move_forward(zend_object_iterator *iter) /* {{{ */
902 {
903 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
904 	spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
905 
906 	spl_dllist_it_helper_move_forward(&iterator->traverse_pointer, &iterator->traverse_position, object->llist, iterator->flags);
907 }
908 /* }}} */
909 
910 /* {{{ Return current array key */
PHP_METHOD(SplDoublyLinkedList,key)911 PHP_METHOD(SplDoublyLinkedList, key)
912 {
913 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
914 
915 	if (zend_parse_parameters_none() == FAILURE) {
916 		RETURN_THROWS();
917 	}
918 
919 	RETURN_LONG(intern->traverse_position);
920 }
921 /* }}} */
922 
923 /* {{{ Move to next entry */
PHP_METHOD(SplDoublyLinkedList,prev)924 PHP_METHOD(SplDoublyLinkedList, prev)
925 {
926 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
927 
928 	if (zend_parse_parameters_none() == FAILURE) {
929 		RETURN_THROWS();
930 	}
931 
932 	spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags ^ SPL_DLLIST_IT_LIFO);
933 }
934 /* }}} */
935 
936 /* {{{ Move to next entry */
PHP_METHOD(SplDoublyLinkedList,next)937 PHP_METHOD(SplDoublyLinkedList, next)
938 {
939 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
940 
941 	if (zend_parse_parameters_none() == FAILURE) {
942 		RETURN_THROWS();
943 	}
944 
945 	spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
946 }
947 /* }}} */
948 
949 /* {{{ Check whether the datastructure contains more entries */
PHP_METHOD(SplDoublyLinkedList,valid)950 PHP_METHOD(SplDoublyLinkedList, valid)
951 {
952 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
953 
954 	if (zend_parse_parameters_none() == FAILURE) {
955 		RETURN_THROWS();
956 	}
957 
958 	RETURN_BOOL(intern->traverse_pointer != NULL);
959 }
960 /* }}} */
961 
962 /* {{{ Rewind the datastructure back to the start */
PHP_METHOD(SplDoublyLinkedList,rewind)963 PHP_METHOD(SplDoublyLinkedList, rewind)
964 {
965 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
966 
967 	if (zend_parse_parameters_none() == FAILURE) {
968 		RETURN_THROWS();
969 	}
970 
971 	spl_dllist_it_helper_rewind(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
972 }
973 /* }}} */
974 
975 /* {{{ Return current datastructure entry */
PHP_METHOD(SplDoublyLinkedList,current)976 PHP_METHOD(SplDoublyLinkedList, current)
977 {
978 	spl_dllist_object     *intern  = Z_SPLDLLIST_P(ZEND_THIS);
979 	spl_ptr_llist_element *element = intern->traverse_pointer;
980 
981 	if (zend_parse_parameters_none() == FAILURE) {
982 		RETURN_THROWS();
983 	}
984 
985 	if (element == NULL || Z_ISUNDEF(element->data)) {
986 		RETURN_NULL();
987 	} else {
988 		RETURN_COPY_DEREF(&element->data);
989 	}
990 }
991 /* }}} */
992 
993 /* {{{ Serializes storage */
PHP_METHOD(SplDoublyLinkedList,serialize)994 PHP_METHOD(SplDoublyLinkedList, serialize)
995 {
996 	spl_dllist_object     *intern   = Z_SPLDLLIST_P(ZEND_THIS);
997 	smart_str              buf      = {0};
998 	spl_ptr_llist_element *current  = intern->llist->head, *next;
999 	zval                   flags;
1000 	php_serialize_data_t   var_hash;
1001 
1002 	if (zend_parse_parameters_none() == FAILURE) {
1003 		RETURN_THROWS();
1004 	}
1005 
1006 	PHP_VAR_SERIALIZE_INIT(var_hash);
1007 
1008 	/* flags */
1009 	ZVAL_LONG(&flags, intern->flags);
1010 	php_var_serialize(&buf, &flags, &var_hash);
1011 
1012 	/* elements */
1013 	while (current) {
1014 		smart_str_appendc(&buf, ':');
1015 		next = current->next;
1016 
1017 		php_var_serialize(&buf, &current->data, &var_hash);
1018 
1019 		current = next;
1020 	}
1021 
1022 	/* done */
1023 	PHP_VAR_SERIALIZE_DESTROY(var_hash);
1024 
1025 	RETURN_STR(smart_str_extract(&buf));
1026 } /* }}} */
1027 
1028 /* {{{ Unserializes storage */
PHP_METHOD(SplDoublyLinkedList,unserialize)1029 PHP_METHOD(SplDoublyLinkedList, unserialize)
1030 {
1031 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1032 	zval *flags, *elem;
1033 	char *buf;
1034 	size_t buf_len;
1035 	const unsigned char *p, *s;
1036 	php_unserialize_data_t var_hash;
1037 
1038 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1039 		RETURN_THROWS();
1040 	}
1041 
1042 	if (buf_len == 0) {
1043 		return;
1044 	}
1045 
1046 	while (intern->llist->count > 0) {
1047 		zval tmp;
1048 		spl_ptr_llist_pop(intern->llist, &tmp);
1049 		zval_ptr_dtor(&tmp);
1050 	}
1051 
1052 	s = p = (const unsigned char*)buf;
1053 	PHP_VAR_UNSERIALIZE_INIT(var_hash);
1054 
1055 	/* flags */
1056 	flags = var_tmp_var(&var_hash);
1057 	if (!php_var_unserialize(flags, &p, s + buf_len, &var_hash) || Z_TYPE_P(flags) != IS_LONG) {
1058 		goto error;
1059 	}
1060 
1061 	intern->flags = (int)Z_LVAL_P(flags);
1062 
1063 	/* elements */
1064 	while(*p == ':') {
1065 		++p;
1066 		elem = var_tmp_var(&var_hash);
1067 		if (!php_var_unserialize(elem, &p, s + buf_len, &var_hash)) {
1068 			goto error;
1069 		}
1070 		var_push_dtor(&var_hash, elem);
1071 
1072 		spl_ptr_llist_push(intern->llist, elem);
1073 	}
1074 
1075 	if (*p != '\0') {
1076 		goto error;
1077 	}
1078 
1079 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1080 	return;
1081 
1082 error:
1083 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1084 	zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len);
1085 	RETURN_THROWS();
1086 
1087 } /* }}} */
1088 
1089 /* {{{ */
PHP_METHOD(SplDoublyLinkedList,__serialize)1090 PHP_METHOD(SplDoublyLinkedList, __serialize)
1091 {
1092 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1093 	spl_ptr_llist_element *current = intern->llist->head;
1094 	zval tmp;
1095 
1096 	if (zend_parse_parameters_none() == FAILURE) {
1097 		RETURN_THROWS();
1098 	}
1099 
1100 	array_init(return_value);
1101 
1102 	/* flags */
1103 	ZVAL_LONG(&tmp, intern->flags);
1104 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1105 
1106 	/* elements */
1107 	array_init_size(&tmp, intern->llist->count);
1108 	while (current) {
1109 		zend_hash_next_index_insert(Z_ARRVAL(tmp), &current->data);
1110 		Z_TRY_ADDREF(current->data);
1111 		current = current->next;
1112 	}
1113 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1114 
1115 	/* members */
1116 	ZVAL_ARR(&tmp, zend_proptable_to_symtable(
1117 		zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
1118 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1119 } /* }}} */
1120 
1121 /* {{{ */
PHP_METHOD(SplDoublyLinkedList,__unserialize)1122 PHP_METHOD(SplDoublyLinkedList, __unserialize) {
1123 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1124 	HashTable *data;
1125 	zval *flags_zv, *storage_zv, *members_zv, *elem;
1126 
1127 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1128 		RETURN_THROWS();
1129 	}
1130 
1131 	flags_zv = zend_hash_index_find(data, 0);
1132 	storage_zv = zend_hash_index_find(data, 1);
1133 	members_zv = zend_hash_index_find(data, 2);
1134 	if (!flags_zv || !storage_zv || !members_zv ||
1135 			Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(storage_zv) != IS_ARRAY ||
1136 			Z_TYPE_P(members_zv) != IS_ARRAY) {
1137 		zend_throw_exception(spl_ce_UnexpectedValueException,
1138 			"Incomplete or ill-typed serialization data", 0);
1139 		RETURN_THROWS();
1140 	}
1141 
1142 	intern->flags = (int) Z_LVAL_P(flags_zv);
1143 
1144 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), elem) {
1145 		spl_ptr_llist_push(intern->llist, elem);
1146 	} ZEND_HASH_FOREACH_END();
1147 
1148 	object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1149 } /* }}} */
1150 
1151 /* {{{ Inserts a new entry before the specified $index consisting of $newval. */
PHP_METHOD(SplDoublyLinkedList,add)1152 PHP_METHOD(SplDoublyLinkedList, add)
1153 {
1154 	zval                  *value;
1155 	spl_dllist_object     *intern;
1156 	spl_ptr_llist_element *element;
1157 	zend_long                  index;
1158 
1159 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz", &index, &value) == FAILURE) {
1160 		RETURN_THROWS();
1161 	}
1162 
1163 	intern = Z_SPLDLLIST_P(ZEND_THIS);
1164 
1165 	if (index < 0 || index > intern->llist->count) {
1166 		zend_argument_error(spl_ce_OutOfRangeException, 1, "is out of range");
1167 		RETURN_THROWS();
1168 	}
1169 
1170 	if (index == intern->llist->count) {
1171 		/* If index is the last entry+1 then we do a push because we're not inserting before any entry */
1172 		spl_ptr_llist_push(intern->llist, value);
1173 	} else {
1174 		/* Create the new element we want to insert */
1175 		spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element));
1176 
1177 		/* Get the element we want to insert before */
1178 		element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
1179 		ZEND_ASSERT(element != NULL);
1180 
1181 		ZVAL_COPY(&elem->data, value);
1182 		SPL_LLIST_RC(elem) = 1;
1183 		/* connect to the neighbours */
1184 		elem->next = element;
1185 		elem->prev = element->prev;
1186 
1187 		/* connect the neighbours to this new element */
1188 		if (elem->prev == NULL) {
1189 			intern->llist->head = elem;
1190 		} else {
1191 			element->prev->next = elem;
1192 		}
1193 		element->prev = elem;
1194 
1195 		intern->llist->count++;
1196 	}
1197 } /* }}} */
1198 
1199 /* {{{ */
PHP_METHOD(SplDoublyLinkedList,__debugInfo)1200 PHP_METHOD(SplDoublyLinkedList, __debugInfo)
1201 {
1202 	if (zend_parse_parameters_none() == FAILURE) {
1203 		RETURN_THROWS();
1204 	}
1205 
1206 	RETURN_ARR(spl_dllist_object_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1207 } /* }}} */
1208 
1209 /* {{{ iterator handler table */
1210 static const zend_object_iterator_funcs spl_dllist_it_funcs = {
1211 	spl_dllist_it_dtor,
1212 	spl_dllist_it_valid,
1213 	spl_dllist_it_get_current_data,
1214 	spl_dllist_it_get_current_key,
1215 	spl_dllist_it_move_forward,
1216 	spl_dllist_it_rewind,
1217 	NULL,
1218 	NULL, /* get_gc */
1219 }; /* }}} */
1220 
spl_dllist_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1221 static zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1222 {
1223 	spl_dllist_object *dllist_object = Z_SPLDLLIST_P(object);
1224 
1225 	if (by_ref) {
1226 		zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1227 		return NULL;
1228 	}
1229 
1230 	spl_dllist_it *iterator = emalloc(sizeof(spl_dllist_it));
1231 
1232 	zend_iterator_init(&iterator->intern);
1233 
1234 	ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
1235 	iterator->intern.funcs       = &spl_dllist_it_funcs;
1236 	iterator->traverse_position  = dllist_object->traverse_position;
1237 	iterator->traverse_pointer   = dllist_object->traverse_pointer;
1238 	iterator->flags              = dllist_object->flags & SPL_DLLIST_IT_MASK;
1239 
1240 	SPL_LLIST_CHECK_ADDREF(iterator->traverse_pointer);
1241 
1242 	return &iterator->intern;
1243 }
1244 /* }}} */
1245 
PHP_MINIT_FUNCTION(spl_dllist)1246 PHP_MINIT_FUNCTION(spl_dllist) /* {{{ */
1247 {
1248 	spl_ce_SplDoublyLinkedList = register_class_SplDoublyLinkedList(
1249 		zend_ce_iterator, zend_ce_countable, zend_ce_arrayaccess, zend_ce_serializable
1250 	);
1251 	spl_ce_SplDoublyLinkedList->create_object = spl_dllist_object_new;
1252 	spl_ce_SplDoublyLinkedList->default_object_handlers = &spl_handler_SplDoublyLinkedList;
1253 	spl_ce_SplDoublyLinkedList->get_iterator = spl_dllist_get_iterator;
1254 
1255 	memcpy(&spl_handler_SplDoublyLinkedList, &std_object_handlers, sizeof(zend_object_handlers));
1256 
1257 	spl_handler_SplDoublyLinkedList.offset = XtOffsetOf(spl_dllist_object, std);
1258 	spl_handler_SplDoublyLinkedList.clone_obj = spl_dllist_object_clone;
1259 	spl_handler_SplDoublyLinkedList.count_elements = spl_dllist_object_count_elements;
1260 	spl_handler_SplDoublyLinkedList.get_gc = spl_dllist_object_get_gc;
1261 	spl_handler_SplDoublyLinkedList.free_obj = spl_dllist_object_free_storage;
1262 
1263 	spl_ce_SplQueue = register_class_SplQueue(spl_ce_SplDoublyLinkedList);
1264 	spl_ce_SplQueue->create_object = spl_dllist_object_new;
1265 	spl_ce_SplQueue->get_iterator = spl_dllist_get_iterator;
1266 
1267 	spl_ce_SplStack = register_class_SplStack(spl_ce_SplDoublyLinkedList);
1268 	spl_ce_SplStack->create_object = spl_dllist_object_new;
1269 	spl_ce_SplStack->get_iterator = spl_dllist_get_iterator;
1270 
1271 	return SUCCESS;
1272 }
1273 /* }}} */
1274