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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2012. 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 ERL_WANT_GC_INTERNALS__
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "erl_gc.h"
#include "big.h"
#include "erl_map.h"
#include "erl_binary.h"
#include "erl_bits.h"
#include "dtrace-wrapper.h"
static void move_one_frag(Eterm** hpp, ErlHeapFragment*, ErlOffHeap*, int);
/*
* Copy object "obj" to process p.
*/
Eterm copy_object_x(Eterm obj, Process* to, Uint extra) {
if (!is_immed(obj)) {
Uint size = size_object(obj);
Eterm* hp = HAllocX(to, size, extra);
Eterm res;
#ifdef USE_VM_PROBES
if (DTRACE_ENABLED(copy_object)) {
DTRACE_CHARBUF(proc_name, 64);
erts_snprintf(proc_name, sizeof(DTRACE_CHARBUF_NAME(proc_name)),
"%T", to->common.id);
DTRACE2(copy_object, proc_name, size);
}
#endif
res = copy_struct(obj, size, &hp, &to->off_heap);
#ifdef DEBUG
if (eq(obj, res) == 0) {
erl_exit(ERTS_ABORT_EXIT, "copy not equal to source\n");
}
#endif
return res;
}
return obj;
}
/*
* Return the "flat" size of the object.
*/
Uint size_object(Eterm obj)
{
Uint sum = 0;
Eterm* ptr;
int arity;
#ifdef DEBUG
Eterm mypid = erts_get_current_pid();
#endif
DECLARE_ESTACK(s);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] size_object %p\n", mypid, obj));
for (;;) {
switch (primary_tag(obj)) {
case TAG_PRIMARY_LIST:
sum += 2;
ptr = list_val(obj);
obj = *ptr++;
if (!IS_CONST(obj)) {
ESTACK_PUSH(s, obj);
}
obj = *ptr;
break;
case TAG_PRIMARY_BOXED:
{
Eterm hdr = *boxed_val(obj);
ASSERT(is_header(hdr));
switch (hdr & _TAG_HEADER_MASK) {
case ARITYVAL_SUBTAG:
ptr = tuple_val(obj);
arity = header_arity(hdr);
sum += arity + 1;
if (arity == 0) { /* Empty tuple -- unusual. */
goto pop_next;
}
while (arity-- > 1) {
obj = *++ptr;
if (!IS_CONST(obj)) {
ESTACK_PUSH(s, obj);
}
}
obj = *++ptr;
break;
case FUN_SUBTAG:
{
Eterm* bptr = fun_val(obj);
ErlFunThing* funp = (ErlFunThing *) bptr;
unsigned eterms = 1 /* creator */ + funp->num_free;
unsigned sz = thing_arityval(hdr);
sum += 1 /* header */ + sz + eterms;
bptr += 1 /* header */ + sz;
while (eterms-- > 1) {
obj = *bptr++;
if (!IS_CONST(obj)) {
ESTACK_PUSH(s, obj);
}
}
obj = *bptr;
break;
}
case MAP_SUBTAG:
switch (MAP_HEADER_TYPE(hdr)) {
case MAP_HEADER_TAG_FLATMAP_HEAD :
{
Uint n;
flatmap_t *mp;
mp = (flatmap_t*)flatmap_val(obj);
ptr = (Eterm *)mp;
n = flatmap_get_size(mp) + 1;
sum += n + 2;
ptr += 2; /* hdr + size words */
while (n--) {
obj = *ptr++;
if (!IS_CONST(obj)) {
ESTACK_PUSH(s, obj);
}
}
goto pop_next;
}
case MAP_HEADER_TAG_HAMT_HEAD_BITMAP :
case MAP_HEADER_TAG_HAMT_HEAD_ARRAY :
case MAP_HEADER_TAG_HAMT_NODE_BITMAP :
{
Eterm *head;
Uint sz;
head = hashmap_val(obj);
sz = hashmap_bitcount(MAP_HEADER_VAL(hdr));
sum += 1 + sz + header_arity(hdr);
head += 1 + header_arity(hdr);
if (sz == 0) {
goto pop_next;
}
while(sz-- > 1) {
obj = head[sz];
if (!IS_CONST(obj)) {
ESTACK_PUSH(s, obj);
}
}
obj = head[0];
}
break;
default:
erl_exit(ERTS_ABORT_EXIT, "size_object: bad hashmap type %d\n", MAP_HEADER_TYPE(hdr));
}
break;
case SUB_BINARY_SUBTAG:
{
Eterm real_bin;
ERTS_DECLARE_DUMMY(Uint offset); /* Not used. */
Uint bitsize;
Uint bitoffs;
Uint extra_bytes;
Eterm hdr;
ERTS_GET_REAL_BIN(obj, real_bin, offset, bitoffs, bitsize);
if ((bitsize + bitoffs) > 8) {
sum += ERL_SUB_BIN_SIZE;
extra_bytes = 2;
} else if ((bitsize + bitoffs) > 0) {
sum += ERL_SUB_BIN_SIZE;
extra_bytes = 1;
} else {
extra_bytes = 0;
}
hdr = *binary_val(real_bin);
if (thing_subtag(hdr) == REFC_BINARY_SUBTAG) {
sum += PROC_BIN_SIZE;
} else {
sum += heap_bin_size(binary_size(obj)+extra_bytes);
}
goto pop_next;
}
break;
case BIN_MATCHSTATE_SUBTAG:
erl_exit(ERTS_ABORT_EXIT,
"size_object: matchstate term not allowed");
default:
sum += thing_arityval(hdr) + 1;
goto pop_next;
}
}
break;
case TAG_PRIMARY_IMMED1:
pop_next:
if (ESTACK_ISEMPTY(s)) {
DESTROY_ESTACK(s);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] size was: %u\n", mypid, sum));
return sum;
}
obj = ESTACK_POP(s);
break;
default:
erl_exit(ERTS_ABORT_EXIT, "size_object: bad tag for %#x\n", obj);
}
}
}
/*
* Machinery for sharing preserving information
* Using a WSTACK but not very transparently; consider refactoring
*/
#define DECLARE_BITSTORE(s) \
DECLARE_WSTACK(s); \
int WSTK_CONCAT(s,_bitoffs) = 0; \
int WSTK_CONCAT(s,_offset) = 0; \
UWord WSTK_CONCAT(s,_buffer) = 0
#define DESTROY_BITSTORE(s) DESTROY_WSTACK(s)
#define BITSTORE_PUT(s,i) \
do { \
WSTK_CONCAT(s,_buffer) |= i << WSTK_CONCAT(s,_bitoffs); \
WSTK_CONCAT(s,_bitoffs) += 2; \
if (WSTK_CONCAT(s,_bitoffs) >= 8*sizeof(UWord)) { \
WSTACK_PUSH(s, WSTK_CONCAT(s,_buffer)); \
WSTK_CONCAT(s,_bitoffs) = 0; \
WSTK_CONCAT(s,_buffer) = 0; \
} \
} while(0)
#define BITSTORE_CLOSE(s) \
do { \
if (WSTK_CONCAT(s,_bitoffs) > 0) { \
WSTACK_PUSH(s, WSTK_CONCAT(s,_buffer)); \
WSTK_CONCAT(s,_bitoffs) = 0; \
} \
} while(0)
#define BITSTORE_GET(s) ({ \
UWord result; \
if (WSTK_CONCAT(s,_bitoffs) <= 0) { \
WSTK_CONCAT(s,_buffer) = s.wstart[WSTK_CONCAT(s,_offset)]; \
WSTK_CONCAT(s,_offset)++; \
WSTK_CONCAT(s,_bitoffs) = 8*sizeof(UWord); \
} \
WSTK_CONCAT(s,_bitoffs) -= 2; \
result = WSTK_CONCAT(s,_buffer) & 3; \
WSTK_CONCAT(s,_buffer) >>= 2; \
result; \
})
#define BOXED_VISITED_MASK ((Eterm) 3)
#define BOXED_VISITED ((Eterm) 1)
#define BOXED_SHARED_UNPROCESSED ((Eterm) 2)
#define BOXED_SHARED_PROCESSED ((Eterm) 3)
/*
* Is an object in the local heap of a process?
*/
#define INHEAP_SIMPLE(p, ptr) ( \
(OLD_HEAP(p) && OLD_HEAP(p) <= ptr && ptr < OLD_HEND(p)) || \
(HEAP_START(p) <= ptr && ptr < HEAP_END(p)) \
)
#define INHEAP(p, ptr) ( \
INHEAP_SIMPLE(p, ptr) || \
(force_local ? (force_local = 0, 1) : 0) \
)
#define COUNT_OFF_HEAP 0
/*
* Return the real size of an object and find sharing information
* This currently returns the same as erts_debug:size/1.
* It is argued whether the size of subterms in constant pools
* should be counted or not.
*/
Uint size_shared(Eterm obj)
{
Eterm saved_obj = obj;
Uint sum = 0;
Eterm* ptr;
Process* myself;
DECLARE_EQUEUE(s);
DECLARE_BITSTORE(b);
myself = erts_get_current_process();
if (myself == NULL)
return size_object(obj);
for (;;) {
switch (primary_tag(obj)) {
case TAG_PRIMARY_LIST: {
Eterm head, tail;
ptr = list_val(obj);
/* we're not counting anything that's outside our heap */
if (!COUNT_OFF_HEAP && !INHEAP_SIMPLE(myself, ptr)) {
goto pop_next;
}
head = CAR(ptr);
tail = CDR(ptr);
/* if it's visited, don't count it */
if (primary_tag(tail) == TAG_PRIMARY_HEADER ||
primary_tag(head) == TAG_PRIMARY_HEADER) {
goto pop_next;
}
/* else make it visited now */
switch (primary_tag(tail)) {
case TAG_PRIMARY_LIST:
ptr[1] = (tail - TAG_PRIMARY_LIST) | TAG_PRIMARY_HEADER;
break;
case TAG_PRIMARY_IMMED1:
CAR(ptr) = (head - primary_tag(head)) | TAG_PRIMARY_HEADER;
CDR(ptr) = (tail - TAG_PRIMARY_IMMED1) | primary_tag(head);
break;
case TAG_PRIMARY_BOXED:
BITSTORE_PUT(b, primary_tag(head));
CAR(ptr) = (head - primary_tag(head)) | TAG_PRIMARY_HEADER;
CDR(ptr) = (tail - TAG_PRIMARY_BOXED) | TAG_PRIMARY_HEADER;
break;
}
/* and count it */
sum += 2;
if (!IS_CONST(head)) {
EQUEUE_PUT(s, head);
}
obj = tail;
break;
}
case TAG_PRIMARY_BOXED: {
Eterm hdr;
ptr = boxed_val(obj);
/* we're not counting anything that's outside our heap */
if (!COUNT_OFF_HEAP && !INHEAP_SIMPLE(myself, ptr)) {
goto pop_next;
}
hdr = *ptr;
/* if it's visited, don't count it */
if (primary_tag(hdr) != TAG_PRIMARY_HEADER) {
goto pop_next;
}
/* else make it visited now */
*ptr = (hdr - primary_tag(hdr)) + BOXED_VISITED;
/* and count it */
ASSERT(is_header(hdr));
switch (hdr & _TAG_HEADER_MASK) {
case ARITYVAL_SUBTAG: {
int arity = header_arity(hdr);
sum += arity + 1;
if (arity == 0) { /* Empty tuple -- unusual. */
goto pop_next;
}
while (arity-- > 0) {
obj = *++ptr;
if (!IS_CONST(obj)) {
EQUEUE_PUT(s, obj);
}
}
goto pop_next;
}
case FUN_SUBTAG: {
ErlFunThing* funp = (ErlFunThing *) ptr;
unsigned eterms = 1 /* creator */ + funp->num_free;
unsigned sz = thing_arityval(hdr);
sum += 1 /* header */ + sz + eterms;
ptr += 1 /* header */ + sz;
while (eterms-- > 0) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT(s, obj);
}
}
goto pop_next;
}
case SUB_BINARY_SUBTAG: {
ErlSubBin* sb = (ErlSubBin *) ptr;
Uint extra_bytes;
Eterm hdr;
ASSERT((sb->thing_word & ~BOXED_VISITED_MASK) == HEADER_SUB_BIN);
if (sb->bitsize + sb->bitoffs > 8) {
sum += ERL_SUB_BIN_SIZE;
extra_bytes = 2;
} else if (sb->bitsize + sb->bitoffs > 0) {
sum += ERL_SUB_BIN_SIZE;
extra_bytes = 1;
} else {
extra_bytes = 0;
}
ptr = binary_val(sb->orig);
hdr = (*ptr) & ~BOXED_VISITED_MASK;
if (thing_subtag(hdr) == REFC_BINARY_SUBTAG) {
sum += PROC_BIN_SIZE;
} else {
ASSERT(thing_subtag(hdr) == HEAP_BINARY_SUBTAG);
sum += heap_bin_size(binary_size(obj) + extra_bytes);
}
goto pop_next;
}
case MAP_SUBTAG:
switch (MAP_HEADER_TYPE(hdr)) {
case MAP_HEADER_TAG_FLATMAP_HEAD : {
flatmap_t *mp = (flatmap_t*)flatmap_val(obj);
Uint n = flatmap_get_size(mp) + 1;
ptr = (Eterm *)mp;
sum += n + 2;
ptr += 2; /* hdr + size words */
while (n--) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT(s, obj);
}
}
goto pop_next;
}
case MAP_HEADER_TAG_HAMT_HEAD_BITMAP :
case MAP_HEADER_TAG_HAMT_HEAD_ARRAY :
case MAP_HEADER_TAG_HAMT_NODE_BITMAP : {
Uint n = hashmap_bitcount(MAP_HEADER_VAL(hdr));
sum += 1 + n + header_arity(hdr);
ptr += 1 + header_arity(hdr);
while (n--) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT(s, obj);
}
}
goto pop_next;
}
default:
erl_exit(ERTS_ABORT_EXIT, "size_shared: bad hashmap type %d\n", MAP_HEADER_TYPE(hdr));
}
case BIN_MATCHSTATE_SUBTAG:
erl_exit(ERTS_ABORT_EXIT,
"size_shared: matchstate term not allowed");
default:
sum += thing_arityval(hdr) + 1;
goto pop_next;
}
break;
}
case TAG_PRIMARY_IMMED1:
pop_next:
if (EQUEUE_ISEMPTY(s)) {
goto cleanup;
}
obj = EQUEUE_GET(s);
break;
default:
erl_exit(ERTS_ABORT_EXIT, "size_shared: bad tag for %#x\n", obj);
}
}
cleanup:
obj = saved_obj;
BITSTORE_CLOSE(b);
for (;;) {
switch (primary_tag(obj)) {
case TAG_PRIMARY_LIST: {
Eterm head, tail;
ptr = list_val(obj);
if (!COUNT_OFF_HEAP && !INHEAP_SIMPLE(myself, ptr)) {
goto cleanup_next;
}
head = CAR(ptr);
tail = CDR(ptr);
/* if not already clean, clean it up */
if (primary_tag(tail) == TAG_PRIMARY_HEADER) {
if (primary_tag(head) == TAG_PRIMARY_HEADER) {
Eterm saved = BITSTORE_GET(b);
CAR(ptr) = head = (head - TAG_PRIMARY_HEADER) | saved;
CDR(ptr) = tail = (tail - TAG_PRIMARY_HEADER) | TAG_PRIMARY_BOXED;
} else {
CDR(ptr) = tail = (tail - TAG_PRIMARY_HEADER) | TAG_PRIMARY_LIST;
}
} else if (primary_tag(head) == TAG_PRIMARY_HEADER) {
CAR(ptr) = head = (head - TAG_PRIMARY_HEADER) | primary_tag(tail);
CDR(ptr) = tail = (tail - primary_tag(tail)) | TAG_PRIMARY_IMMED1;
} else {
goto cleanup_next;
}
/* and its children too */
if (!IS_CONST(head)) {
EQUEUE_PUT_UNCHECKED(s, head);
}
obj = tail;
break;
}
case TAG_PRIMARY_BOXED: {
Eterm hdr;
ptr = boxed_val(obj);
if (!COUNT_OFF_HEAP && !INHEAP_SIMPLE(myself, ptr)) {
goto cleanup_next;
}
hdr = *ptr;
/* if not already clean, clean it up */
if (primary_tag(hdr) == TAG_PRIMARY_HEADER) {
goto cleanup_next;
}
else {
ASSERT(primary_tag(hdr) == BOXED_VISITED);
*ptr = hdr = (hdr - BOXED_VISITED) + TAG_PRIMARY_HEADER;
}
/* and its children too */
switch (hdr & _TAG_HEADER_MASK) {
case ARITYVAL_SUBTAG: {
int arity = header_arity(hdr);
if (arity == 0) { /* Empty tuple -- unusual. */
goto cleanup_next;
}
while (arity-- > 0) {
obj = *++ptr;
if (!IS_CONST(obj)) {
EQUEUE_PUT_UNCHECKED(s, obj);
}
}
goto cleanup_next;
}
case FUN_SUBTAG: {
ErlFunThing* funp = (ErlFunThing *) ptr;
unsigned eterms = 1 /* creator */ + funp->num_free;
unsigned sz = thing_arityval(hdr);
ptr += 1 /* header */ + sz;
while (eterms-- > 0) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT_UNCHECKED(s, obj);
}
}
goto cleanup_next;
}
case MAP_SUBTAG:
switch (MAP_HEADER_TYPE(hdr)) {
case MAP_HEADER_TAG_FLATMAP_HEAD : {
flatmap_t *mp = (flatmap_t *) ptr;
Uint n = flatmap_get_size(mp) + 1;
ptr += 2; /* hdr + size words */
while (n--) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT_UNCHECKED(s, obj);
}
}
goto cleanup_next;
}
case MAP_HEADER_TAG_HAMT_HEAD_BITMAP :
case MAP_HEADER_TAG_HAMT_HEAD_ARRAY :
case MAP_HEADER_TAG_HAMT_NODE_BITMAP : {
Uint n = hashmap_bitcount(MAP_HEADER_VAL(hdr));
sum += 1 + n + header_arity(hdr);
ptr += 1 + header_arity(hdr);
while (n--) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT_UNCHECKED(s, obj);
}
}
goto cleanup_next;
}
default:
erl_exit(ERTS_ABORT_EXIT, "size_shared: bad hashmap type %d\n", MAP_HEADER_TYPE(hdr));
}
default:
goto cleanup_next;
}
break;
}
case TAG_PRIMARY_IMMED1:
cleanup_next:
if (EQUEUE_ISEMPTY(s)) {
goto all_clean;
}
obj = EQUEUE_GET(s);
break;
default:
erl_exit(ERTS_ABORT_EXIT, "size_shared: bad tag for %#x\n", obj);
}
}
all_clean:
/* Return the result */
DESTROY_EQUEUE(s);
DESTROY_BITSTORE(b);
return sum;
}
/*
* Copy a structure to a heap.
*/
Eterm copy_struct(Eterm obj, Uint sz, Eterm** hpp, ErlOffHeap* off_heap)
{
char* hstart;
Uint hsize;
Eterm* htop;
Eterm* hbot;
Eterm* hp;
Eterm* objp;
Eterm* tp;
Eterm res;
Eterm elem;
Eterm* tailp;
Eterm* argp;
Eterm* const_tuple;
Eterm hdr;
int i;
#ifdef DEBUG
Eterm org_obj = obj;
Uint org_sz = sz;
Eterm mypid = erts_get_current_pid();
#endif
if (IS_CONST(obj))
return obj;
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] copy_struct %p\n", mypid, obj));
DTRACE1(copy_struct, (int32_t)sz);
hp = htop = *hpp;
hbot = htop + sz;
hstart = (char *)htop;
hsize = (char*) hbot - hstart;
const_tuple = 0;
/* Copy the object onto the heap */
switch (primary_tag(obj)) {
case TAG_PRIMARY_LIST:
argp = &res;
objp = list_val(obj);
goto L_copy_list;
case TAG_PRIMARY_BOXED: argp = &res; goto L_copy_boxed;
default:
erl_exit(ERTS_ABORT_EXIT,
"%s, line %d: Internal error in copy_struct: 0x%08x\n",
__FILE__, __LINE__,obj);
}
L_copy:
while (hp != htop) {
obj = *hp;
switch (primary_tag(obj)) {
case TAG_PRIMARY_IMMED1:
hp++;
break;
case TAG_PRIMARY_LIST:
objp = list_val(obj);
if (ErtsInArea(objp,hstart,hsize)) {
hp++;
break;
}
argp = hp++;
/* Fall through */
L_copy_list:
tailp = argp;
for (;;) {
tp = tailp;
elem = CAR(objp);
if (IS_CONST(elem)) {
hbot -= 2;
CAR(hbot) = elem;
tailp = &CDR(hbot);
}
else {
CAR(htop) = elem;
tailp = &CDR(htop);
htop += 2;
}
*tp = make_list(tailp - 1);
obj = CDR(objp);
if (!is_list(obj)) {
break;
}
objp = list_val(obj);
}
switch (primary_tag(obj)) {
case TAG_PRIMARY_IMMED1: *tailp = obj; goto L_copy;
case TAG_PRIMARY_BOXED: argp = tailp; goto L_copy_boxed;
default:
erl_exit(ERTS_ABORT_EXIT,
"%s, line %d: Internal error in copy_struct: 0x%08x\n",
__FILE__, __LINE__,obj);
}
case TAG_PRIMARY_BOXED:
if (ErtsInArea(boxed_val(obj),hstart,hsize)) {
hp++;
break;
}
argp = hp++;
L_copy_boxed:
objp = boxed_val(obj);
hdr = *objp;
switch (hdr & _TAG_HEADER_MASK) {
case ARITYVAL_SUBTAG:
{
int const_flag = 1; /* assume constant tuple */
i = arityval(hdr);
*argp = make_tuple(htop);
tp = htop; /* tp is pointer to new arity value */
*htop++ = *objp++; /* copy arity value */
while (i--) {
elem = *objp++;
if (!IS_CONST(elem)) {
const_flag = 0;
}
*htop++ = elem;
}
if (const_flag) {
const_tuple = tp; /* this is the latest const_tuple */
}
}
break;
case REFC_BINARY_SUBTAG:
{
ProcBin* pb;
pb = (ProcBin *) objp;
if (pb->flags) {
erts_emasculate_writable_binary(pb);
}
i = thing_arityval(*objp) + 1;
hbot -= i;
tp = hbot;
while (i--) {
*tp++ = *objp++;
}
*argp = make_binary(hbot);
pb = (ProcBin*) hbot;
erts_refc_inc(&pb->val->refc, 2);
pb->next = off_heap->first;
pb->flags = 0;
off_heap->first = (struct erl_off_heap_header*) pb;
OH_OVERHEAD(off_heap, pb->size / sizeof(Eterm));
}
break;
case SUB_BINARY_SUBTAG:
{
ErlSubBin* sb = (ErlSubBin *) objp;
Eterm real_bin = sb->orig;
Uint bit_offset = sb->bitoffs;
Uint bit_size = sb -> bitsize;
Uint offset = sb->offs;
size_t size = sb->size;
Uint extra_bytes;
Uint real_size;
if ((bit_size + bit_offset) > 8) {
extra_bytes = 2;
} else if ((bit_size + bit_offset) > 0) {
extra_bytes = 1;
} else {
extra_bytes = 0;
}
real_size = size+extra_bytes;
objp = binary_val(real_bin);
if (thing_subtag(*objp) == HEAP_BINARY_SUBTAG) {
ErlHeapBin* from = (ErlHeapBin *) objp;
ErlHeapBin* to;
i = heap_bin_size(real_size);
hbot -= i;
to = (ErlHeapBin *) hbot;
to->thing_word = header_heap_bin(real_size);
to->size = real_size;
sys_memcpy(to->data, ((byte *)from->data)+offset, real_size);
} else {
ProcBin* from = (ProcBin *) objp;
ProcBin* to;
ASSERT(thing_subtag(*objp) == REFC_BINARY_SUBTAG);
if (from->flags) {
erts_emasculate_writable_binary(from);
}
hbot -= PROC_BIN_SIZE;
to = (ProcBin *) hbot;
to->thing_word = HEADER_PROC_BIN;
to->size = real_size;
to->val = from->val;
erts_refc_inc(&to->val->refc, 2);
to->bytes = from->bytes + offset;
to->next = off_heap->first;
to->flags = 0;
off_heap->first = (struct erl_off_heap_header*) to;
OH_OVERHEAD(off_heap, to->size / sizeof(Eterm));
}
*argp = make_binary(hbot);
if (extra_bytes != 0) {
ErlSubBin* res;
hbot -= ERL_SUB_BIN_SIZE;
res = (ErlSubBin *) hbot;
res->thing_word = HEADER_SUB_BIN;
res->size = size;
res->bitsize = bit_size;
res->bitoffs = bit_offset;
res->offs = 0;
res->is_writable = 0;
res->orig = *argp;
*argp = make_binary(hbot);
}
break;
}
break;
case FUN_SUBTAG:
{
ErlFunThing* funp = (ErlFunThing *) objp;
i = thing_arityval(hdr) + 2 + funp->num_free;
tp = htop;
while (i--) {
*htop++ = *objp++;
}
funp = (ErlFunThing *) tp;
funp->next = off_heap->first;
off_heap->first = (struct erl_off_heap_header*) funp;
erts_refc_inc(&funp->fe->refc, 2);
*argp = make_fun(tp);
}
break;
case EXTERNAL_PID_SUBTAG:
case EXTERNAL_PORT_SUBTAG:
case EXTERNAL_REF_SUBTAG:
{
ExternalThing *etp = (ExternalThing *) htop;
i = thing_arityval(hdr) + 1;
tp = htop;
while (i--) {
*htop++ = *objp++;
}
etp->next = off_heap->first;
off_heap->first = (struct erl_off_heap_header*)etp;
erts_refc_inc(&etp->node->refc, 2);
*argp = make_external(tp);
}
break;
case MAP_SUBTAG:
tp = htop;
switch (MAP_HEADER_TYPE(hdr)) {
case MAP_HEADER_TAG_FLATMAP_HEAD :
i = flatmap_get_size(objp) + 3;
*argp = make_flatmap(htop);
while (i--) {
*htop++ = *objp++;
}
break;
case MAP_HEADER_TAG_HAMT_HEAD_BITMAP :
case MAP_HEADER_TAG_HAMT_HEAD_ARRAY :
*htop++ = *objp++;
case MAP_HEADER_TAG_HAMT_NODE_BITMAP :
i = 1 + hashmap_bitcount(MAP_HEADER_VAL(hdr));
while (i--) { *htop++ = *objp++; }
*argp = make_hashmap(tp);
break;
default:
erl_exit(ERTS_ABORT_EXIT, "copy_struct: bad hashmap type %d\n", MAP_HEADER_TYPE(hdr));
}
break;
case BIN_MATCHSTATE_SUBTAG:
erl_exit(ERTS_ABORT_EXIT,
"copy_struct: matchstate term not allowed");
default:
i = thing_arityval(hdr)+1;
hbot -= i;
tp = hbot;
*argp = make_boxed(hbot);
while (i--) {
*tp++ = *objp++;
}
}
break;
case TAG_PRIMARY_HEADER:
if (header_is_thing(obj) || hp == const_tuple) {
hp += header_arity(obj) + 1;
} else {
hp++;
}
break;
}
}
#ifdef DEBUG
if (htop != hbot)
erl_exit(ERTS_ABORT_EXIT,
"Internal error in copy_struct() when copying %T:"
" htop=%p != hbot=%p (sz=%beu)\n",
org_obj, htop, hbot, org_sz);
#else
if (htop > hbot) {
erl_exit(ERTS_ABORT_EXIT,
"Internal error in copy_struct(): htop, hbot overrun\n");
}
#endif
*hpp = (Eterm *) (hstart+hsize);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] result is at %p\n", mypid, res));
return res;
}
/*
* Machinery for the table used by the sharing preserving copier
* Using an ESTACK but not very transparently; consider refactoring
*/
#ifdef SHCOPY_DISABLE
int disable_copy_shared = ERTS_SHCOPY_FLG_NONE;
#endif
#define DECLARE_SHTABLE(s) \
DECLARE_ESTACK(s); \
Uint ESTK_CONCAT(s,_offset) = 0
#define DESTROY_SHTABLE(s) DESTROY_ESTACK(s)
#define SHTABLE_INCR 4
#define SHTABLE_NEXT(s) ESTK_CONCAT(s,_offset)
#define SHTABLE_PUSH(s,x,y,b) \
do { \
if (s.sp > s.end - SHTABLE_INCR) { \
erl_grow_estack(&(s), SHTABLE_INCR); \
} \
*s.sp++ = (x); \
*s.sp++ = (y); \
*s.sp++ = (Eterm) NULL; \
*s.sp++ = (Eterm) (b); \
ESTK_CONCAT(s,_offset) += SHTABLE_INCR; \
} while(0)
#define SHTABLE_X(s,e) (s.start[e])
#define SHTABLE_Y(s,e) (s.start[(e)+1])
#define SHTABLE_FWD(s,e) ((Eterm *) (s.start[(e)+2]))
#define SHTABLE_FWD_UPD(s,e,p) (s.start[(e)+2] = (Eterm) (p))
#define SHTABLE_REV(s,e) ((Eterm *) (s.start[(e)+3]))
#define LIST_SHARED_UNPROCESSED ((Eterm) 0)
#define LIST_SHARED_PROCESSED ((Eterm) 1)
#define HEAP_ELEM_TO_BE_FILLED _unchecked_make_list(NULL)
/*
* Specialized macros for using/reusing the persistent state
*/
#define DECLARE_EQUEUE_INIT_INFO(q, info) \
UWord* EQUE_DEF_QUEUE(q) = info->queue_default; \
ErtsEQueue q = { \
EQUE_DEF_QUEUE(q), /* start */ \
EQUE_DEF_QUEUE(q), /* front */ \
EQUE_DEF_QUEUE(q), /* back */ \
1, /* possibly_empty */ \
EQUE_DEF_QUEUE(q) + DEF_EQUEUE_SIZE, /* end */ \
ERTS_ALC_T_ESTACK /* alloc_type */ \
}
#define DECLARE_EQUEUE_FROM_INFO(q, info) \
/* no EQUE_DEF_QUEUE(q), read-only */ \
ErtsEQueue q = { \
info->queue_start, /* start */ \
info->queue_start, /* front */ \
info->queue_start, /* back */ \
1, /* possibly_empty */ \
info->queue_end, /* end */ \
info->queue_alloc_type /* alloc_type */ \
}
#define DECLARE_BITSTORE_INIT_INFO(s, info) \
UWord* WSTK_DEF_STACK(s) = info->bitstore_default; \
ErtsWStack s = { \
WSTK_DEF_STACK(s), /* wstart */ \
WSTK_DEF_STACK(s), /* wsp */ \
WSTK_DEF_STACK(s) + DEF_WSTACK_SIZE, /* wend */ \
WSTK_DEF_STACK(s), /* wdflt */ \
ERTS_ALC_T_ESTACK /* alloc_type */ \
}; \
int WSTK_CONCAT(s,_bitoffs) = 0; \
/* no WSTK_CONCAT(s,_offset), write-only */ \
UWord WSTK_CONCAT(s,_buffer) = 0
#define DECLARE_BITSTORE_FROM_INFO(s, info) \
/* no WSTK_DEF_STACK(s), read-only */ \
ErtsWStack s = { \
info->bitstore_start, /* wstart */ \
NULL, /* wsp, read-only */ \
NULL, /* wend, read-only */ \
NULL, /* wdef, read-only */ \
info->bitstore_alloc_type /* alloc_type */ \
}; \
int WSTK_CONCAT(s,_bitoffs) = 0; \
int WSTK_CONCAT(s,_offset) = 0; \
UWord WSTK_CONCAT(s,_buffer) = 0
#define DECLARE_SHTABLE_INIT_INFO(s, info) \
Eterm* ESTK_DEF_STACK(s) = info->shtable_default; \
ErtsEStack s = { \
ESTK_DEF_STACK(s), /* start */ \
ESTK_DEF_STACK(s), /* sp */ \
ESTK_DEF_STACK(s) + DEF_ESTACK_SIZE, /* end */ \
ESTK_DEF_STACK(s), /* default */ \
ERTS_ALC_T_ESTACK /* alloc_type */ \
}; \
Uint ESTK_CONCAT(s,_offset) = 0
#define DECLARE_SHTABLE_FROM_INFO(s, info) \
/* no ESTK_DEF_STACK(s), read-only */ \
ErtsEStack s = { \
info->shtable_start, /* start */ \
NULL, /* sp, read-only */ \
NULL, /* end, read-only */ \
NULL, /* def, read-only */ \
info->shtable_alloc_type /* alloc_type */ \
}; \
/* no ESTK_CONCAT(s,_offset), read-only */
/*
* Copy object "obj" preserving sharing.
* First half: count size and calculate sharing.
*/
Uint copy_shared_calculate(Eterm obj, erts_shcopy_t *info, Uint32 flags)
{
Uint sum;
Uint e;
unsigned sz;
Eterm* ptr;
Process* myself;
int force_local = flags & ERTS_SHCOPY_FLG_TMPBUF;
DECLARE_EQUEUE_INIT_INFO(s, info);
DECLARE_BITSTORE_INIT_INFO(b, info);
DECLARE_SHTABLE_INIT_INFO(t, info);
/* step #0:
-------------------------------------------------------
get rid of the easy cases first:
- copying constants
- if not a proper process, do flat copy
*/
if (IS_CONST(obj))
return 0;
#ifdef SHCOPY_DISABLE
flags |= disable_copy_shared;
#endif
myself = erts_get_current_process();
if (myself == NULL || (flags & ERTS_SHCOPY_FLG_NONE))
return size_object(obj);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] copy_shared_calculate %p\n", myself->common.id, obj));
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] message is %T\n", myself->common.id, obj));
/* step #1:
-------------------------------------------------------
traverse the term and calculate the size;
when traversing, transform as you do in size_shared
but when you find shared objects:
a. add entry in the table, indexed by i
b. mark them:
b1. boxed terms, set header to (i | 11)
store (old header, NONV, NULL, backptr) in the entry
b2. cons cells, set CDR to NONV, set CAR to i
store (old CAR, old CDR, NULL, backptr) in the entry
*/
sum = 0;
for (;;) {
switch (primary_tag(obj)) {
case TAG_PRIMARY_LIST: {
Eterm head, tail;
ptr = list_val(obj);
/* off heap list pointers are copied verbatim */
if (!INHEAP(myself, ptr)) {
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] bypassed copying %p is %T\n", myself->common.id, ptr, obj));
goto pop_next;
}
head = CAR(ptr);
tail = CDR(ptr);
/* if it's visited, don't count it;
if not already shared, make it shared and store it in the table */
if (primary_tag(tail) == TAG_PRIMARY_HEADER ||
primary_tag(head) == TAG_PRIMARY_HEADER) {
if (tail != THE_NON_VALUE) {
e = SHTABLE_NEXT(t);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] tabling L %p\n", myself->common.id, ptr));
SHTABLE_PUSH(t, head, tail, ptr);
CAR(ptr) = (e << _TAG_PRIMARY_SIZE) | LIST_SHARED_UNPROCESSED;
CDR(ptr) = THE_NON_VALUE;
}
goto pop_next;
}
/* else make it visited now */
switch (primary_tag(tail)) {
case TAG_PRIMARY_LIST:
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] mangling L/L %p\n", myself->common.id, ptr));
CDR(ptr) = (tail - TAG_PRIMARY_LIST) | TAG_PRIMARY_HEADER;
break;
case TAG_PRIMARY_IMMED1:
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] mangling L/I %p\n", myself->common.id, ptr));
CAR(ptr) = (head - primary_tag(head)) | TAG_PRIMARY_HEADER;
CDR(ptr) = (tail - TAG_PRIMARY_IMMED1) | primary_tag(head);
break;
case TAG_PRIMARY_BOXED:
BITSTORE_PUT(b, primary_tag(head));
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] mangling L/B %p\n", myself->common.id, ptr));
CAR(ptr) = (head - primary_tag(head)) | TAG_PRIMARY_HEADER;
CDR(ptr) = (tail - TAG_PRIMARY_BOXED) | TAG_PRIMARY_HEADER;
break;
}
/* and count it */
sum += 2;
if (!IS_CONST(head)) {
EQUEUE_PUT(s, head);
}
obj = tail;
break;
}
case TAG_PRIMARY_BOXED: {
Eterm hdr;
ptr = boxed_val(obj);
/* off heap pointers to boxes are copied verbatim */
if (!INHEAP(myself, ptr)) {
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] bypassed copying %p is %T\n", myself->common.id, ptr, obj));
goto pop_next;
}
hdr = *ptr;
/* if it's visited, don't count it;
if not already shared, make it shared and store it in the table */
if (primary_tag(hdr) != TAG_PRIMARY_HEADER) {
if (primary_tag(hdr) == BOXED_VISITED) {
e = SHTABLE_NEXT(t);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] tabling B %p\n", myself->common.id, ptr));
SHTABLE_PUSH(t, hdr, THE_NON_VALUE, ptr);
*ptr = (e << _TAG_PRIMARY_SIZE) | BOXED_SHARED_UNPROCESSED;
}
goto pop_next;
}
/* else make it visited now */
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] mangling B %p\n", myself->common.id, ptr));
*ptr = (hdr - primary_tag(hdr)) + BOXED_VISITED;
/* and count it */
ASSERT(is_header(hdr));
switch (hdr & _TAG_HEADER_MASK) {
case ARITYVAL_SUBTAG: {
int arity = header_arity(hdr);
sum += arity + 1;
if (arity == 0) { /* Empty tuple -- unusual. */
goto pop_next;
}
while (arity-- > 0) {
obj = *++ptr;
if (!IS_CONST(obj)) {
EQUEUE_PUT(s, obj);
}
}
goto pop_next;
}
case FUN_SUBTAG: {
ErlFunThing* funp = (ErlFunThing *) ptr;
unsigned eterms = 1 /* creator */ + funp->num_free;
sz = thing_arityval(hdr);
sum += 1 /* header */ + sz + eterms;
ptr += 1 /* header */ + sz;
while (eterms-- > 0) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT(s, obj);
}
}
goto pop_next;
}
case SUB_BINARY_SUBTAG: {
ErlSubBin* sb = (ErlSubBin *) ptr;
Eterm real_bin = sb->orig;
Uint bit_offset = sb->bitoffs;
Uint bit_size = sb->bitsize;
size_t size = sb->size;
Uint extra_bytes;
Eterm hdr;
if (bit_size + bit_offset > 8) {
sum += ERL_SUB_BIN_SIZE;
extra_bytes = 2;
} else if (bit_size + bit_offset > 0) {
sum += ERL_SUB_BIN_SIZE;
extra_bytes = 1;
} else {
extra_bytes = 0;
}
ASSERT(is_boxed(real_bin) &&
(((*boxed_val(real_bin)) &
(_TAG_HEADER_MASK - _BINARY_XXX_MASK - BOXED_VISITED_MASK))
== _TAG_HEADER_REFC_BIN));
hdr = *_unchecked_binary_val(real_bin) & ~BOXED_VISITED_MASK;
if (thing_subtag(hdr) == HEAP_BINARY_SUBTAG) {
sum += heap_bin_size(size+extra_bytes);
} else {
ASSERT(thing_subtag(hdr) == REFC_BINARY_SUBTAG);
sum += PROC_BIN_SIZE;
}
goto pop_next;
}
case MAP_SUBTAG:
switch (MAP_HEADER_TYPE(hdr)) {
case MAP_HEADER_TAG_FLATMAP_HEAD : {
flatmap_t *mp = (flatmap_t *) ptr;
Uint n = flatmap_get_size(mp) + 1;
sum += n + 2;
ptr += 2; /* hdr + size words */
while (n--) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT(s, obj);
}
}
goto pop_next;
}
case MAP_HEADER_TAG_HAMT_HEAD_BITMAP :
case MAP_HEADER_TAG_HAMT_HEAD_ARRAY :
case MAP_HEADER_TAG_HAMT_NODE_BITMAP : {
Uint n = hashmap_bitcount(MAP_HEADER_VAL(hdr));
sum += 1 + n + header_arity(hdr);
ptr += 1 + header_arity(hdr);
if (n == 0) {
goto pop_next;
}
while(n--) {
obj = *ptr++;
if (!IS_CONST(obj)) {
EQUEUE_PUT(s, obj);
}
}
goto pop_next;
}
default:
erl_exit(ERTS_ABORT_EXIT, "copy_shared_calculate: bad hashmap type %d\n", MAP_HEADER_TYPE(hdr));
}
case BIN_MATCHSTATE_SUBTAG:
erl_exit(ERTS_ABORT_EXIT,
"size_shared: matchstate term not allowed");
default:
sum += thing_arityval(hdr) + 1;
goto pop_next;
}
break;
}
case TAG_PRIMARY_IMMED1:
pop_next:
if (EQUEUE_ISEMPTY(s)) {
/* add sentinel to the table */
SHTABLE_PUSH(t, THE_NON_VALUE, THE_NON_VALUE, NULL);
/* store persistent info */
BITSTORE_CLOSE(b);
info->queue_start = s.start;
info->queue_end = s.end;
info->queue_alloc_type = s.alloc_type;
info->bitstore_start = b.wstart;
info->bitstore_alloc_type = b.alloc_type;
info->shtable_start = t.start;
info->shtable_alloc_type = t.alloc_type;
/* single point of return: the size of the object */
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] size was: %u\n", myself->common.id, sum));
return sum;
}
obj = EQUEUE_GET(s);
break;
default:
erl_exit(ERTS_ABORT_EXIT, "[pid=%T] size_shared: bad tag for %#x\n", obj);
}
}
}
/*
* Copy object "obj" preserving sharing.
* Second half: copy and restore the object.
*/
Uint copy_shared_perform(Eterm obj, Uint size, erts_shcopy_t *info,
Eterm** hpp, ErlOffHeap* off_heap, Uint32 flags) {
Uint e;
unsigned sz;
Eterm* ptr;
Eterm* hp;
Eterm* hscan;
Eterm result;
Eterm* resp;
unsigned remaining;
Process* myself;
int force_local = flags & ERTS_SHCOPY_FLG_TMPBUF;
#ifdef DEBUG
Eterm saved_obj = obj;
#endif
DECLARE_EQUEUE_FROM_INFO(s, info);
DECLARE_BITSTORE_FROM_INFO(b, info);
DECLARE_SHTABLE_FROM_INFO(t, info);
/* step #0:
-------------------------------------------------------
get rid of the easy cases first:
- copying constants
- if not a proper process, do flat copy
*/
if (IS_CONST(obj))
return obj;
#ifdef SHCOPY_DISABLE
flags |= disable_copy_shared;
#endif
myself = erts_get_current_process();
if (myself == NULL || (flags & ERTS_SHCOPY_FLG_NONE))
return copy_struct(obj, size, hpp, off_heap);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] copy_shared_perform %p\n", myself->common.id, obj));
/* step #2: was performed before this function was called
-------------------------------------------------------
allocate new space
*/
hscan = hp = *hpp;
/* step #3:
-------------------------------------------------------
traverse the term a second time and when traversing:
a. if the object is marked as shared
a1. if the entry contains a forwarding ptr, use that
a2. otherwise, copy it to the new space and store the
forwarding ptr to the entry
b. otherwise, reverse-transform as you do in size_shared
and copy to the new space
*/
resp = &result;
remaining = 0;
for (;;) {
switch (primary_tag(obj)) {
case TAG_PRIMARY_LIST: {
Eterm head, tail;
ptr = list_val(obj);
/* off heap list pointers are copied verbatim */
if (!INHEAP(myself, ptr)) {
*resp = obj;
goto cleanup_next;
}
head = CAR(ptr);
tail = CDR(ptr);
/* if it is shared */
if (tail == THE_NON_VALUE) {
e = head >> _TAG_PRIMARY_SIZE;
/* if it has been processed, just use the forwarding pointer */
if (primary_tag(head) == LIST_SHARED_PROCESSED) {
*resp = make_list(SHTABLE_FWD(t, e));
goto cleanup_next;
}
/* else, let's process it now,
copy it and keep the forwarding pointer */
else {
CAR(ptr) = (head - primary_tag(head)) + LIST_SHARED_PROCESSED;
head = SHTABLE_X(t, e);
tail = SHTABLE_Y(t, e);
ptr = &(SHTABLE_X(t, e));
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] tabled L %p is %p\n", myself->common.id, ptr, SHTABLE_REV(t, e)));
SHTABLE_FWD_UPD(t, e, hp);
}
}
/* if not already clean, clean it up and copy it */
if (primary_tag(tail) == TAG_PRIMARY_HEADER) {
if (primary_tag(head) == TAG_PRIMARY_HEADER) {
Eterm saved = BITSTORE_GET(b);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] unmangling L/B %p\n", myself->common.id, ptr));
CAR(ptr) = head = (head - TAG_PRIMARY_HEADER) + saved;
CDR(ptr) = tail = (tail - TAG_PRIMARY_HEADER) + TAG_PRIMARY_BOXED;
} else {
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] unmangling L/L %p\n", myself->common.id, ptr));
CDR(ptr) = tail = (tail - TAG_PRIMARY_HEADER) + TAG_PRIMARY_LIST;
}
} else if (primary_tag(head) == TAG_PRIMARY_HEADER) {
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] unmangling L/I %p\n", myself->common.id, ptr));
CAR(ptr) = head = (head - TAG_PRIMARY_HEADER) | primary_tag(tail);
CDR(ptr) = tail = (tail - primary_tag(tail)) | TAG_PRIMARY_IMMED1;
} else {
ASSERT(0 && "cannot come here");
goto cleanup_next;
}
/* and its children too */
if (IS_CONST(head)) {
CAR(hp) = head;
} else {
EQUEUE_PUT_UNCHECKED(s, head);
CAR(hp) = HEAP_ELEM_TO_BE_FILLED;
}
*resp = make_list(hp);
resp = &(CDR(hp));
hp += 2;
obj = tail;
break;
}
case TAG_PRIMARY_BOXED: {
Eterm hdr;
ptr = boxed_val(obj);
/* off heap pointers to boxes are copied verbatim */
if (!INHEAP(myself, ptr)) {
*resp = obj;
goto cleanup_next;
}
hdr = *ptr;
/* clean it up, unless it's already clean or shared and processed */
switch (primary_tag(hdr)) {
case TAG_PRIMARY_HEADER:
ASSERT(0 && "cannot come here");
/* if it is shared and has been processed,
just use the forwarding pointer */
case BOXED_SHARED_PROCESSED:
e = hdr >> _TAG_PRIMARY_SIZE;
*resp = make_boxed(SHTABLE_FWD(t, e));
goto cleanup_next;
/* if it is shared but has not been processed yet, let's process
it now: copy it and keep the forwarding pointer */
case BOXED_SHARED_UNPROCESSED:
e = hdr >> _TAG_PRIMARY_SIZE;
*ptr = (hdr - primary_tag(hdr)) + BOXED_SHARED_PROCESSED;
hdr = SHTABLE_X(t, e);
ASSERT(primary_tag(hdr) == BOXED_VISITED);
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] tabled B %p is %p\n", myself->common.id, ptr, SHTABLE_REV(t, e)));
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] unmangling B %p\n", myself->common.id, ptr));
SHTABLE_X(t, e) = hdr = (hdr - BOXED_VISITED) + TAG_PRIMARY_HEADER;
SHTABLE_FWD_UPD(t, e, hp);
break;
case BOXED_VISITED:
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] unmangling B %p\n", myself->common.id, ptr));
*ptr = hdr = (hdr - BOXED_VISITED) + TAG_PRIMARY_HEADER;
break;
}
/* and its children too */
switch (hdr & _TAG_HEADER_MASK) {
case ARITYVAL_SUBTAG: {
int arity = header_arity(hdr);
*resp = make_boxed(hp);
*hp++ = hdr;
while (arity-- > 0) {
obj = *++ptr;
if (IS_CONST(obj)) {
*hp++ = obj;
} else {
EQUEUE_PUT_UNCHECKED(s, obj);
*hp++ = HEAP_ELEM_TO_BE_FILLED;
}
}
goto cleanup_next;
}
case FUN_SUBTAG: {
ErlFunThing* funp = (ErlFunThing *) ptr;
unsigned eterms = 1 /* creator */ + funp->num_free;
sz = thing_arityval(hdr);
funp = (ErlFunThing *) hp;
*resp = make_fun(hp);
*hp++ = hdr;
ptr++;
while (sz-- > 0) {
*hp++ = *ptr++;
}
while (eterms-- > 0) {
obj = *ptr++;
if (IS_CONST(obj)) {
*hp++ = obj;
} else {
EQUEUE_PUT_UNCHECKED(s, obj);
*hp++ = HEAP_ELEM_TO_BE_FILLED;
}
}
funp->next = off_heap->first;
off_heap->first = (struct erl_off_heap_header*) funp;
erts_refc_inc(&funp->fe->refc, 2);
goto cleanup_next;
}
case MAP_SUBTAG:
*resp = make_flatmap(hp);
*hp++ = hdr;
switch (MAP_HEADER_TYPE(hdr)) {
case MAP_HEADER_TAG_FLATMAP_HEAD : {
flatmap_t *mp = (flatmap_t *) ptr;
Uint n = flatmap_get_size(mp) + 1;
*hp++ = *++ptr; /* keys */
while (n--) {
obj = *++ptr;
if (IS_CONST(obj)) {
*hp++ = obj;
} else {
EQUEUE_PUT_UNCHECKED(s, obj);
*hp++ = HEAP_ELEM_TO_BE_FILLED;
}
}
goto cleanup_next;
}
case MAP_HEADER_TAG_HAMT_HEAD_BITMAP :
case MAP_HEADER_TAG_HAMT_HEAD_ARRAY :
*hp++ = *++ptr; /* total map size */
case MAP_HEADER_TAG_HAMT_NODE_BITMAP : {
Uint n = hashmap_bitcount(MAP_HEADER_VAL(hdr));
while (n--) {
obj = *++ptr;
if (IS_CONST(obj)) {
*hp++ = obj;
} else {
EQUEUE_PUT_UNCHECKED(s, obj);
*hp++ = HEAP_ELEM_TO_BE_FILLED;
}
}
goto cleanup_next;
}
default:
erl_exit(ERTS_ABORT_EXIT, "copy_shared_perform: bad hashmap type %d\n", MAP_HEADER_TYPE(hdr));
}
case REFC_BINARY_SUBTAG: {
ProcBin* pb = (ProcBin *) ptr;
sz = thing_arityval(hdr);
if (pb->flags) {
erts_emasculate_writable_binary(pb);
}
pb = (ProcBin *) hp;
*resp = make_binary(hp);
*hp++ = hdr;
ptr++;
while (sz-- > 0) {
*hp++ = *ptr++;
}
erts_refc_inc(&pb->val->refc, 2);
pb->next = off_heap->first;
pb->flags = 0;
off_heap->first = (struct erl_off_heap_header*) pb;
OH_OVERHEAD(off_heap, pb->size / sizeof(Eterm));
goto cleanup_next;
}
case SUB_BINARY_SUBTAG: {
ErlSubBin* sb = (ErlSubBin *) ptr;
Eterm real_bin = sb->orig;
Uint bit_offset = sb->bitoffs;
Uint bit_size = sb->bitsize;
Uint offset = sb->offs;
size_t size = sb->size;
Uint extra_bytes;
Uint real_size;
if ((bit_size + bit_offset) > 8) {
extra_bytes = 2;
} else if ((bit_size + bit_offset) > 0) {
extra_bytes = 1;
} else {
extra_bytes = 0;
}
real_size = size+extra_bytes;
ASSERT(is_boxed(real_bin) &&
(((*boxed_val(real_bin)) &
(_TAG_HEADER_MASK - _BINARY_XXX_MASK - BOXED_VISITED_MASK))
== _TAG_HEADER_REFC_BIN));
ptr = _unchecked_binary_val(real_bin);
*resp = make_binary(hp);
if (extra_bytes != 0) {
ErlSubBin* res = (ErlSubBin *) hp;
hp += ERL_SUB_BIN_SIZE;
res->thing_word = HEADER_SUB_BIN;
res->size = size;
res->bitsize = bit_size;
res->bitoffs = bit_offset;
res->offs = 0;
res->is_writable = 0;
res->orig = make_binary(hp);
}
if (thing_subtag(*ptr & ~BOXED_VISITED_MASK) == HEAP_BINARY_SUBTAG) {
ErlHeapBin* from = (ErlHeapBin *) ptr;
ErlHeapBin* to = (ErlHeapBin *) hp;
hp += heap_bin_size(real_size);
to->thing_word = header_heap_bin(real_size);
to->size = real_size;
sys_memcpy(to->data, ((byte *)from->data)+offset, real_size);
} else {
ProcBin* from = (ProcBin *) ptr;
ProcBin* to = (ProcBin *) hp;
ASSERT(thing_subtag(*ptr & ~BOXED_VISITED_MASK) == REFC_BINARY_SUBTAG);
if (from->flags) {
erts_emasculate_writable_binary(from);
}
hp += PROC_BIN_SIZE;
to->thing_word = HEADER_PROC_BIN;
to->size = real_size;
to->val = from->val;
erts_refc_inc(&to->val->refc, 2);
to->bytes = from->bytes + offset;
to->next = off_heap->first;
to->flags = 0;
off_heap->first = (struct erl_off_heap_header*) to;
OH_OVERHEAD(off_heap, to->size / sizeof(Eterm));
}
goto cleanup_next;
}
case EXTERNAL_PID_SUBTAG:
case EXTERNAL_PORT_SUBTAG:
case EXTERNAL_REF_SUBTAG: {
ExternalThing *etp = (ExternalThing *) hp;
sz = thing_arityval(hdr);
*resp = make_external(hp);
*hp++ = hdr;
ptr++;
while (sz-- > 0) {
*hp++ = *ptr++;
}
etp->next = off_heap->first;
off_heap->first = (struct erl_off_heap_header*) etp;
erts_refc_inc(&etp->node->refc, 2);
goto cleanup_next;
}
default:
sz = thing_arityval(hdr);
*resp = make_boxed(hp);
*hp++ = hdr;
ptr++;
while (sz-- > 0) {
*hp++ = *ptr++;
}
goto cleanup_next;
}
break;
}
case TAG_PRIMARY_IMMED1:
*resp = obj;
cleanup_next:
if (EQUEUE_ISEMPTY(s)) {
goto all_clean;
}
obj = EQUEUE_GET(s);
for (;;) {
ASSERT(hscan < hp);
if (remaining == 0) {
if (*hscan == HEAP_ELEM_TO_BE_FILLED) {
resp = hscan;
hscan += 2;
break; /* scanning loop */
} else if (primary_tag(*hscan) == TAG_PRIMARY_HEADER) {
switch (*hscan & _TAG_HEADER_MASK) {
case ARITYVAL_SUBTAG:
remaining = header_arity(*hscan);
hscan++;
break;
case FUN_SUBTAG: {
ErlFunThing* funp = (ErlFunThing *) hscan;
hscan += 1 + thing_arityval(*hscan);
remaining = 1 + funp->num_free;
break;
}
case MAP_SUBTAG:
switch (MAP_HEADER_TYPE(*hscan)) {
case MAP_HEADER_TAG_FLATMAP_HEAD : {
flatmap_t *mp = (flatmap_t *) hscan;
remaining = flatmap_get_size(mp) + 1;
hscan += 2;
break;
}
case MAP_HEADER_TAG_HAMT_HEAD_BITMAP :
case MAP_HEADER_TAG_HAMT_HEAD_ARRAY :
case MAP_HEADER_TAG_HAMT_NODE_BITMAP :
remaining = hashmap_bitcount(MAP_HEADER_VAL(*hscan));
hscan += MAP_HEADER_ARITY(*hscan) + 1;
break;
default:
erl_exit(ERTS_ABORT_EXIT,
"copy_shared_perform: bad hashmap type %d\n",
MAP_HEADER_TYPE(*hscan));
}
break;
case SUB_BINARY_SUBTAG:
ASSERT(((ErlSubBin *) hscan)->bitoffs +
((ErlSubBin *) hscan)->bitsize > 0);
hscan += ERL_SUB_BIN_SIZE;
break;
default:
hscan += 1 + thing_arityval(*hscan);
break;
}
} else {
hscan++;
}
} else if (*hscan == HEAP_ELEM_TO_BE_FILLED) {
resp = hscan++;
remaining--;
break; /* scanning loop */
} else {
hscan++;
remaining--;
}
}
ASSERT(resp < hp);
break;
default:
erl_exit(ERTS_ABORT_EXIT, "size_shared: bad tag for %#x\n", obj);
}
}
/* step #4:
-------------------------------------------------------
traverse the table and reverse-transform all stored entries
*/
all_clean:
for (e = 0; ; e += SHTABLE_INCR) {
ptr = SHTABLE_REV(t, e);
if (ptr == NULL)
break;
VERBOSE(DEBUG_SHCOPY, ("[copy] restoring shared: %x\n", ptr));
/* entry was a list */
if (SHTABLE_Y(t, e) != THE_NON_VALUE) {
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] untabling L %p\n", myself->common.id, ptr));
CAR(ptr) = SHTABLE_X(t, e);
CDR(ptr) = SHTABLE_Y(t, e);
}
/* entry was boxed */
else {
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] untabling B %p\n", myself->common.id, ptr));
*ptr = SHTABLE_X(t, e);
ASSERT(primary_tag(*ptr) == TAG_PRIMARY_HEADER);
}
}
#ifdef DEBUG
if (eq(saved_obj, result) == 0) {
erts_fprintf(stderr, "original = %T\n", saved_obj);
erts_fprintf(stderr, "copy = %T\n", result);
erl_exit(ERTS_ABORT_EXIT, "copy (shared) not equal to source\n");
}
#endif
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] original was %T\n", myself->common.id, saved_obj));
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] copy is %T\n", myself->common.id, result));
VERBOSE(DEBUG_SHCOPY, ("[pid=%T] result is at %p\n", myself->common.id, result));
ASSERT(hp == *hpp + size);
*hpp = hp;
return result;
}
/*
* Copy a term that is guaranteed to be contained in a single
* heap block. The heap block is copied word by word, and any
* pointers are offsetted to point correctly in the new location.
*
* Typically used to copy a term from an ets table.
*
* NOTE: Assumes that term is a tuple (ptr is an untagged tuple ptr).
*/
Eterm copy_shallow(Eterm* ptr, Uint sz, Eterm** hpp, ErlOffHeap* off_heap)
{
Eterm* tp = ptr;
Eterm* hp = *hpp;
const Eterm res = make_tuple(hp);
const Sint offs = (hp - tp) * sizeof(Eterm);
while (sz--) {
Eterm val = *tp++;
switch (primary_tag(val)) {
case TAG_PRIMARY_IMMED1:
*hp++ = val;
break;
case TAG_PRIMARY_LIST:
case TAG_PRIMARY_BOXED:
*hp++ = byte_offset_ptr(val, offs);
break;
case TAG_PRIMARY_HEADER:
*hp++ = val;
switch (val & _HEADER_SUBTAG_MASK) {
case ARITYVAL_SUBTAG:
break;
case REFC_BINARY_SUBTAG:
{
ProcBin* pb = (ProcBin *) (tp-1);
erts_refc_inc(&pb->val->refc, 2);
OH_OVERHEAD(off_heap, pb->size / sizeof(Eterm));
}
goto off_heap_common;
case FUN_SUBTAG:
{
ErlFunThing* funp = (ErlFunThing *) (tp-1);
erts_refc_inc(&funp->fe->refc, 2);
}
goto off_heap_common;
case EXTERNAL_PID_SUBTAG:
case EXTERNAL_PORT_SUBTAG:
case EXTERNAL_REF_SUBTAG:
{
ExternalThing* etp = (ExternalThing *) (tp-1);
erts_refc_inc(&etp->node->refc, 2);
}
off_heap_common:
{
struct erl_off_heap_header* ohh = (struct erl_off_heap_header*)(hp-1);
int tari = thing_arityval(val);
sz -= tari;
while (tari--) {
*hp++ = *tp++;
}
ohh->next = off_heap->first;
off_heap->first = ohh;
}
break;
default:
{
int tari = header_arity(val);
sz -= tari;
while (tari--) {
*hp++ = *tp++;
}
}
break;
}
break;
}
}
*hpp = hp;
return res;
}
/* Move all terms in heap fragments into heap. The terms must be guaranteed to
* be contained within the fragments. The source terms are destructed with
* move markers.
* Typically used to copy a multi-fragmented message (from NIF).
*/
void erts_move_multi_frags(Eterm** hpp, ErlOffHeap* off_heap, ErlHeapFragment* first,
Eterm* refs, unsigned nrefs, int literals)
{
ErlHeapFragment* bp;
Eterm* hp_start = *hpp;
Eterm* hp_end;
Eterm* hp;
unsigned i;
Eterm literal_tag;
#ifdef TAG_LITERAL_PTR
literal_tag = (Eterm) literals ? TAG_LITERAL_PTR : 0;
#else
literal_tag = (Eterm) 0;
#endif
for (bp=first; bp!=NULL; bp=bp->next) {
move_one_frag(hpp, bp, off_heap, literals);
}
hp_end = *hpp;
for (hp=hp_start; hp<hp_end; ++hp) {
Eterm* ptr;
Eterm val;
Eterm gval = *hp;
switch (primary_tag(gval)) {
case TAG_PRIMARY_BOXED:
ptr = boxed_val(gval);
val = *ptr;
if (IS_MOVED_BOXED(val)) {
ASSERT(is_boxed(val));
#ifdef TAG_LITERAL_PTR
val |= literal_tag;
#endif
*hp = val;
}
break;
case TAG_PRIMARY_LIST:
ptr = list_val(gval);
val = *ptr;
if (IS_MOVED_CONS(val)) {
val = ptr[1];
#ifdef TAG_LITERAL_PTR
val |= literal_tag;
#endif
*hp = val;
}
break;
case TAG_PRIMARY_HEADER:
if (header_is_thing(gval)) {
hp += thing_arityval(gval);
}
break;
}
}
for (i=0; i<nrefs; ++i) {
refs[i] = follow_moved(refs[i], literal_tag);
}
}
static void
move_one_frag(Eterm** hpp, ErlHeapFragment* frag, ErlOffHeap* off_heap, int literals)
{
Eterm* ptr = frag->mem;
Eterm* end = ptr + frag->used_size;
Eterm dummy_ref;
Eterm* hp = *hpp;
while (ptr != end) {
Eterm val;
ASSERT(ptr < end);
val = *ptr;
ASSERT(val != ERTS_HOLE_MARKER);
if (is_header(val)) {
struct erl_off_heap_header* hdr = (struct erl_off_heap_header*)hp;
ASSERT(ptr + header_arity(val) < end);
MOVE_BOXED(ptr, val, hp, &dummy_ref);
switch (val & _HEADER_SUBTAG_MASK) {
case REFC_BINARY_SUBTAG:
case FUN_SUBTAG:
case EXTERNAL_PID_SUBTAG:
case EXTERNAL_PORT_SUBTAG:
case EXTERNAL_REF_SUBTAG:
hdr->next = off_heap->first;
off_heap->first = hdr;
break;
}
}
else { /* must be a cons cell */
ASSERT(ptr+1 < end);
MOVE_CONS(ptr, val, hp, &dummy_ref);
ptr += 2;
}
}
*hpp = hp;
OH_OVERHEAD(off_heap, frag->off_heap.overhead);
frag->off_heap.first = NULL;
}
|