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