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