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