xref: /PHP-7.3/ext/spl/spl_dllist.c (revision b8427729)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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,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 	debug_info = zend_new_array(1);
510 	zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
511 
512 	pnstr = spl_gen_private_prop_name(spl_ce_SplDoublyLinkedList, "flags", sizeof("flags")-1);
513 	ZVAL_LONG(&tmp, intern->flags);
514 	zend_hash_add(debug_info, pnstr, &tmp);
515 	zend_string_release_ex(pnstr, 0);
516 
517 	array_init(&dllist_array);
518 
519 	while (current) {
520 		next = current->next;
521 
522 		add_index_zval(&dllist_array, i, &current->data);
523 		if (Z_REFCOUNTED(current->data)) {
524 			Z_ADDREF(current->data);
525 		}
526 		i++;
527 
528 		current = next;
529 	}
530 
531 	pnstr = spl_gen_private_prop_name(spl_ce_SplDoublyLinkedList, "dllist", sizeof("dllist")-1);
532 	zend_hash_add(debug_info, pnstr, &dllist_array);
533 	zend_string_release_ex(pnstr, 0);
534 
535 	return debug_info;
536 }
537 /* }}}} */
538 
spl_dllist_object_get_gc(zval * obj,zval ** gc_data,int * gc_data_count)539 static HashTable *spl_dllist_object_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
540 {
541 	spl_dllist_object *intern  = Z_SPLDLLIST_P(obj);
542 	spl_ptr_llist_element *current = intern->llist->head;
543 	int i = 0;
544 
545 	if (intern->gc_data_count < intern->llist->count) {
546 		intern->gc_data_count = intern->llist->count;
547 		intern->gc_data = safe_erealloc(intern->gc_data, intern->gc_data_count, sizeof(zval), 0);
548 	}
549 
550 	while (current) {
551 		ZVAL_COPY_VALUE(&intern->gc_data[i++], &current->data);
552 		current = current->next;
553 	}
554 
555 	*gc_data = intern->gc_data;
556 	*gc_data_count = i;
557 	return zend_std_get_properties(obj);
558 }
559 /* }}} */
560 
561 /* {{{ proto bool SplDoublyLinkedList::push(mixed value)
562 	   Push $value on the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,push)563 SPL_METHOD(SplDoublyLinkedList, push)
564 {
565 	zval *value;
566 	spl_dllist_object *intern;
567 
568 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
569 		return;
570 	}
571 
572 	intern = Z_SPLDLLIST_P(getThis());
573 	spl_ptr_llist_push(intern->llist, value);
574 
575 	RETURN_TRUE;
576 }
577 /* }}} */
578 
579 /* {{{ proto bool SplDoublyLinkedList::unshift(mixed value)
580 	   Unshift $value on the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,unshift)581 SPL_METHOD(SplDoublyLinkedList, unshift)
582 {
583 	zval *value;
584 	spl_dllist_object *intern;
585 
586 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
587 		return;
588 	}
589 
590 	intern = Z_SPLDLLIST_P(getThis());
591 	spl_ptr_llist_unshift(intern->llist, value);
592 
593 	RETURN_TRUE;
594 }
595 /* }}} */
596 
597 /* {{{ proto mixed SplDoublyLinkedList::pop()
598 	   Pop an element out of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,pop)599 SPL_METHOD(SplDoublyLinkedList, pop)
600 {
601 	spl_dllist_object *intern;
602 
603 	if (zend_parse_parameters_none() == FAILURE) {
604 		return;
605 	}
606 
607 	intern = Z_SPLDLLIST_P(getThis());
608 	spl_ptr_llist_pop(intern->llist, return_value);
609 
610 	if (Z_ISUNDEF_P(return_value)) {
611 		zend_throw_exception(spl_ce_RuntimeException, "Can't pop from an empty datastructure", 0);
612 		RETURN_NULL();
613 	}
614 }
615 /* }}} */
616 
617 /* {{{ proto mixed SplDoublyLinkedList::shift()
618 	   Shift an element out of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,shift)619 SPL_METHOD(SplDoublyLinkedList, shift)
620 {
621 	spl_dllist_object *intern;
622 
623 	if (zend_parse_parameters_none() == FAILURE) {
624 		return;
625 	}
626 
627 	intern = Z_SPLDLLIST_P(getThis());
628 	spl_ptr_llist_shift(intern->llist, return_value);
629 
630 	if (Z_ISUNDEF_P(return_value)) {
631 		zend_throw_exception(spl_ce_RuntimeException, "Can't shift from an empty datastructure", 0);
632 		RETURN_NULL();
633 	}
634 }
635 /* }}} */
636 
637 /* {{{ proto mixed SplDoublyLinkedList::top()
638 	   Peek at the top element of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,top)639 SPL_METHOD(SplDoublyLinkedList, top)
640 {
641 	zval *value;
642 	spl_dllist_object *intern;
643 
644 	if (zend_parse_parameters_none() == FAILURE) {
645 		return;
646 	}
647 
648 	intern = Z_SPLDLLIST_P(getThis());
649 	value = spl_ptr_llist_last(intern->llist);
650 
651 	if (value == NULL || Z_ISUNDEF_P(value)) {
652 		zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0);
653 		return;
654 	}
655 
656 	ZVAL_COPY_DEREF(return_value, value);
657 }
658 /* }}} */
659 
660 /* {{{ proto mixed SplDoublyLinkedList::bottom()
661 	   Peek at the bottom element of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList,bottom)662 SPL_METHOD(SplDoublyLinkedList, bottom)
663 {
664 	zval *value;
665 	spl_dllist_object *intern;
666 
667 	if (zend_parse_parameters_none() == FAILURE) {
668 		return;
669 	}
670 
671 	intern = Z_SPLDLLIST_P(getThis());
672 	value  = spl_ptr_llist_first(intern->llist);
673 
674 	if (value == NULL || Z_ISUNDEF_P(value)) {
675 		zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0);
676 		return;
677 	}
678 
679 	ZVAL_COPY_DEREF(return_value, value);
680 }
681 /* }}} */
682 
683 /* {{{ proto int SplDoublyLinkedList::count()
684  Return the number of elements in the datastructure. */
SPL_METHOD(SplDoublyLinkedList,count)685 SPL_METHOD(SplDoublyLinkedList, count)
686 {
687 	zend_long count;
688 	spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
689 
690 	if (zend_parse_parameters_none() == FAILURE) {
691 		return;
692 	}
693 
694 	count = spl_ptr_llist_count(intern->llist);
695 	RETURN_LONG(count);
696 }
697 /* }}} */
698 
699 /* {{{ proto int SplDoublyLinkedList::isEmpty()
700  Return true if the SplDoublyLinkedList is empty. */
SPL_METHOD(SplDoublyLinkedList,isEmpty)701 SPL_METHOD(SplDoublyLinkedList, isEmpty)
702 {
703 	zend_long count;
704 
705 	if (zend_parse_parameters_none() == FAILURE) {
706 		return;
707 	}
708 
709 	spl_dllist_object_count_elements(getThis(), &count);
710 	RETURN_BOOL(count == 0);
711 }
712 /* }}} */
713 
714 /* {{{ proto int SplDoublyLinkedList::setIteratorMode(int flags)
715  Set the mode of iteration */
SPL_METHOD(SplDoublyLinkedList,setIteratorMode)716 SPL_METHOD(SplDoublyLinkedList, setIteratorMode)
717 {
718 	zend_long value;
719 	spl_dllist_object *intern;
720 
721 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &value) == FAILURE) {
722 		return;
723 	}
724 
725 	intern = Z_SPLDLLIST_P(getThis());
726 
727 	if (intern->flags & SPL_DLLIST_IT_FIX
728 		&& (intern->flags & SPL_DLLIST_IT_LIFO) != (value & SPL_DLLIST_IT_LIFO)) {
729 		zend_throw_exception(spl_ce_RuntimeException, "Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen", 0);
730 		return;
731 	}
732 
733 	intern->flags = (value & SPL_DLLIST_IT_MASK) | (intern->flags & SPL_DLLIST_IT_FIX);
734 
735 	RETURN_LONG(intern->flags);
736 }
737 /* }}} */
738 
739 /* {{{ proto int SplDoublyLinkedList::getIteratorMode()
740  Return the mode of iteration */
SPL_METHOD(SplDoublyLinkedList,getIteratorMode)741 SPL_METHOD(SplDoublyLinkedList, getIteratorMode)
742 {
743 	spl_dllist_object *intern;
744 
745 	if (zend_parse_parameters_none() == FAILURE) {
746 		return;
747 	}
748 
749 	intern = Z_SPLDLLIST_P(getThis());
750 
751 	RETURN_LONG(intern->flags);
752 }
753 /* }}} */
754 
755 /* {{{ proto bool SplDoublyLinkedList::offsetExists(mixed index)
756  Returns whether the requested $index exists. */
SPL_METHOD(SplDoublyLinkedList,offsetExists)757 SPL_METHOD(SplDoublyLinkedList, offsetExists)
758 {
759 	zval              *zindex;
760 	spl_dllist_object *intern;
761 	zend_long               index;
762 
763 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
764 		return;
765 	}
766 
767 	intern = Z_SPLDLLIST_P(getThis());
768 	index  = spl_offset_convert_to_long(zindex);
769 
770 	RETURN_BOOL(index >= 0 && index < intern->llist->count);
771 } /* }}} */
772 
773 /* {{{ proto mixed SplDoublyLinkedList::offsetGet(mixed index)
774  Returns the value at the specified $index. */
SPL_METHOD(SplDoublyLinkedList,offsetGet)775 SPL_METHOD(SplDoublyLinkedList, offsetGet)
776 {
777 	zval                  *zindex;
778 	zend_long                   index;
779 	spl_dllist_object     *intern;
780 	spl_ptr_llist_element *element;
781 
782 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
783 		return;
784 	}
785 
786 	intern = Z_SPLDLLIST_P(getThis());
787 	index  = spl_offset_convert_to_long(zindex);
788 
789 	if (index < 0 || index >= intern->llist->count) {
790 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid or out of range", 0);
791 		return;
792 	}
793 
794 	element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
795 
796 	if (element != NULL) {
797 		zval *value = &element->data;
798 
799 		ZVAL_COPY_DEREF(return_value, value);
800 	} else {
801 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
802 	}
803 } /* }}} */
804 
805 /* {{{ proto void SplDoublyLinkedList::offsetSet(mixed index, mixed newval)
806  Sets the value at the specified $index to $newval. */
SPL_METHOD(SplDoublyLinkedList,offsetSet)807 SPL_METHOD(SplDoublyLinkedList, offsetSet)
808 {
809 	zval                  *zindex, *value;
810 	spl_dllist_object     *intern;
811 
812 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
813 		return;
814 	}
815 
816 	intern = Z_SPLDLLIST_P(getThis());
817 
818 	if (Z_TYPE_P(zindex) == IS_NULL) {
819 		/* $obj[] = ... */
820 		spl_ptr_llist_push(intern->llist, value);
821 	} else {
822 		/* $obj[$foo] = ... */
823 		zend_long                   index;
824 		spl_ptr_llist_element *element;
825 
826 		index = spl_offset_convert_to_long(zindex);
827 
828 		if (index < 0 || index >= intern->llist->count) {
829 			zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid or out of range", 0);
830 			return;
831 		}
832 
833 		element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
834 
835 		if (element != NULL) {
836 			/* call dtor on the old element as in spl_ptr_llist_pop */
837 			if (intern->llist->dtor) {
838 				intern->llist->dtor(element);
839 			}
840 
841 			/* the element is replaced, delref the old one as in
842 			 * SplDoublyLinkedList::pop() */
843 			zval_ptr_dtor(&element->data);
844 			ZVAL_COPY_VALUE(&element->data, value);
845 
846 			/* new element, call ctor as in spl_ptr_llist_push */
847 			if (intern->llist->ctor) {
848 				intern->llist->ctor(element);
849 			}
850 		} else {
851 			zval_ptr_dtor(value);
852 			zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
853 			return;
854 		}
855 	}
856 } /* }}} */
857 
858 /* {{{ proto void SplDoublyLinkedList::offsetUnset(mixed index)
859  Unsets the value at the specified $index. */
SPL_METHOD(SplDoublyLinkedList,offsetUnset)860 SPL_METHOD(SplDoublyLinkedList, offsetUnset)
861 {
862 	zval                  *zindex;
863 	zend_long             index;
864 	spl_dllist_object     *intern;
865 	spl_ptr_llist_element *element;
866 	spl_ptr_llist         *llist;
867 
868 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
869 		return;
870 	}
871 
872 	intern = Z_SPLDLLIST_P(getThis());
873 	index  = spl_offset_convert_to_long(zindex);
874 	llist  = intern->llist;
875 
876 	if (index < 0 || index >= intern->llist->count) {
877 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset out of range", 0);
878 		return;
879 	}
880 
881 	element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
882 
883 	if (element != NULL) {
884 		/* connect the neightbors */
885 		if (element->prev) {
886 			element->prev->next = element->next;
887 		}
888 
889 		if (element->next) {
890 			element->next->prev = element->prev;
891 		}
892 
893 		/* take care of head/tail */
894 		if (element == llist->head) {
895 			llist->head = element->next;
896 		}
897 
898 		if (element == llist->tail) {
899 			llist->tail = element->prev;
900 		}
901 
902 		/* finally, delete the element */
903 		llist->count--;
904 
905 		if(llist->dtor) {
906 			llist->dtor(element);
907 		}
908 
909 		if (intern->traverse_pointer == element) {
910 			SPL_LLIST_DELREF(element);
911 			intern->traverse_pointer = NULL;
912 		}
913 		zval_ptr_dtor(&element->data);
914 		ZVAL_UNDEF(&element->data);
915 
916 		SPL_LLIST_DELREF(element);
917 	} else {
918 		zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
919 		return;
920 	}
921 } /* }}} */
922 
spl_dllist_it_dtor(zend_object_iterator * iter)923 static void spl_dllist_it_dtor(zend_object_iterator *iter) /* {{{ */
924 {
925 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
926 
927 	SPL_LLIST_CHECK_DELREF(iterator->traverse_pointer);
928 
929 	zend_user_it_invalidate_current(iter);
930 	zval_ptr_dtor(&iterator->intern.it.data);
931 }
932 /* }}} */
933 
spl_dllist_it_helper_rewind(spl_ptr_llist_element ** traverse_pointer_ptr,int * traverse_position_ptr,spl_ptr_llist * llist,int flags)934 static void spl_dllist_it_helper_rewind(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */
935 {
936 	SPL_LLIST_CHECK_DELREF(*traverse_pointer_ptr);
937 
938 	if (flags & SPL_DLLIST_IT_LIFO) {
939 		*traverse_position_ptr = llist->count-1;
940 		*traverse_pointer_ptr  = llist->tail;
941 	} else {
942 		*traverse_position_ptr = 0;
943 		*traverse_pointer_ptr  = llist->head;
944 	}
945 
946 	SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
947 }
948 /* }}} */
949 
spl_dllist_it_helper_move_forward(spl_ptr_llist_element ** traverse_pointer_ptr,int * traverse_position_ptr,spl_ptr_llist * llist,int flags)950 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) /* {{{ */
951 {
952 	if (*traverse_pointer_ptr) {
953 		spl_ptr_llist_element *old = *traverse_pointer_ptr;
954 
955 		if (flags & SPL_DLLIST_IT_LIFO) {
956 			*traverse_pointer_ptr = old->prev;
957 			(*traverse_position_ptr)--;
958 
959 			if (flags & SPL_DLLIST_IT_DELETE) {
960 				zval prev;
961 				spl_ptr_llist_pop(llist, &prev);
962 
963 				zval_ptr_dtor(&prev);
964 			}
965 		} else {
966 			*traverse_pointer_ptr = old->next;
967 
968 			if (flags & SPL_DLLIST_IT_DELETE) {
969 				zval prev;
970 				spl_ptr_llist_shift(llist, &prev);
971 
972 				zval_ptr_dtor(&prev);
973 			} else {
974 				(*traverse_position_ptr)++;
975 			}
976 		}
977 
978 		SPL_LLIST_DELREF(old);
979 		SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
980 	}
981 }
982 /* }}} */
983 
spl_dllist_it_rewind(zend_object_iterator * iter)984 static void spl_dllist_it_rewind(zend_object_iterator *iter) /* {{{ */
985 {
986 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
987 	spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
988 	spl_ptr_llist *llist = object->llist;
989 
990 	spl_dllist_it_helper_rewind(&iterator->traverse_pointer, &iterator->traverse_position, llist, object->flags);
991 }
992 /* }}} */
993 
spl_dllist_it_valid(zend_object_iterator * iter)994 static int spl_dllist_it_valid(zend_object_iterator *iter) /* {{{ */
995 {
996 	spl_dllist_it         *iterator = (spl_dllist_it *)iter;
997 	spl_ptr_llist_element *element  = iterator->traverse_pointer;
998 
999 	return (element != NULL ? SUCCESS : FAILURE);
1000 }
1001 /* }}} */
1002 
spl_dllist_it_get_current_data(zend_object_iterator * iter)1003 static zval *spl_dllist_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1004 {
1005 	spl_dllist_it         *iterator = (spl_dllist_it *)iter;
1006 	spl_ptr_llist_element *element  = iterator->traverse_pointer;
1007 
1008 	if (element == NULL || Z_ISUNDEF(element->data)) {
1009 		return NULL;
1010 	}
1011 
1012 	return &element->data;
1013 }
1014 /* }}} */
1015 
spl_dllist_it_get_current_key(zend_object_iterator * iter,zval * key)1016 static void spl_dllist_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1017 {
1018 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
1019 
1020 	ZVAL_LONG(key, iterator->traverse_position);
1021 }
1022 /* }}} */
1023 
spl_dllist_it_move_forward(zend_object_iterator * iter)1024 static void spl_dllist_it_move_forward(zend_object_iterator *iter) /* {{{ */
1025 {
1026 	spl_dllist_it *iterator = (spl_dllist_it *)iter;
1027 	spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
1028 
1029 	zend_user_it_invalidate_current(iter);
1030 
1031 	spl_dllist_it_helper_move_forward(&iterator->traverse_pointer, &iterator->traverse_position, object->llist, object->flags);
1032 }
1033 /* }}} */
1034 
1035 /* {{{  proto int SplDoublyLinkedList::key()
1036    Return current array key */
SPL_METHOD(SplDoublyLinkedList,key)1037 SPL_METHOD(SplDoublyLinkedList, key)
1038 {
1039 	spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
1040 
1041 	if (zend_parse_parameters_none() == FAILURE) {
1042 		return;
1043 	}
1044 
1045 	RETURN_LONG(intern->traverse_position);
1046 }
1047 /* }}} */
1048 
1049 /* {{{ proto void SplDoublyLinkedList::prev()
1050    Move to next entry */
SPL_METHOD(SplDoublyLinkedList,prev)1051 SPL_METHOD(SplDoublyLinkedList, prev)
1052 {
1053 	spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
1054 
1055 	if (zend_parse_parameters_none() == FAILURE) {
1056 		return;
1057 	}
1058 
1059 	spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags ^ SPL_DLLIST_IT_LIFO);
1060 }
1061 /* }}} */
1062 
1063 /* {{{ proto void SplDoublyLinkedList::next()
1064    Move to next entry */
SPL_METHOD(SplDoublyLinkedList,next)1065 SPL_METHOD(SplDoublyLinkedList, next)
1066 {
1067 	spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
1068 
1069 	if (zend_parse_parameters_none() == FAILURE) {
1070 		return;
1071 	}
1072 
1073 	spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
1074 }
1075 /* }}} */
1076 
1077 /* {{{ proto bool SplDoublyLinkedList::valid()
1078    Check whether the datastructure contains more entries */
SPL_METHOD(SplDoublyLinkedList,valid)1079 SPL_METHOD(SplDoublyLinkedList, valid)
1080 {
1081 	spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
1082 
1083 	if (zend_parse_parameters_none() == FAILURE) {
1084 		return;
1085 	}
1086 
1087 	RETURN_BOOL(intern->traverse_pointer != NULL);
1088 }
1089 /* }}} */
1090 
1091 /* {{{ proto void SplDoublyLinkedList::rewind()
1092    Rewind the datastructure back to the start */
SPL_METHOD(SplDoublyLinkedList,rewind)1093 SPL_METHOD(SplDoublyLinkedList, rewind)
1094 {
1095 	spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
1096 
1097 	if (zend_parse_parameters_none() == FAILURE) {
1098 		return;
1099 	}
1100 
1101 	spl_dllist_it_helper_rewind(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
1102 }
1103 /* }}} */
1104 
1105 /* {{{ proto mixed|NULL SplDoublyLinkedList::current()
1106    Return current datastructure entry */
SPL_METHOD(SplDoublyLinkedList,current)1107 SPL_METHOD(SplDoublyLinkedList, current)
1108 {
1109 	spl_dllist_object     *intern  = Z_SPLDLLIST_P(getThis());
1110 	spl_ptr_llist_element *element = intern->traverse_pointer;
1111 
1112 	if (zend_parse_parameters_none() == FAILURE) {
1113 		return;
1114 	}
1115 
1116 	if (element == NULL || Z_ISUNDEF(element->data)) {
1117 		RETURN_NULL();
1118 	} else {
1119 		zval *value = &element->data;
1120 
1121 		ZVAL_COPY_DEREF(return_value, value);
1122 	}
1123 }
1124 /* }}} */
1125 
1126 /* {{{ proto string SplDoublyLinkedList::serialize()
1127  Serializes storage */
SPL_METHOD(SplDoublyLinkedList,serialize)1128 SPL_METHOD(SplDoublyLinkedList, serialize)
1129 {
1130 	spl_dllist_object     *intern   = Z_SPLDLLIST_P(getThis());
1131 	smart_str              buf      = {0};
1132 	spl_ptr_llist_element *current  = intern->llist->head, *next;
1133 	zval                   flags;
1134 	php_serialize_data_t   var_hash;
1135 
1136 	if (zend_parse_parameters_none() == FAILURE) {
1137 		return;
1138 	}
1139 
1140 	PHP_VAR_SERIALIZE_INIT(var_hash);
1141 
1142 	/* flags */
1143 	ZVAL_LONG(&flags, intern->flags);
1144 	php_var_serialize(&buf, &flags, &var_hash);
1145 
1146 	/* elements */
1147 	while (current) {
1148 		smart_str_appendc(&buf, ':');
1149 		next = current->next;
1150 
1151 		php_var_serialize(&buf, &current->data, &var_hash);
1152 
1153 		current = next;
1154 	}
1155 
1156 	smart_str_0(&buf);
1157 
1158 	/* done */
1159 	PHP_VAR_SERIALIZE_DESTROY(var_hash);
1160 
1161 	if (buf.s) {
1162 		RETURN_NEW_STR(buf.s);
1163 	} else {
1164 		RETURN_NULL();
1165 	}
1166 
1167 } /* }}} */
1168 
1169 /* {{{ proto void SplDoublyLinkedList::unserialize(string serialized)
1170  Unserializes storage */
SPL_METHOD(SplDoublyLinkedList,unserialize)1171 SPL_METHOD(SplDoublyLinkedList, unserialize)
1172 {
1173 	spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
1174 	zval *flags, *elem;
1175 	char *buf;
1176 	size_t buf_len;
1177 	const unsigned char *p, *s;
1178 	php_unserialize_data_t var_hash;
1179 
1180 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1181 		return;
1182 	}
1183 
1184 	if (buf_len == 0) {
1185 		return;
1186 	}
1187 
1188 	while (intern->llist->count > 0) {
1189 		zval tmp;
1190 		spl_ptr_llist_pop(intern->llist, &tmp);
1191 		zval_ptr_dtor(&tmp);
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 %zd of %zd bytes", ((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 	Z_TRY_ADDREF_P(value);
1253 	if (index == intern->llist->count) {
1254 		/* If index is the last entry+1 then we do a push because we're not inserting before any entry */
1255 		spl_ptr_llist_push(intern->llist, value);
1256 	} else {
1257 		/* Create the new element we want to insert */
1258 		spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element));
1259 
1260 		/* Get the element we want to insert before */
1261 		element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
1262 
1263 		ZVAL_COPY_VALUE(&elem->data, value);
1264 		elem->rc   = 1;
1265 		/* connect to the neighbours */
1266 		elem->next = element;
1267 		elem->prev = element->prev;
1268 
1269 		/* connect the neighbours to this new element */
1270 		if (elem->prev == NULL) {
1271 			intern->llist->head = elem;
1272 		} else {
1273 			element->prev->next = elem;
1274 		}
1275 		element->prev = elem;
1276 
1277 		intern->llist->count++;
1278 
1279 		if (intern->llist->ctor) {
1280 			intern->llist->ctor(elem);
1281 		}
1282 	}
1283 } /* }}} */
1284 
1285 /* {{{ iterator handler table */
1286 static const zend_object_iterator_funcs spl_dllist_it_funcs = {
1287 	spl_dllist_it_dtor,
1288 	spl_dllist_it_valid,
1289 	spl_dllist_it_get_current_data,
1290 	spl_dllist_it_get_current_key,
1291 	spl_dllist_it_move_forward,
1292 	spl_dllist_it_rewind,
1293 	NULL
1294 }; /* }}} */
1295 
spl_dllist_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1296 zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1297 {
1298 	spl_dllist_it *iterator;
1299 	spl_dllist_object *dllist_object = Z_SPLDLLIST_P(object);
1300 
1301 	if (by_ref) {
1302 		zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
1303 		return NULL;
1304 	}
1305 
1306 	iterator = emalloc(sizeof(spl_dllist_it));
1307 
1308 	zend_iterator_init((zend_object_iterator*)iterator);
1309 
1310 	ZVAL_COPY(&iterator->intern.it.data, object);
1311 	iterator->intern.it.funcs    = &spl_dllist_it_funcs;
1312 	iterator->intern.ce          = ce;
1313 	iterator->traverse_position  = dllist_object->traverse_position;
1314 	iterator->traverse_pointer   = dllist_object->traverse_pointer;
1315 	iterator->flags              = dllist_object->flags & SPL_DLLIST_IT_MASK;
1316 	ZVAL_UNDEF(&iterator->intern.value);
1317 
1318 	SPL_LLIST_CHECK_ADDREF(iterator->traverse_pointer);
1319 
1320 	return &iterator->intern.it;
1321 }
1322 /* }}} */
1323 
1324 /*  Function/Class/Method definitions */
1325 ZEND_BEGIN_ARG_INFO(arginfo_dllist_setiteratormode, 0)
1326 	ZEND_ARG_INFO(0, flags)
1327 ZEND_END_ARG_INFO()
1328 
1329 ZEND_BEGIN_ARG_INFO(arginfo_dllist_push, 0)
1330 	ZEND_ARG_INFO(0, value)
1331 ZEND_END_ARG_INFO()
1332 
1333 ZEND_BEGIN_ARG_INFO_EX(arginfo_dllist_offsetGet, 0, 0, 1)
1334 	ZEND_ARG_INFO(0, index)
1335 ZEND_END_ARG_INFO()
1336 
1337 ZEND_BEGIN_ARG_INFO_EX(arginfo_dllist_offsetSet, 0, 0, 2)
1338 	ZEND_ARG_INFO(0, index)
1339 	ZEND_ARG_INFO(0, newval)
1340 ZEND_END_ARG_INFO()
1341 
1342 ZEND_BEGIN_ARG_INFO(arginfo_dllist_void, 0)
1343 ZEND_END_ARG_INFO()
1344 
1345 ZEND_BEGIN_ARG_INFO(arginfo_dllist_serialized, 0)
1346 	ZEND_ARG_INFO(0, serialized)
1347 ZEND_END_ARG_INFO();
1348 
1349 static const zend_function_entry spl_funcs_SplQueue[] = {
1350 	SPL_MA(SplQueue, enqueue, SplDoublyLinkedList, push,  arginfo_dllist_push, ZEND_ACC_PUBLIC)
1351 	SPL_MA(SplQueue, dequeue, SplDoublyLinkedList, shift, arginfo_dllist_void, ZEND_ACC_PUBLIC)
1352 	PHP_FE_END
1353 };
1354 
1355 static const zend_function_entry spl_funcs_SplDoublyLinkedList[] = {
1356 	SPL_ME(SplDoublyLinkedList, pop,             arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1357 	SPL_ME(SplDoublyLinkedList, shift,           arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1358 	SPL_ME(SplDoublyLinkedList, push,            arginfo_dllist_push,            ZEND_ACC_PUBLIC)
1359 	SPL_ME(SplDoublyLinkedList, unshift,         arginfo_dllist_push,            ZEND_ACC_PUBLIC)
1360 	SPL_ME(SplDoublyLinkedList, top,             arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1361 	SPL_ME(SplDoublyLinkedList, bottom,          arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1362 	SPL_ME(SplDoublyLinkedList, isEmpty,         arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1363 	SPL_ME(SplDoublyLinkedList, setIteratorMode, arginfo_dllist_setiteratormode, ZEND_ACC_PUBLIC)
1364 	SPL_ME(SplDoublyLinkedList, getIteratorMode, arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1365 	/* Countable */
1366 	SPL_ME(SplDoublyLinkedList, count,           arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1367 	/* ArrayAccess */
1368 	SPL_ME(SplDoublyLinkedList, offsetExists,    arginfo_dllist_offsetGet,       ZEND_ACC_PUBLIC)
1369 	SPL_ME(SplDoublyLinkedList, offsetGet,       arginfo_dllist_offsetGet,       ZEND_ACC_PUBLIC)
1370 	SPL_ME(SplDoublyLinkedList, offsetSet,       arginfo_dllist_offsetSet,       ZEND_ACC_PUBLIC)
1371 	SPL_ME(SplDoublyLinkedList, offsetUnset,     arginfo_dllist_offsetGet,       ZEND_ACC_PUBLIC)
1372 
1373 	SPL_ME(SplDoublyLinkedList, add,             arginfo_dllist_offsetSet,       ZEND_ACC_PUBLIC)
1374 
1375 	/* Iterator */
1376 	SPL_ME(SplDoublyLinkedList, rewind,          arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1377 	SPL_ME(SplDoublyLinkedList, current,         arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1378 	SPL_ME(SplDoublyLinkedList, key,             arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1379 	SPL_ME(SplDoublyLinkedList, next,            arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1380 	SPL_ME(SplDoublyLinkedList, prev,            arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1381 	SPL_ME(SplDoublyLinkedList, valid,           arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1382 	/* Serializable */
1383 	SPL_ME(SplDoublyLinkedList,  unserialize,    arginfo_dllist_serialized,      ZEND_ACC_PUBLIC)
1384 	SPL_ME(SplDoublyLinkedList,  serialize,      arginfo_dllist_void,            ZEND_ACC_PUBLIC)
1385 	PHP_FE_END
1386 };
1387 /* }}} */
1388 
PHP_MINIT_FUNCTION(spl_dllist)1389 PHP_MINIT_FUNCTION(spl_dllist) /* {{{ */
1390 {
1391 	REGISTER_SPL_STD_CLASS_EX(SplDoublyLinkedList, spl_dllist_object_new, spl_funcs_SplDoublyLinkedList);
1392 	memcpy(&spl_handler_SplDoublyLinkedList, &std_object_handlers, sizeof(zend_object_handlers));
1393 
1394 	spl_handler_SplDoublyLinkedList.offset = XtOffsetOf(spl_dllist_object, std);
1395 	spl_handler_SplDoublyLinkedList.clone_obj = spl_dllist_object_clone;
1396 	spl_handler_SplDoublyLinkedList.count_elements = spl_dllist_object_count_elements;
1397 	spl_handler_SplDoublyLinkedList.get_debug_info = spl_dllist_object_get_debug_info;
1398 	spl_handler_SplDoublyLinkedList.get_gc = spl_dllist_object_get_gc;
1399 	spl_handler_SplDoublyLinkedList.dtor_obj = zend_objects_destroy_object;
1400 	spl_handler_SplDoublyLinkedList.free_obj = spl_dllist_object_free_storage;
1401 
1402 	REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_LIFO",  SPL_DLLIST_IT_LIFO);
1403 	REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_FIFO",  0);
1404 	REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_DELETE",SPL_DLLIST_IT_DELETE);
1405 	REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_KEEP",  0);
1406 
1407 	REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Iterator);
1408 	REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Countable);
1409 	REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, ArrayAccess);
1410 	REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Serializable);
1411 
1412 	spl_ce_SplDoublyLinkedList->get_iterator = spl_dllist_get_iterator;
1413 
1414 	REGISTER_SPL_SUB_CLASS_EX(SplQueue,           SplDoublyLinkedList,        spl_dllist_object_new, spl_funcs_SplQueue);
1415 	REGISTER_SPL_SUB_CLASS_EX(SplStack,           SplDoublyLinkedList,        spl_dllist_object_new, NULL);
1416 
1417 	spl_ce_SplQueue->get_iterator = spl_dllist_get_iterator;
1418 	spl_ce_SplStack->get_iterator = spl_dllist_get_iterator;
1419 
1420 	return SUCCESS;
1421 }
1422 /* }}} */
1423 
1424 /*
1425  * Local variables:
1426  * tab-width: 4
1427  * c-basic-offset: 4
1428  * End:
1429  * vim600: fdm=marker
1430  * vim: noet sw=4 ts=4
1431  */
1432