xref: /PHP-7.4/ext/spl/spl_dllist.c (revision 22a077b6)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Etienne Kneuss <colder@php.net>                             |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "zend_exceptions.h"
25 #include "zend_hash.h"
26 
27 #include "php_spl.h"
28 #include "ext/standard/info.h"
29 #include "ext/standard/php_var.h"
30 #include "zend_smart_str.h"
31 #include "spl_functions.h"
32 #include "spl_engine.h"
33 #include "spl_iterators.h"
34 #include "spl_dllist.h"
35 #include "spl_exceptions.h"
36 
37 zend_object_handlers spl_handler_SplDoublyLinkedList;
38 PHPAPI zend_class_entry  *spl_ce_SplDoublyLinkedList;
39 PHPAPI zend_class_entry  *spl_ce_SplQueue;
40 PHPAPI zend_class_entry  *spl_ce_SplStack;
41 
42 #define SPL_LLIST_DELREF(elem) if(!--(elem)->rc) { \
43 	efree(elem); \
44 }
45 
46 #define SPL_LLIST_CHECK_DELREF(elem) if((elem) && !--(elem)->rc) { \
47 	efree(elem); \
48 }
49 
50 #define SPL_LLIST_ADDREF(elem) (elem)->rc++
51 #define SPL_LLIST_CHECK_ADDREF(elem) if(elem) (elem)->rc++
52 
53 #define SPL_DLLIST_IT_DELETE 0x00000001 /* Delete flag makes the iterator delete the current element on next */
54 #define SPL_DLLIST_IT_LIFO   0x00000002 /* LIFO flag makes the iterator traverse the structure as a LastInFirstOut */
55 #define SPL_DLLIST_IT_MASK   0x00000003 /* Mask to isolate flags related to iterators */
56 #define SPL_DLLIST_IT_FIX    0x00000004 /* Backward/Forward bit is fixed */
57 
58 #ifdef accept
59 #undef accept
60 #endif
61 
62 typedef struct _spl_ptr_llist_element {
63 	struct _spl_ptr_llist_element *prev;
64 	struct _spl_ptr_llist_element *next;
65 	int                            rc;
66 	zval                           data;
67 } spl_ptr_llist_element;
68 
69 typedef void (*spl_ptr_llist_dtor_func)(spl_ptr_llist_element *);
70 typedef void (*spl_ptr_llist_ctor_func)(spl_ptr_llist_element *);
71 
72 typedef struct _spl_ptr_llist {
73 	spl_ptr_llist_element   *head;
74 	spl_ptr_llist_element   *tail;
75 	spl_ptr_llist_dtor_func  dtor;
76 	spl_ptr_llist_ctor_func  ctor;
77 	int count;
78 } spl_ptr_llist;
79 
80 typedef struct _spl_dllist_object spl_dllist_object;
81 typedef struct _spl_dllist_it spl_dllist_it;
82 
83 struct _spl_dllist_object {
84 	spl_ptr_llist         *llist;
85 	int                    traverse_position;
86 	spl_ptr_llist_element *traverse_pointer;
87 	int                    flags;
88 	zend_function         *fptr_offset_get;
89 	zend_function         *fptr_offset_set;
90 	zend_function         *fptr_offset_has;
91 	zend_function         *fptr_offset_del;
92 	zend_function         *fptr_count;
93 	zend_class_entry      *ce_get_iterator;
94 	zval                  *gc_data;
95 	int                    gc_data_count;
96 	zend_object            std;
97 };
98 
99 /* define an overloaded iterator structure */
100 struct _spl_dllist_it {
101 	zend_user_iterator     intern;
102 	spl_ptr_llist_element *traverse_pointer;
103 	int                    traverse_position;
104 	int                    flags;
105 };
106 
spl_dllist_from_obj(zend_object * obj)107 static inline spl_dllist_object *spl_dllist_from_obj(zend_object *obj) /* {{{ */ {
108 	return (spl_dllist_object*)((char*)(obj) - XtOffsetOf(spl_dllist_object, std));
109 }
110 /* }}} */
111 
112 #define Z_SPLDLLIST_P(zv)  spl_dllist_from_obj(Z_OBJ_P((zv)))
113 
114 /* {{{  spl_ptr_llist */
spl_ptr_llist_zval_dtor(spl_ptr_llist_element * elem)115 static void spl_ptr_llist_zval_dtor(spl_ptr_llist_element *elem) { /* {{{ */
116 	if (!Z_ISUNDEF(elem->data)) {
117 		zval_ptr_dtor(&elem->data);
118 		ZVAL_UNDEF(&elem->data);
119 	}
120 }
121 /* }}} */
122 
spl_ptr_llist_zval_ctor(spl_ptr_llist_element * elem)123 static void spl_ptr_llist_zval_ctor(spl_ptr_llist_element *elem) { /* {{{ */
124 	if (Z_REFCOUNTED(elem->data)) {
125 		Z_ADDREF(elem->data);
126 	}
127 }
128 /* }}} */
129 
spl_ptr_llist_init(spl_ptr_llist_ctor_func ctor,spl_ptr_llist_dtor_func dtor)130 static spl_ptr_llist *spl_ptr_llist_init(spl_ptr_llist_ctor_func ctor, spl_ptr_llist_dtor_func dtor) /* {{{ */
131 {
132 	spl_ptr_llist *llist = emalloc(sizeof(spl_ptr_llist));
133 
134 	llist->head  = NULL;
135 	llist->tail  = NULL;
136 	llist->count = 0;
137 	llist->dtor  = dtor;
138 	llist->ctor  = ctor;
139 
140 	return llist;
141 }
142 /* }}} */
143 
spl_ptr_llist_count(spl_ptr_llist * llist)144 static zend_long spl_ptr_llist_count(spl_ptr_llist *llist) /* {{{ */
145 {
146 	return (zend_long)llist->count;
147 }
148 /* }}} */
149 
spl_ptr_llist_destroy(spl_ptr_llist * llist)150 static void spl_ptr_llist_destroy(spl_ptr_llist *llist) /* {{{ */
151 {
152 	spl_ptr_llist_element   *current = llist->head, *next;
153 	spl_ptr_llist_dtor_func  dtor    = llist->dtor;
154 
155 	while (current) {
156 		next = current->next;
157 		if (dtor) {
158 			dtor(current);
159 		}
160 		SPL_LLIST_DELREF(current);
161 		current = next;
162 	}
163 
164 	efree(llist);
165 }
166 /* }}} */
167 
spl_ptr_llist_offset(spl_ptr_llist * llist,zend_long offset,int backward)168 static spl_ptr_llist_element *spl_ptr_llist_offset(spl_ptr_llist *llist, zend_long offset, int backward) /* {{{ */
169 {
170 
171 	spl_ptr_llist_element *current;
172 	int pos = 0;
173 
174 	if (backward) {
175 		current = llist->tail;
176 	} else {
177 		current = llist->head;
178 	}
179 
180 	while (current && pos < offset) {
181 		pos++;
182 		if (backward) {
183 			current = current->prev;
184 		} else {
185 			current = current->next;
186 		}
187 	}
188 
189 	return current;
190 }
191 /* }}} */
192 
spl_ptr_llist_unshift(spl_ptr_llist * llist,zval * data)193 static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */
194 {
195 	spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element));
196 
197 	elem->rc   = 1;
198 	elem->prev = NULL;
199 	elem->next = llist->head;
200 	ZVAL_COPY_VALUE(&elem->data, data);
201 
202 	if (llist->head) {
203 		llist->head->prev = elem;
204 	} else {
205 		llist->tail = elem;
206 	}
207 
208 	llist->head = elem;
209 	llist->count++;
210 
211 	if (llist->ctor) {
212 		llist->ctor(elem);
213 	}
214 }
215 /* }}} */
216 
spl_ptr_llist_push(spl_ptr_llist * llist,zval * data)217 static void spl_ptr_llist_push(spl_ptr_llist *llist, zval *data) /* {{{ */
218 {
219 	spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element));
220 
221 	elem->rc   = 1;
222 	elem->prev = llist->tail;
223 	elem->next = NULL;
224 	ZVAL_COPY_VALUE(&elem->data, data);
225 
226 	if (llist->tail) {
227 		llist->tail->next = elem;
228 	} else {
229 		llist->head = elem;
230 	}
231 
232 	llist->tail = elem;
233 	llist->count++;
234 
235 	if (llist->ctor) {
236 		llist->ctor(elem);
237 	}
238 }
239 /* }}} */
240 
spl_ptr_llist_pop(spl_ptr_llist * llist,zval * ret)241 static void spl_ptr_llist_pop(spl_ptr_llist *llist, zval *ret) /* {{{ */
242 {
243 	spl_ptr_llist_element    *tail = llist->tail;
244 
245 	if (tail == NULL) {
246 		ZVAL_UNDEF(ret);
247 		return;
248 	}
249 
250 	if (tail->prev) {
251 		tail->prev->next = NULL;
252 	} else {
253 		llist->head = NULL;
254 	}
255 
256 	llist->tail = tail->prev;
257 	llist->count--;
258 	ZVAL_COPY(ret, &tail->data);
259 
260 	tail->prev = NULL;
261 	if (llist->dtor) {
262 		llist->dtor(tail);
263 	}
264 
265 	ZVAL_UNDEF(&tail->data);
266 
267 	SPL_LLIST_DELREF(tail);
268 }
269 /* }}} */
270 
spl_ptr_llist_last(spl_ptr_llist * llist)271 static zval *spl_ptr_llist_last(spl_ptr_llist *llist) /* {{{ */
272 {
273 	spl_ptr_llist_element *tail = llist->tail;
274 
275 	if (tail == NULL) {
276 		return NULL;
277 	} else {
278 		return &tail->data;
279 	}
280 }
281 /* }}} */
282 
spl_ptr_llist_first(spl_ptr_llist * llist)283 static zval *spl_ptr_llist_first(spl_ptr_llist *llist) /* {{{ */
284 {
285 	spl_ptr_llist_element *head = llist->head;
286 
287 	if (head == NULL) {
288 		return NULL;
289 	} else {
290 		return &head->data;
291 	}
292 }
293 /* }}} */
294 
spl_ptr_llist_shift(spl_ptr_llist * llist,zval * ret)295 static void spl_ptr_llist_shift(spl_ptr_llist *llist, zval *ret) /* {{{ */
296 {
297 	spl_ptr_llist_element   *head = llist->head;
298 
299 	if (head == NULL) {
300 		ZVAL_UNDEF(ret);
301 		return;
302 	}
303 
304 	if (head->next) {
305 		head->next->prev = NULL;
306 	} else {
307 		llist->tail = NULL;
308 	}
309 
310 	llist->head = head->next;
311 	llist->count--;
312 	ZVAL_COPY(ret, &head->data);
313 
314 	head->next = NULL;
315 	if (llist->dtor) {
316 		llist->dtor(head);
317 	}
318 	ZVAL_UNDEF(&head->data);
319 
320 	SPL_LLIST_DELREF(head);
321 }
322 /* }}} */
323 
spl_ptr_llist_copy(spl_ptr_llist * from,spl_ptr_llist * to)324 static void spl_ptr_llist_copy(spl_ptr_llist *from, spl_ptr_llist *to) /* {{{ */
325 {
326 	spl_ptr_llist_element *current = from->head, *next;
327 //???	spl_ptr_llist_ctor_func ctor = from->ctor;
328 
329 	while (current) {
330 		next = current->next;
331 
332 		/*??? FIXME
333 		if (ctor) {
334 			ctor(current);
335 		}
336 		*/
337 
338 		spl_ptr_llist_push(to, &current->data);
339 		current = next;
340 	}
341 
342 }
343 /* }}} */
344 
345 /* }}} */
346 
spl_dllist_object_free_storage(zend_object * object)347 static void spl_dllist_object_free_storage(zend_object *object) /* {{{ */
348 {
349 	spl_dllist_object *intern = spl_dllist_from_obj(object);
350 	zval tmp;
351 
352 	zend_object_std_dtor(&intern->std);
353 
354 	while (intern->llist->count > 0) {
355 		spl_ptr_llist_pop(intern->llist, &tmp);
356 		zval_ptr_dtor(&tmp);
357 	}
358 
359 	if (intern->gc_data != NULL) {
360 		efree(intern->gc_data);
361 	};
362 
363 	spl_ptr_llist_destroy(intern->llist);
364 	SPL_LLIST_CHECK_DELREF(intern->traverse_pointer);
365 }
366 /* }}} */
367 
368 zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
369 
spl_dllist_object_new_ex(zend_class_entry * class_type,zval * orig,int clone_orig)370 static zend_object *spl_dllist_object_new_ex(zend_class_entry *class_type, zval *orig, int clone_orig) /* {{{ */
371 {
372 	spl_dllist_object *intern;
373 	zend_class_entry  *parent = class_type;
374 	int                inherited = 0;
375 
376 	intern = zend_object_alloc(sizeof(spl_dllist_object), parent);
377 
378 	zend_object_std_init(&intern->std, class_type);
379 	object_properties_init(&intern->std, class_type);
380 
381 	intern->flags = 0;
382 	intern->traverse_position = 0;
383 
384 	if (orig) {
385 		spl_dllist_object *other = Z_SPLDLLIST_P(orig);
386 		intern->ce_get_iterator = other->ce_get_iterator;
387 
388 		if (clone_orig) {
389 			intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(other->llist->ctor, other->llist->dtor);
390 			spl_ptr_llist_copy(other->llist, intern->llist);
391 			intern->traverse_pointer  = intern->llist->head;
392 			SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
393 		} else {
394 			intern->llist = other->llist;
395 			intern->traverse_pointer  = intern->llist->head;
396 			SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
397 		}
398 
399 		intern->flags = other->flags;
400 	} else {
401 		intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(spl_ptr_llist_zval_ctor, spl_ptr_llist_zval_dtor);
402 		intern->traverse_pointer  = intern->llist->head;
403 		SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
404 	}
405 
406 	while (parent) {
407 		if (parent == spl_ce_SplStack) {
408 			intern->flags |= (SPL_DLLIST_IT_FIX | SPL_DLLIST_IT_LIFO);
409 			intern->std.handlers = &spl_handler_SplDoublyLinkedList;
410 		} else if (parent == spl_ce_SplQueue) {
411 			intern->flags |= SPL_DLLIST_IT_FIX;
412 			intern->std.handlers = &spl_handler_SplDoublyLinkedList;
413 		}
414 
415 		if (parent == spl_ce_SplDoublyLinkedList) {
416 			intern->std.handlers = &spl_handler_SplDoublyLinkedList;
417 			break;
418 		}
419 
420 		parent = parent->parent;
421 		inherited = 1;
422 	}
423 
424 	if (!parent) { /* this must never happen */
425 		php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of SplDoublyLinkedList");
426 	}
427 	if (inherited) {
428 		intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
429 		if (intern->fptr_offset_get->common.scope == parent) {
430 			intern->fptr_offset_get = NULL;
431 		}
432 		intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
433 		if (intern->fptr_offset_set->common.scope == parent) {
434 			intern->fptr_offset_set = NULL;
435 		}
436 		intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
437 		if (intern->fptr_offset_has->common.scope == parent) {
438 			intern->fptr_offset_has = NULL;
439 		}
440 		intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);
441 		if (intern->fptr_offset_del->common.scope == parent) {
442 			intern->fptr_offset_del = NULL;
443 		}
444 		intern->fptr_count = zend_hash_str_find_ptr(&class_type->function_table, "count", sizeof("count") - 1);
445 		if (intern->fptr_count->common.scope == parent) {
446 			intern->fptr_count = NULL;
447 		}
448 	}
449 
450 	return &intern->std;
451 }
452 /* }}} */
453 
spl_dllist_object_new(zend_class_entry * class_type)454 static zend_object *spl_dllist_object_new(zend_class_entry *class_type) /* {{{ */
455 {
456 	return spl_dllist_object_new_ex(class_type, NULL, 0);
457 }
458 /* }}} */
459 
spl_dllist_object_clone(zval * zobject)460 static zend_object *spl_dllist_object_clone(zval *zobject) /* {{{ */
461 {
462 	zend_object        *old_object;
463 	zend_object        *new_object;
464 
465 	old_object  = Z_OBJ_P(zobject);
466 	new_object = spl_dllist_object_new_ex(old_object->ce, zobject, 1);
467 
468 	zend_objects_clone_members(new_object, old_object);
469 
470 	return new_object;
471 }
472 /* }}} */
473 
spl_dllist_object_count_elements(zval * object,zend_long * count)474 static int spl_dllist_object_count_elements(zval *object, zend_long *count) /* {{{ */
475 {
476 	spl_dllist_object *intern = Z_SPLDLLIST_P(object);
477 
478 	if (intern->fptr_count) {
479 		zval rv;
480 		zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
481 		if (!Z_ISUNDEF(rv)) {
482 			*count = zval_get_long(&rv);
483 			zval_ptr_dtor(&rv);
484 			return SUCCESS;
485 		}
486 		*count = 0;
487 		return FAILURE;
488 	}
489 
490 	*count = spl_ptr_llist_count(intern->llist);
491 	return SUCCESS;
492 }
493 /* }}} */
494 
spl_dllist_object_get_debug_info(zval * obj)495 static inline HashTable* spl_dllist_object_get_debug_info(zval *obj) /* {{{{ */
496 {
497 	spl_dllist_object     *intern  = Z_SPLDLLIST_P(obj);
498 	spl_ptr_llist_element *current = intern->llist->head, *next;
499 	zval tmp, dllist_array;
500 	zend_string *pnstr;
501 	int  i = 0;
502 	HashTable *debug_info;
503 
504 	if (!intern->std.properties) {
505 		rebuild_object_properties(&intern->std);
506 	}
507 
508 	debug_info = zend_new_array(1);
509 	zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
510 
511 	pnstr = spl_gen_private_prop_name(spl_ce_SplDoublyLinkedList, "flags", sizeof("flags")-1);
512 	ZVAL_LONG(&tmp, intern->flags);
513 	zend_hash_add(debug_info, pnstr, &tmp);
514 	zend_string_release_ex(pnstr, 0);
515 
516 	array_init(&dllist_array);
517 
518 	while (current) {
519 		next = current->next;
520 
521 		add_index_zval(&dllist_array, i, &current->data);
522 		if (Z_REFCOUNTED(current->data)) {
523 			Z_ADDREF(current->data);
524 		}
525 		i++;
526 
527 		current = next;
528 	}
529 
530 	pnstr = spl_gen_private_prop_name(spl_ce_SplDoublyLinkedList, "dllist", sizeof("dllist")-1);
531 	zend_hash_add(debug_info, pnstr, &dllist_array);
532 	zend_string_release_ex(pnstr, 0);
533 
534 	return debug_info;
535 }
536 /* }}}} */
537 
spl_dllist_object_get_gc(zval * obj,zval ** gc_data,int * gc_data_count)538 static HashTable *spl_dllist_object_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
539 {
540 	spl_dllist_object *intern  = Z_SPLDLLIST_P(obj);
541 	spl_ptr_llist_element *current = intern->llist->head;
542 	int i = 0;
543 
544 	if (intern->gc_data_count < intern->llist->count) {
545 		intern->gc_data_count = intern->llist->count;
546 		intern->gc_data = safe_erealloc(intern->gc_data, intern->gc_data_count, sizeof(zval), 0);
547 	}
548 
549 	while (current) {
550 		ZVAL_COPY_VALUE(&intern->gc_data[i++], &current->data);
551 		current = current->next;
552 	}
553 
554 	*gc_data = intern->gc_data;
555 	*gc_data_count = i;
556 	return zend_std_get_properties(obj);
557 }
558 /* }}} */
559 
560 /* {{{ proto bool SplDoublyLinkedList::push(mixed value)
561 	   Push $value on the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,push)562 SPL_METHOD(SplDoublyLinkedList, push)
563 {
564 	zval *value;
565 	spl_dllist_object *intern;
566 
567 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
568 		return;
569 	}
570 
571 	intern = Z_SPLDLLIST_P(ZEND_THIS);
572 	spl_ptr_llist_push(intern->llist, value);
573 
574 	RETURN_TRUE;
575 }
576 /* }}} */
577 
578 /* {{{ proto bool SplDoublyLinkedList::unshift(mixed value)
579 	   Unshift $value on the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,unshift)580 SPL_METHOD(SplDoublyLinkedList, unshift)
581 {
582 	zval *value;
583 	spl_dllist_object *intern;
584 
585 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
586 		return;
587 	}
588 
589 	intern = Z_SPLDLLIST_P(ZEND_THIS);
590 	spl_ptr_llist_unshift(intern->llist, value);
591 
592 	RETURN_TRUE;
593 }
594 /* }}} */
595 
596 /* {{{ proto mixed SplDoublyLinkedList::pop()
597 	   Pop an element out of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,pop)598 SPL_METHOD(SplDoublyLinkedList, pop)
599 {
600 	spl_dllist_object *intern;
601 
602 	if (zend_parse_parameters_none() == FAILURE) {
603 		return;
604 	}
605 
606 	intern = Z_SPLDLLIST_P(ZEND_THIS);
607 	spl_ptr_llist_pop(intern->llist, return_value);
608 
609 	if (Z_ISUNDEF_P(return_value)) {
610 		zend_throw_exception(spl_ce_RuntimeException, "Can't pop from an empty datastructure", 0);
611 		RETURN_NULL();
612 	}
613 }
614 /* }}} */
615 
616 /* {{{ proto mixed SplDoublyLinkedList::shift()
617 	   Shift an element out of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,shift)618 SPL_METHOD(SplDoublyLinkedList, shift)
619 {
620 	spl_dllist_object *intern;
621 
622 	if (zend_parse_parameters_none() == FAILURE) {
623 		return;
624 	}
625 
626 	intern = Z_SPLDLLIST_P(ZEND_THIS);
627 	spl_ptr_llist_shift(intern->llist, return_value);
628 
629 	if (Z_ISUNDEF_P(return_value)) {
630 		zend_throw_exception(spl_ce_RuntimeException, "Can't shift from an empty datastructure", 0);
631 		RETURN_NULL();
632 	}
633 }
634 /* }}} */
635 
636 /* {{{ proto mixed SplDoublyLinkedList::top()
637 	   Peek at the top element of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,top)638 SPL_METHOD(SplDoublyLinkedList, top)
639 {
640 	zval *value;
641 	spl_dllist_object *intern;
642 
643 	if (zend_parse_parameters_none() == FAILURE) {
644 		return;
645 	}
646 
647 	intern = Z_SPLDLLIST_P(ZEND_THIS);
648 	value = spl_ptr_llist_last(intern->llist);
649 
650 	if (value == NULL || Z_ISUNDEF_P(value)) {
651 		zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0);
652 		return;
653 	}
654 
655 	ZVAL_COPY_DEREF(return_value, value);
656 }
657 /* }}} */
658 
659 /* {{{ proto mixed SplDoublyLinkedList::bottom()
660 	   Peek at the bottom element of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,bottom)661 SPL_METHOD(SplDoublyLinkedList, bottom)
662 {
663 	zval *value;
664 	spl_dllist_object *intern;
665 
666 	if (zend_parse_parameters_none() == FAILURE) {
667 		return;
668 	}
669 
670 	intern = Z_SPLDLLIST_P(ZEND_THIS);
671 	value  = spl_ptr_llist_first(intern->llist);
672 
673 	if (value == NULL || Z_ISUNDEF_P(value)) {
674 		zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0);
675 		return;
676 	}
677 
678 	ZVAL_COPY_DEREF(return_value, value);
679 }
680 /* }}} */
681 
682 /* {{{ proto int SplDoublyLinkedList::count()
683  Return the number of elements in the datastructure. */
SPL_METHOD(SplDoublyLinkedList,count)684 SPL_METHOD(SplDoublyLinkedList, count)
685 {
686 	zend_long count;
687 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
688 
689 	if (zend_parse_parameters_none() == FAILURE) {
690 		return;
691 	}
692 
693 	count = spl_ptr_llist_count(intern->llist);
694 	RETURN_LONG(count);
695 }
696 /* }}} */
697 
698 /* {{{ proto int SplDoublyLinkedList::isEmpty()
699  Return true if the SplDoublyLinkedList is empty. */
SPL_METHOD(SplDoublyLinkedList,isEmpty)700 SPL_METHOD(SplDoublyLinkedList, isEmpty)
701 {
702 	zend_long count;
703 
704 	if (zend_parse_parameters_none() == FAILURE) {
705 		return;
706 	}
707 
708 	spl_dllist_object_count_elements(ZEND_THIS, &count);
709 	RETURN_BOOL(count == 0);
710 }
711 /* }}} */
712 
713 /* {{{ proto int SplDoublyLinkedList::setIteratorMode(int mode)
714  Set the mode of iteration */
SPL_METHOD(SplDoublyLinkedList,setIteratorMode)715 SPL_METHOD(SplDoublyLinkedList, setIteratorMode)
716 {
717 	zend_long value;
718 	spl_dllist_object *intern;
719 
720 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &value) == FAILURE) {
721 		return;
722 	}
723 
724 	intern = Z_SPLDLLIST_P(ZEND_THIS);
725 
726 	if (intern->flags & SPL_DLLIST_IT_FIX
727 		&& (intern->flags & SPL_DLLIST_IT_LIFO) != (value & SPL_DLLIST_IT_LIFO)) {
728 		zend_throw_exception(spl_ce_RuntimeException, "Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen", 0);
729 		return;
730 	}
731 
732 	intern->flags = (value & SPL_DLLIST_IT_MASK) | (intern->flags & SPL_DLLIST_IT_FIX);
733 
734 	RETURN_LONG(intern->flags);
735 }
736 /* }}} */
737 
738 /* {{{ proto int SplDoublyLinkedList::getIteratorMode()
739  Return the mode of iteration */
SPL_METHOD(SplDoublyLinkedList,getIteratorMode)740 SPL_METHOD(SplDoublyLinkedList, getIteratorMode)
741 {
742 	spl_dllist_object *intern;
743 
744 	if (zend_parse_parameters_none() == FAILURE) {
745 		return;
746 	}
747 
748 	intern = Z_SPLDLLIST_P(ZEND_THIS);
749 
750 	RETURN_LONG(intern->flags);
751 }
752 /* }}} */
753 
754 /* {{{ proto bool SplDoublyLinkedList::offsetExists(mixed index)
755  Returns whether the requested $index exists. */
SPL_METHOD(SplDoublyLinkedList,offsetExists)756 SPL_METHOD(SplDoublyLinkedList, offsetExists)
757 {
758 	zval              *zindex;
759 	spl_dllist_object *intern;
760 	zend_long               index;
761 
762 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
763 		return;
764 	}
765 
766 	intern = Z_SPLDLLIST_P(ZEND_THIS);
767 	index  = spl_offset_convert_to_long(zindex);
768 
769 	RETURN_BOOL(index >= 0 && index < intern->llist->count);
770 } /* }}} */
771 
772 /* {{{ proto mixed SplDoublyLinkedList::offsetGet(mixed index)
773  Returns the value at the specified $index. */
SPL_METHOD(SplDoublyLinkedList,offsetGet)774 SPL_METHOD(SplDoublyLinkedList, offsetGet)
775 {
776 	zval                  *zindex;
777 	zend_long                   index;
778 	spl_dllist_object     *intern;
779 	spl_ptr_llist_element *element;
780 
781 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
782 		return;
783 	}
784 
785 	intern = Z_SPLDLLIST_P(ZEND_THIS);
786 	index  = spl_offset_convert_to_long(zindex);
787 
788 	if (index < 0 || index >= intern->llist->count) {
789 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid or out of range", 0);
790 		return;
791 	}
792 
793 	element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
794 
795 	if (element != NULL) {
796 		zval *value = &element->data;
797 
798 		ZVAL_COPY_DEREF(return_value, value);
799 	} else {
800 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
801 	}
802 } /* }}} */
803 
804 /* {{{ proto void SplDoublyLinkedList::offsetSet(mixed index, mixed newval)
805  Sets the value at the specified $index to $newval. */
SPL_METHOD(SplDoublyLinkedList,offsetSet)806 SPL_METHOD(SplDoublyLinkedList, offsetSet)
807 {
808 	zval                  *zindex, *value;
809 	spl_dllist_object     *intern;
810 
811 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
812 		return;
813 	}
814 
815 	intern = Z_SPLDLLIST_P(ZEND_THIS);
816 
817 	if (Z_TYPE_P(zindex) == IS_NULL) {
818 		/* $obj[] = ... */
819 		spl_ptr_llist_push(intern->llist, value);
820 	} else {
821 		/* $obj[$foo] = ... */
822 		zend_long                   index;
823 		spl_ptr_llist_element *element;
824 
825 		index = spl_offset_convert_to_long(zindex);
826 
827 		if (index < 0 || index >= intern->llist->count) {
828 			zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid or out of range", 0);
829 			return;
830 		}
831 
832 		element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
833 
834 		if (element != NULL) {
835 			/* call dtor on the old element as in spl_ptr_llist_pop */
836 			if (intern->llist->dtor) {
837 				intern->llist->dtor(element);
838 			}
839 
840 			/* the element is replaced, delref the old one as in
841 			 * SplDoublyLinkedList::pop() */
842 			zval_ptr_dtor(&element->data);
843 			ZVAL_COPY_VALUE(&element->data, value);
844 
845 			/* new element, call ctor as in spl_ptr_llist_push */
846 			if (intern->llist->ctor) {
847 				intern->llist->ctor(element);
848 			}
849 		} else {
850 			zval_ptr_dtor(value);
851 			zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
852 			return;
853 		}
854 	}
855 } /* }}} */
856 
857 /* {{{ proto void SplDoublyLinkedList::offsetUnset(mixed index)
858  Unsets the value at the specified $index. */
SPL_METHOD(SplDoublyLinkedList,offsetUnset)859 SPL_METHOD(SplDoublyLinkedList, offsetUnset)
860 {
861 	zval                  *zindex;
862 	zend_long             index;
863 	spl_dllist_object     *intern;
864 	spl_ptr_llist_element *element;
865 	spl_ptr_llist         *llist;
866 
867 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
868 		return;
869 	}
870 
871 	intern = Z_SPLDLLIST_P(ZEND_THIS);
872 	index  = spl_offset_convert_to_long(zindex);
873 	llist  = intern->llist;
874 
875 	if (index < 0 || index >= intern->llist->count) {
876 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset out of range", 0);
877 		return;
878 	}
879 
880 	element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
881 
882 	if (element != NULL) {
883 		/* connect the neightbors */
884 		if (element->prev) {
885 			element->prev->next = element->next;
886 		}
887 
888 		if (element->next) {
889 			element->next->prev = element->prev;
890 		}
891 
892 		/* take care of head/tail */
893 		if (element == llist->head) {
894 			llist->head = element->next;
895 		}
896 
897 		if (element == llist->tail) {
898 			llist->tail = element->prev;
899 		}
900 
901 		/* finally, delete the element */
902 		llist->count--;
903 
904 		if(llist->dtor) {
905 			llist->dtor(element);
906 		}
907 
908 		if (intern->traverse_pointer == element) {
909 			SPL_LLIST_DELREF(element);
910 			intern->traverse_pointer = NULL;
911 		}
912 		zval_ptr_dtor(&element->data);
913 		ZVAL_UNDEF(&element->data);
914 
915 		SPL_LLIST_DELREF(element);
916 	} else {
917 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
918 		return;
919 	}
920 } /* }}} */
921 
spl_dllist_it_dtor(zend_object_iterator * iter)922 static void spl_dllist_it_dtor(zend_object_iterator *iter) /* {{{ */
923 {
924 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
925 
926 	SPL_LLIST_CHECK_DELREF(iterator->traverse_pointer);
927 
928 	zend_user_it_invalidate_current(iter);
929 	zval_ptr_dtor(&iterator->intern.it.data);
930 }
931 /* }}} */
932 
spl_dllist_it_helper_rewind(spl_ptr_llist_element ** traverse_pointer_ptr,int * traverse_position_ptr,spl_ptr_llist * llist,int flags)933 static void spl_dllist_it_helper_rewind(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */
934 {
935 	SPL_LLIST_CHECK_DELREF(*traverse_pointer_ptr);
936 
937 	if (flags & SPL_DLLIST_IT_LIFO) {
938 		*traverse_position_ptr = llist->count-1;
939 		*traverse_pointer_ptr  = llist->tail;
940 	} else {
941 		*traverse_position_ptr = 0;
942 		*traverse_pointer_ptr  = llist->head;
943 	}
944 
945 	SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
946 }
947 /* }}} */
948 
spl_dllist_it_helper_move_forward(spl_ptr_llist_element ** traverse_pointer_ptr,int * traverse_position_ptr,spl_ptr_llist * llist,int flags)949 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) /* {{{ */
950 {
951 	if (*traverse_pointer_ptr) {
952 		spl_ptr_llist_element *old = *traverse_pointer_ptr;
953 
954 		if (flags & SPL_DLLIST_IT_LIFO) {
955 			*traverse_pointer_ptr = old->prev;
956 			(*traverse_position_ptr)--;
957 
958 			if (flags & SPL_DLLIST_IT_DELETE) {
959 				zval prev;
960 				spl_ptr_llist_pop(llist, &prev);
961 
962 				zval_ptr_dtor(&prev);
963 			}
964 		} else {
965 			*traverse_pointer_ptr = old->next;
966 
967 			if (flags & SPL_DLLIST_IT_DELETE) {
968 				zval prev;
969 				spl_ptr_llist_shift(llist, &prev);
970 
971 				zval_ptr_dtor(&prev);
972 			} else {
973 				(*traverse_position_ptr)++;
974 			}
975 		}
976 
977 		SPL_LLIST_DELREF(old);
978 		SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
979 	}
980 }
981 /* }}} */
982 
spl_dllist_it_rewind(zend_object_iterator * iter)983 static void spl_dllist_it_rewind(zend_object_iterator *iter) /* {{{ */
984 {
985 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
986 	spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
987 	spl_ptr_llist *llist = object->llist;
988 
989 	spl_dllist_it_helper_rewind(&iterator->traverse_pointer, &iterator->traverse_position, llist, object->flags);
990 }
991 /* }}} */
992 
spl_dllist_it_valid(zend_object_iterator * iter)993 static int spl_dllist_it_valid(zend_object_iterator *iter) /* {{{ */
994 {
995 	spl_dllist_it         *iterator = (spl_dllist_it *)iter;
996 	spl_ptr_llist_element *element  = iterator->traverse_pointer;
997 
998 	return (element != NULL ? SUCCESS : FAILURE);
999 }
1000 /* }}} */
1001 
spl_dllist_it_get_current_data(zend_object_iterator * iter)1002 static zval *spl_dllist_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1003 {
1004 	spl_dllist_it         *iterator = (spl_dllist_it *)iter;
1005 	spl_ptr_llist_element *element  = iterator->traverse_pointer;
1006 
1007 	if (element == NULL || Z_ISUNDEF(element->data)) {
1008 		return NULL;
1009 	}
1010 
1011 	return &element->data;
1012 }
1013 /* }}} */
1014 
spl_dllist_it_get_current_key(zend_object_iterator * iter,zval * key)1015 static void spl_dllist_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1016 {
1017 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
1018 
1019 	ZVAL_LONG(key, iterator->traverse_position);
1020 }
1021 /* }}} */
1022 
spl_dllist_it_move_forward(zend_object_iterator * iter)1023 static void spl_dllist_it_move_forward(zend_object_iterator *iter) /* {{{ */
1024 {
1025 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
1026 	spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
1027 
1028 	zend_user_it_invalidate_current(iter);
1029 
1030 	spl_dllist_it_helper_move_forward(&iterator->traverse_pointer, &iterator->traverse_position, object->llist, object->flags);
1031 }
1032 /* }}} */
1033 
1034 /* {{{  proto int SplDoublyLinkedList::key()
1035    Return current array key */
SPL_METHOD(SplDoublyLinkedList,key)1036 SPL_METHOD(SplDoublyLinkedList, key)
1037 {
1038 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1039 
1040 	if (zend_parse_parameters_none() == FAILURE) {
1041 		return;
1042 	}
1043 
1044 	RETURN_LONG(intern->traverse_position);
1045 }
1046 /* }}} */
1047 
1048 /* {{{ proto void SplDoublyLinkedList::prev()
1049    Move to next entry */
SPL_METHOD(SplDoublyLinkedList,prev)1050 SPL_METHOD(SplDoublyLinkedList, prev)
1051 {
1052 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1053 
1054 	if (zend_parse_parameters_none() == FAILURE) {
1055 		return;
1056 	}
1057 
1058 	spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags ^ SPL_DLLIST_IT_LIFO);
1059 }
1060 /* }}} */
1061 
1062 /* {{{ proto void SplDoublyLinkedList::next()
1063    Move to next entry */
SPL_METHOD(SplDoublyLinkedList,next)1064 SPL_METHOD(SplDoublyLinkedList, next)
1065 {
1066 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1067 
1068 	if (zend_parse_parameters_none() == FAILURE) {
1069 		return;
1070 	}
1071 
1072 	spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
1073 }
1074 /* }}} */
1075 
1076 /* {{{ proto bool SplDoublyLinkedList::valid()
1077    Check whether the datastructure contains more entries */
SPL_METHOD(SplDoublyLinkedList,valid)1078 SPL_METHOD(SplDoublyLinkedList, valid)
1079 {
1080 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1081 
1082 	if (zend_parse_parameters_none() == FAILURE) {
1083 		return;
1084 	}
1085 
1086 	RETURN_BOOL(intern->traverse_pointer != NULL);
1087 }
1088 /* }}} */
1089 
1090 /* {{{ proto void SplDoublyLinkedList::rewind()
1091    Rewind the datastructure back to the start */
SPL_METHOD(SplDoublyLinkedList,rewind)1092 SPL_METHOD(SplDoublyLinkedList, rewind)
1093 {
1094 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1095 
1096 	if (zend_parse_parameters_none() == FAILURE) {
1097 		return;
1098 	}
1099 
1100 	spl_dllist_it_helper_rewind(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
1101 }
1102 /* }}} */
1103 
1104 /* {{{ proto mixed|NULL SplDoublyLinkedList::current()
1105    Return current datastructure entry */
SPL_METHOD(SplDoublyLinkedList,current)1106 SPL_METHOD(SplDoublyLinkedList, current)
1107 {
1108 	spl_dllist_object     *intern  = Z_SPLDLLIST_P(ZEND_THIS);
1109 	spl_ptr_llist_element *element = intern->traverse_pointer;
1110 
1111 	if (zend_parse_parameters_none() == FAILURE) {
1112 		return;
1113 	}
1114 
1115 	if (element == NULL || Z_ISUNDEF(element->data)) {
1116 		RETURN_NULL();
1117 	} else {
1118 		zval *value = &element->data;
1119 
1120 		ZVAL_COPY_DEREF(return_value, value);
1121 	}
1122 }
1123 /* }}} */
1124 
1125 /* {{{ proto string SplDoublyLinkedList::serialize()
1126  Serializes storage */
SPL_METHOD(SplDoublyLinkedList,serialize)1127 SPL_METHOD(SplDoublyLinkedList, serialize)
1128 {
1129 	spl_dllist_object     *intern   = Z_SPLDLLIST_P(ZEND_THIS);
1130 	smart_str              buf      = {0};
1131 	spl_ptr_llist_element *current  = intern->llist->head, *next;
1132 	zval                   flags;
1133 	php_serialize_data_t   var_hash;
1134 
1135 	if (zend_parse_parameters_none() == FAILURE) {
1136 		return;
1137 	}
1138 
1139 	PHP_VAR_SERIALIZE_INIT(var_hash);
1140 
1141 	/* flags */
1142 	ZVAL_LONG(&flags, intern->flags);
1143 	php_var_serialize(&buf, &flags, &var_hash);
1144 
1145 	/* elements */
1146 	while (current) {
1147 		smart_str_appendc(&buf, ':');
1148 		next = current->next;
1149 
1150 		php_var_serialize(&buf, &current->data, &var_hash);
1151 
1152 		current = next;
1153 	}
1154 
1155 	smart_str_0(&buf);
1156 
1157 	/* done */
1158 	PHP_VAR_SERIALIZE_DESTROY(var_hash);
1159 
1160 	if (buf.s) {
1161 		RETURN_NEW_STR(buf.s);
1162 	} else {
1163 		RETURN_NULL();
1164 	}
1165 
1166 } /* }}} */
1167 
1168 /* {{{ proto void SplDoublyLinkedList::unserialize(string serialized)
1169  Unserializes storage */
SPL_METHOD(SplDoublyLinkedList,unserialize)1170 SPL_METHOD(SplDoublyLinkedList, unserialize)
1171 {
1172 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1173 	zval *flags, *elem;
1174 	char *buf;
1175 	size_t buf_len;
1176 	const unsigned char *p, *s;
1177 	php_unserialize_data_t var_hash;
1178 
1179 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1180 		return;
1181 	}
1182 
1183 	if (buf_len == 0) {
1184 		return;
1185 	}
1186 
1187 	while (intern->llist->count > 0) {
1188 		zval tmp;
1189 		spl_ptr_llist_pop(intern->llist, &tmp);
1190 		zval_ptr_dtor(&tmp);
1191 	}
1192 
1193 	s = p = (const unsigned char*)buf;
1194 	PHP_VAR_UNSERIALIZE_INIT(var_hash);
1195 
1196 	/* flags */
1197 	flags = var_tmp_var(&var_hash);
1198 	if (!php_var_unserialize(flags, &p, s + buf_len, &var_hash) || Z_TYPE_P(flags) != IS_LONG) {
1199 		goto error;
1200 	}
1201 
1202 	intern->flags = (int)Z_LVAL_P(flags);
1203 
1204 	/* elements */
1205 	while(*p == ':') {
1206 		++p;
1207 		elem = var_tmp_var(&var_hash);
1208 		if (!php_var_unserialize(elem, &p, s + buf_len, &var_hash)) {
1209 			goto error;
1210 		}
1211 		var_push_dtor(&var_hash, elem);
1212 
1213 		spl_ptr_llist_push(intern->llist, elem);
1214 	}
1215 
1216 	if (*p != '\0') {
1217 		goto error;
1218 	}
1219 
1220 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1221 	return;
1222 
1223 error:
1224 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1225 	zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len);
1226 	return;
1227 
1228 } /* }}} */
1229 
1230 /* {{{ proto array SplDoublyLinkedList::__serialize() */
SPL_METHOD(SplDoublyLinkedList,__serialize)1231 SPL_METHOD(SplDoublyLinkedList, __serialize)
1232 {
1233 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1234 	spl_ptr_llist_element *current = intern->llist->head;
1235 	zval tmp;
1236 
1237 	if (zend_parse_parameters_none_throw() == FAILURE) {
1238 		return;
1239 	}
1240 
1241 	array_init(return_value);
1242 
1243 	/* flags */
1244 	ZVAL_LONG(&tmp, intern->flags);
1245 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1246 
1247 	/* elements */
1248 	array_init_size(&tmp, intern->llist->count);
1249 	while (current) {
1250 		zend_hash_next_index_insert(Z_ARRVAL(tmp), &current->data);
1251 		Z_TRY_ADDREF(current->data);
1252 		current = current->next;
1253 	}
1254 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1255 
1256 	/* members */
1257 	ZVAL_ARR(&tmp, zend_std_get_properties(ZEND_THIS));
1258 	Z_TRY_ADDREF(tmp);
1259 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1260 } /* }}} */
1261 
1262 /* {{{ proto void SplDoublyLinkedList::__unserialize(array serialized) */
SPL_METHOD(SplDoublyLinkedList,__unserialize)1263 SPL_METHOD(SplDoublyLinkedList, __unserialize) {
1264 	spl_dllist_object *intern = Z_SPLDLLIST_P(ZEND_THIS);
1265 	HashTable *data;
1266 	zval *flags_zv, *storage_zv, *members_zv, *elem;
1267 
1268 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1269 		return;
1270 	}
1271 
1272 	flags_zv = zend_hash_index_find(data, 0);
1273 	storage_zv = zend_hash_index_find(data, 1);
1274 	members_zv = zend_hash_index_find(data, 2);
1275 	if (!flags_zv || !storage_zv || !members_zv ||
1276 			Z_TYPE_P(flags_zv) != IS_LONG || Z_TYPE_P(storage_zv) != IS_ARRAY ||
1277 			Z_TYPE_P(members_zv) != IS_ARRAY) {
1278 		zend_throw_exception(spl_ce_UnexpectedValueException,
1279 			"Incomplete or ill-typed serialization data", 0);
1280 		return;
1281 	}
1282 
1283 	intern->flags = (int) Z_LVAL_P(flags_zv);
1284 
1285 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), elem) {
1286 		spl_ptr_llist_push(intern->llist, elem);
1287 	} ZEND_HASH_FOREACH_END();
1288 
1289 	object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
1290 } /* }}} */
1291 
1292 /* {{{ proto void SplDoublyLinkedList::add(mixed index, mixed newval)
1293  Inserts a new entry before the specified $index consisting of $newval. */
SPL_METHOD(SplDoublyLinkedList,add)1294 SPL_METHOD(SplDoublyLinkedList, add)
1295 {
1296 	zval                  *zindex, *value;
1297 	spl_dllist_object     *intern;
1298 	spl_ptr_llist_element *element;
1299 	zend_long                  index;
1300 
1301 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
1302 		return;
1303 	}
1304 
1305 	intern = Z_SPLDLLIST_P(ZEND_THIS);
1306 	index  = spl_offset_convert_to_long(zindex);
1307 
1308 	if (index < 0 || index > intern->llist->count) {
1309 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid or out of range", 0);
1310 		return;
1311 	}
1312 
1313 	Z_TRY_ADDREF_P(value);
1314 	if (index == intern->llist->count) {
1315 		/* If index is the last entry+1 then we do a push because we're not inserting before any entry */
1316 		spl_ptr_llist_push(intern->llist, value);
1317 	} else {
1318 		/* Create the new element we want to insert */
1319 		spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element));
1320 
1321 		/* Get the element we want to insert before */
1322 		element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
1323 
1324 		ZVAL_COPY_VALUE(&elem->data, value);
1325 		elem->rc   = 1;
1326 		/* connect to the neighbours */
1327 		elem->next = element;
1328 		elem->prev = element->prev;
1329 
1330 		/* connect the neighbours to this new element */
1331 		if (elem->prev == NULL) {
1332 			intern->llist->head = elem;
1333 		} else {
1334 			element->prev->next = elem;
1335 		}
1336 		element->prev = elem;
1337 
1338 		intern->llist->count++;
1339 
1340 		if (intern->llist->ctor) {
1341 			intern->llist->ctor(elem);
1342 		}
1343 	}
1344 } /* }}} */
1345 
1346 /* {{{ proto void SplDoublyLinkedList::__debugInfo() */
SPL_METHOD(SplDoublyLinkedList,__debugInfo)1347 SPL_METHOD(SplDoublyLinkedList, __debugInfo)
1348 {
1349 	if (zend_parse_parameters_none() == FAILURE) {
1350 		return;
1351 	}
1352 
1353 	RETURN_ARR(spl_dllist_object_get_debug_info(getThis()));
1354 } /* }}} */
1355 
1356 /* {{{ iterator handler table */
1357 static const zend_object_iterator_funcs spl_dllist_it_funcs = {
1358 	spl_dllist_it_dtor,
1359 	spl_dllist_it_valid,
1360 	spl_dllist_it_get_current_data,
1361 	spl_dllist_it_get_current_key,
1362 	spl_dllist_it_move_forward,
1363 	spl_dllist_it_rewind,
1364 	NULL
1365 }; /* }}} */
1366 
spl_dllist_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1367 zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1368 {
1369 	spl_dllist_it *iterator;
1370 	spl_dllist_object *dllist_object = Z_SPLDLLIST_P(object);
1371 
1372 	if (by_ref) {
1373 		zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
1374 		return NULL;
1375 	}
1376 
1377 	iterator = emalloc(sizeof(spl_dllist_it));
1378 
1379 	zend_iterator_init((zend_object_iterator*)iterator);
1380 
1381 	Z_ADDREF_P(object);
1382 	ZVAL_OBJ(&iterator->intern.it.data, Z_OBJ_P(object));
1383 	iterator->intern.it.funcs    = &spl_dllist_it_funcs;
1384 	iterator->intern.ce          = ce;
1385 	iterator->traverse_position  = dllist_object->traverse_position;
1386 	iterator->traverse_pointer   = dllist_object->traverse_pointer;
1387 	iterator->flags              = dllist_object->flags & SPL_DLLIST_IT_MASK;
1388 	ZVAL_UNDEF(&iterator->intern.value);
1389 
1390 	SPL_LLIST_CHECK_ADDREF(iterator->traverse_pointer);
1391 
1392 	return &iterator->intern.it;
1393 }
1394 /* }}} */
1395 
1396 /*  Function/Class/Method definitions */
1397 ZEND_BEGIN_ARG_INFO(arginfo_dllist_setiteratormode, 0)
1398 	ZEND_ARG_INFO(0, mode)
1399 ZEND_END_ARG_INFO()
1400 
1401 ZEND_BEGIN_ARG_INFO(arginfo_dllist_push, 0)
1402 	ZEND_ARG_INFO(0, value)
1403 ZEND_END_ARG_INFO()
1404 
1405 ZEND_BEGIN_ARG_INFO_EX(arginfo_dllist_offsetGet, 0, 0, 1)
1406 	ZEND_ARG_INFO(0, index)
1407 ZEND_END_ARG_INFO()
1408 
1409 ZEND_BEGIN_ARG_INFO_EX(arginfo_dllist_offsetSet, 0, 0, 2)
1410 	ZEND_ARG_INFO(0, index)
1411 	ZEND_ARG_INFO(0, newval)
1412 ZEND_END_ARG_INFO()
1413 
1414 ZEND_BEGIN_ARG_INFO(arginfo_dllist_void, 0)
1415 ZEND_END_ARG_INFO()
1416 
1417 ZEND_BEGIN_ARG_INFO(arginfo_dllist_serialized, 0)
1418 	ZEND_ARG_INFO(0, serialized)
1419 ZEND_END_ARG_INFO();
1420 
1421 static const zend_function_entry spl_funcs_SplQueue[] = {
1422 	SPL_MA(SplQueue, enqueue, SplDoublyLinkedList, push,  arginfo_dllist_push, ZEND_ACC_PUBLIC)
1423 	SPL_MA(SplQueue, dequeue, SplDoublyLinkedList, shift, arginfo_dllist_void, ZEND_ACC_PUBLIC)
1424 	PHP_FE_END
1425 };
1426 
1427 static const zend_function_entry spl_funcs_SplDoublyLinkedList[] = {
1428 	SPL_ME(SplDoublyLinkedList, pop,             arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1429 	SPL_ME(SplDoublyLinkedList, shift,           arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1430 	SPL_ME(SplDoublyLinkedList, push,            arginfo_dllist_push,            ZEND_ACC_PUBLIC)
1431 	SPL_ME(SplDoublyLinkedList, unshift,         arginfo_dllist_push,            ZEND_ACC_PUBLIC)
1432 	SPL_ME(SplDoublyLinkedList, top,             arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1433 	SPL_ME(SplDoublyLinkedList, bottom,          arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1434 	SPL_ME(SplDoublyLinkedList, isEmpty,         arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1435 	SPL_ME(SplDoublyLinkedList, setIteratorMode, arginfo_dllist_setiteratormode, ZEND_ACC_PUBLIC)
1436 	SPL_ME(SplDoublyLinkedList, getIteratorMode, arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1437 	SPL_ME(SplDoublyLinkedList, __debugInfo,     arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1438 	/* Countable */
1439 	SPL_ME(SplDoublyLinkedList, count,           arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1440 	/* ArrayAccess */
1441 	SPL_ME(SplDoublyLinkedList, offsetExists,    arginfo_dllist_offsetGet,       ZEND_ACC_PUBLIC)
1442 	SPL_ME(SplDoublyLinkedList, offsetGet,       arginfo_dllist_offsetGet,       ZEND_ACC_PUBLIC)
1443 	SPL_ME(SplDoublyLinkedList, offsetSet,       arginfo_dllist_offsetSet,       ZEND_ACC_PUBLIC)
1444 	SPL_ME(SplDoublyLinkedList, offsetUnset,     arginfo_dllist_offsetGet,       ZEND_ACC_PUBLIC)
1445 
1446 	SPL_ME(SplDoublyLinkedList, add,             arginfo_dllist_offsetSet,       ZEND_ACC_PUBLIC)
1447 
1448 	/* Iterator */
1449 	SPL_ME(SplDoublyLinkedList, rewind,          arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1450 	SPL_ME(SplDoublyLinkedList, current,         arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1451 	SPL_ME(SplDoublyLinkedList, key,             arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1452 	SPL_ME(SplDoublyLinkedList, next,            arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1453 	SPL_ME(SplDoublyLinkedList, prev,            arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1454 	SPL_ME(SplDoublyLinkedList, valid,           arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1455 	/* Serializable */
1456 	SPL_ME(SplDoublyLinkedList,  unserialize,    arginfo_dllist_serialized,      ZEND_ACC_PUBLIC)
1457 	SPL_ME(SplDoublyLinkedList,  serialize,      arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1458 	SPL_ME(SplDoublyLinkedList,  __unserialize,    arginfo_dllist_serialized,    ZEND_ACC_PUBLIC)
1459 	SPL_ME(SplDoublyLinkedList,  __serialize,      arginfo_dllist_void,          ZEND_ACC_PUBLIC)
1460 	PHP_FE_END
1461 };
1462 /* }}} */
1463 
PHP_MINIT_FUNCTION(spl_dllist)1464 PHP_MINIT_FUNCTION(spl_dllist) /* {{{ */
1465 {
1466 	REGISTER_SPL_STD_CLASS_EX(SplDoublyLinkedList, spl_dllist_object_new, spl_funcs_SplDoublyLinkedList);
1467 	memcpy(&spl_handler_SplDoublyLinkedList, &std_object_handlers, sizeof(zend_object_handlers));
1468 
1469 	spl_handler_SplDoublyLinkedList.offset = XtOffsetOf(spl_dllist_object, std);
1470 	spl_handler_SplDoublyLinkedList.clone_obj = spl_dllist_object_clone;
1471 	spl_handler_SplDoublyLinkedList.count_elements = spl_dllist_object_count_elements;
1472 	spl_handler_SplDoublyLinkedList.get_gc = spl_dllist_object_get_gc;
1473 	spl_handler_SplDoublyLinkedList.dtor_obj = zend_objects_destroy_object;
1474 	spl_handler_SplDoublyLinkedList.free_obj = spl_dllist_object_free_storage;
1475 
1476 	REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_LIFO",  SPL_DLLIST_IT_LIFO);
1477 	REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_FIFO",  0);
1478 	REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_DELETE",SPL_DLLIST_IT_DELETE);
1479 	REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_KEEP",  0);
1480 
1481 	REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Iterator);
1482 	REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Countable);
1483 	REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, ArrayAccess);
1484 	REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Serializable);
1485 
1486 	spl_ce_SplDoublyLinkedList->get_iterator = spl_dllist_get_iterator;
1487 
1488 	REGISTER_SPL_SUB_CLASS_EX(SplQueue,           SplDoublyLinkedList,        spl_dllist_object_new, spl_funcs_SplQueue);
1489 	REGISTER_SPL_SUB_CLASS_EX(SplStack,           SplDoublyLinkedList,        spl_dllist_object_new, NULL);
1490 
1491 	spl_ce_SplQueue->get_iterator = spl_dllist_get_iterator;
1492 	spl_ce_SplStack->get_iterator = spl_dllist_get_iterator;
1493 
1494 	return SUCCESS;
1495 }
1496 /* }}} */
1497