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
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
|
/**
* @file llmemory.cpp
* @brief Very special memory allocation/deallocation stuff here
*
* $LicenseInfo:firstyear=2002&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llthread.h"
#if defined(LL_WINDOWS)
# include <psapi.h>
#elif defined(LL_DARWIN)
# include <sys/types.h>
# include <mach/task.h>
# include <mach/mach_init.h>
#elif LL_LINUX || LL_SOLARIS
# include <unistd.h>
#endif
#include "llmemory.h"
#include "llsys.h"
#include "llframetimer.h"
//----------------------------------------------------------------------------
//static
char* LLMemory::reserveMem = 0;
U32Kibibytes LLMemory::sAvailPhysicalMemInKB(U32_MAX);
U32Kibibytes LLMemory::sMaxPhysicalMemInKB(0);
U32Kibibytes LLMemory::sAllocatedMemInKB(0);
U32Kibibytes LLMemory::sAllocatedPageSizeInKB(0);
U32Kibibytes LLMemory::sMaxHeapSizeInKB(U32_MAX);
BOOL LLMemory::sEnableMemoryFailurePrevention = FALSE;
#if __DEBUG_PRIVATE_MEM__
LLPrivateMemoryPoolManager::mem_allocation_info_t LLPrivateMemoryPoolManager::sMemAllocationTracker;
#endif
void ll_assert_aligned_func(uintptr_t ptr,U32 alignment)
{
#ifdef SHOW_ASSERT
// Redundant, place to set breakpoints.
if (ptr%alignment!=0)
{
LL_WARNS() << "alignment check failed" << LL_ENDL;
}
llassert(ptr%alignment==0);
#endif
}
//static
void LLMemory::initClass()
{
if (!reserveMem)
{
reserveMem = new char[16*1024]; // reserve 16K for out of memory error handling
}
}
//static
void LLMemory::cleanupClass()
{
delete [] reserveMem;
reserveMem = NULL;
}
//static
void LLMemory::freeReserve()
{
delete [] reserveMem;
reserveMem = NULL;
}
//static
void LLMemory::initMaxHeapSizeGB(F32 max_heap_size_gb, BOOL prevent_heap_failure)
{
sMaxHeapSizeInKB = (U32)(max_heap_size_gb * 1024 * 1024) ;
sEnableMemoryFailurePrevention = prevent_heap_failure ;
}
//static
void LLMemory::updateMemoryInfo()
{
#if LL_WINDOWS
HANDLE self = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS counters;
if (!GetProcessMemoryInfo(self, &counters, sizeof(counters)))
{
LL_WARNS() << "GetProcessMemoryInfo failed" << LL_ENDL;
return ;
}
sAllocatedMemInKB = (U32)(counters.WorkingSetSize / 1024) ;
sAllocatedPageSizeInKB = (U32)(counters.PagefileUsage / 1024) ;
U32Kibibytes avail_phys, avail_virtual;
LLMemoryInfo::getAvailableMemoryKB(avail_phys, avail_virtual) ;
sMaxPhysicalMemInKB = llmin(avail_phys + sAllocatedMemInKB, sMaxHeapSizeInKB);
if(sMaxPhysicalMemInKB > sAllocatedMemInKB)
{
sAvailPhysicalMemInKB = sMaxPhysicalMemInKB - sAllocatedMemInKB ;
}
else
{
sAvailPhysicalMemInKB = 0 ;
}
#else
//not valid for other systems for now.
sAllocatedMemInKB = (U32)(LLMemory::getCurrentRSS() / 1024) ;
sMaxPhysicalMemInKB = U32_MAX ;
sAvailPhysicalMemInKB = U32_MAX ;
#endif
return ;
}
//
//this function is to test if there is enough space with the size in the virtual address space.
//it does not do any real allocation
//if success, it returns the address where the memory chunk can fit in;
//otherwise it returns NULL.
//
//static
void* LLMemory::tryToAlloc(void* address, U32 size)
{
#if LL_WINDOWS
address = VirtualAlloc(address, size, MEM_RESERVE | MEM_TOP_DOWN, PAGE_NOACCESS) ;
if(address)
{
if(!VirtualFree(address, 0, MEM_RELEASE))
{
LL_ERRS() << "error happens when free some memory reservation." << LL_ENDL ;
}
}
return address ;
#else
return (void*)0x01 ; //skip checking
#endif
}
//static
void LLMemory::logMemoryInfo(BOOL update)
{
if(update)
{
updateMemoryInfo() ;
LLPrivateMemoryPoolManager::getInstance()->updateStatistics() ;
}
LL_INFOS() << "Current allocated physical memory(KB): " << sAllocatedMemInKB << LL_ENDL ;
LL_INFOS() << "Current allocated page size (KB): " << sAllocatedPageSizeInKB << LL_ENDL ;
LL_INFOS() << "Current availabe physical memory(KB): " << sAvailPhysicalMemInKB << LL_ENDL ;
LL_INFOS() << "Current max usable memory(KB): " << sMaxPhysicalMemInKB << LL_ENDL ;
LL_INFOS() << "--- private pool information -- " << LL_ENDL ;
LL_INFOS() << "Total reserved (KB): " << LLPrivateMemoryPoolManager::getInstance()->mTotalReservedSize / 1024 << LL_ENDL ;
LL_INFOS() << "Total allocated (KB): " << LLPrivateMemoryPoolManager::getInstance()->mTotalAllocatedSize / 1024 << LL_ENDL ;
}
//return 0: everything is normal;
//return 1: the memory pool is low, but not in danger;
//return -1: the memory pool is in danger, is about to crash.
//static
bool LLMemory::isMemoryPoolLow()
{
static const U32 LOW_MEMEOY_POOL_THRESHOLD_KB = 64 * 1024 ; //64 MB for emergency use
const static U32 MAX_SIZE_CHECKED_MEMORY_BLOCK = 64 * 1024 * 1024 ; //64 MB
static void* last_reserved_address = NULL ;
if(!sEnableMemoryFailurePrevention)
{
return false ; //no memory failure prevention.
}
if(sAvailPhysicalMemInKB < (LOW_MEMEOY_POOL_THRESHOLD_KB >> 2)) //out of physical memory
{
return true ;
}
if(sAllocatedPageSizeInKB + (LOW_MEMEOY_POOL_THRESHOLD_KB >> 2) > sMaxHeapSizeInKB) //out of virtual address space.
{
return true ;
}
bool is_low = (S32)(sAvailPhysicalMemInKB < LOW_MEMEOY_POOL_THRESHOLD_KB ||
sAllocatedPageSizeInKB + LOW_MEMEOY_POOL_THRESHOLD_KB > sMaxHeapSizeInKB) ;
//check the virtual address space fragmentation
if(!is_low)
{
if(!last_reserved_address)
{
last_reserved_address = LLMemory::tryToAlloc(last_reserved_address, MAX_SIZE_CHECKED_MEMORY_BLOCK) ;
}
else
{
last_reserved_address = LLMemory::tryToAlloc(last_reserved_address, MAX_SIZE_CHECKED_MEMORY_BLOCK) ;
if(!last_reserved_address) //failed, try once more
{
last_reserved_address = LLMemory::tryToAlloc(last_reserved_address, MAX_SIZE_CHECKED_MEMORY_BLOCK) ;
}
}
is_low = !last_reserved_address ; //allocation failed
}
return is_low ;
}
//static
U32Kibibytes LLMemory::getAvailableMemKB()
{
return sAvailPhysicalMemInKB ;
}
//static
U32Kibibytes LLMemory::getMaxMemKB()
{
return sMaxPhysicalMemInKB ;
}
//static
U32Kibibytes LLMemory::getAllocatedMemKB()
{
return sAllocatedMemInKB ;
}
//----------------------------------------------------------------------------
#if defined(LL_WINDOWS)
U64 LLMemory::getCurrentRSS()
{
HANDLE self = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS counters;
if (!GetProcessMemoryInfo(self, &counters, sizeof(counters)))
{
LL_WARNS() << "GetProcessMemoryInfo failed" << LL_ENDL;
return 0;
}
return counters.WorkingSetSize;
}
//static
U32 LLMemory::getWorkingSetSize()
{
PROCESS_MEMORY_COUNTERS pmc ;
U32 ret = 0 ;
if (GetProcessMemoryInfo( GetCurrentProcess(), &pmc, sizeof(pmc)) )
{
ret = pmc.WorkingSetSize ;
}
return ret ;
}
#elif defined(LL_DARWIN)
/*
The API used here is not capable of dealing with 64-bit memory sizes, but is available before 10.4.
Once we start requiring 10.4, we can use the updated API, which looks like this:
task_basic_info_64_data_t basicInfo;
mach_msg_type_number_t basicInfoCount = TASK_BASIC_INFO_64_COUNT;
if (task_info(mach_task_self(), TASK_BASIC_INFO_64, (task_info_t)&basicInfo, &basicInfoCount) == KERN_SUCCESS)
Of course, this doesn't gain us anything unless we start building the viewer as a 64-bit executable, since that's the only way
for our memory allocation to exceed 2^32.
*/
// if (sysctl(ctl, 2, &page_size, &size, NULL, 0) == -1)
// {
// LL_WARNS() << "Couldn't get page size" << LL_ENDL;
// return 0;
// } else {
// return page_size;
// }
// }
U64 LLMemory::getCurrentRSS()
{
U64 residentSize = 0;
task_basic_info_data_t basicInfo;
mach_msg_type_number_t basicInfoCount = TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&basicInfo, &basicInfoCount) == KERN_SUCCESS)
{
residentSize = basicInfo.resident_size;
// If we ever wanted it, the process virtual size is also available as:
// virtualSize = basicInfo.virtual_size;
// LL_INFOS() << "resident size is " << residentSize << LL_ENDL;
}
else
{
LL_WARNS() << "task_info failed" << LL_ENDL;
}
return residentSize;
}
U32 LLMemory::getWorkingSetSize()
{
return 0 ;
}
#elif defined(LL_LINUX)
U64 LLMemory::getCurrentRSS()
{
static const char statPath[] = "/proc/self/stat";
LLFILE *fp = LLFile::fopen(statPath, "r");
U64 rss = 0;
if (fp == NULL)
{
LL_WARNS() << "couldn't open " << statPath << LL_ENDL;
goto bail;
}
// Eee-yew! See Documentation/filesystems/proc.txt in your
// nearest friendly kernel tree for details.
{
int ret = fscanf(fp, "%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*d %*d "
"%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %Lu",
&rss);
if (ret != 1)
{
LL_WARNS() << "couldn't parse contents of " << statPath << LL_ENDL;
rss = 0;
}
}
fclose(fp);
bail:
return rss;
}
U32 LLMemory::getWorkingSetSize()
{
return 0 ;
}
#elif LL_SOLARIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define _STRUCTURED_PROC 1
#include <sys/procfs.h>
U64 LLMemory::getCurrentRSS()
{
char path [LL_MAX_PATH]; /* Flawfinder: ignore */
sprintf(path, "/proc/%d/psinfo", (int)getpid());
int proc_fd = -1;
if((proc_fd = open(path, O_RDONLY)) == -1){
LL_WARNS() << "LLmemory::getCurrentRSS() unable to open " << path << ". Returning 0 RSS!" << LL_ENDL;
return 0;
}
psinfo_t proc_psinfo;
if(read(proc_fd, &proc_psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t)){
LL_WARNS() << "LLmemory::getCurrentRSS() Unable to read from " << path << ". Returning 0 RSS!" << LL_ENDL;
close(proc_fd);
return 0;
}
close(proc_fd);
return((U64)proc_psinfo.pr_rssize * 1024);
}
U32 LLMemory::getWorkingSetSize()
{
return 0 ;
}
#else
U64 LLMemory::getCurrentRSS()
{
return 0;
}
U32 LLMemory::getWorkingSetSize()
{
return 0;
}
#endif
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//minimum slot size and minimal slot size interval
const U32 ATOMIC_MEM_SLOT = 16 ; //bytes
//minimum block sizes (page size) for small allocation, medium allocation, large allocation
const U32 MIN_BLOCK_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {2 << 10, 4 << 10, 16 << 10} ; //
//maximum block sizes for small allocation, medium allocation, large allocation
const U32 MAX_BLOCK_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {64 << 10, 1 << 20, 4 << 20} ;
//minimum slot sizes for small allocation, medium allocation, large allocation
const U32 MIN_SLOT_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {ATOMIC_MEM_SLOT, 2 << 10, 512 << 10};
//maximum slot sizes for small allocation, medium allocation, large allocation
const U32 MAX_SLOT_SIZES[LLPrivateMemoryPool::SUPER_ALLOCATION] = {(2 << 10) - ATOMIC_MEM_SLOT, (512 - 2) << 10, 4 << 20};
//size of a block with multiple slots can not exceed CUT_OFF_SIZE
const U32 CUT_OFF_SIZE = (64 << 10) ; //64 KB
//max number of slots in a block
const U32 MAX_NUM_SLOTS_IN_A_BLOCK = llmin(MIN_BLOCK_SIZES[0] / ATOMIC_MEM_SLOT, ATOMIC_MEM_SLOT * 8) ;
//-------------------------------------------------------------
//align val to be integer times of ATOMIC_MEM_SLOT
U32 align(U32 val)
{
U32 aligned = (val / ATOMIC_MEM_SLOT) * ATOMIC_MEM_SLOT ;
if(aligned < val)
{
aligned += ATOMIC_MEM_SLOT ;
}
return aligned ;
}
//-------------------------------------------------------------
//class LLPrivateMemoryPool::LLMemoryBlock
//-------------------------------------------------------------
//
//each memory block could fit for two page sizes: 0.75 * mSlotSize, which starts from the beginning of the memory chunk and grow towards the end of the
//the block; another is mSlotSize, which starts from the end of the block and grows towards the beginning of the block.
//
LLPrivateMemoryPool::LLMemoryBlock::LLMemoryBlock()
{
//empty
}
LLPrivateMemoryPool::LLMemoryBlock::~LLMemoryBlock()
{
//empty
}
//create and initialize a memory block
void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 slot_size)
{
mBuffer = buffer ;
mBufferSize = buffer_size ;
mSlotSize = slot_size ;
mTotalSlots = buffer_size / mSlotSize ;
llassert_always(buffer_size / mSlotSize <= MAX_NUM_SLOTS_IN_A_BLOCK) ; //max number is 128
mAllocatedSlots = 0 ;
mDummySize = 0 ;
//init the bit map.
//mark free bits
if(mTotalSlots > 32) //reserve extra space from mBuffer to store bitmap if needed.
{
mDummySize = ATOMIC_MEM_SLOT ;
mTotalSlots -= (mDummySize + mSlotSize - 1) / mSlotSize ;
mUsageBits = 0 ;
S32 usage_bit_len = (mTotalSlots + 31) / 32 ;
for(S32 i = 0 ; i < usage_bit_len - 1 ; i++)
{
*((U32*)mBuffer + i) = 0 ;
}
for(S32 i = usage_bit_len - 1 ; i < mDummySize / sizeof(U32) ; i++)
{
*((U32*)mBuffer + i) = 0xffffffff ;
}
if(mTotalSlots & 31)
{
*((U32*)mBuffer + usage_bit_len - 2) = (0xffffffff << (mTotalSlots & 31)) ;
}
}
else//no extra bitmap space reserved
{
mUsageBits = 0 ;
if(mTotalSlots & 31)
{
mUsageBits = (0xffffffff << (mTotalSlots & 31)) ;
}
}
mSelf = this ;
mNext = NULL ;
mPrev = NULL ;
llassert_always(mTotalSlots > 0) ;
}
//mark this block to be free with the memory [mBuffer, mBuffer + mBufferSize).
void LLPrivateMemoryPool::LLMemoryBlock::setBuffer(char* buffer, U32 buffer_size)
{
mBuffer = buffer ;
mBufferSize = buffer_size ;
mSelf = NULL ;
mTotalSlots = 0 ; //set the block is free.
}
//reserve a slot
char* LLPrivateMemoryPool::LLMemoryBlock::allocate()
{
llassert_always(mAllocatedSlots < mTotalSlots) ;
//find a free slot
U32* bits = NULL ;
U32 k = 0 ;
if(mUsageBits != 0xffffffff)
{
bits = &mUsageBits ;
}
else if(mDummySize > 0)//go to extra space
{
for(S32 i = 0 ; i < mDummySize / sizeof(U32); i++)
{
if(*((U32*)mBuffer + i) != 0xffffffff)
{
bits = (U32*)mBuffer + i ;
k = i + 1 ;
break ;
}
}
}
S32 idx = 0 ;
U32 tmp = *bits ;
for(; tmp & 1 ; tmp >>= 1, idx++) ;
//set the slot reserved
if(!idx)
{
*bits |= 1 ;
}
else
{
*bits |= (1 << idx) ;
}
mAllocatedSlots++ ;
return mBuffer + mDummySize + (k * 32 + idx) * mSlotSize ;
}
//free a slot
void LLPrivateMemoryPool::LLMemoryBlock::freeMem(void* addr)
{
//bit index
U32 idx = ((U32)addr - (U32)mBuffer - mDummySize) / mSlotSize ;
U32* bits = &mUsageBits ;
if(idx >= 32)
{
bits = (U32*)mBuffer + (idx - 32) / 32 ;
}
//reset the bit
if(idx & 31)
{
*bits &= ~(1 << (idx & 31)) ;
}
else
{
*bits &= ~1 ;
}
mAllocatedSlots-- ;
}
//for debug use: reset the entire bitmap.
void LLPrivateMemoryPool::LLMemoryBlock::resetBitMap()
{
for(S32 i = 0 ; i < mDummySize / sizeof(U32) ; i++)
{
*((U32*)mBuffer + i) = 0 ;
}
mUsageBits = 0 ;
}
//-------------------------------------------------------------------
//class LLMemoryChunk
//--------------------------------------------------------------------
LLPrivateMemoryPool::LLMemoryChunk::LLMemoryChunk()
{
//empty
}
LLPrivateMemoryPool::LLMemoryChunk::~LLMemoryChunk()
{
//empty
}
//create and init a memory chunk
void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 min_slot_size, U32 max_slot_size, U32 min_block_size, U32 max_block_size)
{
mBuffer = buffer ;
mBufferSize = buffer_size ;
mAlloatedSize = 0 ;
mMetaBuffer = mBuffer + sizeof(LLMemoryChunk) ;
mMinBlockSize = min_block_size; //page size
mMinSlotSize = min_slot_size;
mMaxSlotSize = max_slot_size ;
mBlockLevels = mMaxSlotSize / mMinSlotSize ;
mPartitionLevels = max_block_size / mMinBlockSize + 1 ;
S32 max_num_blocks = (buffer_size - sizeof(LLMemoryChunk) - mBlockLevels * sizeof(LLMemoryBlock*) - mPartitionLevels * sizeof(LLMemoryBlock*)) /
(mMinBlockSize + sizeof(LLMemoryBlock)) ;
//meta data space
mBlocks = (LLMemoryBlock*)mMetaBuffer ; //space reserved for all memory blocks.
mAvailBlockList = (LLMemoryBlock**)((char*)mBlocks + sizeof(LLMemoryBlock) * max_num_blocks) ;
mFreeSpaceList = (LLMemoryBlock**)((char*)mAvailBlockList + sizeof(LLMemoryBlock*) * mBlockLevels) ;
//data buffer, which can be used for allocation
mDataBuffer = (char*)mFreeSpaceList + sizeof(LLMemoryBlock*) * mPartitionLevels ;
//alignmnet
mDataBuffer = mBuffer + align(mDataBuffer - mBuffer) ;
//init
for(U32 i = 0 ; i < mBlockLevels; i++)
{
mAvailBlockList[i] = NULL ;
}
for(U32 i = 0 ; i < mPartitionLevels ; i++)
{
mFreeSpaceList[i] = NULL ;
}
//assign the entire chunk to the first block
mBlocks[0].mPrev = NULL ;
mBlocks[0].mNext = NULL ;
mBlocks[0].setBuffer(mDataBuffer, buffer_size - (mDataBuffer - mBuffer)) ;
addToFreeSpace(&mBlocks[0]) ;
mNext = NULL ;
mPrev = NULL ;
}
//static
U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 min_slot_size,
U32 max_slot_size, U32 min_block_size, U32 max_block_size)
{
//for large allocations, reserve some extra memory for meta data to avoid wasting much
if(data_buffer_size / min_slot_size < 64) //large allocations
{
U32 overhead = sizeof(LLMemoryChunk) + (data_buffer_size / min_block_size) * sizeof(LLMemoryBlock) +
sizeof(LLMemoryBlock*) * (max_slot_size / min_slot_size) + sizeof(LLMemoryBlock*) * (max_block_size / min_block_size + 1) ;
//round to integer times of min_block_size
overhead = ((overhead + min_block_size - 1) / min_block_size) * min_block_size ;
return overhead ;
}
else
{
return 0 ; //do not reserve extra overhead if for small allocations
}
}
char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size)
{
if(mMinSlotSize > size)
{
size = mMinSlotSize ;
}
if(mAlloatedSize + size > mBufferSize - (mDataBuffer - mBuffer))
{
return NULL ; //no enough space in this chunk.
}
char* p = NULL ;
U32 blk_idx = getBlockLevel(size);
LLMemoryBlock* blk = NULL ;
//check if there is free block available
if(mAvailBlockList[blk_idx])
{
blk = mAvailBlockList[blk_idx] ;
p = blk->allocate() ;
if(blk->isFull())
{
popAvailBlockList(blk_idx) ;
}
}
//ask for a new block
if(!p)
{
blk = addBlock(blk_idx) ;
if(blk)
{
p = blk->allocate() ;
if(blk->isFull())
{
popAvailBlockList(blk_idx) ;
}
}
}
//ask for space from larger blocks
if(!p)
{
for(S32 i = blk_idx + 1 ; i < mBlockLevels; i++)
{
if(mAvailBlockList[i])
{
blk = mAvailBlockList[i] ;
p = blk->allocate() ;
if(blk->isFull())
{
popAvailBlockList(i) ;
}
break ;
}
}
}
if(p && blk)
{
mAlloatedSize += blk->getSlotSize() ;
}
return p ;
}
void LLPrivateMemoryPool::LLMemoryChunk::freeMem(void* addr)
{
U32 blk_idx = getPageIndex((U32)addr) ;
LLMemoryBlock* blk = (LLMemoryBlock*)(mMetaBuffer + blk_idx * sizeof(LLMemoryBlock)) ;
blk = blk->mSelf ;
bool was_full = blk->isFull() ;
blk->freeMem(addr) ;
mAlloatedSize -= blk->getSlotSize() ;
if(blk->empty())
{
removeBlock(blk) ;
}
else if(was_full)
{
addToAvailBlockList(blk) ;
}
}
bool LLPrivateMemoryPool::LLMemoryChunk::empty()
{
return !mAlloatedSize ;
}
bool LLPrivateMemoryPool::LLMemoryChunk::containsAddress(const char* addr) const
{
return (U32)mBuffer <= (U32)addr && (U32)mBuffer + mBufferSize > (U32)addr ;
}
//debug use
void LLPrivateMemoryPool::LLMemoryChunk::dump()
{
#if 0
//sanity check
//for(S32 i = 0 ; i < mBlockLevels ; i++)
//{
// LLMemoryBlock* blk = mAvailBlockList[i] ;
// while(blk)
// {
// blk_list.push_back(blk) ;
// blk = blk->mNext ;
// }
//}
for(S32 i = 0 ; i < mPartitionLevels ; i++)
{
LLMemoryBlock* blk = mFreeSpaceList[i] ;
while(blk)
{
blk_list.push_back(blk) ;
blk = blk->mNext ;
}
}
std::sort(blk_list.begin(), blk_list.end(), LLMemoryBlock::CompareAddress());
U32 total_size = blk_list[0]->getBufferSize() ;
for(U32 i = 1 ; i < blk_list.size(); i++)
{
total_size += blk_list[i]->getBufferSize() ;
if((U32)blk_list[i]->getBuffer() < (U32)blk_list[i-1]->getBuffer() + blk_list[i-1]->getBufferSize())
{
LL_ERRS() << "buffer corrupted." << LL_ENDL ;
}
}
llassert_always(total_size + mMinBlockSize >= mBufferSize - ((U32)mDataBuffer - (U32)mBuffer)) ;
U32 blk_num = (mBufferSize - (mDataBuffer - mBuffer)) / mMinBlockSize ;
for(U32 i = 0 ; i < blk_num ; )
{
LLMemoryBlock* blk = &mBlocks[i] ;
if(blk->mSelf)
{
U32 end = blk->getBufferSize() / mMinBlockSize ;
for(U32 j = 0 ; j < end ; j++)
{
llassert_always(blk->mSelf == blk || !blk->mSelf) ;
}
i += end ;
}
else
{
LL_ERRS() << "gap happens" << LL_ENDL ;
}
}
#endif
#if 0
LL_INFOS() << "---------------------------" << LL_ENDL ;
LL_INFOS() << "Chunk buffer: " << (U32)getBuffer() << " size: " << getBufferSize() << LL_ENDL ;
LL_INFOS() << "available blocks ... " << LL_ENDL ;
for(S32 i = 0 ; i < mBlockLevels ; i++)
{
LLMemoryBlock* blk = mAvailBlockList[i] ;
while(blk)
{
LL_INFOS() << "blk buffer " << (U32)blk->getBuffer() << " size: " << blk->getBufferSize() << LL_ENDL ;
blk = blk->mNext ;
}
}
LL_INFOS() << "free blocks ... " << LL_ENDL ;
for(S32 i = 0 ; i < mPartitionLevels ; i++)
{
LLMemoryBlock* blk = mFreeSpaceList[i] ;
while(blk)
{
LL_INFOS() << "blk buffer " << (U32)blk->getBuffer() << " size: " << blk->getBufferSize() << LL_ENDL ;
blk = blk->mNext ;
}
}
#endif
}
//compute the size for a block, the size is round to integer times of mMinBlockSize.
U32 LLPrivateMemoryPool::LLMemoryChunk::calcBlockSize(U32 slot_size)
{
//
//Note: we try to make a block to have 32 slots if the size is not over 32 pages
//32 is the number of bits of an integer in a 32-bit system
//
U32 block_size;
U32 cut_off_size = llmin(CUT_OFF_SIZE, (U32)(mMinBlockSize << 5)) ;
if((slot_size << 5) <= mMinBlockSize)//for small allocations, return one page
{
block_size = mMinBlockSize ;
}
else if(slot_size >= cut_off_size)//for large allocations, return one-slot block
{
block_size = (slot_size / mMinBlockSize) * mMinBlockSize ;
if(block_size < slot_size)
{
block_size += mMinBlockSize ;
}
}
else //medium allocations
{
if((slot_size << 5) >= cut_off_size)
{
block_size = cut_off_size ;
}
else
{
block_size = ((slot_size << 5) / mMinBlockSize) * mMinBlockSize ;
}
}
llassert_always(block_size >= slot_size) ;
return block_size ;
}
//create a new block in the chunk
LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock(U32 blk_idx)
{
U32 slot_size = mMinSlotSize * (blk_idx + 1) ;
U32 preferred_block_size = calcBlockSize(slot_size) ;
U16 idx = getPageLevel(preferred_block_size);
LLMemoryBlock* blk = NULL ;
if(mFreeSpaceList[idx])//if there is free slot for blk_idx
{
blk = createNewBlock(mFreeSpaceList[idx], preferred_block_size, slot_size, blk_idx) ;
}
else if(mFreeSpaceList[mPartitionLevels - 1]) //search free pool
{
blk = createNewBlock(mFreeSpaceList[mPartitionLevels - 1], preferred_block_size, slot_size, blk_idx) ;
}
else //search for other non-preferred but enough space slot.
{
S32 min_idx = 0 ;
if(slot_size > mMinBlockSize)
{
min_idx = getPageLevel(slot_size) ;
}
for(S32 i = (S32)idx - 1 ; i >= min_idx ; i--) //search the small slots first
{
if(mFreeSpaceList[i])
{
U32 new_preferred_block_size = mFreeSpaceList[i]->getBufferSize();
new_preferred_block_size = (new_preferred_block_size / mMinBlockSize) * mMinBlockSize ; //round to integer times of mMinBlockSize.
//create a NEW BLOCK THERE.
if(new_preferred_block_size >= slot_size) //at least there is space for one slot.
{
blk = createNewBlock(mFreeSpaceList[i], new_preferred_block_size, slot_size, blk_idx) ;
}
break ;
}
}
if(!blk)
{
for(U16 i = idx + 1 ; i < mPartitionLevels - 1; i++) //search the large slots
{
if(mFreeSpaceList[i])
{
//create a NEW BLOCK THERE.
blk = createNewBlock(mFreeSpaceList[i], preferred_block_size, slot_size, blk_idx) ;
break ;
}
}
}
}
return blk ;
}
//create a new block at the designed location
LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNewBlock(LLMemoryBlock* blk, U32 buffer_size, U32 slot_size, U32 blk_idx)
{
//unlink from the free space
removeFromFreeSpace(blk) ;
//check the rest space
U32 new_free_blk_size = blk->getBufferSize() - buffer_size ;
if(new_free_blk_size < mMinBlockSize) //can not partition the memory into size smaller than mMinBlockSize
{
new_free_blk_size = 0 ; //discard the last small extra space.
}
//add the rest space back to the free list
if(new_free_blk_size > 0) //blk still has free space
{
LLMemoryBlock* next_blk = blk + (buffer_size / mMinBlockSize) ;
next_blk->mPrev = NULL ;
next_blk->mNext = NULL ;
next_blk->setBuffer(blk->getBuffer() + buffer_size, new_free_blk_size) ;
addToFreeSpace(next_blk) ;
}
blk->init(blk->getBuffer(), buffer_size, slot_size) ;
//insert to the available block list...
mAvailBlockList[blk_idx] = blk ;
//mark the address map: all blocks covered by this block space pointing back to this block.
U32 end = (buffer_size / mMinBlockSize) ;
for(U32 i = 1 ; i < end ; i++)
{
(blk + i)->mSelf = blk ;
}
return blk ;
}
//delete a block, release the block to the free pool.
void LLPrivateMemoryPool::LLMemoryChunk::removeBlock(LLMemoryBlock* blk)
{
//remove from the available block list
if(blk->mPrev)
{
blk->mPrev->mNext = blk->mNext ;
}
if(blk->mNext)
{
blk->mNext->mPrev = blk->mPrev ;
}
U32 blk_idx = getBlockLevel(blk->getSlotSize());
if(mAvailBlockList[blk_idx] == blk)
{
mAvailBlockList[blk_idx] = blk->mNext ;
}
blk->mNext = NULL ;
blk->mPrev = NULL ;
//mark it free
blk->setBuffer(blk->getBuffer(), blk->getBufferSize()) ;
#if 1
//merge blk with neighbors if possible
if(blk->getBuffer() > mDataBuffer) //has the left neighbor
{
if((blk - 1)->mSelf->isFree())
{
LLMemoryBlock* left_blk = (blk - 1)->mSelf ;
removeFromFreeSpace((blk - 1)->mSelf);
left_blk->setBuffer(left_blk->getBuffer(), left_blk->getBufferSize() + blk->getBufferSize()) ;
blk = left_blk ;
}
}
if(blk->getBuffer() + blk->getBufferSize() <= mBuffer + mBufferSize - mMinBlockSize) //has the right neighbor
{
U32 d = blk->getBufferSize() / mMinBlockSize ;
if((blk + d)->isFree())
{
LLMemoryBlock* right_blk = blk + d ;
removeFromFreeSpace(blk + d) ;
blk->setBuffer(blk->getBuffer(), blk->getBufferSize() + right_blk->getBufferSize()) ;
}
}
#endif
addToFreeSpace(blk) ;
return ;
}
//the top block in the list is full, pop it out of the list
void LLPrivateMemoryPool::LLMemoryChunk::popAvailBlockList(U32 blk_idx)
{
if(mAvailBlockList[blk_idx])
{
LLMemoryBlock* next = mAvailBlockList[blk_idx]->mNext ;
if(next)
{
next->mPrev = NULL ;
}
mAvailBlockList[blk_idx]->mPrev = NULL ;
mAvailBlockList[blk_idx]->mNext = NULL ;
mAvailBlockList[blk_idx] = next ;
}
}
//add the block back to the free pool
void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk)
{
llassert_always(!blk->mPrev) ;
llassert_always(!blk->mNext) ;
U16 free_idx = blk->getBufferSize() / mMinBlockSize - 1;
(blk + free_idx)->mSelf = blk ; //mark the end pointing back to the head.
free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ;
blk->mNext = mFreeSpaceList[free_idx] ;
if(mFreeSpaceList[free_idx])
{
mFreeSpaceList[free_idx]->mPrev = blk ;
}
mFreeSpaceList[free_idx] = blk ;
blk->mPrev = NULL ;
blk->mSelf = blk ;
return ;
}
//remove the space from the free pool
void LLPrivateMemoryPool::LLMemoryChunk::removeFromFreeSpace(LLMemoryBlock* blk)
{
U16 free_idx = blk->getBufferSize() / mMinBlockSize - 1;
free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ;
if(mFreeSpaceList[free_idx] == blk)
{
mFreeSpaceList[free_idx] = blk->mNext ;
}
if(blk->mPrev)
{
blk->mPrev->mNext = blk->mNext ;
}
if(blk->mNext)
{
blk->mNext->mPrev = blk->mPrev ;
}
blk->mNext = NULL ;
blk->mPrev = NULL ;
blk->mSelf = NULL ;
return ;
}
void LLPrivateMemoryPool::LLMemoryChunk::addToAvailBlockList(LLMemoryBlock* blk)
{
llassert_always(!blk->mPrev) ;
llassert_always(!blk->mNext) ;
U32 blk_idx = getBlockLevel(blk->getSlotSize());
blk->mNext = mAvailBlockList[blk_idx] ;
if(blk->mNext)
{
blk->mNext->mPrev = blk ;
}
blk->mPrev = NULL ;
mAvailBlockList[blk_idx] = blk ;
return ;
}
U32 LLPrivateMemoryPool::LLMemoryChunk::getPageIndex(U32 addr)
{
return (addr - (U32)mDataBuffer) / mMinBlockSize ;
}
//for mAvailBlockList
U32 LLPrivateMemoryPool::LLMemoryChunk::getBlockLevel(U32 size)
{
llassert(size >= mMinSlotSize && size <= mMaxSlotSize) ;
//start from 0
return (size + mMinSlotSize - 1) / mMinSlotSize - 1 ;
}
//for mFreeSpaceList
U16 LLPrivateMemoryPool::LLMemoryChunk::getPageLevel(U32 size)
{
//start from 0
U16 level = size / mMinBlockSize - 1 ;
if(level >= mPartitionLevels)
{
level = mPartitionLevels - 1 ;
}
return level ;
}
//-------------------------------------------------------------------
//class LLPrivateMemoryPool
//--------------------------------------------------------------------
const U32 CHUNK_SIZE = 4 << 20 ; //4 MB
const U32 LARGE_CHUNK_SIZE = 4 * CHUNK_SIZE ; //16 MB
LLPrivateMemoryPool::LLPrivateMemoryPool(S32 type, U32 max_pool_size) :
mMutexp(NULL),
mReservedPoolSize(0),
mHashFactor(1),
mType(type),
mMaxPoolSize(max_pool_size)
{
if(type == STATIC_THREADED || type == VOLATILE_THREADED)
{
mMutexp = new LLMutex(NULL) ;
}
for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)
{
mChunkList[i] = NULL ;
}
mNumOfChunks = 0 ;
}
LLPrivateMemoryPool::~LLPrivateMemoryPool()
{
destroyPool();
delete mMutexp ;
}
char* LLPrivateMemoryPool::allocate(U32 size)
{
if(!size)
{
return NULL ;
}
//if the asked size larger than MAX_BLOCK_SIZE, fetch from heap directly, the pool does not manage it
if(size >= CHUNK_SIZE)
{
return (char*)ll_aligned_malloc_16(size) ;
}
char* p = NULL ;
//find the appropriate chunk
S32 chunk_idx = getChunkIndex(size) ;
lock() ;
LLMemoryChunk* chunk = mChunkList[chunk_idx];
while(chunk)
{
if((p = chunk->allocate(size)))
{
break ;
}
chunk = chunk->mNext ;
}
//fetch new memory chunk
if(!p)
{
if(mReservedPoolSize + CHUNK_SIZE > mMaxPoolSize)
{
chunk = mChunkList[chunk_idx];
while(chunk)
{
if((p = chunk->allocate(size)))
{
break ;
}
chunk = chunk->mNext ;
}
}
else
{
chunk = addChunk(chunk_idx) ;
if(chunk)
{
p = chunk->allocate(size) ;
}
}
}
unlock() ;
if(!p) //to get memory from the private pool failed, try the heap directly
{
static bool to_log = true ;
if(to_log)
{
LL_WARNS() << "The memory pool overflows, now using heap directly!" << LL_ENDL ;
to_log = false ;
}
return (char*)ll_aligned_malloc_16(size) ;
}
return p ;
}
void LLPrivateMemoryPool::freeMem(void* addr)
{
if(!addr)
{
return ;
}
lock() ;
LLMemoryChunk* chunk = findChunk((char*)addr) ;
if(!chunk)
{
ll_aligned_free_16(addr) ; //release from heap
}
else
{
chunk->freeMem(addr) ;
if(chunk->empty())
{
removeChunk(chunk) ;
}
}
unlock() ;
}
void LLPrivateMemoryPool::dump()
{
}
U32 LLPrivateMemoryPool::getTotalAllocatedSize()
{
U32 total_allocated = 0 ;
LLMemoryChunk* chunk ;
for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)
{
chunk = mChunkList[i];
while(chunk)
{
total_allocated += chunk->getAllocatedSize() ;
chunk = chunk->mNext ;
}
}
return total_allocated ;
}
void LLPrivateMemoryPool::lock()
{
if(mMutexp)
{
mMutexp->lock() ;
}
}
void LLPrivateMemoryPool::unlock()
{
if(mMutexp)
{
mMutexp->unlock() ;
}
}
S32 LLPrivateMemoryPool::getChunkIndex(U32 size)
{
S32 i ;
for(i = 0 ; size > MAX_SLOT_SIZES[i]; i++);
llassert_always(i < SUPER_ALLOCATION);
return i ;
}
//destroy the entire pool
void LLPrivateMemoryPool::destroyPool()
{
lock() ;
if(mNumOfChunks > 0)
{
LL_WARNS() << "There is some memory not freed when destroy the memory pool!" << LL_ENDL ;
}
mNumOfChunks = 0 ;
mChunkHashList.clear() ;
mHashFactor = 1 ;
for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)
{
mChunkList[i] = NULL ;
}
unlock() ;
}
bool LLPrivateMemoryPool::checkSize(U32 asked_size)
{
if(mReservedPoolSize + asked_size > mMaxPoolSize)
{
LL_INFOS() << "Max pool size: " << mMaxPoolSize << LL_ENDL ;
LL_INFOS() << "Total reserved size: " << mReservedPoolSize + asked_size << LL_ENDL ;
LL_INFOS() << "Total_allocated Size: " << getTotalAllocatedSize() << LL_ENDL ;
//LL_ERRS() << "The pool is overflowing..." << LL_ENDL ;
return false ;
}
return true ;
}
LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_index)
{
U32 preferred_size ;
U32 overhead ;
if(chunk_index < LARGE_ALLOCATION)
{
preferred_size = CHUNK_SIZE ; //4MB
overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_SLOT_SIZES[chunk_index],
MAX_SLOT_SIZES[chunk_index], MIN_BLOCK_SIZES[chunk_index], MAX_BLOCK_SIZES[chunk_index]) ;
}
else
{
preferred_size = LARGE_CHUNK_SIZE ; //16MB
overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_SLOT_SIZES[chunk_index],
MAX_SLOT_SIZES[chunk_index], MIN_BLOCK_SIZES[chunk_index], MAX_BLOCK_SIZES[chunk_index]) ;
}
if(!checkSize(preferred_size + overhead))
{
return NULL ;
}
mReservedPoolSize += preferred_size + overhead ;
char* buffer = (char*)ll_aligned_malloc_16(preferred_size + overhead) ;
if(!buffer)
{
return NULL ;
}
LLMemoryChunk* chunk = new (buffer) LLMemoryChunk() ;
chunk->init(buffer, preferred_size + overhead, MIN_SLOT_SIZES[chunk_index],
MAX_SLOT_SIZES[chunk_index], MIN_BLOCK_SIZES[chunk_index], MAX_BLOCK_SIZES[chunk_index]) ;
//add to the tail of the linked list
{
if(!mChunkList[chunk_index])
{
mChunkList[chunk_index] = chunk ;
}
else
{
LLMemoryChunk* cur = mChunkList[chunk_index] ;
while(cur->mNext)
{
cur = cur->mNext ;
}
cur->mNext = chunk ;
chunk->mPrev = cur ;
}
}
//insert into the hash table
addToHashTable(chunk) ;
mNumOfChunks++;
return chunk ;
}
void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk)
{
if(!chunk)
{
return ;
}
//remove from the linked list
for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)
{
if(mChunkList[i] == chunk)
{
mChunkList[i] = chunk->mNext ;
}
}
if(chunk->mPrev)
{
chunk->mPrev->mNext = chunk->mNext ;
}
if(chunk->mNext)
{
chunk->mNext->mPrev = chunk->mPrev ;
}
//remove from the hash table
removeFromHashTable(chunk) ;
mNumOfChunks--;
mReservedPoolSize -= chunk->getBufferSize() ;
//release memory
ll_aligned_free_16(chunk->getBuffer()) ;
}
U16 LLPrivateMemoryPool::findHashKey(const char* addr)
{
return (((U32)addr) / CHUNK_SIZE) % mHashFactor ;
}
LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::findChunk(const char* addr)
{
U16 key = findHashKey(addr) ;
if(mChunkHashList.size() <= key)
{
return NULL ;
}
return mChunkHashList[key].findChunk(addr) ;
}
void LLPrivateMemoryPool::addToHashTable(LLMemoryChunk* chunk)
{
static const U16 HASH_FACTORS[] = {41, 83, 193, 317, 419, 523, 719, 997, 1523, 0xFFFF};
U16 i ;
if(mChunkHashList.empty())
{
mHashFactor = HASH_FACTORS[0] ;
rehash() ;
}
U16 start_key = findHashKey(chunk->getBuffer()) ;
U16 end_key = findHashKey(chunk->getBuffer() + chunk->getBufferSize() - 1) ;
bool need_rehash = false ;
if(mChunkHashList[start_key].hasElement(chunk))
{
return; //already inserted.
}
need_rehash = mChunkHashList[start_key].add(chunk) ;
if(start_key == end_key && !need_rehash)
{
return ; //done
}
if(!need_rehash)
{
need_rehash = mChunkHashList[end_key].add(chunk) ;
}
if(!need_rehash)
{
if(end_key < start_key)
{
need_rehash = fillHashTable(start_key + 1, mHashFactor, chunk) ;
if(!need_rehash)
{
need_rehash = fillHashTable(0, end_key, chunk) ;
}
}
else
{
need_rehash = fillHashTable(start_key + 1, end_key, chunk) ;
}
}
if(need_rehash)
{
i = 0 ;
while(HASH_FACTORS[i] <= mHashFactor) i++;
mHashFactor = HASH_FACTORS[i] ;
llassert_always(mHashFactor != 0xFFFF) ;//stop point to prevent endlessly recursive calls
rehash() ;
}
}
void LLPrivateMemoryPool::removeFromHashTable(LLMemoryChunk* chunk)
{
U16 start_key = findHashKey(chunk->getBuffer()) ;
U16 end_key = findHashKey(chunk->getBuffer() + chunk->getBufferSize() - 1) ;
mChunkHashList[start_key].remove(chunk) ;
if(start_key == end_key)
{
return ; //done
}
mChunkHashList[end_key].remove(chunk) ;
if(end_key < start_key)
{
for(U16 i = start_key + 1 ; i < mHashFactor; i++)
{
mChunkHashList[i].remove(chunk) ;
}
for(U16 i = 0 ; i < end_key; i++)
{
mChunkHashList[i].remove(chunk) ;
}
}
else
{
for(U16 i = start_key + 1 ; i < end_key; i++)
{
mChunkHashList[i].remove(chunk) ;
}
}
}
void LLPrivateMemoryPool::rehash()
{
LL_INFOS() << "new hash factor: " << mHashFactor << LL_ENDL ;
mChunkHashList.clear() ;
mChunkHashList.resize(mHashFactor) ;
LLMemoryChunk* chunk ;
for(U16 i = 0 ; i < SUPER_ALLOCATION ; i++)
{
chunk = mChunkList[i] ;
while(chunk)
{
addToHashTable(chunk) ;
chunk = chunk->mNext ;
}
}
}
bool LLPrivateMemoryPool::fillHashTable(U16 start, U16 end, LLMemoryChunk* chunk)
{
for(U16 i = start; i < end; i++)
{
if(mChunkHashList[i].add(chunk))
{
return true ;
}
}
return false ;
}
//--------------------------------------------------------------------
// class LLChunkHashElement
//--------------------------------------------------------------------
LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::LLChunkHashElement::findChunk(const char* addr)
{
if(mFirst && mFirst->containsAddress(addr))
{
return mFirst ;
}
else if(mSecond && mSecond->containsAddress(addr))
{
return mSecond ;
}
return NULL ;
}
//return false if successfully inserted to the hash slot.
bool LLPrivateMemoryPool::LLChunkHashElement::add(LLPrivateMemoryPool::LLMemoryChunk* chunk)
{
llassert_always(!hasElement(chunk)) ;
if(!mFirst)
{
mFirst = chunk ;
}
else if(!mSecond)
{
mSecond = chunk ;
}
else
{
return true ; //failed
}
return false ;
}
void LLPrivateMemoryPool::LLChunkHashElement::remove(LLPrivateMemoryPool::LLMemoryChunk* chunk)
{
if(mFirst == chunk)
{
mFirst = NULL ;
}
else if(mSecond ==chunk)
{
mSecond = NULL ;
}
else
{
LL_ERRS() << "This slot does not contain this chunk!" << LL_ENDL ;
}
}
//--------------------------------------------------------------------
//class LLPrivateMemoryPoolManager
//--------------------------------------------------------------------
LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::sInstance = NULL ;
BOOL LLPrivateMemoryPoolManager::sPrivatePoolEnabled = FALSE ;
std::vector<LLPrivateMemoryPool*> LLPrivateMemoryPoolManager::sDanglingPoolList ;
LLPrivateMemoryPoolManager::LLPrivateMemoryPoolManager(BOOL enabled, U32 max_pool_size)
{
mPoolList.resize(LLPrivateMemoryPool::MAX_TYPES) ;
for(S32 i = 0 ; i < LLPrivateMemoryPool::MAX_TYPES; i++)
{
mPoolList[i] = NULL ;
}
sPrivatePoolEnabled = enabled ;
const U32 MAX_POOL_SIZE = 256 * 1024 * 1024 ; //256 MB
mMaxPrivatePoolSize = llmax(max_pool_size, MAX_POOL_SIZE) ;
}
LLPrivateMemoryPoolManager::~LLPrivateMemoryPoolManager()
{
#if __DEBUG_PRIVATE_MEM__
if(!sMemAllocationTracker.empty())
{
LL_WARNS() << "there is potential memory leaking here. The list of not freed memory blocks are from: " <<LL_ENDL ;
S32 k = 0 ;
for(mem_allocation_info_t::iterator iter = sMemAllocationTracker.begin() ; iter != sMemAllocationTracker.end() ; ++iter)
{
LL_INFOS() << k++ << ", " << (U32)iter->first << " : " << iter->second << LL_ENDL ;
}
sMemAllocationTracker.clear() ;
}
#endif
#if 0
//all private pools should be released by their owners before reaching here.
for(S32 i = 0 ; i < LLPrivateMemoryPool::MAX_TYPES; i++)
{
llassert_always(!mPoolList[i]) ;
}
mPoolList.clear() ;
#else
//forcefully release all memory
for(S32 i = 0 ; i < LLPrivateMemoryPool::MAX_TYPES; i++)
{
if(mPoolList[i])
{
if(mPoolList[i]->isEmpty())
{
delete mPoolList[i] ;
}
else
{
//can not delete this pool because it has alloacted memory to be freed.
//move it to the dangling list.
sDanglingPoolList.push_back(mPoolList[i]) ;
}
mPoolList[i] = NULL ;
}
}
mPoolList.clear() ;
#endif
}
//static
void LLPrivateMemoryPoolManager::initClass(BOOL enabled, U32 max_pool_size)
{
llassert_always(!sInstance) ;
sInstance = new LLPrivateMemoryPoolManager(enabled, max_pool_size) ;
}
//static
LLPrivateMemoryPoolManager* LLPrivateMemoryPoolManager::getInstance()
{
//if(!sInstance)
//{
// sInstance = new LLPrivateMemoryPoolManager(FALSE) ;
//}
return sInstance ;
}
//static
void LLPrivateMemoryPoolManager::destroyClass()
{
if(sInstance)
{
delete sInstance ;
sInstance = NULL ;
}
}
LLPrivateMemoryPool* LLPrivateMemoryPoolManager::newPool(S32 type)
{
if(!sPrivatePoolEnabled)
{
return NULL ;
}
if(!mPoolList[type])
{
mPoolList[type] = new LLPrivateMemoryPool(type, mMaxPrivatePoolSize) ;
}
return mPoolList[type] ;
}
void LLPrivateMemoryPoolManager::deletePool(LLPrivateMemoryPool* pool)
{
if(pool && pool->isEmpty())
{
mPoolList[pool->getType()] = NULL ;
delete pool;
}
}
//debug
void LLPrivateMemoryPoolManager::updateStatistics()
{
mTotalReservedSize = 0 ;
mTotalAllocatedSize = 0 ;
for(U32 i = 0; i < mPoolList.size(); i++)
{
if(mPoolList[i])
{
mTotalReservedSize += mPoolList[i]->getTotalReservedSize() ;
mTotalAllocatedSize += mPoolList[i]->getTotalAllocatedSize() ;
}
}
}
#if __DEBUG_PRIVATE_MEM__
//static
char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size, const char* function, const int line)
{
char* p ;
if(!poolp)
{
p = (char*)ll_aligned_malloc_16(size) ;
}
else
{
p = poolp->allocate(size) ;
}
if(p)
{
char num[16] ;
sprintf(num, " line: %d ", line) ;
std::string str(function) ;
str += num;
sMemAllocationTracker[p] = str ;
}
return p ;
}
#else
//static
char* LLPrivateMemoryPoolManager::allocate(LLPrivateMemoryPool* poolp, U32 size)
{
if(poolp)
{
return poolp->allocate(size) ;
}
else
{
return (char*)ll_aligned_malloc_16(size) ;
}
}
#endif
//static
void LLPrivateMemoryPoolManager::freeMem(LLPrivateMemoryPool* poolp, void* addr)
{
if(!addr)
{
return ;
}
#if __DEBUG_PRIVATE_MEM__
sMemAllocationTracker.erase((char*)addr) ;
#endif
if(poolp)
{
poolp->freeMem(addr) ;
}
else
{
if(!sPrivatePoolEnabled)
{
ll_aligned_free_16(addr) ; //private pool is disabled.
}
else if(!sInstance) //the private memory manager is destroyed, try the dangling list
{
for(S32 i = 0 ; i < sDanglingPoolList.size(); i++)
{
if(sDanglingPoolList[i]->findChunk((char*)addr))
{
sDanglingPoolList[i]->freeMem(addr) ;
if(sDanglingPoolList[i]->isEmpty())
{
delete sDanglingPoolList[i] ;
if(i < sDanglingPoolList.size() - 1)
{
sDanglingPoolList[i] = sDanglingPoolList[sDanglingPoolList.size() - 1] ;
}
sDanglingPoolList.pop_back() ;
}
addr = NULL ;
break ;
}
}
llassert_always(!addr) ; //addr should be release before hitting here!
}
else
{
LL_ERRS() << "private pool is used before initialized.!" << LL_ENDL ;
}
}
}
//--------------------------------------------------------------------
//class LLPrivateMemoryPoolTester
//--------------------------------------------------------------------
#if 0
LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::sInstance = NULL ;
LLPrivateMemoryPool* LLPrivateMemoryPoolTester::sPool = NULL ;
LLPrivateMemoryPoolTester::LLPrivateMemoryPoolTester()
{
}
LLPrivateMemoryPoolTester::~LLPrivateMemoryPoolTester()
{
}
//static
LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::getInstance()
{
if(!sInstance)
{
sInstance = ::new LLPrivateMemoryPoolTester() ;
}
return sInstance ;
}
//static
void LLPrivateMemoryPoolTester::destroy()
{
if(sInstance)
{
::delete sInstance ;
sInstance = NULL ;
}
if(sPool)
{
LLPrivateMemoryPoolManager::getInstance()->deletePool(sPool) ;
sPool = NULL ;
}
}
void LLPrivateMemoryPoolTester::run(S32 type)
{
if(sPool)
{
LLPrivateMemoryPoolManager::getInstance()->deletePool(sPool) ;
}
sPool = LLPrivateMemoryPoolManager::getInstance()->newPool(type) ;
//run the test
correctnessTest() ;
performanceTest() ;
//fragmentationtest() ;
//release pool.
LLPrivateMemoryPoolManager::getInstance()->deletePool(sPool) ;
sPool = NULL ;
}
void LLPrivateMemoryPoolTester::test(U32 min_size, U32 max_size, U32 stride, U32 times,
bool random_deletion, bool output_statistics)
{
U32 levels = (max_size - min_size) / stride + 1 ;
char*** p ;
U32 i, j ;
U32 total_allocated_size = 0 ;
//allocate space for p ;
if(!(p = ::new char**[times]) || !(*p = ::new char*[times * levels]))
{
LL_ERRS() << "memory initialization for p failed" << LL_ENDL ;
}
//init
for(i = 0 ; i < times; i++)
{
p[i] = *p + i * levels ;
for(j = 0 ; j < levels; j++)
{
p[i][j] = NULL ;
}
}
//allocation
U32 size ;
for(i = 0 ; i < times ; i++)
{
for(j = 0 ; j < levels; j++)
{
size = min_size + j * stride ;
p[i][j] = ALLOCATE_MEM(sPool, size) ;
total_allocated_size+= size ;
*(U32*)p[i][j] = i ;
*((U32*)p[i][j] + 1) = j ;
//p[i][j][size - 1] = '\0' ; //access the last element to verify the success of the allocation.
//randomly release memory
if(random_deletion)
{
S32 k = rand() % levels ;
if(p[i][k])
{
llassert_always(*(U32*)p[i][k] == i && *((U32*)p[i][k] + 1) == k) ;
FREE_MEM(sPool, p[i][k]) ;
total_allocated_size -= min_size + k * stride ;
p[i][k] = NULL ;
}
}
}
}
//output pool allocation statistics
if(output_statistics)
{
}
//release all memory allocations
for(i = 0 ; i < times; i++)
{
for(j = 0 ; j < levels; j++)
{
if(p[i][j])
{
llassert_always(*(U32*)p[i][j] == i && *((U32*)p[i][j] + 1) == j) ;
FREE_MEM(sPool, p[i][j]) ;
total_allocated_size -= min_size + j * stride ;
p[i][j] = NULL ;
}
}
}
::delete[] *p ;
::delete[] p ;
}
void LLPrivateMemoryPoolTester::testAndTime(U32 size, U32 times)
{
LLTimer timer ;
LL_INFOS() << " -**********************- " << LL_ENDL ;
LL_INFOS() << "test size: " << size << " test times: " << times << LL_ENDL ;
timer.reset() ;
char** p = new char*[times] ;
//using the customized memory pool
//allocation
for(U32 i = 0 ; i < times; i++)
{
p[i] = ALLOCATE_MEM(sPool, size) ;
if(!p[i])
{
LL_ERRS() << "allocation failed" << LL_ENDL ;
}
}
//de-allocation
for(U32 i = 0 ; i < times; i++)
{
FREE_MEM(sPool, p[i]) ;
p[i] = NULL ;
}
LL_INFOS() << "time spent using customized memory pool: " << timer.getElapsedTimeF32() << LL_ENDL ;
timer.reset() ;
//using the standard allocator/de-allocator:
//allocation
for(U32 i = 0 ; i < times; i++)
{
p[i] = ::new char[size] ;
if(!p[i])
{
LL_ERRS() << "allocation failed" << LL_ENDL ;
}
}
//de-allocation
for(U32 i = 0 ; i < times; i++)
{
::delete[] p[i] ;
p[i] = NULL ;
}
LL_INFOS() << "time spent using standard allocator/de-allocator: " << timer.getElapsedTimeF32() << LL_ENDL ;
delete[] p;
}
void LLPrivateMemoryPoolTester::correctnessTest()
{
//try many different sized allocation, and all kinds of edge cases, access the allocated memory
//to see if allocation is right.
//edge case
char* p = ALLOCATE_MEM(sPool, 0) ;
FREE_MEM(sPool, p) ;
//small sized
// [8 bytes, 2KB), each asks for 256 allocations and deallocations
test(8, 2040, 8, 256, true, true) ;
//medium sized
//[2KB, 512KB), each asks for 16 allocations and deallocations
test(2048, 512 * 1024 - 2048, 2048, 16, true, true) ;
//large sized
//[512KB, 4MB], each asks for 8 allocations and deallocations
test(512 * 1024, 4 * 1024 * 1024, 64 * 1024, 6, true, true) ;
}
void LLPrivateMemoryPoolTester::performanceTest()
{
U32 test_size[3] = {768, 3* 1024, 3* 1024 * 1024};
//small sized
testAndTime(test_size[0], 8) ;
//medium sized
testAndTime(test_size[1], 8) ;
//large sized
testAndTime(test_size[2], 8) ;
}
void LLPrivateMemoryPoolTester::fragmentationtest()
{
//for internal fragmentation statistics:
//every time when asking for a new chunk during correctness test, and performance test,
//print out the chunk usage statistices.
}
#endif
//--------------------------------------------------------------------
|