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