/
erl_mmap.c
2960 lines (2585 loc) · 78.3 KB
/
erl_mmap.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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2002-2024. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#define ERTS_WANT_MEM_MAPPERS
#include "sys.h"
#include "erl_process.h"
#include "atom.h"
#include "erl_mmap.h"
#include <stddef.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
int erts_mem_guard(void *p, UWord size) {
#if defined(WIN32)
DWORD oldProtect;
BOOL success;
success = VirtualProtect((LPVOID*)p,
size,
PAGE_NOACCESS,
&oldProtect);
return success ? 0 : -1;
#elif defined(HAVE_SYS_MMAN_H)
return mprotect(p, size, PROT_NONE);
#else
errno = ENOTSUP;
return -1;
#endif
}
#if HAVE_ERTS_MMAP
/* #define ERTS_MMAP_OP_RINGBUF_SZ 100 */
#if defined(DEBUG) || 0
# undef ERTS_MMAP_DEBUG
# define ERTS_MMAP_DEBUG
# ifndef ERTS_MMAP_OP_RINGBUF_SZ
# define ERTS_MMAP_OP_RINGBUF_SZ 100
# endif
#endif
#ifndef ERTS_MMAP_OP_RINGBUF_SZ
# define ERTS_MMAP_OP_RINGBUF_SZ 0
#endif
/* #define ERTS_MMAP_DEBUG_FILL_AREAS */
#ifdef ERTS_MMAP_DEBUG
# define ERTS_MMAP_ASSERT ERTS_ASSERT
#else
# define ERTS_MMAP_ASSERT(A) ((void) 1)
#endif
/*
* `mm->sa.bot` and `mm->sua.top` are read only after
* initialization, but the other pointers are not; i.e., only
* ERTS_MMAP_IN_SUPERCARRIER() is allowed without the mutex held.
*/
#define ERTS_MMAP_IN_SUPERCARRIER(PTR) \
(((UWord) (PTR)) - ((UWord) mm->sa.bot) \
< ((UWord) mm->sua.top) - ((UWord) mm->sa.bot))
#define ERTS_MMAP_IN_SUPERALIGNED_AREA(PTR) \
(ERTS_LC_ASSERT(erts_lc_mtx_is_locked(&mm->mtx)), \
(((UWord) (PTR)) - ((UWord) mm->sa.bot) \
< ((UWord) mm->sa.top) - ((UWord) mm->sa.bot)))
#define ERTS_MMAP_IN_SUPERUNALIGNED_AREA(PTR) \
(ERTS_LC_ASSERT(erts_lc_mtx_is_locked(&mm->mtx)), \
(((UWord) (PTR)) - ((UWord) mm->sua.bot) \
< ((UWord) mm->sua.top) - ((UWord) mm->sua.bot)))
UWord erts_page_inv_mask;
#if defined(DEBUG) || defined(ERTS_MMAP_DEBUG)
# undef RBT_DEBUG
# define RBT_DEBUG
#endif
#ifdef RBT_DEBUG
# define RBT_ASSERT ERTS_ASSERT
# define IF_RBT_DEBUG(C) C
#else
# define RBT_ASSERT(x)
# define IF_RBT_DEBUG(C)
#endif
typedef struct RBTNode_ RBTNode;
struct RBTNode_ {
UWord parent_and_color; /* color in bit 0 of parent ptr */
RBTNode *left;
RBTNode *right;
};
#define RED_FLG (1)
#define IS_RED(N) ((N) && ((N)->parent_and_color & RED_FLG))
#define IS_BLACK(N) (!IS_RED(N))
#define SET_RED(N) ((N)->parent_and_color |= RED_FLG)
#define SET_BLACK(N) ((N)->parent_and_color &= ~RED_FLG)
static ERTS_INLINE RBTNode* parent(RBTNode* node)
{
return (RBTNode*) (node->parent_and_color & ~RED_FLG);
}
static ERTS_INLINE void set_parent(RBTNode* node, RBTNode* parent)
{
RBT_ASSERT(!((UWord)parent & RED_FLG));
node->parent_and_color = ((UWord)parent) | (node->parent_and_color & RED_FLG);
}
static ERTS_INLINE UWord parent_and_color(RBTNode* parent, int color)
{
RBT_ASSERT(!((UWord)parent & RED_FLG));
RBT_ASSERT(!(color & ~RED_FLG));
return ((UWord)parent) | color;
}
enum SortOrder {
ADDR_ORDER, /* only address order */
SA_SZ_ADDR_ORDER, /* first super-aligned size then address order */
SZ_REVERSE_ADDR_ORDER /* first size then reverse address order */
};
#ifdef HARD_DEBUG
static const char* sort_order_names[] = {"Address","SuperAlignedSize-Address","Size-RevAddress"};
#endif
typedef struct {
RBTNode* root;
enum SortOrder order;
}RBTree;
#ifdef HARD_DEBUG
# define HARD_CHECK_IS_MEMBER(ROOT,NODE) rbt_assert_is_member(ROOT,NODE)
# define HARD_CHECK_TREE(TREE,SZ) check_tree(TREE, SZ)
static int rbt_assert_is_member(RBTNode* root, RBTNode* node);
static RBTNode* check_tree(RBTree* tree, Uint);
#else
# define HARD_CHECK_IS_MEMBER(ROOT,NODE)
# define HARD_CHECK_TREE(TREE,SZ)
#endif
#if ERTS_MMAP_OP_RINGBUF_SZ
static int mmap_op_ix;
typedef enum {
ERTS_OP_TYPE_NONE,
ERTS_OP_TYPE_MMAP,
ERTS_OP_TYPE_MUNMAP,
ERTS_OP_TYPE_MREMAP
} ErtsMMapOpType;
typedef struct {
ErtsMMapOpType type;
void *result;
UWord in_size;
UWord out_size;
void *old_ptr;
UWord old_size;
} ErtsMMapOp;
static ErtsMMapOp mmap_ops[ERTS_MMAP_OP_RINGBUF_SZ];
#define ERTS_MMAP_OP_RINGBUF_INIT() \
do { \
int ix__; \
for (ix__ = 0; ix__ < ERTS_MMAP_OP_RINGBUF_SZ; ix__++) {\
mmap_ops[ix__].type = ERTS_OP_TYPE_NONE; \
mmap_ops[ix__].result = NULL; \
mmap_ops[ix__].in_size = 0; \
mmap_ops[ix__].out_size = 0; \
mmap_ops[ix__].old_ptr = NULL; \
mmap_ops[ix__].old_size = 0; \
} \
mmap_op_ix = ERTS_MMAP_OP_RINGBUF_SZ-1; \
} while (0)
#define ERTS_MMAP_OP_START(SZ) \
do { \
int ix__; \
if (++mmap_op_ix >= ERTS_MMAP_OP_RINGBUF_SZ) \
mmap_op_ix = 0; \
ix__ = mmap_op_ix; \
mmap_ops[ix__].type = ERTS_OP_TYPE_MMAP; \
mmap_ops[ix__].result = NULL; \
mmap_ops[ix__].in_size = (SZ); \
mmap_ops[ix__].out_size = 0; \
mmap_ops[ix__].old_ptr = NULL; \
mmap_ops[ix__].old_size = 0; \
} while (0)
#define ERTS_MMAP_OP_END(PTR, SZ) \
do { \
int ix__ = mmap_op_ix; \
mmap_ops[ix__].result = (PTR); \
mmap_ops[ix__].out_size = (SZ); \
} while (0)
#define ERTS_MMAP_OP_LCK(RES, IN_SZ, OUT_SZ) \
do { \
erts_mtx_lock(&mm->mtx); \
ERTS_MMAP_OP_START((IN_SZ)); \
ERTS_MMAP_OP_END((RES), (OUT_SZ)); \
erts_mtx_unlock(&mm->mtx); \
} while (0)
#define ERTS_MUNMAP_OP(PTR, SZ) \
do { \
int ix__; \
if (++mmap_op_ix >= ERTS_MMAP_OP_RINGBUF_SZ) \
mmap_op_ix = 0; \
ix__ = mmap_op_ix; \
mmap_ops[ix__].type = ERTS_OP_TYPE_MUNMAP; \
mmap_ops[ix__].result = NULL; \
mmap_ops[ix__].in_size = 0; \
mmap_ops[ix__].out_size = 0; \
mmap_ops[ix__].old_ptr = (PTR); \
mmap_ops[ix__].old_size = (SZ); \
} while (0)
#define ERTS_MUNMAP_OP_LCK(PTR, SZ) \
do { \
erts_mtx_lock(&mm->mtx); \
ERTS_MUNMAP_OP((PTR), (SZ)); \
erts_mtx_unlock(&mm->mtx); \
} while (0)
#define ERTS_MREMAP_OP_START(OLD_PTR, OLD_SZ, IN_SZ) \
do { \
int ix__; \
if (++mmap_op_ix >= ERTS_MMAP_OP_RINGBUF_SZ) \
mmap_op_ix = 0; \
ix__ = mmap_op_ix; \
mmap_ops[ix__].type = ERTS_OP_TYPE_MREMAP; \
mmap_ops[ix__].result = NULL; \
mmap_ops[ix__].in_size = (IN_SZ); \
mmap_ops[ix__].out_size = (OLD_SZ); \
mmap_ops[ix__].old_ptr = (OLD_PTR); \
mmap_ops[ix__].old_size = (OLD_SZ); \
} while (0)
#define ERTS_MREMAP_OP_END(PTR, SZ) \
do { \
int ix__ = mmap_op_ix; \
mmap_ops[ix__].result = (PTR); \
mmap_ops[mmap_op_ix].out_size = (SZ); \
} while (0)
#define ERTS_MREMAP_OP_LCK(RES, OLD_PTR, OLD_SZ, IN_SZ, OUT_SZ) \
do { \
erts_mtx_lock(&mm->mtx); \
ERTS_MREMAP_OP_START((OLD_PTR), (OLD_SZ), (IN_SZ)); \
ERTS_MREMAP_OP_END((RES), (OUT_SZ)); \
erts_mtx_unlock(&mm->mtx); \
} while (0)
#define ERTS_MMAP_OP_ABORT() \
do { \
int ix__ = mmap_op_ix; \
mmap_ops[ix__].type = ERTS_OP_TYPE_NONE; \
mmap_ops[ix__].result = NULL; \
mmap_ops[ix__].in_size = 0; \
mmap_ops[ix__].out_size = 0; \
mmap_ops[ix__].old_ptr = NULL; \
mmap_ops[ix__].old_size = 0; \
if (--mmap_op_ix < 0) \
mmap_op_ix = ERTS_MMAP_OP_RINGBUF_SZ-1; \
} while (0)
#else
#define ERTS_MMAP_OP_RINGBUF_INIT()
#define ERTS_MMAP_OP_START(SZ)
#define ERTS_MMAP_OP_END(PTR, SZ)
#define ERTS_MMAP_OP_LCK(RES, IN_SZ, OUT_SZ)
#define ERTS_MUNMAP_OP(PTR, SZ)
#define ERTS_MUNMAP_OP_LCK(PTR, SZ)
#define ERTS_MREMAP_OP_START(OLD_PTR, OLD_SZ, IN_SZ)
#define ERTS_MREMAP_OP_END(PTR, SZ)
#define ERTS_MREMAP_OP_LCK(RES, OLD_PTR, OLD_SZ, IN_SZ, OUT_SZ)
#define ERTS_MMAP_OP_ABORT()
#endif
typedef struct {
RBTNode snode; /* node in 'stree' */
RBTNode anode; /* node in 'atree' */
char* start;
char* end;
}ErtsFreeSegDesc;
typedef struct {
RBTree stree; /* size ordered tree */
RBTree atree; /* address ordered tree */
Uint nseg;
}ErtsFreeSegMap;
struct ErtsMemMapper_ {
int (*reserve_physical)(char *, UWord);
void (*unreserve_physical)(char *, UWord);
int supercarrier;
int no_os_mmap;
/*
* Super unaligned area is located above super aligned
* area. That is, `sa.bot` is beginning of the super
* carrier, `sua.top` is the end of the super carrier,
* and sa.top and sua.bot moves towards each other.
*/
struct {
char *top;
char *bot;
ErtsFreeSegMap map;
} sua;
struct {
char *top;
char *bot;
ErtsFreeSegMap map;
} sa;
#if HAVE_MMAP && (!defined(MAP_ANON) && !defined(MAP_ANONYMOUS))
int mmap_fd;
#endif
erts_mtx_t mtx;
struct {
char *free_list;
char *unused_start;
char *unused_end;
char *new_area_hint;
Uint reserved;
} desc;
struct {
UWord free_seg_descs;
struct {
UWord curr;
UWord max;
} free_segs;
} no;
struct {
struct {
UWord total;
struct {
UWord total;
UWord sa;
UWord sua;
} used;
} supercarrier;
struct {
UWord used;
} os;
} size;
};
ErtsMemMapper erts_dflt_mmapper;
#if defined(ARCH_64) && defined(ERTS_HAVE_OS_PHYSICAL_MEMORY_RESERVATION)
ErtsMemMapper erts_literal_mmapper;
char* erts_literals_start;
UWord erts_literals_size;
#endif
#define ERTS_MMAP_SIZE_SC_SA_INC(SZ) \
do { \
mm->size.supercarrier.used.total += (SZ); \
mm->size.supercarrier.used.sa += (SZ); \
ERTS_MMAP_ASSERT(mm->size.supercarrier.used.total \
<= mm->size.supercarrier.total); \
ERTS_MMAP_ASSERT(mm->size.supercarrier.used.sa \
<= mm->size.supercarrier.used.total); \
} while (0)
#define ERTS_MMAP_SIZE_SC_SA_DEC(SZ) \
do { \
ERTS_MMAP_ASSERT(mm->size.supercarrier.used.total >= (SZ)); \
mm->size.supercarrier.used.total -= (SZ); \
ERTS_MMAP_ASSERT(mm->size.supercarrier.used.sa >= (SZ)); \
mm->size.supercarrier.used.sa -= (SZ); \
} while (0)
#define ERTS_MMAP_SIZE_SC_SUA_INC(SZ) \
do { \
mm->size.supercarrier.used.total += (SZ); \
mm->size.supercarrier.used.sua += (SZ); \
ERTS_MMAP_ASSERT(mm->size.supercarrier.used.total \
<= mm->size.supercarrier.total); \
ERTS_MMAP_ASSERT(mm->size.supercarrier.used.sua \
<= mm->size.supercarrier.used.total); \
} while (0)
#define ERTS_MMAP_SIZE_SC_SUA_DEC(SZ) \
do { \
ERTS_MMAP_ASSERT(mm->size.supercarrier.used.total >= (SZ)); \
mm->size.supercarrier.used.total -= (SZ); \
ERTS_MMAP_ASSERT(mm->size.supercarrier.used.sua >= (SZ)); \
mm->size.supercarrier.used.sua -= (SZ); \
} while (0)
#define ERTS_MMAP_SIZE_OS_INC(SZ) \
do { \
ERTS_MMAP_ASSERT(mm->size.os.used + (SZ) >= (SZ)); \
mm->size.os.used += (SZ); \
} while (0)
#define ERTS_MMAP_SIZE_OS_DEC(SZ) \
do { \
ERTS_MMAP_ASSERT(mm->size.os.used >= (SZ)); \
mm->size.os.used -= (SZ); \
} while (0)
static void
add_free_desc_area(ErtsMemMapper* mm, char *start, char *end)
{
ERTS_MMAP_ASSERT(end == (void *) 0 || end > start);
if (sizeof(ErtsFreeSegDesc) <= ((UWord) end) - ((UWord) start)) {
UWord no;
ErtsFreeSegDesc *prev_desc, *desc;
char *desc_end;
no = 1;
prev_desc = (ErtsFreeSegDesc *) start;
prev_desc->start = mm->desc.free_list;
desc = (ErtsFreeSegDesc *) (start + sizeof(ErtsFreeSegDesc));
desc_end = start + 2*sizeof(ErtsFreeSegDesc);
while (desc_end <= end) {
desc->start = (char *) prev_desc;
prev_desc = desc;
desc = (ErtsFreeSegDesc *) desc_end;
desc_end += sizeof(ErtsFreeSegDesc);
no++;
}
mm->desc.free_list = (char *) prev_desc;
mm->no.free_seg_descs += no;
}
}
static ErtsFreeSegDesc *
add_unused_free_desc_area(ErtsMemMapper* mm)
{
char *ptr;
if (!mm->desc.unused_start)
return NULL;
ERTS_MMAP_ASSERT(mm->desc.unused_end);
ERTS_MMAP_ASSERT(ERTS_PAGEALIGNED_SIZE
<= mm->desc.unused_end - mm->desc.unused_start);
ptr = mm->desc.unused_start + ERTS_PAGEALIGNED_SIZE;
add_free_desc_area(mm, mm->desc.unused_start, ptr);
if ((mm->desc.unused_end - ptr) >= ERTS_PAGEALIGNED_SIZE)
mm->desc.unused_start = ptr;
else
mm->desc.unused_end = mm->desc.unused_start = NULL;
ERTS_MMAP_ASSERT(mm->desc.free_list);
return (ErtsFreeSegDesc *) mm->desc.free_list;
}
static ERTS_INLINE ErtsFreeSegDesc *
alloc_desc(ErtsMemMapper* mm)
{
ErtsFreeSegDesc *res;
res = (ErtsFreeSegDesc *) mm->desc.free_list;
if (!res) {
res = add_unused_free_desc_area(mm);
if (!res)
return NULL;
}
mm->desc.free_list = res->start;
ASSERT(mm->no.free_segs.curr < mm->no.free_seg_descs);
mm->no.free_segs.curr++;
if (mm->no.free_segs.max < mm->no.free_segs.curr)
mm->no.free_segs.max = mm->no.free_segs.curr;
return res;
}
static ERTS_INLINE void
free_desc(ErtsMemMapper* mm, ErtsFreeSegDesc *desc)
{
desc->start = mm->desc.free_list;
mm->desc.free_list = (char *) desc;
ERTS_MMAP_ASSERT(mm->no.free_segs.curr > 0);
mm->no.free_segs.curr--;
}
static ERTS_INLINE ErtsFreeSegDesc* anode_to_desc(RBTNode* anode)
{
return (ErtsFreeSegDesc*) ((char*)anode - offsetof(ErtsFreeSegDesc, anode));
}
static ERTS_INLINE ErtsFreeSegDesc* snode_to_desc(RBTNode* snode)
{
return (ErtsFreeSegDesc*) ((char*)snode - offsetof(ErtsFreeSegDesc, snode));
}
static ERTS_INLINE ErtsFreeSegDesc* node_to_desc(enum SortOrder order, RBTNode* node)
{
return order==ADDR_ORDER ? anode_to_desc(node) : snode_to_desc(node);
}
static ERTS_INLINE SWord usable_size(enum SortOrder order,
ErtsFreeSegDesc* desc)
{
return ((order == SA_SZ_ADDR_ORDER) ?
ERTS_SUPERALIGNED_FLOOR(desc->end) - ERTS_SUPERALIGNED_CEILING(desc->start)
: desc->end - desc->start);
}
#ifdef HARD_DEBUG
static ERTS_INLINE SWord cmp_nodes(enum SortOrder order,
RBTNode* lhs, RBTNode* rhs)
{
ErtsFreeSegDesc* ldesc = node_to_desc(order, lhs);
ErtsFreeSegDesc* rdesc = node_to_desc(order, rhs);
RBT_ASSERT(lhs != rhs);
if (order != ADDR_ORDER) {
SWord diff = usable_size(order, ldesc) - usable_size(order, rdesc);
if (diff) return diff;
}
if (order != SZ_REVERSE_ADDR_ORDER) {
return (char*)ldesc->start - (char*)rdesc->start;
}
else {
return (char*)rdesc->start - (char*)ldesc->start;
}
}
#endif /* HARD_DEBUG */
static ERTS_INLINE SWord cmp_with_node(enum SortOrder order,
SWord sz, char* addr, RBTNode* rhs)
{
ErtsFreeSegDesc* rdesc;
if (order != ADDR_ORDER) {
SWord diff;
rdesc = snode_to_desc(rhs);
diff = sz - usable_size(order, rdesc);
if (diff) return diff;
}
else
rdesc = anode_to_desc(rhs);
if (order != SZ_REVERSE_ADDR_ORDER)
return addr - (char*)rdesc->start;
else
return (char*)rdesc->start - addr;
}
static ERTS_INLINE void
left_rotate(RBTNode **root, RBTNode *x)
{
RBTNode *y = x->right;
x->right = y->left;
if (y->left)
set_parent(y->left, x);
set_parent(y, parent(x));
if (!parent(y)) {
RBT_ASSERT(*root == x);
*root = y;
}
else if (x == parent(x)->left)
parent(x)->left = y;
else {
RBT_ASSERT(x == parent(x)->right);
parent(x)->right = y;
}
y->left = x;
set_parent(x, y);
}
static ERTS_INLINE void
right_rotate(RBTNode **root, RBTNode *x)
{
RBTNode *y = x->left;
x->left = y->right;
if (y->right)
set_parent(y->right, x);
set_parent(y, parent(x));
if (!parent(y)) {
RBT_ASSERT(*root == x);
*root = y;
}
else if (x == parent(x)->right)
parent(x)->right = y;
else {
RBT_ASSERT(x == parent(x)->left);
parent(x)->left = y;
}
y->right = x;
set_parent(x, y);
}
/*
* Replace node x with node y
* NOTE: segment descriptor of y is not changed
*/
static ERTS_INLINE void
replace(RBTNode **root, RBTNode *x, RBTNode *y)
{
if (!parent(x)) {
RBT_ASSERT(*root == x);
*root = y;
}
else if (x == parent(x)->left)
parent(x)->left = y;
else {
RBT_ASSERT(x == parent(x)->right);
parent(x)->right = y;
}
if (x->left) {
RBT_ASSERT(parent(x->left) == x);
set_parent(x->left, y);
}
if (x->right) {
RBT_ASSERT(parent(x->right) == x);
set_parent(x->right, y);
}
y->parent_and_color = x->parent_and_color;
y->right = x->right;
y->left = x->left;
}
static void
tree_insert_fixup(RBTNode** root, RBTNode *node)
{
RBTNode *x = node, *y, *papa_x, *granpa_x;
/*
* Rearrange the tree so that it satisfies the Red-Black Tree properties
*/
papa_x = parent(x);
RBT_ASSERT(x != *root && IS_RED(papa_x));
do {
/*
* x and its parent are both red. Move the red pair up the tree
* until we get to the root or until we can separate them.
*/
granpa_x = parent(papa_x);
RBT_ASSERT(IS_RED(x));
RBT_ASSERT(IS_BLACK(granpa_x));
RBT_ASSERT(granpa_x);
if (papa_x == granpa_x->left) {
y = granpa_x->right;
if (IS_RED(y)) {
SET_BLACK(y);
SET_BLACK(papa_x);
SET_RED(granpa_x);
x = granpa_x;
}
else {
if (x == papa_x->right) {
left_rotate(root, papa_x);
papa_x = x;
x = papa_x->left;
}
RBT_ASSERT(x == granpa_x->left->left);
RBT_ASSERT(IS_RED(x));
RBT_ASSERT(IS_RED(papa_x));
RBT_ASSERT(IS_BLACK(granpa_x));
RBT_ASSERT(IS_BLACK(y));
SET_BLACK(papa_x);
SET_RED(granpa_x);
right_rotate(root, granpa_x);
RBT_ASSERT(x == parent(x)->left);
RBT_ASSERT(IS_RED(x));
RBT_ASSERT(IS_RED(parent(x)->right));
RBT_ASSERT(IS_BLACK(parent(x)));
break;
}
}
else {
RBT_ASSERT(papa_x == granpa_x->right);
y = granpa_x->left;
if (IS_RED(y)) {
SET_BLACK(y);
SET_BLACK(papa_x);
SET_RED(granpa_x);
x = granpa_x;
}
else {
if (x == papa_x->left) {
right_rotate(root, papa_x);
papa_x = x;
x = papa_x->right;
}
RBT_ASSERT(x == granpa_x->right->right);
RBT_ASSERT(IS_RED(x));
RBT_ASSERT(IS_RED(papa_x));
RBT_ASSERT(IS_BLACK(granpa_x));
RBT_ASSERT(IS_BLACK(y));
SET_BLACK(papa_x);
SET_RED(granpa_x);
left_rotate(root, granpa_x);
RBT_ASSERT(x == parent(x)->right);
RBT_ASSERT(IS_RED(x));
RBT_ASSERT(IS_RED(parent(x)->left));
RBT_ASSERT(IS_BLACK(parent(x)));
break;
}
}
} while (x != *root && (papa_x=parent(x), IS_RED(papa_x)));
SET_BLACK(*root);
}
static void
rbt_delete(RBTree* tree, RBTNode* del)
{
Uint spliced_is_black;
RBTNode *x, *y, *z = del, *papa_y;
RBTNode null_x; /* null_x is used to get the fixup started when we
splice out a node without children. */
HARD_CHECK_IS_MEMBER(tree->root, del);
HARD_CHECK_TREE(tree, 0);
null_x.parent_and_color = parent_and_color(NULL, !RED_FLG);
/* Remove node from tree... */
/* Find node to splice out */
if (!z->left || !z->right)
y = z;
else
/* Set y to z:s successor */
for(y = z->right; y->left; y = y->left)
;
/* splice out y */
x = y->left ? y->left : y->right;
spliced_is_black = IS_BLACK(y);
papa_y = parent(y);
if (x) {
set_parent(x, papa_y);
}
else if (spliced_is_black) {
x = &null_x;
x->right = x->left = NULL;
x->parent_and_color = parent_and_color(papa_y, !RED_FLG);
y->left = x;
}
if (!papa_y) {
RBT_ASSERT(tree->root == y);
tree->root = x;
}
else {
if (y == papa_y->left) {
papa_y->left = x;
}
else {
RBT_ASSERT(y == papa_y->right);
papa_y->right = x;
}
}
if (y != z) {
/* We spliced out the successor of z; replace z by the successor */
RBT_ASSERT(z != &null_x);
replace(&tree->root, z, y);
}
if (spliced_is_black) {
RBTNode* papa_x;
/* We removed a black node which makes the resulting tree
violate the Red-Black Tree properties. Fixup tree... */
papa_x = parent(x);
while (IS_BLACK(x) && papa_x) {
/*
* x has an "extra black" which we move up the tree
* until we reach the root or until we can get rid of it.
*
* y is the sibbling of x
*/
if (x == papa_x->left) {
y = papa_x->right;
RBT_ASSERT(y);
if (IS_RED(y)) {
RBT_ASSERT(y->right);
RBT_ASSERT(y->left);
SET_BLACK(y);
RBT_ASSERT(IS_BLACK(papa_x));
SET_RED(papa_x);
left_rotate(&tree->root, papa_x);
RBT_ASSERT(papa_x == parent(x));
y = papa_x->right;
}
RBT_ASSERT(y);
RBT_ASSERT(IS_BLACK(y));
if (IS_BLACK(y->left) && IS_BLACK(y->right)) {
SET_RED(y);
}
else {
if (IS_BLACK(y->right)) {
SET_BLACK(y->left);
SET_RED(y);
right_rotate(&tree->root, y);
RBT_ASSERT(papa_x == parent(x));
y = papa_x->right;
}
RBT_ASSERT(y);
if (IS_RED(papa_x)) {
SET_BLACK(papa_x);
SET_RED(y);
}
RBT_ASSERT(y->right);
SET_BLACK(y->right);
left_rotate(&tree->root, papa_x);
x = tree->root;
break;
}
}
else {
RBT_ASSERT(x == papa_x->right);
y = papa_x->left;
RBT_ASSERT(y);
if (IS_RED(y)) {
RBT_ASSERT(y->right);
RBT_ASSERT(y->left);
SET_BLACK(y);
RBT_ASSERT(IS_BLACK(papa_x));
SET_RED(papa_x);
right_rotate(&tree->root, papa_x);
RBT_ASSERT(papa_x == parent(x));
y = papa_x->left;
}
RBT_ASSERT(y);
RBT_ASSERT(IS_BLACK(y));
if (IS_BLACK(y->right) && IS_BLACK(y->left)) {
SET_RED(y);
}
else {
if (IS_BLACK(y->left)) {
SET_BLACK(y->right);
SET_RED(y);
left_rotate(&tree->root, y);
RBT_ASSERT(papa_x == parent(x));
y = papa_x->left;
}
RBT_ASSERT(y);
if (IS_RED(papa_x)) {
SET_BLACK(papa_x);
SET_RED(y);
}
RBT_ASSERT(y->left);
SET_BLACK(y->left);
right_rotate(&tree->root, papa_x);
x = tree->root;
break;
}
}
x = papa_x;
papa_x = parent(x);
}
SET_BLACK(x);
papa_x = parent(&null_x);
if (papa_x) {
if (papa_x->left == &null_x)
papa_x->left = NULL;
else {
RBT_ASSERT(papa_x->right == &null_x);
papa_x->right = NULL;
}
RBT_ASSERT(!null_x.left);
RBT_ASSERT(!null_x.right);
}
else if (tree->root == &null_x) {
tree->root = NULL;
RBT_ASSERT(!null_x.left);
RBT_ASSERT(!null_x.right);
}
}
HARD_CHECK_TREE(tree, 0);
}
static void
rbt_insert(RBTree* tree, RBTNode* node)
{
#ifdef RBT_DEBUG
ErtsFreeSegDesc *dbg_under=NULL, *dbg_over=NULL;
#endif
ErtsFreeSegDesc* desc = node_to_desc(tree->order, node);
char* seg_addr = desc->start;
SWord seg_sz = desc->end - desc->start;
HARD_CHECK_TREE(tree, 0);
node->left = NULL;
node->right = NULL;
if (!tree->root) {
node->parent_and_color = parent_and_color(NULL, !RED_FLG);
tree->root = node;
}
else {
RBTNode *x = tree->root;
while (1) {
SWord diff = cmp_with_node(tree->order, seg_sz, seg_addr, x);
if (diff < 0) {
IF_RBT_DEBUG(dbg_over = node_to_desc(tree->order, x));
if (!x->left) {
node->parent_and_color = parent_and_color(x, RED_FLG);
x->left = node;
break;
}
x = x->left;
}
else {
RBT_ASSERT(diff > 0);
IF_RBT_DEBUG(dbg_under = node_to_desc(tree->order, x));
if (!x->right) {
node->parent_and_color = parent_and_color(x, RED_FLG);
x->right = node;
break;
}
x = x->right;
}
}
RBT_ASSERT(parent(node));
#ifdef RBT_DEBUG
if (tree->order == ADDR_ORDER) {
RBT_ASSERT(!dbg_under || dbg_under->end < desc->start);
RBT_ASSERT(!dbg_over || dbg_over->start > desc->end);
}
#endif
RBT_ASSERT(IS_RED(node));
if (IS_RED(parent(node)))
tree_insert_fixup(&tree->root, node);
}
HARD_CHECK_TREE(tree, 0);
}
/*
* Traverse tree in (reverse) sorting order
*/
static void
rbt_foreach_node(RBTree* tree,
void (*fn)(RBTNode*,void*),
void* arg, int reverse)
{
#ifdef HARD_DEBUG
Sint blacks = -1;
Sint curr_blacks = 1;
Uint depth = 1;
Uint max_depth = 0;
Uint node_cnt = 0;
#endif
enum { RECURSE_LEFT, DO_NODE, RECURSE_RIGHT, RETURN_TO_PARENT }state;
RBTNode *x = tree->root;
RBT_ASSERT(!x || !parent(x));
state = reverse ? RECURSE_RIGHT : RECURSE_LEFT;
while (x) {
switch (state) {
case RECURSE_LEFT:
if (x->left) {
RBT_ASSERT(parent(x->left) == x);
#ifdef HARD_DEBUG
++depth;
if (IS_BLACK(x->left))
curr_blacks++;