-
Notifications
You must be signed in to change notification settings - Fork 4
/
teds_stricttreemap.c
1805 lines (1585 loc) · 55.9 KB
/
teds_stricttreemap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| teds extension for PHP |
| See COPYING file for further copyright information |
+----------------------------------------------------------------------+
| Author: Tyson Andre <tandre@php.net> |
+----------------------------------------------------------------------+
*/
/* This is a binary search tree. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_exceptions.h"
#include "php_teds.h"
#include "teds_stricttreemap_arginfo.h"
#include "teds_stricttreemap.h"
#include "teds_util.h"
#include "teds_interfaces.h"
#include "teds_exceptions.h"
#include "teds.h"
// #include "ext/spl/spl_functions.h"
#include "ext/spl/spl_engine.h"
#include "ext/spl/spl_exceptions.h"
#include "ext/spl/spl_iterators.h"
#include "ext/json/php_json.h"
#include <stdbool.h>
#define RBTREE_INVARIANT_ASSERT(cond) ZEND_ASSERT((cond))
#define DEBUG_PRINTF(...) do { } while (0)
zend_object_handlers teds_handler_StrictTreeMap;
zend_class_entry *teds_ce_StrictTreeMap;
static zend_always_inline teds_stricttreemap_node *teds_stricttreemap_tree_find_key(const teds_stricttreemap_tree *tree, zval *key)
{
teds_stricttreemap_node *it = tree->root;
while (it != NULL) {
const int comparison = teds_stable_compare(key, &it->key);
if (comparison > 0) {
it = it->right;
} else if (comparison < 0) {
it = it->left;
} else {
return it;
}
}
return NULL;
}
static zend_always_inline void teds_stricttreemap_tree_remove_node(teds_stricttreemap_tree *tree, teds_stricttreemap_node *const node, bool free_zvals);
static zend_always_inline teds_stricttreemap_node *teds_stricttreemap_node_alloc(const zval *key, const zval *value, teds_stricttreemap_node *parent) {
teds_stricttreemap_node *n = emalloc(sizeof(teds_stricttreemap_node));
n->parent = parent;
ZVAL_COPY(&n->key, key);
ZVAL_COPY(&n->value, value);
TEDS_STRICTTREEMAP_NODE_COLOR(n) = TEDS_NODE_RED;
return n;
}
/* Given a node, rotate it so that the child node becomes the parent of that node.
* teds_stricttreemap_tree_rotate_dir_root should be used if node might be the root node. */
static zend_always_inline void teds_stricttreemap_node_rotate_dir(teds_stricttreemap_node *const node, int dir) {
teds_stricttreemap_node *const c = node->children[dir];
teds_stricttreemap_node *const parent = node->parent;
teds_stricttreemap_node *const transfer = c->children[1 - dir];
ZEND_ASSERT(transfer == NULL || transfer->parent == c);
ZEND_ASSERT(parent != NULL);
if (parent->children[dir] == node) {
parent->children[dir] = c;
} else {
ZEND_ASSERT(parent->children[1 - dir] == node);
parent->children[1 - dir] = c;
}
c->parent = parent;
c->children[1 - dir] = node;
node->parent = c;
node->children[dir] = transfer;
if (transfer != NULL) {
transfer->parent = node;
}
}
static zend_always_inline void teds_stricttreemap_tree_rotate_dir_root(teds_stricttreemap_tree *const tree, teds_stricttreemap_node *const node, int dir) {
teds_stricttreemap_node *const c = node->children[dir];
ZEND_ASSERT(c != NULL);
teds_stricttreemap_node *const parent = node->parent;
teds_stricttreemap_node *const transfer = c->children[1 - dir];
ZEND_ASSERT(transfer == NULL || transfer->parent == c);
if (parent != NULL) {
if (parent->children[dir] == node) {
parent->children[dir] = c;
} else {
ZEND_ASSERT(parent->children[1 - dir] == node);
parent->children[1 - dir] = c;
}
} else {
ZEND_ASSERT(tree->root == node);
tree->root = c;
}
c->parent = parent;
c->children[1 - dir] = node;
node->parent = c;
node->children[dir] = transfer;
if (transfer != NULL) {
transfer->parent = node;
}
}
static zend_always_inline void teds_stricttreemap_tree_rebalance_after_insert(teds_stricttreemap_tree *tree, teds_stricttreemap_node *node) {
while (true) {
teds_stricttreemap_node *parent = node->parent;
// ZEND_ASSERT(TEDS_STRICTTREEMAP_NODE_COLOR(node) == TEDS_NODE_RED);
/* Based on https://en.wikipedia.org/wiki/Red%E2%80%93black_tree */
if (parent == NULL || TEDS_STRICTTREEMAP_NODE_COLOR(parent) == TEDS_NODE_BLACK) {
return;
}
teds_stricttreemap_node *const grandparent = parent->parent;
if (grandparent == NULL) {
TEDS_STRICTTREEMAP_NODE_COLOR(parent) = TEDS_NODE_BLACK;
return;
}
/* In a valid tree, the grandparent node would be black if parent was red */
const int dir = grandparent->right == parent ? 1 : 0;
ZEND_ASSERT(grandparent->children[dir] == parent);
teds_stricttreemap_node *uncle;
if (dir) {
uncle = grandparent->left;
} else {
uncle = grandparent->right;
}
if (uncle && TEDS_STRICTTREEMAP_NODE_COLOR(uncle) == TEDS_NODE_RED) {
TEDS_STRICTTREEMAP_NODE_COLOR(uncle) = TEDS_NODE_BLACK;
TEDS_STRICTTREEMAP_NODE_COLOR(parent) = TEDS_NODE_BLACK;
TEDS_STRICTTREEMAP_NODE_COLOR(grandparent) = TEDS_NODE_RED;
node = grandparent;
continue;
}
/* Parent node is red but the uncle node is black. */
/* In a valid tree, the grandparent node would be black */
if (node == parent->children[1 - dir]) {
teds_stricttreemap_node_rotate_dir(parent, 1 - dir);
teds_stricttreemap_node *orig_node = node;
node = parent;
parent = orig_node;
}
ZEND_ASSERT(grandparent->children[dir] == parent);
ZEND_ASSERT(parent->children[dir] == node);
ZEND_ASSERT(parent->parent == grandparent);
ZEND_ASSERT(node->parent == parent);
teds_stricttreemap_tree_rotate_dir_root(tree, grandparent, dir);
TEDS_STRICTTREEMAP_NODE_COLOR(parent) = TEDS_NODE_BLACK;
TEDS_STRICTTREEMAP_NODE_COLOR(grandparent) = TEDS_NODE_RED;
return;
}
}
static zend_always_inline void teds_stricttreemap_node_release(teds_stricttreemap_node *node) {
ZEND_ASSERT(node != NULL);
ZEND_ASSERT(!Z_ISUNDEF(node->key));
efree_size(node, sizeof(teds_stricttreemap_node));
}
/* Returns true if a new entry was added to the map, false if updated. Based on _zend_hash_add_or_update_i. */
static zend_always_inline bool teds_stricttreemap_tree_insert(teds_stricttreemap_tree *tree, zval *key, zval *value, bool add_new)
{
ZEND_ASSERT(Z_TYPE_P(key) != IS_UNDEF);
ZEND_ASSERT(Z_TYPE_P(value) != IS_UNDEF);
teds_stricttreemap_node *it = tree->root;
if (it == NULL) {
/* Initialize this tree as a new binary search tree of size 1 */
it = teds_stricttreemap_node_alloc(key, value, NULL);
tree->root = it;
it->left = NULL;
it->right = NULL;
tree->nNumOfElements++;
TEDS_SET_SHOULD_REBUILD_PROPERTIES(tree, true);
return true;
}
/* c must be declared in outer scope for goto to work. */
teds_stricttreemap_node *c;
while (true) {
const int comparison = teds_stable_compare(key, &it->key);
if (comparison > 0) {
if (it->right == NULL) {
c = teds_stricttreemap_node_alloc(key, value, it);
c->left = NULL;
c->right = NULL;
it->right = c;
finish_insert:
tree->nNumOfElements++;
TEDS_SET_SHOULD_REBUILD_PROPERTIES(tree, true);
if (UNEXPECTED(tree->nNumOfElements >= TEDS_MAX_ZVAL_PAIR_COUNT)) {
/* Mainly, the reason to do that is that get_properties returns the tree of properties for get_gc, which expects a uint32_t in php-src/Zend/zend_gc.c
* A less severe reason is that this is converted to an tree in var_dump/var_export for debugging, but the latter can be avoided */
zend_error_noreturn(E_ERROR, "exceeded max valid Teds\\StrictTreeMap capacity");
ZEND_UNREACHABLE();
return false;
}
teds_stricttreemap_tree_rebalance_after_insert(tree, c);
return true;
}
it = it->right;
} else if ((add_new && !ZEND_DEBUG) || comparison < 0) {
if (it->left == NULL) {
c = teds_stricttreemap_node_alloc(key, value, it);
c->left = NULL;
c->right = NULL;
it->left = c;
goto finish_insert;
}
it = it->left;
} else {
ZEND_ASSERT(!add_new);
/* Overwrite the existing entry in the tree */
zval old_value;
ZVAL_COPY_VALUE(&old_value, &it->value);
ZVAL_COPY(&it->value, value);
zval_ptr_dtor(&old_value);
TEDS_SET_SHOULD_REBUILD_PROPERTIES(tree, true);
return false;
}
}
}
static void teds_stricttreemap_tree_clear(teds_stricttreemap_tree *tree);
/* Used by InternalIterator returned by StrictTreeMap->getIterator() */
typedef struct _teds_stricttreemap_it {
zend_object_iterator intern;
teds_stricttreemap_node *node;
teds_intrusive_dllist_node dllist_node;
bool is_before_first;
} teds_stricttreemap_it;
static zend_always_inline teds_stricttreemap *teds_stricttreemap_from_object(zend_object *obj)
{
return (teds_stricttreemap*)((char*)(obj) - XtOffsetOf(teds_stricttreemap, std));
}
static zend_always_inline teds_stricttreemap_tree *teds_stricttreemap_tree_from_object(zend_object *obj)
{
return &teds_stricttreemap_from_object(obj)->tree;
}
static zend_always_inline teds_stricttreemap_it *teds_stricttreemap_it_from_dllist_node(teds_intrusive_dllist_node *node)
{
return (teds_stricttreemap_it*)((char*)(node) - XtOffsetOf(teds_stricttreemap_it, dllist_node));
}
#define Z_STRICTTREEMAP_P(zv) teds_stricttreemap_from_object(Z_OBJ_P((zv)))
#define Z_STRICTTREEMAP_TREE_P(zv) (&teds_stricttreemap_from_object(Z_OBJ_P((zv)))->tree)
static zend_always_inline bool teds_stricttreemap_tree_uninitialized(teds_stricttreemap_tree *tree)
{
if (tree->initialized) {
return false;
}
ZEND_ASSERT(tree->root == NULL);
ZEND_ASSERT(tree->nNumOfElements == 0);
return true;
}
static zend_always_inline void teds_stricttreemap_tree_set_empty_tree(teds_stricttreemap_tree *tree)
{
tree->root = NULL;
tree->nNumOfElements = 0;
tree->initialized = true;
}
static teds_stricttreemap_node *teds_stricttreemap_node_build_tree_from_sorted_nodes_helper(teds_stricttreemap_node **nodes, const uint32_t n, teds_stricttreemap_node *left_parent, teds_stricttreemap_node *right_parent, int leaf_depth)
{
ZEND_ASSERT(n > 0);
const uint32_t mid = n/2;
teds_stricttreemap_node *const root = nodes[mid];
ZEND_ASSERT(leaf_depth >= 0);
TEDS_STRICTTREEMAP_NODE_COLOR(root) = (leaf_depth == 0 ? TEDS_NODE_RED : TEDS_NODE_BLACK);
leaf_depth--;
{
if (mid > 0) {
teds_stricttreemap_node *const left = teds_stricttreemap_node_build_tree_from_sorted_nodes_helper(nodes, mid, left_parent, root, leaf_depth);
root->left = left;
left->parent = root;
ZEND_ASSERT(root != left);
} else {
root->left = NULL;
}
}
{
const uint32_t right_count = n - mid - 1;
if (right_count > 0) {
teds_stricttreemap_node *const right = teds_stricttreemap_node_build_tree_from_sorted_nodes_helper(nodes + mid + 1, right_count, root, right_parent, leaf_depth);
root->right = right;
ZEND_ASSERT(root != right);
right->parent = root;
} else {
root->right = NULL;
}
}
return root;
}
static teds_stricttreemap_node *teds_stricttreemap_node_build_tree_from_sorted_nodes(teds_stricttreemap_node **nodes, const uint32_t n)
{
ZEND_ASSERT(n >= 1);
int leaf_depth = 1;
uint32_t i = n + 1;
/* for n = 0..2, i=1..3, leaf_depth is 1 (first layer is black) */
/* for n = 3..6, i=4..7, leaf_depth is 2 (first 2 layers are black) */
/* for n = 7..14, i=8..15, leaf_depth is 3 */
while (i >= 4) {
leaf_depth++;
i >>= 1;
}
return teds_stricttreemap_node_build_tree_from_sorted_nodes_helper(nodes, n, NULL, NULL, leaf_depth);
}
void teds_stricttreemap_tree_init_from_array(teds_stricttreemap_tree *tree, zend_array *values)
{
teds_stricttreemap_tree_set_empty_tree(tree);
if (values->nNumOfElements == 0) {
return;
}
TEDS_SET_SHOULD_REBUILD_PROPERTIES(tree, true);
teds_stricttreemap_node **nodes = emalloc(values->nNumOfElements * sizeof(teds_stricttreemap_node*));
teds_stricttreemap_node *prev = NULL;
uint32_t i = 0;
zend_long nkey;
zend_string *skey;
zval *val;
/* Note: The comparison is redundant for a packed tree and can be sped up more. */
ZEND_HASH_FOREACH_KEY_VAL(values, nkey, skey, val) {
zval key;
if (skey) {
ZVAL_STR(&key, skey);
} else {
ZVAL_LONG(&key, nkey);
}
ZVAL_DEREF(val);
if (nodes != NULL) {
if (i == 0 || teds_stable_compare(&key, &prev->key) > 0) {
prev = teds_stricttreemap_node_alloc(&key, val, NULL);
nodes[i] = prev;
i++;
continue;
}
tree->root = teds_stricttreemap_node_build_tree_from_sorted_nodes(nodes, i);
TEDS_SET_SHOULD_REBUILD_PROPERTIES(tree, true);
tree->nNumOfElements = i;
efree(nodes);
nodes = NULL;
}
bool created = teds_stricttreemap_tree_insert(tree, &key, val, true);
ZEND_ASSERT(created);
} ZEND_HASH_FOREACH_END();
if (nodes != NULL) {
tree->root = teds_stricttreemap_node_build_tree_from_sorted_nodes(nodes, i);
tree->nNumOfElements = i;
efree(nodes);
}
}
void teds_stricttreemap_tree_init_from_traversable(teds_stricttreemap_tree *tree, zend_object *obj)
{
zend_class_entry *ce = obj->ce;
zend_object_iterator *iter;
teds_stricttreemap_tree_set_empty_tree(tree);
zval tmp_obj;
ZVAL_OBJ(&tmp_obj, obj);
iter = ce->get_iterator(ce, &tmp_obj, 0);
if (UNEXPECTED(EG(exception))) {
return;
}
const zend_object_iterator_funcs *funcs = iter->funcs;
if (funcs->rewind) {
funcs->rewind(iter);
if (UNEXPECTED(EG(exception))) {
goto cleanup_iter;
}
}
while (funcs->valid(iter) == SUCCESS) {
if (UNEXPECTED(EG(exception))) {
break;
}
zval *value = funcs->get_current_data(iter);
if (UNEXPECTED(EG(exception)) || value == NULL) {
break;
}
zval key;
if (funcs->get_current_key) {
funcs->get_current_key(iter, &key);
if (UNEXPECTED(EG(exception))) {
break;
}
} else {
ZVAL_NULL(&key);
}
ZVAL_DEREF(value);
/* The key's reference count was already increased by get_current_key. We need to free it after attempting to update the entry */
const bool created_new_entry = teds_stricttreemap_tree_insert(tree, &key, value, false);
zval_ptr_dtor(&key);
if (!created_new_entry) {
if (UNEXPECTED(EG(exception))) {
break;
}
}
iter->index++;
funcs->move_forward(iter);
if (UNEXPECTED(EG(exception))) {
break;
}
}
cleanup_iter:
if (iter) {
zend_iterator_dtor(iter);
}
if (UNEXPECTED(EG(exception))) {
teds_stricttreemap_tree_clear(tree);
}
}
static teds_stricttreemap_node *teds_stricttreemap_node_copy_ctor_recursive(const teds_stricttreemap_node *from, teds_stricttreemap_node *parent, teds_stricttreemap_node *left_parent_node, teds_stricttreemap_node *right_parent_node) {
ZEND_ASSERT(from != NULL);
teds_stricttreemap_node *copy = teds_stricttreemap_node_alloc(&from->key, &from->value, parent);
if (from->left) {
copy->left = teds_stricttreemap_node_copy_ctor_recursive(from->left, copy, left_parent_node, copy);
} else {
copy->left = NULL;
}
if (from->right) {
copy->right = teds_stricttreemap_node_copy_ctor_recursive(from->right, copy, copy, right_parent_node);
} else {
copy->right = NULL;
}
return copy;
}
static void teds_stricttreemap_tree_copy_ctor(teds_stricttreemap_tree *to, teds_stricttreemap_tree *from)
{
teds_stricttreemap_tree_set_empty_tree(to);
/* Copy the original tree structure. It will be balanced if the original tree is balanced. */
to->nNumOfElements = from->nNumOfElements;
TEDS_SET_SHOULD_REBUILD_PROPERTIES(to, true);
to->initialized = true;
if (!teds_stricttreemap_tree_empty_size(from)) {
to->root = teds_stricttreemap_node_copy_ctor_recursive(from->root, NULL, NULL, NULL);
} else {
to->root = NULL;
}
}
static void teds_stricttreemap_node_dtor(teds_stricttreemap_node *node)
{
/* Free keys and values in sorted order */
while (node != NULL) {
teds_stricttreemap_node_dtor(node->left);
zval_ptr_dtor(&node->key);
zval_ptr_dtor(&node->value);
teds_stricttreemap_node *right = node->right;
teds_stricttreemap_node_release(node);
node = right;
}
}
/* Destructs and frees contents and the tree itself.
* If you want to re-use the tree then you need to re-initialize it.
*/
void teds_stricttreemap_tree_dtor(teds_stricttreemap_tree *tree)
{
if (teds_stricttreemap_tree_empty_size(tree)) {
return;
}
teds_stricttreemap_node *root = tree->root;
teds_stricttreemap_tree_set_empty_tree(tree);
teds_stricttreemap_node_dtor(root);
}
static HashTable* teds_stricttreemap_get_gc(zend_object *obj, zval **table, int *table_count)
{
teds_stricttreemap *intern = teds_stricttreemap_from_object(obj);
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
if (intern->tree.nNumOfElements > 0) {
zval *key, *val;
TEDS_STRICTTREEMAP_FOREACH_KEY_VAL(&intern->tree, key, val) {
zend_get_gc_buffer_add_zval(gc_buffer, key);
zend_get_gc_buffer_add_zval(gc_buffer, val);
} TEDS_STRICTTREEMAP_FOREACH_END();
}
/* Overwrites table and table_count. The caller of get_gc does not initialize these. */
zend_get_gc_buffer_use(gc_buffer, table, table_count);
return obj->properties;
}
#if PHP_VERSION_ID < 80300
static HashTable* teds_stricttreemap_get_and_populate_properties(zend_object *obj)
{
teds_stricttreemap_tree *const tree = teds_stricttreemap_tree_from_object(obj);
HashTable *ht = zend_std_get_properties(obj);
/* Re-initialize properties array */
/*
* Usually, the reference count of the hash table is 1,
* except during cyclic reference cycles.
*
* Maintain the DEBUG invariant that a hash table isn't modified during iteration,
* and avoid unnecessary work rebuilding a hash table for unmodified properties.
*
* See https://github.com/php/php-src/issues/8079 and tests/Deque/var_export_recursion.phpt
* Also see https://github.com/php/php-src/issues/8044 for alternate considered approaches.
*/
if (!tree->should_rebuild_properties) {
return ht;
}
tree->should_rebuild_properties = false;
if (!tree->nNumOfElements && !zend_hash_num_elements(ht)) {
/* Nothing to add, update, or remove. */
return ht;
}
if (UNEXPECTED(GC_REFCOUNT(ht) > 1)) {
obj->properties = zend_array_dup(ht);
GC_DELREF(ht);
}
// Note that destructors may mutate the original array.
// FIXME create a temporary buffer.
uint32_t i = 0;
zval *key, *value;
TEDS_STRICTTREEMAP_FOREACH_KEY_VAL(tree, key, value) {
zval tmp;
Z_TRY_ADDREF_P(key);
Z_TRY_ADDREF_P(value);
ZVAL_ARR(&tmp, zend_new_pair(key, value));
zend_hash_index_update(ht, i, &tmp);
i++;
} TEDS_STRICTTREEMAP_FOREACH_END();
const uint32_t properties_size = zend_hash_num_elements(ht);
if (UNEXPECTED(properties_size > i)) {
for (; i < properties_size; i++) {
zend_hash_index_del(ht, i);
}
}
#if PHP_VERSION_ID >= 80200
if (HT_IS_PACKED(ht)) {
/* Engine doesn't expect packed array */
zend_hash_packed_to_hash(ht);
}
#endif
return ht;
}
#endif
static zend_array *teds_stricttreemap_tree_to_refcounted_array(const teds_stricttreemap_tree *tree);
static HashTable* teds_stricttreemap_get_properties_for(zend_object *obj, zend_prop_purpose purpose)
{
teds_stricttreemap_tree *tree = teds_stricttreemap_tree_from_object(obj);
if (!tree->nNumOfElements && !obj->properties) {
/* Similar to ext/ffi/ffi.c zend_fake_get_properties */
/* debug_zval_dump DEBUG purpose requires null or a refcounted array. */
return NULL;
}
switch (purpose) {
case ZEND_PROP_PURPOSE_VAR_EXPORT:
case ZEND_PROP_PURPOSE_DEBUG:
#if PHP_VERSION_ID < 80300
{
HashTable *ht = teds_stricttreemap_get_and_populate_properties(obj);
GC_TRY_ADDREF(ht);
return ht;
}
#endif
case ZEND_PROP_PURPOSE_ARRAY_CAST:
case ZEND_PROP_PURPOSE_SERIALIZE:
return teds_stricttreemap_tree_to_refcounted_array(tree);
case ZEND_PROP_PURPOSE_JSON: /* jsonSerialize and get_properties() is used instead. */
default:
ZEND_UNREACHABLE();
return NULL;
}
}
static void teds_stricttreemap_free_storage(zend_object *object)
{
teds_stricttreemap *intern = teds_stricttreemap_from_object(object);
teds_stricttreemap_tree_dtor(&intern->tree);
zend_object_std_dtor(&intern->std);
}
static zend_object *teds_stricttreemap_new_ex(zend_class_entry *class_type, zend_object *orig, bool clone_orig)
{
teds_stricttreemap *intern;
intern = zend_object_alloc(sizeof(teds_stricttreemap), class_type);
/* This is a final class */
ZEND_ASSERT(class_type == teds_ce_StrictTreeMap);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
intern->std.handlers = &teds_handler_StrictTreeMap;
if (orig && clone_orig) {
teds_stricttreemap *other = teds_stricttreemap_from_object(orig);
teds_stricttreemap_tree_copy_ctor(&intern->tree, &other->tree);
} else {
intern->tree.root = NULL;
}
return &intern->std;
}
static zend_object *teds_stricttreemap_new(zend_class_entry *class_type)
{
return teds_stricttreemap_new_ex(class_type, NULL, 0);
}
static zend_object *teds_stricttreemap_clone(zend_object *old_object)
{
zend_object *new_object = teds_stricttreemap_new_ex(old_object->ce, old_object, 1);
teds_assert_object_has_empty_member_list(new_object);
return new_object;
}
static TEDS_COUNT_ELEMENTS_RETURN_TYPE teds_stricttreemap_count_elements(zend_object *object, zend_long *count)
{
teds_stricttreemap *intern;
intern = teds_stricttreemap_from_object(object);
*count = intern->tree.nNumOfElements;
return SUCCESS;
}
/* Get number of entries in this StrictTreeMap */
PHP_METHOD(Teds_StrictTreeMap, count)
{
zval *object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_NONE();
teds_stricttreemap *intern = Z_STRICTTREEMAP_P(object);
RETURN_LONG(intern->tree.nNumOfElements);
}
/* Get whether this StrictTreeMap is empty */
PHP_METHOD(Teds_StrictTreeMap, isEmpty)
{
zval *object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_NONE();
teds_stricttreemap *intern = Z_STRICTTREEMAP_P(object);
RETURN_BOOL(intern->tree.nNumOfElements == 0);
}
/* Create this from an iterable */
PHP_METHOD(Teds_StrictTreeMap, __construct)
{
zval* iterable = NULL;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_ITERABLE(iterable)
ZEND_PARSE_PARAMETERS_END();
teds_stricttreemap *intern = Z_STRICTTREEMAP_P(ZEND_THIS);
if (UNEXPECTED(!teds_stricttreemap_tree_uninitialized(&intern->tree))) {
zend_throw_exception(spl_ce_RuntimeException, "Called Teds\\StrictTreeMap::__construct twice", 0);
/* called __construct() twice, bail out */
RETURN_THROWS();
}
if (iterable == NULL) {
teds_stricttreemap_tree_set_empty_tree(&intern->tree);
return;
}
switch (Z_TYPE_P(iterable)) {
case IS_ARRAY:
teds_stricttreemap_tree_init_from_array(&intern->tree, Z_ARRVAL_P(iterable));
return;
case IS_OBJECT:
teds_stricttreemap_tree_init_from_traversable(&intern->tree, Z_OBJ_P(iterable));
return;
EMPTY_SWITCH_DEFAULT_CASE();
}
}
PHP_METHOD(Teds_StrictTreeMap, getIterator)
{
ZEND_PARSE_PARAMETERS_NONE();
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
}
static void teds_stricttreemap_it_dtor(zend_object_iterator *iter)
{
teds_intrusive_dllist_remove(&Z_STRICTTREEMAP_TREE_P(&iter->data)->active_iterators, &((teds_stricttreemap_it*)iter)->dllist_node);
zval_ptr_dtor(&iter->data);
}
static void teds_stricttreemap_it_rewind(zend_object_iterator *iter)
{
teds_stricttreemap *object = Z_STRICTTREEMAP_P(&iter->data);
teds_stricttreemap_node *new_node = teds_stricttreemap_tree_get_first(&object->tree);
((teds_stricttreemap_it*)iter)->node = new_node;
((teds_stricttreemap_it*)iter)->is_before_first = new_node == NULL;
}
static zend_always_inline bool teds_stricttreemap_node_valid(const teds_stricttreemap_node *node) {
return node != NULL;
}
static int teds_stricttreemap_it_valid(zend_object_iterator *iter)
{
teds_stricttreemap_it *iterator = (teds_stricttreemap_it*)iter;
return teds_stricttreemap_node_valid(iterator->node) ? SUCCESS : FAILURE;
}
static zval *teds_stricttreemap_it_get_current_data(zend_object_iterator *iter)
{
teds_stricttreemap_node *node = ((teds_stricttreemap_it*)iter)->node;
if (teds_stricttreemap_node_valid(node)) {
return &node->value;
} else {
return &EG(uninitialized_zval);
}
}
static void teds_stricttreemap_it_get_current_key(zend_object_iterator *iter, zval *key)
{
teds_stricttreemap_node *node = ((teds_stricttreemap_it*)iter)->node;
if (teds_stricttreemap_node_valid(node)) {
ZVAL_COPY(key, &node->key);
} else {
ZVAL_NULL(key);
}
}
static void teds_stricttreemap_it_move_forward(zend_object_iterator *iter)
{
teds_stricttreemap_it *tree_iter = (teds_stricttreemap_it *)iter;
const teds_stricttreemap_node *const node = tree_iter->node;
if (!teds_stricttreemap_node_valid(node)) {
if (tree_iter->is_before_first) {
teds_stricttreemap_tree *tree = Z_STRICTTREEMAP_TREE_P(&iter->data);
tree_iter->node = teds_stricttreemap_tree_get_first(tree);
tree_iter->is_before_first = false;
}
return;
}
teds_stricttreemap_node *const next = teds_stricttreemap_node_get_next(node);
tree_iter->node = next;
ZEND_ASSERT(!tree_iter->is_before_first);
}
/* iterator handler table */
static const zend_object_iterator_funcs teds_stricttreemap_it_funcs = {
teds_stricttreemap_it_dtor,
teds_stricttreemap_it_valid,
teds_stricttreemap_it_get_current_data,
teds_stricttreemap_it_get_current_key,
teds_stricttreemap_it_move_forward,
teds_stricttreemap_it_rewind,
NULL,
teds_internaliterator_get_gc,
};
zend_object_iterator *teds_stricttreemap_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
{
teds_stricttreemap_it *iterator;
if (UNEXPECTED(by_ref)) {
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}
iterator = emalloc(sizeof(teds_stricttreemap_it));
zend_iterator_init((zend_object_iterator*)iterator);
zend_object *obj = Z_OBJ_P(object);
ZVAL_OBJ_COPY(&iterator->intern.data, obj);
iterator->intern.funcs = &teds_stricttreemap_it_funcs;
iterator->node = teds_stricttreemap_tree_get_first(&Z_STRICTTREEMAP_P(object)->tree);
(void) ce;
teds_intrusive_dllist_prepend(&teds_stricttreemap_tree_from_object(obj)->active_iterators, &iterator->dllist_node);
return &iterator->intern;
}
PHP_METHOD(Teds_StrictTreeMap, __unserialize)
{
HashTable *raw_data;
zval *val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &raw_data) == FAILURE) {
RETURN_THROWS();
}
const uint32_t raw_size = zend_hash_num_elements(raw_data);
if (UNEXPECTED(raw_size % 2 != 0)) {
zend_throw_exception(spl_ce_UnexpectedValueException, "Odd number of elements", 0);
RETURN_THROWS();
}
teds_stricttreemap *const intern = Z_STRICTTREEMAP_P(ZEND_THIS);
teds_stricttreemap_tree *const tree = &intern->tree;
if (UNEXPECTED(!teds_stricttreemap_tree_uninitialized(tree))) {
zend_throw_exception(spl_ce_RuntimeException, "Already unserialized", 0);
RETURN_THROWS();
}
teds_stricttreemap_tree_set_empty_tree(tree);
zend_string *str;
zval key;
bool is_key = true;
teds_stricttreemap_node **nodes = emalloc(raw_size / 2 * sizeof(teds_stricttreemap_node*));
teds_stricttreemap_node *prev = NULL;
uint32_t sorted_nodes_count = 0;
ZEND_HASH_FOREACH_STR_KEY_VAL(raw_data, str, val) {
if (UNEXPECTED(str)) {
teds_stricttreemap_tree_clear(tree);
zend_throw_exception(spl_ce_UnexpectedValueException, "Teds\\StrictTreeMap::__unserialize saw unexpected string key, expected sequence of keys and values", 0);
if (nodes != NULL) {
while (sorted_nodes_count > 0) {
teds_stricttreemap_node *n = nodes[--sorted_nodes_count];
zval_ptr_dtor(&n->key);
zval_ptr_dtor(&n->value);
teds_stricttreemap_node_release(n);
}
efree(nodes);
}
RETURN_THROWS();
}
ZVAL_DEREF(val);
if (!is_key) {
is_key = true;
if (nodes != NULL) {
if (sorted_nodes_count == 0 || teds_stable_compare(&key, &prev->key) > 0) {
prev = teds_stricttreemap_node_alloc(&key, val, NULL);
nodes[sorted_nodes_count] = prev;
sorted_nodes_count++;
continue;
}
tree->root = teds_stricttreemap_node_build_tree_from_sorted_nodes(nodes, sorted_nodes_count);
tree->nNumOfElements = sorted_nodes_count;
efree(nodes);
nodes = NULL;
}
teds_stricttreemap_tree_insert(tree, &key, val, false);
} else {
ZVAL_COPY_VALUE(&key, val);
is_key = false;
}
} ZEND_HASH_FOREACH_END();
if (nodes != NULL) {
tree->root = teds_stricttreemap_node_build_tree_from_sorted_nodes(nodes, sorted_nodes_count);
tree->nNumOfElements = sorted_nodes_count;
efree(nodes);
}
}
static bool teds_stricttreemap_tree_insert_from_pair(teds_stricttreemap_tree *tree, zval *raw_val)
{
ZVAL_DEREF(raw_val);
if (UNEXPECTED(Z_TYPE_P(raw_val) != IS_ARRAY)) {
zend_throw_exception(spl_ce_UnexpectedValueException, "Expected to find pair in array but got non-array", 0);
return false;
}
HashTable *ht = Z_ARRVAL_P(raw_val);
zval *key = zend_hash_index_find(ht, 0);
if (UNEXPECTED(!key)) {
zend_throw_exception(spl_ce_UnexpectedValueException, "Expected to find key at index 0", 0);
return false;
}
zval *value = zend_hash_index_find(ht, 1);
if (UNEXPECTED(!value)) {
zend_throw_exception(spl_ce_UnexpectedValueException, "Expected to find value at index 1", 0);
return false;
}
ZVAL_DEREF(key);
ZVAL_DEREF(value);
teds_stricttreemap_tree_insert(tree, key, value, false);
return true;
}
static void teds_stricttreemap_tree_init_from_array_pairs(teds_stricttreemap_tree *tree, zend_array *raw_data)
{
const uint32_t num_entries = zend_hash_num_elements(raw_data);
if (num_entries == 0) {
teds_stricttreemap_tree_set_empty_tree(tree);
return;
}
teds_stricttreemap_tree_set_empty_tree(tree);
zval *val;
ZEND_HASH_FOREACH_VAL(raw_data, val) {
if (!teds_stricttreemap_tree_insert_from_pair(tree, val)) {
break;
}
} ZEND_HASH_FOREACH_END();
}
static void teds_stricttreemap_tree_init_from_traversable_pairs(teds_stricttreemap_tree *tree, zend_object *obj)
{
zend_class_entry *ce = obj->ce;
zend_object_iterator *iter;
teds_stricttreemap_tree_set_empty_tree(tree);
zval tmp_obj;
ZVAL_OBJ(&tmp_obj, obj);
iter = ce->get_iterator(ce, &tmp_obj, 0);
if (UNEXPECTED(EG(exception))) {
return;
}
const zend_object_iterator_funcs *funcs = iter->funcs;
if (funcs->rewind) {
funcs->rewind(iter);
if (UNEXPECTED(EG(exception))) {
return;
}
}
while (funcs->valid(iter) == SUCCESS) {
if (EG(exception)) {
break;
}
zval *pair = funcs->get_current_data(iter);
if (UNEXPECTED(EG(exception))) {
break;
}
if (!teds_stricttreemap_tree_insert_from_pair(tree, pair)) {
break;
}
iter->index++;
funcs->move_forward(iter);
if (EG(exception)) {
break;
}
}
if (iter) {
zend_iterator_dtor(iter);
}
}
static zend_object* create_from_pairs(zval *iterable) {