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