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