summaryrefslogtreecommitdiffstats
path: root/src/corelib/arch/symbian/newallocator.cpp
blob: 625037112793c93b55ae8a73e25eb9fc244e367a (plain)
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
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Symbian application wrapper of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
** The memory allocator is backported from Symbian OS, and can eventually
** be removed from Qt once it is built in to all supported OS versions.
** The allocator is a composite of three allocators:
**  - A page allocator, for large allocations
**  - A slab allocator, for small allocations
**  - Doug Lea's allocator, for medium size allocations
****************************************************************************/
#include <qglobal.h>
#include <e32std.h>
#include <e32cmn.h>
#include <hal.h>
#include <e32panic.h>
#define RND_SDK
#ifndef RND_SDK
struct SThreadCreateInfo
    {
    TAny* iHandle;
    TInt iType;
    TThreadFunction iFunction;
    TAny* iPtr;
    TAny* iSupervisorStack;
    TInt iSupervisorStackSize;
    TAny* iUserStack;
    TInt iUserStackSize;
    TInt iInitialThreadPriority;
    TPtrC iName;
    TInt iTotalSize;    // Size including any extras (must be a multiple of 8 bytes)
    };

struct SStdEpocThreadCreateInfo : public SThreadCreateInfo
    {
    RAllocator* iAllocator;
    TInt iHeapInitialSize;
    TInt iHeapMaxSize;
    TInt iPadding;      // Make structure size a multiple of 8 bytes
    };
#else
#include <u32std.h>
#endif
#include <e32svr.h>

//Named local chunks require support from the kernel, which depends on Symbian^3
#define NO_NAMED_LOCAL_CHUNKS
//Reserving a minimum heap size is not supported, because the implementation does not know what type of
//memory to use. DLA memory grows upwards, slab and page allocators grow downwards.
//This would need kernel support to do properly.
#define NO_RESERVE_MEMORY

//The BTRACE debug framework requires Symbian OS 9.4 or higher.
//Required header files are not included in S60 5.0 SDKs, but
//they are available for open source versions of Symbian OS.
//Note that although Symbian OS 9.3 supports BTRACE, the usage in this file
//depends on 9.4 header files.

//This debug flag uses BTRACE to emit debug traces to identify the heaps.
//Note that it uses the ETest1 trace category which is not reserved
#define TRACING_HEAPS
//This debug flag uses BTRACE to emit debug traces to aid with debugging
//allocs, frees & reallocs. It should be used together with the KUSERHEAPTRACE
//kernel trace flag to enable heap tracing.
#define TRACING_ALLOCS

#if defined(TRACING_ALLOCS) || defined(TRACING_HEAPS)
#include <e32btrace.h>
#endif

#ifndef __WINS__
#pragma push
#pragma arm
#endif

#ifdef QT_USE_NEW_SYMBIAN_ALLOCATOR

#include "dla_p.h"
#include "newallocator_p.h"

// if non zero this causes the slabs to be configured only when the chunk size exceeds this level
#define DELAYED_SLAB_THRESHOLD (64*1024)		// 64KB seems about right based on trace data
#define SLAB_CONFIG (0xabe)

_LIT(KDLHeapPanicCategory, "DL Heap");
#define	GET_PAGE_SIZE(x)			HAL::Get(HALData::EMemoryPageSize, x)
#define	__CHECK_CELL(p)
#define __POWER_OF_2(x)				((TUint32)((x)^((x)-1))>=(TUint32)(x))
#define HEAP_PANIC(r)               Panic(r)

LOCAL_C void Panic(TCdtPanic aPanic)
// Panic the process with USER as the category.
	{
	User::Panic(_L("USER"),aPanic);
	}

size_t getpagesize()
{
    TInt size;
    TInt err = GET_PAGE_SIZE(size);
    if(err != KErrNone)
        return (size_t)0x1000;
    return (size_t)size;
}

#define gm  (&iGlobalMallocState)

RNewAllocator::RNewAllocator(TInt aMaxLength, TInt aAlign, TBool aSingleThread)
// constructor for a fixed heap. Just use DL allocator
	:iMinLength(aMaxLength), iMaxLength(aMaxLength), iOffset(0), iGrowBy(0), iChunkHandle(0),
	iNestingLevel(0), iAllocCount(0), iFailType(ENone), iTestData(NULL), iChunkSize(aMaxLength)
	{

	if ((TUint32)aAlign>=sizeof(TAny*) && __POWER_OF_2(iAlign))
		{
		iAlign = aAlign;
		}
	else
		{
		iAlign = 4;
		}
	iPageSize = 0;
	iFlags = aSingleThread ? (ESingleThreaded|EFixedSize) : EFixedSize;

	Init(0, 0, 0);
	}

RNewAllocator::RNewAllocator(TInt aChunkHandle, TInt aOffset, TInt aMinLength, TInt aMaxLength, TInt aGrowBy,
			TInt aAlign, TBool aSingleThread)
		: iMinLength(aMinLength), iMaxLength(aMaxLength), iOffset(aOffset), iChunkHandle(aChunkHandle), iAlign(aAlign), iNestingLevel(0), iAllocCount(0),
			iFailType(ENone), iTestData(NULL), iChunkSize(aMinLength),iHighWaterMark(aMinLength)
	{
	iPageSize = malloc_getpagesize;
	__ASSERT_ALWAYS(aOffset >=0, User::Panic(KDLHeapPanicCategory, ETHeapNewBadOffset));
	iGrowBy = _ALIGN_UP(aGrowBy, iPageSize);
	iFlags = aSingleThread ? ESingleThreaded : 0;

	// Initialise
	// if the heap is created with aMinLength==aMaxLength then it cannot allocate slab or page memory
	// so these sub-allocators should be disabled. Otherwise initialise with default values
	if (aMinLength == aMaxLength)
		Init(0, 0, 0);
	else
		Init(0xabe, 16, iPageSize*4);	// slabs {48, 40, 32, 24, 20, 16, 12, 8}, page {64KB}, trim {16KB}
#ifdef TRACING_HEAPS
	RChunk chunk;
	chunk.SetHandle(iChunkHandle);
	TKName chunk_name;
	chunk.FullName(chunk_name);
	BTraceContextBig(BTrace::ETest1, 2, 22, chunk_name.Ptr(), chunk_name.Size());

	TUint32 traceData[4];
	traceData[0] = iChunkHandle;
	traceData[1] = iMinLength;
	traceData[2] = iMaxLength;
	traceData[3] = iAlign;
	BTraceContextN(BTrace::ETest1, 1, (TUint32)this, 11, traceData, sizeof(traceData));
#endif

	}

TAny* RNewAllocator::operator new(TUint aSize, TAny* aBase) __NO_THROW
	{
	__ASSERT_ALWAYS(aSize>=sizeof(RNewAllocator), HEAP_PANIC(ETHeapNewBadSize));
	RNewAllocator* h = (RNewAllocator*)aBase;
	h->iAlign = 0x80000000;	// garbage value
	h->iBase = ((TUint8*)aBase) + aSize;
	return aBase;
	}

void RNewAllocator::Init(TInt aBitmapSlab, TInt aPagePower, size_t aTrimThreshold)
	{
	__ASSERT_ALWAYS((TUint32)iAlign>=sizeof(TAny*) && __POWER_OF_2(iAlign), HEAP_PANIC(ETHeapNewBadAlignment));

	/*Moved code which does initialization */
	iTop = (TUint8*)this + iMinLength;
	iAllocCount = 0;
	memset(&mparams,0,sizeof(mparams));

	Init_Dlmalloc(iTop - iBase, 0, aTrimThreshold);

	slab_init();
	slab_config_bits = aBitmapSlab;
#ifdef DELAYED_SLAB_THRESHOLD
	if (iChunkSize < DELAYED_SLAB_THRESHOLD)
		{
		slab_init_threshold = DELAYED_SLAB_THRESHOLD;
		}
	else
#endif // DELAYED_SLAB_THRESHOLD
		{
		slab_init_threshold = KMaxTUint;
		slab_config(aBitmapSlab);
		}

	/*10-1K,11-2K,12-4k,13-8K,14-16K,15-32K,16-64K*/
	paged_init(aPagePower);

#ifdef TRACING_ALLOCS
		TUint32 traceData[3];
		traceData[0] = aBitmapSlab;
		traceData[1] = aPagePower;
		traceData[2] = aTrimThreshold;
		BTraceContextN(BTrace::ETest1, BTrace::EHeapAlloc, (TUint32)this, 0, traceData, sizeof(traceData));
#endif

	}

RNewAllocator::SCell* RNewAllocator::GetAddress(const TAny* aCell) const
//
// As much as possible, check a cell address and backspace it
// to point at the cell header.
//
	{

	TLinAddr m = TLinAddr(iAlign - 1);
	__ASSERT_ALWAYS(!(TLinAddr(aCell)&m), HEAP_PANIC(ETHeapBadCellAddress));

	SCell* pC = (SCell*)(((TUint8*)aCell)-EAllocCellSize);
	__CHECK_CELL(pC);

	return pC;
	}

TInt RNewAllocator::AllocLen(const TAny* aCell) const
{
	if (ptrdiff(aCell, this) >= 0)
	{
		mchunkptr m = mem2chunk(aCell);
		return chunksize(m) - overhead_for(m);
	}
	if (lowbits(aCell, pagesize) > cellalign)
		return header_size(slab::slabfor(aCell)->header);
	if (lowbits(aCell, pagesize) == cellalign)
		return *(unsigned*)(offset(aCell,-int(cellalign)))-cellalign;
	return paged_descriptor(aCell)->size;
}

TAny* RNewAllocator::Alloc(TInt aSize)
{
	__ASSERT_ALWAYS((TUint)aSize<(KMaxTInt/2),HEAP_PANIC(ETHeapBadAllocatedCellSize));

	TAny* addr;

#ifdef TRACING_ALLOCS
	TInt aCnt=0;
#endif
	Lock();
	if (aSize < slab_threshold)
	{
		TInt ix = sizemap[(aSize+3)>>2];
		ASSERT(ix != 0xff);
		addr = slab_allocate(slaballoc[ix]);
	}else if((aSize >> page_threshold)==0)
		{
#ifdef TRACING_ALLOCS
		aCnt=1;
#endif
		addr = dlmalloc(aSize);
		}
	else
		{
#ifdef TRACING_ALLOCS
		aCnt=2;
#endif
		addr = paged_allocate(aSize);
		}

	iCellCount++;
	iTotalAllocSize += aSize;
	Unlock();

#ifdef TRACING_ALLOCS
	if (iFlags & ETraceAllocs)
		{
		TUint32 traceData[3];
		traceData[0] = AllocLen(addr);
		traceData[1] = aSize;
		traceData[2] = aCnt;
		BTraceContextN(BTrace::EHeap, BTrace::EHeapAlloc, (TUint32)this, (TUint32)addr, traceData, sizeof(traceData));
		}
#endif

	return addr;
}

TInt RNewAllocator::Compress()
	{
	if (iFlags & EFixedSize)
		return 0;

	Lock();
	dlmalloc_trim(0);
	if (spare_page)
		{
		unmap(spare_page,pagesize);
		spare_page = 0;
		}
	Unlock();
	return 0;
	}

void RNewAllocator::Free(TAny* aPtr)
{

#ifdef TRACING_ALLOCS
	TInt aCnt=0;
#endif
#ifdef ENABLE_DEBUG_TRACE
	RThread me;
	TBuf<100> thName;
	me.FullName(thName);
#endif
    //if (!aPtr) return; //return in case of NULL pointer

	Lock();

	if (!aPtr)
		;
	else if (ptrdiff(aPtr, this) >= 0)
		{
#ifdef TRACING_ALLOCS
		aCnt = 1;
#endif
		dlfree( aPtr);
		}
	else if (lowbits(aPtr, pagesize) <= cellalign)
		{
#ifdef TRACING_ALLOCS
		aCnt = 2;
#endif
		paged_free(aPtr);
		}
	else
		{
#ifdef TRACING_ALLOCS
		aCnt = 0;
#endif
		slab_free(aPtr);
		}
	iCellCount--;
	Unlock();

#ifdef TRACING_ALLOCS
	if (iFlags & ETraceAllocs)
		{
		TUint32 traceData;
		traceData = aCnt;
		BTraceContextN(BTrace::EHeap, BTrace::EHeapFree, (TUint32)this, (TUint32)aPtr, &traceData, sizeof(traceData));
		}
#endif
}


void RNewAllocator::Reset()
	{
	// TODO free everything
    User::Panic(_L("RNewAllocator"), 1); //this should never be called
	}

inline void RNewAllocator::TraceReAlloc(TAny* aPtr, TInt aSize, TAny* aNewPtr, TInt aZone)
	{
#ifdef TRACING_ALLOCS
    if (aNewPtr && (iFlags & ETraceAllocs))
        {
        TUint32 traceData[3];
        traceData[0] = AllocLen(aNewPtr);
        traceData[1] = aSize;
        traceData[2] = (TUint32)aPtr;
        BTraceContextN(BTrace::EHeap, BTrace::EHeapReAlloc,(TUint32)this, (TUint32)aNewPtr,traceData, sizeof(traceData));
        
        //workaround for SAW not handling reallocs properly
        if(aZone >= 0 && aPtr != aNewPtr) {
            BTraceContextN(BTrace::EHeap, BTrace::EHeapFree, (TUint32)this, (TUint32)aPtr, &aZone, sizeof(aZone));
        }
        }
#else
    Q_UNUSED(aPtr);
    Q_UNUSED(aSize);
    Q_UNUSED(aNewPtr);
    Q_UNUSED(aZone);
#endif
	}

TAny* RNewAllocator::ReAlloc(TAny* aPtr, TInt aSize, TInt /*aMode = 0*/)
	{
    if(ptrdiff(aPtr,this)>=0)
    {
        // original cell is in DL zone
        if(aSize >= slab_threshold && (aSize>>page_threshold)==0)
            {
            // and so is the new one
            Lock();
            TAny* addr = dlrealloc(aPtr,aSize);
            Unlock();
            TraceReAlloc(aPtr, aSize, addr, 0);
            return addr;
            }
    }
    else if(lowbits(aPtr,pagesize)<=cellalign)
    {
        // original cell is either NULL or in paged zone
        if (!aPtr)
            return Alloc(aSize);
        if(aSize >> page_threshold)
            {
            // and so is the new one
            Lock();
            TAny* addr = paged_reallocate(aPtr,aSize);
            Unlock();
            TraceReAlloc(aPtr, aSize, addr, 2);
            return addr;
            }
    }
    else
    {
        // original cell is in slab znoe
        if(aSize <= header_size(slab::slabfor(aPtr)->header)) {
            TraceReAlloc(aPtr, aSize, aPtr, 1);
            return aPtr;
        }
    }
    TAny* newp = Alloc(aSize);
    if(newp)
    {
        TInt oldsize = AllocLen(aPtr);
        memcpy(newp,aPtr,oldsize<aSize?oldsize:aSize);
        Free(aPtr);
    }
    return newp;
	}

TInt RNewAllocator::Available(TInt& aBiggestBlock) const
{
	//TODO: consider page and slab allocators

	//this gets free space in DL region - the C ported code doesn't respect const yet.
	RNewAllocator* self = const_cast<RNewAllocator*> (this);
	mallinfo info = self->dlmallinfo();
	aBiggestBlock = info.largestBlock;
	return info.fordblks;
}
TInt RNewAllocator::AllocSize(TInt& aTotalAllocSize) const
{
	aTotalAllocSize = iTotalAllocSize;
	return iCellCount;
}

TInt RNewAllocator::DebugFunction(TInt aFunc, TAny* a1, TAny* /*a2*/)
	{
    TInt r = KErrNotSupported;
    TInt* a1int = reinterpret_cast<TInt*>(a1);
    switch(aFunc) {
    case RAllocator::ECount:
    {
        struct mallinfo mi = dlmallinfo();
        *a1int = mi.fordblks;
        r = mi.uordblks;
    }
        break;
    case RAllocator::EMarkStart:
    case RAllocator::EMarkEnd:
    case RAllocator::ESetFail:
    case RAllocator::ECheck:
        r = KErrNone;
        break;
    }
	return r;
	}

TInt RNewAllocator::Extension_(TUint /* aExtensionId */, TAny*& /* a0 */, TAny* /* a1 */)
	{
	return KErrNotSupported;
	}

///////////////////////////////////////////////////////////////////////////////
// imported from dla.cpp
///////////////////////////////////////////////////////////////////////////////

//#include <unistd.h>
//#define DEBUG_REALLOC
#ifdef DEBUG_REALLOC
#include <e32debug.h>
#endif
int RNewAllocator::init_mparams(size_t aTrimThreshold /*= DEFAULT_TRIM_THRESHOLD*/)
{
	if (mparams.page_size == 0)
	{
		size_t s;
		mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
		mparams.trim_threshold = aTrimThreshold;
		#if MORECORE_CONTIGUOUS
			mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT;
		#else  /* MORECORE_CONTIGUOUS */
			mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT;
		#endif /* MORECORE_CONTIGUOUS */

			s = (size_t)0x58585858U;
		ACQUIRE_MAGIC_INIT_LOCK(&mparams);
		if (mparams.magic == 0) {
		  mparams.magic = s;
		  /* Set up lock for main malloc area */
		  INITIAL_LOCK(&gm->mutex);
		  gm->mflags = mparams.default_mflags;
		}
		RELEASE_MAGIC_INIT_LOCK(&mparams);

		mparams.page_size = malloc_getpagesize;

		mparams.granularity = ((DEFAULT_GRANULARITY != 0)?
							   DEFAULT_GRANULARITY : mparams.page_size);

		/* Sanity-check configuration:
		   size_t must be unsigned and as wide as pointer type.
		   ints must be at least 4 bytes.
		   alignment must be at least 8.
		   Alignment, min chunk size, and page size must all be powers of 2.
		*/

		if ((sizeof(size_t) != sizeof(TUint8*)) ||
			(MAX_SIZE_T < MIN_CHUNK_SIZE)  ||
			(sizeof(int) < 4)  ||
			(MALLOC_ALIGNMENT < (size_t)8U) ||
			((MALLOC_ALIGNMENT    & (MALLOC_ALIGNMENT-SIZE_T_ONE))    != 0) ||
			((MCHUNK_SIZE         & (MCHUNK_SIZE-SIZE_T_ONE))         != 0) ||
			((mparams.granularity & (mparams.granularity-SIZE_T_ONE)) != 0) ||
			((mparams.page_size   & (mparams.page_size-SIZE_T_ONE))   != 0))
		  ABORT;
	}
	return 0;
}

void RNewAllocator::init_bins(mstate m) {
  /* Establish circular links for smallbins */
  bindex_t i;
  for (i = 0; i < NSMALLBINS; ++i) {
    sbinptr bin = smallbin_at(m,i);
    bin->fd = bin->bk = bin;
  }
}
/* ---------------------------- malloc support --------------------------- */

/* allocate a large request from the best fitting chunk in a treebin */
void* RNewAllocator::tmalloc_large(mstate m, size_t nb) {
  tchunkptr v = 0;
  size_t rsize = -nb; /* Unsigned negation */
  tchunkptr t;
  bindex_t idx;
  compute_tree_index(nb, idx);

  if ((t = *treebin_at(m, idx)) != 0) {
    /* Traverse tree for this bin looking for node with size == nb */
    size_t sizebits =
    nb <<
    leftshift_for_tree_index(idx);
    tchunkptr rst = 0;  /* The deepest untaken right subtree */
    for (;;) {
      tchunkptr rt;
      size_t trem = chunksize(t) - nb;
      if (trem < rsize) {
        v = t;
        if ((rsize = trem) == 0)
          break;
      }
      rt = t->child[1];
      t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1];
      if (rt != 0 && rt != t)
        rst = rt;
      if (t == 0) {
        t = rst; /* set t to least subtree holding sizes > nb */
        break;
      }
      sizebits <<= 1;
    }
  }
  if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */
    binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap;
    if (leftbits != 0) {
      bindex_t i;
      binmap_t leastbit = least_bit(leftbits);
      compute_bit2idx(leastbit, i);
      t = *treebin_at(m, i);
    }
  }
  while (t != 0) { /* find smallest of tree or subtree */
    size_t trem = chunksize(t) - nb;
    if (trem < rsize) {
      rsize = trem;
      v = t;
    }
    t = leftmost_child(t);
  }
  /*  If dv is a better fit, return 0 so malloc will use it */
  if (v != 0 && rsize < (size_t)(m->dvsize - nb)) {
    if (RTCHECK(ok_address(m, v))) { /* split */
      mchunkptr r = chunk_plus_offset(v, nb);
      assert(chunksize(v) == rsize + nb);
      if (RTCHECK(ok_next(v, r))) {
        unlink_large_chunk(m, v);
        if (rsize < MIN_CHUNK_SIZE)
          set_inuse_and_pinuse(m, v, (rsize + nb));
        else {
          set_size_and_pinuse_of_inuse_chunk(m, v, nb);
          set_size_and_pinuse_of_free_chunk(r, rsize);
          insert_chunk(m, r, rsize);
        }
        return chunk2mem(v);
      }
    }
    CORRUPTION_ERROR_ACTION(m);
  }
  return 0;
}

/* allocate a small request from the best fitting chunk in a treebin */
void* RNewAllocator::tmalloc_small(mstate m, size_t nb) {
  tchunkptr t, v;
  size_t rsize;
  bindex_t i;
  binmap_t leastbit = least_bit(m->treemap);
  compute_bit2idx(leastbit, i);

  v = t = *treebin_at(m, i);
  rsize = chunksize(t) - nb;

  while ((t = leftmost_child(t)) != 0) {
    size_t trem = chunksize(t) - nb;
    if (trem < rsize) {
      rsize = trem;
      v = t;
    }
  }

  if (RTCHECK(ok_address(m, v))) {
    mchunkptr r = chunk_plus_offset(v, nb);
    assert(chunksize(v) == rsize + nb);
    if (RTCHECK(ok_next(v, r))) {
      unlink_large_chunk(m, v);
      if (rsize < MIN_CHUNK_SIZE)
        set_inuse_and_pinuse(m, v, (rsize + nb));
      else {
        set_size_and_pinuse_of_inuse_chunk(m, v, nb);
        set_size_and_pinuse_of_free_chunk(r, rsize);
        replace_dv(m, r, rsize);
      }
      return chunk2mem(v);
    }
  }
  CORRUPTION_ERROR_ACTION(m);
  return 0;
}

void RNewAllocator::init_top(mstate m, mchunkptr p, size_t psize)
{
	/* Ensure alignment */
	size_t offset = align_offset(chunk2mem(p));
	p = (mchunkptr)((TUint8*)p + offset);
	psize -= offset;
	m->top = p;
	m->topsize = psize;
	p->head = psize | PINUSE_BIT;
	/* set size of fake trailing chunk holding overhead space only once */
	mchunkptr chunkPlusOff = chunk_plus_offset(p, psize);
	chunkPlusOff->head = TOP_FOOT_SIZE;
	m->trim_check = mparams.trim_threshold; /* reset on each update */
}

void* RNewAllocator::internal_realloc(mstate m, void* oldmem, size_t bytes)
{
  if (bytes >= MAX_REQUEST) {
    MALLOC_FAILURE_ACTION;
    return 0;
  }
  if (!PREACTION(m)) {
    mchunkptr oldp = mem2chunk(oldmem);
    size_t oldsize = chunksize(oldp);
    mchunkptr next = chunk_plus_offset(oldp, oldsize);
    mchunkptr newp = 0;
    void* extra = 0;

    /* Try to either shrink or extend into top. Else malloc-copy-free */

    if (RTCHECK(ok_address(m, oldp) && ok_cinuse(oldp) &&
                ok_next(oldp, next) && ok_pinuse(next))) {
      size_t nb = request2size(bytes);
      if (is_mmapped(oldp))
        newp = mmap_resize(m, oldp, nb);
      else
	  if (oldsize >= nb) { /* already big enough */
        size_t rsize = oldsize - nb;
        newp = oldp;
        if (rsize >= MIN_CHUNK_SIZE) {
          mchunkptr remainder = chunk_plus_offset(newp, nb);
          set_inuse(m, newp, nb);
          set_inuse(m, remainder, rsize);
          extra = chunk2mem(remainder);
        }
      }
		/*AMOD: Modified to optimized*/
		else if (next == m->top && oldsize + m->topsize > nb)
		{
			/* Expand into top */
			if(oldsize + m->topsize > nb)
			{
		        size_t newsize = oldsize + m->topsize;
		        size_t newtopsize = newsize - nb;
		        mchunkptr newtop = chunk_plus_offset(oldp, nb);
		        set_inuse(m, oldp, nb);
		        newtop->head = newtopsize |PINUSE_BIT;
		        m->top = newtop;
		        m->topsize = newtopsize;
		        newp = oldp;
			}
      }
    }
    else {
      USAGE_ERROR_ACTION(m, oldmem);
      POSTACTION(m);
      return 0;
    }

    POSTACTION(m);

    if (newp != 0) {
      if (extra != 0) {
        internal_free(m, extra);
      }
      check_inuse_chunk(m, newp);
      return chunk2mem(newp);
    }
    else {
      void* newmem = internal_malloc(m, bytes);
      if (newmem != 0) {
        size_t oc = oldsize - overhead_for(oldp);
        memcpy(newmem, oldmem, (oc < bytes)? oc : bytes);
        internal_free(m, oldmem);
      }
      return newmem;
    }
  }
  return 0;
}
/* ----------------------------- statistics ------------------------------ */
mallinfo RNewAllocator::internal_mallinfo(mstate m) {
  struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  TInt chunkCnt = 0;
  if (!PREACTION(m)) {
    check_malloc_state(m);
    if (is_initialized(m)) {
      size_t nfree = SIZE_T_ONE; /* top always free */
      size_t mfree = m->topsize + TOP_FOOT_SIZE;
      size_t sum = mfree;
      msegmentptr s = &m->seg;
      while (s != 0) {
        mchunkptr q = align_as_chunk(s->base);
        chunkCnt++;
        while (segment_holds(s, q) &&
               q != m->top && q->head != FENCEPOST_HEAD) {
          size_t sz = chunksize(q);
          sum += sz;
          if (!cinuse(q)) {
            if (sz > nm.largestBlock)
              nm.largestBlock = sz;
            mfree += sz;
            ++nfree;
          }
          q = next_chunk(q);
        }
        s = s->next;
      }
      nm.arena    = sum;
      nm.ordblks  = nfree;
      nm.hblkhd   = m->footprint - sum;
      nm.usmblks  = m->max_footprint;
      nm.uordblks = m->footprint - mfree;
      nm.fordblks = mfree;
      nm.keepcost = m->topsize;
      nm.cellCount= chunkCnt;/*number of chunks allocated*/
    }
    POSTACTION(m);
  }
  return nm;
}

void  RNewAllocator::internal_malloc_stats(mstate m) {
if (!PREACTION(m)) {
  size_t fp = 0;
  size_t used = 0;
  check_malloc_state(m);
  if (is_initialized(m)) {
    msegmentptr s = &m->seg;
    size_t maxfp = m->max_footprint;
    fp = m->footprint;
    used = fp - (m->topsize + TOP_FOOT_SIZE);

    while (s != 0) {
      mchunkptr q = align_as_chunk(s->base);
      while (segment_holds(s, q) &&
             q != m->top && q->head != FENCEPOST_HEAD) {
        if (!cinuse(q))
          used -= chunksize(q);
        q = next_chunk(q);
      }
      s = s->next;
    }
  }
  POSTACTION(m);
}
}
/* support for mallopt */
int RNewAllocator::change_mparam(int param_number, int value) {
  size_t val = (size_t)value;
  init_mparams(DEFAULT_TRIM_THRESHOLD);
  switch(param_number) {
  case M_TRIM_THRESHOLD:
    mparams.trim_threshold = val;
    return 1;
  case M_GRANULARITY:
    if (val >= mparams.page_size && ((val & (val-1)) == 0)) {
      mparams.granularity = val;
      return 1;
    }
    else
      return 0;
  case M_MMAP_THRESHOLD:
    mparams.mmap_threshold = val;
    return 1;
  default:
    return 0;
  }
}
/* Get memory from system using MORECORE or MMAP */
void* RNewAllocator::sys_alloc(mstate m, size_t nb)
{
	TUint8* tbase = CMFAIL;
	size_t tsize = 0;
	flag_t mmap_flag = 0;
	//init_mparams();/*No need to do init_params here*/
	/* Directly map large chunks */
	if (use_mmap(m) && nb >= mparams.mmap_threshold)
	{
		void* mem = mmap_alloc(m, nb);
		if (mem != 0)
			return mem;
	}
  /*
    Try getting memory in any of three ways (in most-preferred to
    least-preferred order):
    1. A call to MORECORE that can normally contiguously extend memory.
       (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or
       or main space is mmapped or a previous contiguous call failed)
    2. A call to MMAP new space (disabled if not HAVE_MMAP).
       Note that under the default settings, if MORECORE is unable to
       fulfill a request, and HAVE_MMAP is true, then mmap is
       used as a noncontiguous system allocator. This is a useful backup
       strategy for systems with holes in address spaces -- in this case
       sbrk cannot contiguously expand the heap, but mmap may be able to
       find space.
    3. A call to MORECORE that cannot usually contiguously extend memory.
       (disabled if not HAVE_MORECORE)
  */
  /*Trying to allocate the memory*/
	if(MORECORE_CONTIGUOUS && !use_noncontiguous(m))
	{
	TUint8* br = CMFAIL;
    msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (TUint8*)m->top);
    size_t asize = 0;
    ACQUIRE_MORECORE_LOCK(m);
    if (ss == 0)
	{  /* First time through or recovery */
		TUint8* base = (TUint8*)CALL_MORECORE(0);
		if (base != CMFAIL)
		{
			asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
			/* Adjust to end on a page boundary */
			if (!is_page_aligned(base))
				asize += (page_align((size_t)base) - (size_t)base);
			/* Can't call MORECORE if size is negative when treated as signed */
			if (asize < HALF_MAX_SIZE_T &&(br = (TUint8*)(CALL_MORECORE(asize))) == base)
			{
				tbase = base;
				tsize = asize;
			}
		}
    }
    else
	{
      /* Subtract out existing available top space from MORECORE request. */
		asize = granularity_align(nb - m->topsize + TOP_FOOT_SIZE + SIZE_T_ONE);
    /* Use mem here only if it did continuously extend old space */
      if (asize < HALF_MAX_SIZE_T &&
          (br = (TUint8*)(CALL_MORECORE(asize))) == ss->base+ss->size) {
        tbase = br;
        tsize = asize;
      }
    }
    if (tbase == CMFAIL) {    /* Cope with partial failure */
      if (br != CMFAIL) {    /* Try to use/extend the space we did get */
        if (asize < HALF_MAX_SIZE_T &&
            asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) {
          size_t esize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE - asize);
          if (esize < HALF_MAX_SIZE_T) {
            TUint8* end = (TUint8*)CALL_MORECORE(esize);
            if (end != CMFAIL)
              asize += esize;
            else {            /* Can't use; try to release */
              CALL_MORECORE(-asize);
              br = CMFAIL;
            }
          }
        }
      }
      if (br != CMFAIL) {    /* Use the space we did get */
        tbase = br;
        tsize = asize;
      }
      else
        disable_contiguous(m); /* Don't try contiguous path in the future */
    }
    RELEASE_MORECORE_LOCK(m);
  }
  if (HAVE_MMAP && tbase == CMFAIL) {  /* Try MMAP */
    size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE;
    size_t rsize = granularity_align(req);
    if (rsize > nb) { /* Fail if wraps around zero */
      TUint8* mp = (TUint8*)(CALL_MMAP(rsize));
      if (mp != CMFAIL) {
        tbase = mp;
        tsize = rsize;
        mmap_flag = IS_MMAPPED_BIT;
      }
    }
  }
  if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */
    size_t asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
    if (asize < HALF_MAX_SIZE_T) {
      TUint8* br = CMFAIL;
      TUint8* end = CMFAIL;
      ACQUIRE_MORECORE_LOCK(m);
      br = (TUint8*)(CALL_MORECORE(asize));
      end = (TUint8*)(CALL_MORECORE(0));
      RELEASE_MORECORE_LOCK(m);
      if (br != CMFAIL && end != CMFAIL && br < end) {
        size_t ssize = end - br;
        if (ssize > nb + TOP_FOOT_SIZE) {
          tbase = br;
          tsize = ssize;
        }
      }
    }
  }
  if (tbase != CMFAIL) {
    if ((m->footprint += tsize) > m->max_footprint)
      m->max_footprint = m->footprint;
    if (!is_initialized(m)) { /* first-time initialization */
      m->seg.base = m->least_addr = tbase;
      m->seg.size = tsize;
      m->seg.sflags = mmap_flag;
      m->magic = mparams.magic;
      init_bins(m);
      if (is_global(m))
        init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);
      else {
        /* Offset top by embedded malloc_state */
        mchunkptr mn = next_chunk(mem2chunk(m));
        init_top(m, mn, (size_t)((tbase + tsize) - (TUint8*)mn) -TOP_FOOT_SIZE);
      }
    }else {
      /* Try to merge with an existing segment */
      msegmentptr sp = &m->seg;
      while (sp != 0 && tbase != sp->base + sp->size)
        sp = sp->next;
      if (sp != 0 && !is_extern_segment(sp) &&
          (sp->sflags & IS_MMAPPED_BIT) == mmap_flag &&
          segment_holds(sp, m->top))
    	  { /* append */
        sp->size += tsize;
        init_top(m, m->top, m->topsize + tsize);
      }
      else {
        if (tbase < m->least_addr)
          m->least_addr = tbase;
        sp = &m->seg;
        while (sp != 0 && sp->base != tbase + tsize)
          sp = sp->next;
        if (sp != 0 &&
            !is_extern_segment(sp) &&
            (sp->sflags & IS_MMAPPED_BIT) == mmap_flag) {
          TUint8* oldbase = sp->base;
          sp->base = tbase;
          sp->size += tsize;
          return prepend_alloc(m, tbase, oldbase, nb);
        }
        else
          add_segment(m, tbase, tsize, mmap_flag);
      }
    }
    if (nb < m->topsize) { /* Allocate from new or extended top space */
      size_t rsize = m->topsize -= nb;
      mchunkptr p = m->top;
      mchunkptr r = m->top = chunk_plus_offset(p, nb);
      r->head = rsize | PINUSE_BIT;
      set_size_and_pinuse_of_inuse_chunk(m, p, nb);
      check_top_chunk(m, m->top);
      check_malloced_chunk(m, chunk2mem(p), nb);
      return chunk2mem(p);
    }
  }
  /*need to check this*/
  //errno = -1;
  return 0;
}
msegmentptr RNewAllocator::segment_holding(mstate m, TUint8* addr) {
  msegmentptr sp = &m->seg;
  for (;;) {
    if (addr >= sp->base && addr < sp->base + sp->size)
      return sp;
    if ((sp = sp->next) == 0)
      return 0;
  }
}
/* Unlink the first chunk from a smallbin */
inline void RNewAllocator::unlink_first_small_chunk(mstate M,mchunkptr B,mchunkptr P,bindex_t& I)
{
  mchunkptr F = P->fd;
  assert(P != B);
  assert(P != F);
  assert(chunksize(P) == small_index2size(I));
  if (B == F)
    clear_smallmap(M, I);
  else if (RTCHECK(ok_address(M, F))) {
    B->fd = F;
    F->bk = B;
  }
  else {
    CORRUPTION_ERROR_ACTION(M);
  }
}
/* Link a free chunk into a smallbin  */
inline void RNewAllocator::insert_small_chunk(mstate M,mchunkptr P, size_t S)
{
  bindex_t I  = small_index(S);
  mchunkptr B = smallbin_at(M, I);
  mchunkptr F = B;
  assert(S >= MIN_CHUNK_SIZE);
  if (!smallmap_is_marked(M, I))
    mark_smallmap(M, I);
  else if (RTCHECK(ok_address(M, B->fd)))
    F = B->fd;
  else {
    CORRUPTION_ERROR_ACTION(M);
  }
  B->fd = P;
  F->bk = P;
  P->fd = F;
  P->bk = B;
}


inline void RNewAllocator::insert_chunk(mstate M,mchunkptr P,size_t S)
{
	if (is_small(S))
		insert_small_chunk(M, P, S);
	else{
		tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S);
	 }
}

inline void RNewAllocator::unlink_large_chunk(mstate M,tchunkptr X)
{
  tchunkptr XP = X->parent;
  tchunkptr R;
  if (X->bk != X) {
    tchunkptr F = X->fd;
    R = X->bk;
    if (RTCHECK(ok_address(M, F))) {
      F->bk = R;
      R->fd = F;
    }
    else {
      CORRUPTION_ERROR_ACTION(M);
    }
  }
  else {
    tchunkptr* RP;
    if (((R = *(RP = &(X->child[1]))) != 0) ||
        ((R = *(RP = &(X->child[0]))) != 0)) {
      tchunkptr* CP;
      while ((*(CP = &(R->child[1])) != 0) ||
             (*(CP = &(R->child[0])) != 0)) {
        R = *(RP = CP);
      }
      if (RTCHECK(ok_address(M, RP)))
        *RP = 0;
      else {
        CORRUPTION_ERROR_ACTION(M);
      }
    }
  }
  if (XP != 0) {
    tbinptr* H = treebin_at(M, X->index);
    if (X == *H) {
      if ((*H = R) == 0)
        clear_treemap(M, X->index);
    }
    else if (RTCHECK(ok_address(M, XP))) {
      if (XP->child[0] == X)
        XP->child[0] = R;
      else
        XP->child[1] = R;
    }
    else
      CORRUPTION_ERROR_ACTION(M);
    if (R != 0) {
      if (RTCHECK(ok_address(M, R))) {
        tchunkptr C0, C1;
        R->parent = XP;
        if ((C0 = X->child[0]) != 0) {
          if (RTCHECK(ok_address(M, C0))) {
            R->child[0] = C0;
            C0->parent = R;
          }
          else
            CORRUPTION_ERROR_ACTION(M);
        }
        if ((C1 = X->child[1]) != 0) {
          if (RTCHECK(ok_address(M, C1))) {
            R->child[1] = C1;
            C1->parent = R;
          }
          else
            CORRUPTION_ERROR_ACTION(M);
        }
      }
      else
        CORRUPTION_ERROR_ACTION(M);
    }
  }
}

/* Unlink a chunk from a smallbin  */
inline void RNewAllocator::unlink_small_chunk(mstate M, mchunkptr P,size_t S)
{
  mchunkptr F = P->fd;
  mchunkptr B = P->bk;
  bindex_t I = small_index(S);
  assert(P != B);
  assert(P != F);
  assert(chunksize(P) == small_index2size(I));
  if (F == B)
    clear_smallmap(M, I);
  else if (RTCHECK((F == smallbin_at(M,I) || ok_address(M, F)) &&
                   (B == smallbin_at(M,I) || ok_address(M, B)))) {
    F->bk = B;
    B->fd = F;
  }
  else {
    CORRUPTION_ERROR_ACTION(M);
  }
}

inline void RNewAllocator::unlink_chunk(mstate M, mchunkptr P, size_t S)
{
  if (is_small(S))
	unlink_small_chunk(M, P, S);
  else
  {
	  tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP);
  }
}

inline void RNewAllocator::compute_tree_index(size_t S, bindex_t& I)
{
  size_t X = S >> TREEBIN_SHIFT;
  if (X == 0)
    I = 0;
  else if (X > 0xFFFF)
    I = NTREEBINS-1;
  else {
    unsigned int Y = (unsigned int)X;
    unsigned int N = ((Y - 0x100) >> 16) & 8;
    unsigned int K = (((Y <<= N) - 0x1000) >> 16) & 4;
    N += K;
    N += K = (((Y <<= K) - 0x4000) >> 16) & 2;
    K = 14 - N + ((Y <<= K) >> 15);
    I = (K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1));
  }
}

/* ------------------------- Operations on trees ------------------------- */

/* Insert chunk into tree */
inline void RNewAllocator::insert_large_chunk(mstate M,tchunkptr X,size_t S)
{
  tbinptr* H;
  bindex_t I;
  compute_tree_index(S, I);
  H = treebin_at(M, I);
  X->index = I;
  X->child[0] = X->child[1] = 0;
  if (!treemap_is_marked(M, I)) {
    mark_treemap(M, I);
    *H = X;
    X->parent = (tchunkptr)H;
    X->fd = X->bk = X;
  }
  else {
    tchunkptr T = *H;
    size_t K = S << leftshift_for_tree_index(I);
    for (;;) {
      if (chunksize(T) != S) {
        tchunkptr* C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);
        K <<= 1;
        if (*C != 0)
          T = *C;
        else if (RTCHECK(ok_address(M, C))) {
          *C = X;
          X->parent = T;
          X->fd = X->bk = X;
          break;
        }
        else {
          CORRUPTION_ERROR_ACTION(M);
          break;
        }
      }
      else {
        tchunkptr F = T->fd;
        if (RTCHECK(ok_address(M, T) && ok_address(M, F))) {
          T->fd = F->bk = X;
          X->fd = F;
          X->bk = T;
          X->parent = 0;
          break;
        }
        else {
          CORRUPTION_ERROR_ACTION(M);
          break;
        }
      }
    }
  }
}

/*
  Unlink steps:

  1. If x is a chained node, unlink it from its same-sized fd/bk links
     and choose its bk node as its replacement.
  2. If x was the last node of its size, but not a leaf node, it must
     be replaced with a leaf node (not merely one with an open left or
     right), to make sure that lefts and rights of descendents
     correspond properly to bit masks.  We use the rightmost descendent
     of x.  We could use any other leaf, but this is easy to locate and
     tends to counteract removal of leftmosts elsewhere, and so keeps
     paths shorter than minimally guaranteed.  This doesn't loop much
     because on average a node in a tree is near the bottom.
  3. If x is the base of a chain (i.e., has parent links) relink
     x's parent and children to x's replacement (or null if none).
*/

/* Replace dv node, binning the old one */
/* Used only when dvsize known to be small */
inline void RNewAllocator::replace_dv(mstate M, mchunkptr P, size_t S)
{
  size_t DVS = M->dvsize;
  if (DVS != 0) {
    mchunkptr DV = M->dv;
    assert(is_small(DVS));
    insert_small_chunk(M, DV, DVS);
  }
  M->dvsize = S;
  M->dv = P;
}

inline void RNewAllocator::compute_bit2idx(binmap_t X,bindex_t& I)
{
	unsigned int Y = X - 1;
	unsigned int K = Y >> (16-4) & 16;
	unsigned int N = K;        Y >>= K;
	N += K = Y >> (8-3) &  8;  Y >>= K;
	N += K = Y >> (4-2) &  4;  Y >>= K;
	N += K = Y >> (2-1) &  2;  Y >>= K;
	N += K = Y >> (1-0) &  1;  Y >>= K;
	I = (bindex_t)(N + Y);
}

void RNewAllocator::add_segment(mstate m, TUint8* tbase, size_t tsize, flag_t mmapped) {
  /* Determine locations and sizes of segment, fenceposts, old top */
  TUint8* old_top = (TUint8*)m->top;
  msegmentptr oldsp = segment_holding(m, old_top);
  TUint8* old_end = oldsp->base + oldsp->size;
  size_t ssize = pad_request(sizeof(struct malloc_segment));
  TUint8* rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
  size_t offset = align_offset(chunk2mem(rawsp));
  TUint8* asp = rawsp + offset;
  TUint8* csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp;
  mchunkptr sp = (mchunkptr)csp;
  msegmentptr ss = (msegmentptr)(chunk2mem(sp));
  mchunkptr tnext = chunk_plus_offset(sp, ssize);
  mchunkptr p = tnext;
  int nfences = 0;

  /* reset top to new space */
  init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);

  /* Set up segment record */
  assert(is_aligned(ss));
  set_size_and_pinuse_of_inuse_chunk(m, sp, ssize);
  *ss = m->seg; /* Push current record */
  m->seg.base = tbase;
  m->seg.size = tsize;
  m->seg.sflags = mmapped;
  m->seg.next = ss;

  /* Insert trailing fenceposts */
  for (;;) {
    mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE);
    p->head = FENCEPOST_HEAD;
    ++nfences;
    if ((TUint8*)(&(nextp->head)) < old_end)
      p = nextp;
    else
      break;
  }
  assert(nfences >= 2);

  /* Insert the rest of old top into a bin as an ordinary free chunk */
  if (csp != old_top) {
    mchunkptr q = (mchunkptr)old_top;
    size_t psize = csp - old_top;
    mchunkptr tn = chunk_plus_offset(q, psize);
    set_free_with_pinuse(q, psize, tn);
    insert_chunk(m, q, psize);
  }

  check_top_chunk(m, m->top);
}


void* RNewAllocator::prepend_alloc(mstate m, TUint8* newbase, TUint8* oldbase,
                           size_t nb) {
  mchunkptr p = align_as_chunk(newbase);
  mchunkptr oldfirst = align_as_chunk(oldbase);
  size_t psize = (TUint8*)oldfirst - (TUint8*)p;
  mchunkptr q = chunk_plus_offset(p, nb);
  size_t qsize = psize - nb;
  set_size_and_pinuse_of_inuse_chunk(m, p, nb);

  assert((TUint8*)oldfirst > (TUint8*)q);
  assert(pinuse(oldfirst));
  assert(qsize >= MIN_CHUNK_SIZE);

  /* consolidate remainder with first chunk of old base */
  if (oldfirst == m->top) {
    size_t tsize = m->topsize += qsize;
    m->top = q;
    q->head = tsize | PINUSE_BIT;
    check_top_chunk(m, q);
  }
  else if (oldfirst == m->dv) {
    size_t dsize = m->dvsize += qsize;
    m->dv = q;
    set_size_and_pinuse_of_free_chunk(q, dsize);
  }
  else {
    if (!cinuse(oldfirst)) {
      size_t nsize = chunksize(oldfirst);
      unlink_chunk(m, oldfirst, nsize);
      oldfirst = chunk_plus_offset(oldfirst, nsize);
      qsize += nsize;
    }
    set_free_with_pinuse(q, qsize, oldfirst);
    insert_chunk(m, q, qsize);
    check_free_chunk(m, q);
  }

  check_malloced_chunk(m, chunk2mem(p), nb);
  return chunk2mem(p);
}

void* RNewAllocator::mmap_alloc(mstate m, size_t nb) {
  size_t mmsize = granularity_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
  if (mmsize > nb) {     /* Check for wrap around 0 */
    TUint8* mm = (TUint8*)(DIRECT_MMAP(mmsize));
    if (mm != CMFAIL) {
      size_t offset = align_offset(chunk2mem(mm));
      size_t psize = mmsize - offset - MMAP_FOOT_PAD;
      mchunkptr p = (mchunkptr)(mm + offset);
      p->prev_foot = offset | IS_MMAPPED_BIT;
      (p)->head = (psize|CINUSE_BIT);
      mark_inuse_foot(m, p, psize);
      chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD;
      chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0;

      if (mm < m->least_addr)
        m->least_addr = mm;
      if ((m->footprint += mmsize) > m->max_footprint)
        m->max_footprint = m->footprint;
      assert(is_aligned(chunk2mem(p)));
      check_mmapped_chunk(m, p);
      return chunk2mem(p);
    }
  }
  return 0;
}

	int RNewAllocator::sys_trim(mstate m, size_t pad)
	{
	  size_t released = 0;
	  if (pad < MAX_REQUEST && is_initialized(m)) {
	    pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */

	    if (m->topsize > pad) {
	      /* Shrink top space in granularity-size units, keeping at least one */
	      size_t unit = mparams.granularity;
				size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - SIZE_T_ONE) * unit;
	      msegmentptr sp = segment_holding(m, (TUint8*)m->top);

	      if (!is_extern_segment(sp)) {
	        if (is_mmapped_segment(sp)) {
	          if (HAVE_MMAP &&
	              sp->size >= extra &&
	              !has_segment_link(m, sp)) { /* can't shrink if pinned */
	            size_t newsize = sp->size - extra;
	            /* Prefer mremap, fall back to munmap */
	            if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL) ||
	                (CALL_MUNMAP(sp->base + newsize, extra) == 0)) {
	              released = extra;
	            }
	          }
	        }
	        else if (HAVE_MORECORE) {
	          if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */
	            extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit;
	          ACQUIRE_MORECORE_LOCK(m);
	          {
	            /* Make sure end of memory is where we last set it. */
	            TUint8* old_br = (TUint8*)(CALL_MORECORE(0));
	            if (old_br == sp->base + sp->size) {
	              TUint8* rel_br = (TUint8*)(CALL_MORECORE(-extra));
	              TUint8* new_br = (TUint8*)(CALL_MORECORE(0));
	              if (rel_br != CMFAIL && new_br < old_br)
	                released = old_br - new_br;
	            }
	          }
	          RELEASE_MORECORE_LOCK(m);
	        }
	      }

	      if (released != 0) {
	        sp->size -= released;
	        m->footprint -= released;
	        init_top(m, m->top, m->topsize - released);
	        check_top_chunk(m, m->top);
	      }
	    }

	    /* Unmap any unused mmapped segments */
	    if (HAVE_MMAP)
	      released += release_unused_segments(m);

	    /* On failure, disable autotrim to avoid repeated failed future calls */
	    if (released == 0)
	      m->trim_check = MAX_SIZE_T;
	  }

	  return (released != 0)? 1 : 0;
	}

	inline int RNewAllocator::has_segment_link(mstate m, msegmentptr ss)
	{
	  msegmentptr sp = &m->seg;
	  for (;;) {
	    if ((TUint8*)sp >= ss->base && (TUint8*)sp < ss->base + ss->size)
	      return 1;
	    if ((sp = sp->next) == 0)
	      return 0;
	  }
	}

	/* Unmap and unlink any mmapped segments that don't contain used chunks */
	size_t RNewAllocator::release_unused_segments(mstate m)
	{
	  size_t released = 0;
	  msegmentptr pred = &m->seg;
	  msegmentptr sp = pred->next;
	  while (sp != 0) {
	    TUint8* base = sp->base;
	    size_t size = sp->size;
	    msegmentptr next = sp->next;
	    if (is_mmapped_segment(sp) && !is_extern_segment(sp)) {
	      mchunkptr p = align_as_chunk(base);
	      size_t psize = chunksize(p);
	      /* Can unmap if first chunk holds entire segment and not pinned */
	      if (!cinuse(p) && (TUint8*)p + psize >= base + size - TOP_FOOT_SIZE) {
	        tchunkptr tp = (tchunkptr)p;
	        assert(segment_holds(sp, (TUint8*)sp));
	        if (p == m->dv) {
	          m->dv = 0;
	          m->dvsize = 0;
	        }
	        else {
	          unlink_large_chunk(m, tp);
	        }
	        if (CALL_MUNMAP(base, size) == 0) {
	          released += size;
	          m->footprint -= size;
	          /* unlink obsoleted record */
	          sp = pred;
	          sp->next = next;
	        }
	        else { /* back out if cannot unmap */
	          insert_large_chunk(m, tp, psize);
	        }
	      }
	    }
	    pred = sp;
	    sp = next;
	  }/*End of while*/
	  return released;
	}
	/* Realloc using mmap */
	inline	mchunkptr RNewAllocator::mmap_resize(mstate m, mchunkptr oldp, size_t nb)
	{
	  size_t oldsize = chunksize(oldp);
	  if (is_small(nb)) /* Can't shrink mmap regions below small size */
	    return 0;
	  /* Keep old chunk if big enough but not too big */
	  if (oldsize >= nb + SIZE_T_SIZE &&
	      (oldsize - nb) <= (mparams.granularity << 1))
	    return oldp;
	  else {
	    size_t offset = oldp->prev_foot & ~IS_MMAPPED_BIT;
	    size_t oldmmsize = oldsize + offset + MMAP_FOOT_PAD;
	    size_t newmmsize = granularity_align(nb + SIX_SIZE_T_SIZES +
	                                         CHUNK_ALIGN_MASK);
	    TUint8* cp = (TUint8*)CALL_MREMAP((char*)oldp - offset,
	                                  oldmmsize, newmmsize, 1);
	    if (cp != CMFAIL) {
	      mchunkptr newp = (mchunkptr)(cp + offset);
	      size_t psize = newmmsize - offset - MMAP_FOOT_PAD;
	      newp->head = (psize|CINUSE_BIT);
	      mark_inuse_foot(m, newp, psize);
	      chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD;
	      chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0;

	      if (cp < m->least_addr)
	        m->least_addr = cp;
	      if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint)
	        m->max_footprint = m->footprint;
	      check_mmapped_chunk(m, newp);
	      return newp;
	    }
	  }
	  return 0;
	}


void RNewAllocator::Init_Dlmalloc(size_t capacity, int locked, size_t aTrimThreshold)
	{
		memset(gm,0,sizeof(malloc_state));
		init_mparams(aTrimThreshold); /* Ensure pagesize etc initialized */
		// The maximum amount that can be allocated can be calculated as:-
		// 2^sizeof(size_t) - sizeof(malloc_state) - TOP_FOOT_SIZE - page size (all accordingly padded)
		// If the capacity exceeds this, no allocation will be done.
		gm->seg.base = gm->least_addr = iBase;
		gm->seg.size = capacity;
		gm->seg.sflags = !IS_MMAPPED_BIT;
		set_lock(gm, locked);
		gm->magic = mparams.magic;
		init_bins(gm);
		init_top(gm, (mchunkptr)iBase, capacity - TOP_FOOT_SIZE);
	}

void* RNewAllocator::dlmalloc(size_t bytes) {
  /*
     Basic algorithm:
     If a small request (< 256 bytes minus per-chunk overhead):
       1. If one exists, use a remainderless chunk in associated smallbin.
          (Remainderless means that there are too few excess bytes to
          represent as a chunk.)
       2. If it is big enough, use the dv chunk, which is normally the
          chunk adjacent to the one used for the most recent small request.
       3. If one exists, split the smallest available chunk in a bin,
          saving remainder in dv.
       4. If it is big enough, use the top chunk.
       5. If available, get memory from system and use it
     Otherwise, for a large request:
       1. Find the smallest available binned chunk that fits, and use it
          if it is better fitting than dv chunk, splitting if necessary.
       2. If better fitting than any binned chunk, use the dv chunk.
       3. If it is big enough, use the top chunk.
       4. If request size >= mmap threshold, try to directly mmap this chunk.
       5. If available, get memory from system and use it

     The ugly goto's here ensure that postaction occurs along all paths.
  */
  if (!PREACTION(gm)) {
    void* mem;
    size_t nb;
    if (bytes <= MAX_SMALL_REQUEST) {
      bindex_t idx;
      binmap_t smallbits;
      nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes);
      idx = small_index(nb);
      smallbits = gm->smallmap >> idx;

      if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */
        mchunkptr b, p;
        idx += ~smallbits & 1;       /* Uses next bin if idx empty */
        b = smallbin_at(gm, idx);
        p = b->fd;
        assert(chunksize(p) == small_index2size(idx));
        unlink_first_small_chunk(gm, b, p, idx);
        set_inuse_and_pinuse(gm, p, small_index2size(idx));
        mem = chunk2mem(p);
        check_malloced_chunk(gm, mem, nb);
        goto postaction;
      }

      else if (nb > gm->dvsize) {
        if (smallbits != 0) { /* Use chunk in next nonempty smallbin */
          mchunkptr b, p, r;
          size_t rsize;
          bindex_t i;
          binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx));
          binmap_t leastbit = least_bit(leftbits);
          compute_bit2idx(leastbit, i);
          b = smallbin_at(gm, i);
          p = b->fd;
          assert(chunksize(p) == small_index2size(i));
          unlink_first_small_chunk(gm, b, p, i);
          rsize = small_index2size(i) - nb;
          /* Fit here cannot be remainderless if 4byte sizes */
          if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE)
            set_inuse_and_pinuse(gm, p, small_index2size(i));
          else {
            set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
            r = chunk_plus_offset(p, nb);
            set_size_and_pinuse_of_free_chunk(r, rsize);
            replace_dv(gm, r, rsize);
          }
          mem = chunk2mem(p);
          check_malloced_chunk(gm, mem, nb);
          goto postaction;
        }

        else if (gm->treemap != 0 && (mem = tmalloc_small(gm, nb)) != 0) {
          check_malloced_chunk(gm, mem, nb);
          goto postaction;
        }
      }
    }
    else if (bytes >= MAX_REQUEST)
      nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */
    else {
      nb = pad_request(bytes);
      if (gm->treemap != 0 && (mem = tmalloc_large(gm, nb)) != 0) {
        check_malloced_chunk(gm, mem, nb);
        goto postaction;
      }
    }

    if (nb <= gm->dvsize) {
      size_t rsize = gm->dvsize - nb;
      mchunkptr p = gm->dv;
      if (rsize >= MIN_CHUNK_SIZE) { /* split dv */
        mchunkptr r = gm->dv = chunk_plus_offset(p, nb);
        gm->dvsize = rsize;
        set_size_and_pinuse_of_free_chunk(r, rsize);
        set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
      }
      else { /* exhaust dv */
        size_t dvs = gm->dvsize;
        gm->dvsize = 0;
        gm->dv = 0;
        set_inuse_and_pinuse(gm, p, dvs);
      }
      mem = chunk2mem(p);
      check_malloced_chunk(gm, mem, nb);
      goto postaction;
    }

    else if (nb < gm->topsize) { /* Split top */
      size_t rsize = gm->topsize -= nb;
      mchunkptr p = gm->top;
      mchunkptr r = gm->top = chunk_plus_offset(p, nb);
      r->head = rsize | PINUSE_BIT;
      set_size_and_pinuse_of_inuse_chunk(gm, p, nb);
      mem = chunk2mem(p);
      check_top_chunk(gm, gm->top);
      check_malloced_chunk(gm, mem, nb);
      goto postaction;
    }

    mem = sys_alloc(gm, nb);

  postaction:
    POSTACTION(gm);
    return mem;
  }

  return 0;
}

void RNewAllocator::dlfree(void* mem) {
  /*
     Consolidate freed chunks with preceeding or succeeding bordering
     free chunks, if they exist, and then place in a bin.  Intermixed
     with special cases for top, dv, mmapped chunks, and usage errors.
  */

	if (mem != 0)
	{
		mchunkptr p  = mem2chunk(mem);
#if FOOTERS
		mstate fm = get_mstate_for(p);
		if (!ok_magic(fm))
		{
			USAGE_ERROR_ACTION(fm, p);
			return;
		}
#else /* FOOTERS */
#define fm gm
#endif /* FOOTERS */

		if (!PREACTION(fm))
		{
			check_inuse_chunk(fm, p);
			if (RTCHECK(ok_address(fm, p) && ok_cinuse(p)))
			{
				size_t psize = chunksize(p);
				iTotalAllocSize -= psize;
				mchunkptr next = chunk_plus_offset(p, psize);
				if (!pinuse(p))
				{
					size_t prevsize = p->prev_foot;
					if ((prevsize & IS_MMAPPED_BIT) != 0)
					{
						prevsize &= ~IS_MMAPPED_BIT;
						psize += prevsize + MMAP_FOOT_PAD;
						/*TInt tmp = TOP_FOOT_SIZE;
						TUint8* top = (TUint8*)fm->top + fm->topsize + 40;
						if((top == (TUint8*)p)&& fm->topsize > 4096)
						{
							fm->topsize += psize;
							msegmentptr sp = segment_holding(fm, (TUint8*)fm->top);
							sp->size+=psize;
							if (should_trim(fm, fm->topsize))
								sys_trim(fm, 0);
 							goto postaction;
						}
						else*/
						{
							if (CALL_MUNMAP((char*)p - prevsize, psize) == 0)
								fm->footprint -= psize;
							goto postaction;
						}
					}
					else
					{
						mchunkptr prev = chunk_minus_offset(p, prevsize);
						psize += prevsize;
						p = prev;
						if (RTCHECK(ok_address(fm, prev)))
						{ /* consolidate backward */
							if (p != fm->dv)
							{
								unlink_chunk(fm, p, prevsize);
							}
							else if ((next->head & INUSE_BITS) == INUSE_BITS)
							{
								fm->dvsize = psize;
								set_free_with_pinuse(p, psize, next);
								goto postaction;
							}
						}
						else
							goto erroraction;
					}
				}

				if (RTCHECK(ok_next(p, next) && ok_pinuse(next)))
				{
					if (!cinuse(next))
					{  /* consolidate forward */
						if (next == fm->top)
						{
							size_t tsize = fm->topsize += psize;
							fm->top = p;
							p->head = tsize | PINUSE_BIT;
							if (p == fm->dv)
							{
								fm->dv = 0;
								fm->dvsize = 0;
							}
							if (should_trim(fm, tsize))
								sys_trim(fm, 0);
							goto postaction;
						}
						else if (next == fm->dv)
						{
							size_t dsize = fm->dvsize += psize;
							fm->dv = p;
							set_size_and_pinuse_of_free_chunk(p, dsize);
							goto postaction;
						}
						else
						{
							size_t nsize = chunksize(next);
							psize += nsize;
							unlink_chunk(fm, next, nsize);
							set_size_and_pinuse_of_free_chunk(p, psize);
							if (p == fm->dv)
							{
								fm->dvsize = psize;
								goto postaction;
							}
						}
					}
					else
						set_free_with_pinuse(p, psize, next);
					insert_chunk(fm, p, psize);
					check_free_chunk(fm, p);
					goto postaction;
				}
			}
erroraction:
    	USAGE_ERROR_ACTION(fm, p);
postaction:
    	POSTACTION(fm);
		}
	}
#if !FOOTERS
#undef fm
#endif /* FOOTERS */
}

void* RNewAllocator::dlrealloc(void* oldmem, size_t bytes) {
  if (oldmem == 0)
    return dlmalloc(bytes);
#ifdef REALLOC_ZERO_BYTES_FREES
  if (bytes == 0) {
    dlfree(oldmem);
    return 0;
  }
#endif /* REALLOC_ZERO_BYTES_FREES */
  else {
#if ! FOOTERS
    mstate m = gm;
#else /* FOOTERS */
    mstate m = get_mstate_for(mem2chunk(oldmem));
    if (!ok_magic(m)) {
      USAGE_ERROR_ACTION(m, oldmem);
      return 0;
    }
#endif /* FOOTERS */
    return internal_realloc(m, oldmem, bytes);
  }
}


int RNewAllocator::dlmalloc_trim(size_t pad) {
  int result = 0;
  if (!PREACTION(gm)) {
    result = sys_trim(gm, pad);
    POSTACTION(gm);
  }
  return result;
}

size_t RNewAllocator::dlmalloc_footprint(void) {
  return gm->footprint;
}

size_t RNewAllocator::dlmalloc_max_footprint(void) {
  return gm->max_footprint;
}

#if !NO_MALLINFO
struct mallinfo RNewAllocator::dlmallinfo(void) {
  return internal_mallinfo(gm);
}
#endif /* NO_MALLINFO */

void RNewAllocator::dlmalloc_stats() {
  internal_malloc_stats(gm);
}

int RNewAllocator::dlmallopt(int param_number, int value) {
  return change_mparam(param_number, value);
}

//inline slab* slab::slabfor(void* p)
inline slab* slab::slabfor( const void* p)
	{return (slab*)(floor(p, slabsize));}


void RNewAllocator::tree_remove(slab* s)
{
	slab** r = s->parent;
	slab* c1 = s->child1;
	slab* c2 = s->child2;
	for (;;)
	{
		if (!c2)
		{
			*r = c1;
			if (c1)
				c1->parent = r;
			return;
		}
		if (!c1)
		{
			*r = c2;
			c2->parent = r;
			return;
		}
		if (c1 > c2)
		{
			slab* c3 = c1;
			c1 = c2;
			c2 = c3;
		}
		slab* newc2 = c1->child2;
		*r = c1;
		c1->parent = r;
		c1->child2 = c2;
		c2->parent = &c1->child2;
		s = c1;
		c1 = s->child1;
		c2 = newc2;
		r = &s->child1;
	}
}
void RNewAllocator::tree_insert(slab* s,slab** r)
	{
		slab* n = *r;
		for (;;)
		{
			if (!n)
			{	// tree empty
				*r = s;
				s->parent = r;
				s->child1 = s->child2 = 0;
				break;
			}
			if (s < n)
			{	// insert between parent and n
				*r = s;
				s->parent = r;
				s->child1 = n;
				s->child2 = 0;
				n->parent = &s->child1;
				break;
			}
			slab* c1 = n->child1;
			slab* c2 = n->child2;
			if (c1 < c2)
			{
				r = &n->child1;
				n = c1;
			}
			else
			{
				r = &n->child2;
				n = c2;
			}
		}
	}
void* RNewAllocator::allocnewslab(slabset& allocator)
//
// Acquire and initialise a new slab, returning a cell from the slab
// The strategy is:
// 1. Use the lowest address free slab, if available. This is done by using the lowest slab
//    in the page at the root of the partial_page heap (which is address ordered). If the
//    is now fully used, remove it from the partial_page heap.
// 2. Allocate a new page for slabs if no empty slabs are available
//
{
	page* p = page::pagefor(partial_page);
	if (!p)
		return allocnewpage(allocator);

	unsigned h = p->slabs[0].header;
	unsigned pagemap = header_pagemap(h);
	ASSERT(&p->slabs[hibit(pagemap)] == partial_page);

	unsigned slabix = lowbit(pagemap);
	p->slabs[0].header = h &~ (0x100<<slabix);
	if (!(pagemap &~ (1<<slabix)))
	{
		tree_remove(partial_page);	// last free slab in page
	}
	return initnewslab(allocator,&p->slabs[slabix]);
}

/**Defination of this functionis not there in proto code***/
#if 0
void RNewAllocator::partial_insert(slab* s)
	{
		// slab has had first cell freed and needs to be linked back into partial tree
		slabset& ss = slaballoc[sizemap[s->clz]];

		ASSERT(s->used == slabfull);
		s->used = ss.fulluse - s->clz;		// full-1 loading
		tree_insert(s,&ss.partial);
		checktree(ss.partial);
	}
/**Defination of this functionis not there in proto code***/
#endif

void* RNewAllocator::allocnewpage(slabset& allocator)
//
// Acquire and initialise a new page, returning a cell from a new slab
// The partial_page tree is empty (otherwise we'd have used a slab from there)
// The partial_page link is put in the highest addressed slab in the page, and the
// lowest addressed slab is used to fulfill the allocation request
//
{
	page* p	 = spare_page;
	if (p)
		spare_page = 0;
	else
	{
		p = static_cast<page*>(map(0,pagesize));
		if (!p)
			return 0;
	}
	ASSERT(p == floor(p,pagesize));
	p->slabs[0].header = ((1<<3) + (1<<2) + (1<<1))<<8;		// set pagemap
	p->slabs[3].parent = &partial_page;
	p->slabs[3].child1 = p->slabs[3].child2 = 0;
	partial_page = &p->slabs[3];
	return initnewslab(allocator,&p->slabs[0]);
}

void RNewAllocator::freepage(page* p)
//
// Release an unused page to the OS
// A single page is cached for reuse to reduce thrashing
// the OS allocator.
//
{
	ASSERT(ceiling(p,pagesize) == p);
	if (!spare_page)
	{
		spare_page = p;
		return;
	}
	unmap(p,pagesize);
}

void RNewAllocator::freeslab(slab* s)
//
// Release an empty slab to the slab manager
// The strategy is:
// 1. The page containing the slab is checked to see the state of the other slabs in the page by
//    inspecting the pagemap field in the header of the first slab in the page.
// 2. The pagemap is updated to indicate the new unused slab
// 3. If this is the only unused slab in the page then the slab header is used to add the page to
//    the partial_page tree/heap
// 4. If all the slabs in the page are now unused the page is release back to the OS
// 5. If this slab has a higher address than the one currently used to track this page in
//    the partial_page heap, the linkage is moved to the new unused slab
//
{
	tree_remove(s);
	checktree(*s->parent);
	ASSERT(header_usedm4(s->header) == header_size(s->header)-4);
	CHECK(s->header |= 0xFF00000);			// illegal value for debug purposes
	page* p = page::pagefor(s);
	unsigned h = p->slabs[0].header;
	int slabix = s - &p->slabs[0];
	unsigned pagemap = header_pagemap(h);
	p->slabs[0].header = h | (0x100<<slabix);
	if (pagemap == 0)
	{	// page was full before, use this slab as link in empty heap
		tree_insert(s, &partial_page);
	}
	else
	{	// find the current empty-link slab
		slab* sl = &p->slabs[hibit(pagemap)];
		pagemap ^= (1<<slabix);
		if (pagemap == 0xf)
		{	// page is now empty so recycle page to os
			tree_remove(sl);
			freepage(p);
			return;
		}
		// ensure the free list link is in highest address slab in page
		if (s > sl)
		{	// replace current link with new one. Address-order tree so position stays the same
			slab** r = sl->parent;
			slab* c1 = sl->child1;
			slab* c2 = sl->child2;
			s->parent = r;
			s->child1 = c1;
			s->child2 = c2;
			*r = s;
			if (c1)
				c1->parent = &s->child1;
			if (c2)
				c2->parent = &s->child2;
		}
		CHECK(if (s < sl) s=sl);
	}
	ASSERT(header_pagemap(p->slabs[0].header) != 0);
	ASSERT(hibit(header_pagemap(p->slabs[0].header)) == unsigned(s - &p->slabs[0]));
}

void RNewAllocator::slab_init()
{
	slab_threshold=0;
	partial_page = 0;
	spare_page = 0;
	memset(&sizemap[0],0xff,sizeof(sizemap));
	memset(&slaballoc[0],0,sizeof(slaballoc));
}

void RNewAllocator::slab_config(unsigned slabbitmap)
{
	ASSERT((slabbitmap & ~okbits) == 0);
	ASSERT(maxslabsize <= 60);

	unsigned char ix = 0xff;
	unsigned bit = 1<<((maxslabsize>>2)-1);
	for (int sz = maxslabsize; sz >= 0; sz -= 4, bit >>= 1)
	{
		if (slabbitmap & bit)
		{
			if (ix == 0xff)
				slab_threshold=sz+1;
			ix = (sz>>2)-1;
		}
		sizemap[sz>>2] = ix;
	}
}

void* RNewAllocator::slab_allocate(slabset& ss)
//
// Allocate a cell from the given slabset
// Strategy:
// 1. Take the partially full slab at the top of the heap (lowest address).
// 2. If there is no such slab, allocate from a new slab
// 3. If the slab has a non-empty freelist, pop the cell from the front of the list and update the slab
// 4. Otherwise, if the slab is not full, return the cell at the end of the currently used region of
//    the slab, updating the slab
// 5. Otherwise, release the slab from the partial tree/heap, marking it as 'floating' and go back to
//    step 1
//
{
	for (;;)
	{
		slab *s = ss.partial;
		if (!s)
			break;
		unsigned h = s->header;
		unsigned free = h & 0xff;		// extract free cell positiong
		if (free)
		{
			ASSERT(((free<<2)-sizeof(slabhdr))%header_size(h) == 0);
			void* p = offset(s,free<<2);
			free = *(unsigned char*)p;	// get next pos in free list
			h += (h&0x3C000)<<6;		// update usedm4
			h &= ~0xff;
			h |= free;					// update freelist
			s->header = h;
			ASSERT(header_free(h) == 0 || ((header_free(h)<<2)-sizeof(slabhdr))%header_size(h) == 0);
			ASSERT(header_usedm4(h) <= 0x3F8u);
			ASSERT((header_usedm4(h)+4)%header_size(h) == 0);
			return p;
		}
		unsigned h2 = h + ((h&0x3C000)<<6);
		if (h2 < 0xfc00000)
		{
			ASSERT((header_usedm4(h2)+4)%header_size(h2) == 0);
			s->header = h2;
			return offset(s,(h>>18) + sizeof(unsigned) + sizeof(slabhdr));
		}
		h |= 0x80000000;				// mark the slab as full-floating
		s->header = h;
		tree_remove(s);
		checktree(ss.partial);
		// go back and try the next slab...
	}
	// no partial slabs found, so allocate from a new slab
	return allocnewslab(ss);
}

void RNewAllocator::slab_free(void* p)
//
// Free a cell from the slab allocator
// Strategy:
// 1. Find the containing slab (round down to nearest 1KB boundary)
// 2. Push the cell into the slab's freelist, and update the slab usage count
// 3. If this is the last allocated cell, free the slab to the main slab manager
// 4. If the slab was full-floating then insert the slab in it's respective partial tree
//
{
	ASSERT(lowbits(p,3)==0);
	slab* s = slab::slabfor(p);

	unsigned pos = lowbits(p, slabsize);
	unsigned h = s->header;
	ASSERT(header_usedm4(h) != 0x3fC);		// slab is empty already
	ASSERT((pos-sizeof(slabhdr))%header_size(h) == 0);
	*(unsigned char*)p = (unsigned char)h;
	h &= ~0xFF;
	h |= (pos>>2);
	unsigned size = h & 0x3C000;
	iTotalAllocSize -= size;
	if (int(h) >= 0)
	{
		h -= size<<6;
		if (int(h)>=0)
		{
			s->header = h;
			return;
		}
		freeslab(s);
		return;
	}
	h -= size<<6;
	h &= ~0x80000000;
	s->header = h;
	slabset& ss = slaballoc[(size>>14)-1];
	tree_insert(s,&ss.partial);
	checktree(ss.partial);
}

void* RNewAllocator::initnewslab(slabset& allocator, slab* s)
//
// initialise an empty slab for this allocator and return the fist cell
// pre-condition: the slabset has no partial slabs for allocation
//
{
	ASSERT(allocator.partial==0);
	TInt size = 4 + ((&allocator-&slaballoc[0])<<2);	// infer size from slab allocator address
	unsigned h = s->header & 0xF00;	// preserve pagemap only
	h |= (size<<12);					// set size
	h |= (size-4)<<18;					// set usedminus4 to one object minus 4
	s->header = h;
	allocator.partial = s;
	s->parent = &allocator.partial;
	s->child1 = s->child2 = 0;
	return offset(s,sizeof(slabhdr));
}

TAny* RNewAllocator::SetBrk(TInt32 aDelta)
{
	if (iFlags & EFixedSize)
		return MFAIL;

	if (aDelta < 0)
		{
		unmap(offset(iTop, aDelta), -aDelta);
		}
	else if (aDelta > 0)
		{
		if (!map(iTop, aDelta))
			return MFAIL;
		}
	void * p =iTop;
	iTop = offset(iTop, aDelta);
	return p;
}

void* RNewAllocator::map(void* p,unsigned sz)
//
// allocate pages in the chunk
// if p is NULL, find an allocate the required number of pages (which must lie in the lower half)
// otherwise commit the pages specified
//
{
ASSERT(p == floor(p, pagesize));
ASSERT(sz == ceiling(sz, pagesize));
ASSERT(sz > 0);

	if (iChunkSize + sz > iMaxLength)
		return 0;

	RChunk chunk;
	chunk.SetHandle(iChunkHandle);
	if (p)
	{
		TInt r = chunk.Commit(iOffset + ptrdiff(p, this),sz);
		if (r < 0)
			return 0;
		//ASSERT(p = offset(this, r - iOffset));
	}
	else
	{
		TInt r = chunk.Allocate(sz);
		if (r < 0)
			return 0;
		if (r > iOffset)
		{
			// can't allow page allocations in DL zone
			chunk.Decommit(r, sz);
			return 0;
		}
		p = offset(this, r - iOffset);
	}
	iChunkSize += sz;
#ifdef TRACING_HEAPS
	if(iChunkSize > iHighWaterMark)
		{
			iHighWaterMark = ceiling(iChunkSize,16*pagesize);


			RChunk chunk;
			chunk.SetHandle(iChunkHandle);
			TKName chunk_name;
			chunk.FullName(chunk_name);
			BTraceContextBig(BTrace::ETest1, 4, 44, chunk_name.Ptr(), chunk_name.Size());

			TUint32 traceData[6];
			traceData[0] = iChunkHandle;
			traceData[1] = iMinLength;
			traceData[2] = iMaxLength;
			traceData[3] = sz;
			traceData[4] = iChunkSize;
			traceData[5] = iHighWaterMark;
			BTraceContextN(BTrace::ETest1, 3, (TUint32)this, 33, traceData, sizeof(traceData));
		}
#endif
	if (iChunkSize >= slab_init_threshold)
	{	// set up slab system now that heap is large enough
		slab_config(slab_config_bits);
		slab_init_threshold = KMaxTUint;
	}
	return p;
}

void* RNewAllocator::remap(void* p,unsigned oldsz,unsigned sz)
{
	if (oldsz > sz)
		{	// shrink
		unmap(offset(p,sz), oldsz-sz);
		}
	else if (oldsz < sz)
		{	// grow, try and do this in place first
		if (!map(offset(p, oldsz), sz-oldsz))
			{
			// need to allocate-copy-free
			void* newp = map(0, sz);
			memcpy(newp, p, oldsz);
			unmap(p,oldsz);
			return newp;
			}
		}
	return p;
}

void RNewAllocator::unmap(void* p,unsigned sz)
{
	ASSERT(p == floor(p, pagesize));
	ASSERT(sz == ceiling(sz, pagesize));
	ASSERT(sz > 0);

	RChunk chunk;
	chunk.SetHandle(iChunkHandle);
	TInt r = chunk.Decommit(ptrdiff(p, offset(this,-iOffset)), sz);
	//TInt offset = (TUint8*)p-(TUint8*)chunk.Base();
	//TInt r = chunk.Decommit(offset,sz);

	ASSERT(r >= 0);
	iChunkSize -= sz;
}

void RNewAllocator::paged_init(unsigned pagepower)
	{
		if (pagepower == 0)
			pagepower = 31;
		else if (pagepower < minpagepower)
			pagepower = minpagepower;
		page_threshold = pagepower;
		for (int i=0;i<npagecells;++i)
		{
			pagelist[i].page = 0;
			pagelist[i].size = 0;
		}
	}

void* RNewAllocator::paged_allocate(unsigned size)
{
	unsigned nbytes = ceiling(size, pagesize);
	if (nbytes < size + cellalign)
	{	// not enough extra space for header and alignment, try and use cell list
		for (pagecell *c = pagelist,*e = c + npagecells;c < e;++c)
			if (c->page == 0)
			{
				void* p = map(0, nbytes);
				if (!p)
					return 0;
				c->page = p;
				c->size = nbytes;
				return p;
			}
	}
	// use a cell header
	nbytes = ceiling(size + cellalign, pagesize);
	void* p = map(0, nbytes);
	if (!p)
		return 0;
	*static_cast<unsigned*>(p) = nbytes;
	return offset(p, cellalign);
}

void* RNewAllocator::paged_reallocate(void* p, unsigned size)
{
	if (lowbits(p, pagesize) == 0)
	{	// continue using descriptor
		pagecell* c = paged_descriptor(p);
		unsigned nbytes = ceiling(size, pagesize);
		void* newp = remap(p, c->size, nbytes);
		if (!newp)
			return 0;
		c->page = newp;
		c->size = nbytes;
		return newp;
	}
	else
	{	// use a cell header
		ASSERT(lowbits(p,pagesize) == cellalign);
		p = offset(p,-int(cellalign));
		unsigned nbytes = ceiling(size + cellalign, pagesize);
		unsigned obytes = *static_cast<unsigned*>(p);
		void* newp = remap(p, obytes, nbytes);
		if (!newp)
			return 0;
		*static_cast<unsigned*>(newp) = nbytes;
		return offset(newp, cellalign);
	}
}

void RNewAllocator::paged_free(void* p)
{
	if (lowbits(p,pagesize) == 0)
	{	// check pagelist
		pagecell* c = paged_descriptor(p);

		iTotalAllocSize -= c->size;

		unmap(p, c->size);
		c->page = 0;
		c->size = 0;
	}
	else
	{	// check page header
		unsigned* page = static_cast<unsigned*>(offset(p,-int(cellalign)));
		unsigned size = *page;
		unmap(page,size);
	}
}

pagecell* RNewAllocator::paged_descriptor(const void* p) const
{
	ASSERT(lowbits(p,pagesize) == 0);
	// Double casting to keep the compiler happy. Seems to think we can trying to
	// change a non-const member (pagelist) in a const function
	pagecell* c = (pagecell*)((void*)pagelist);
	pagecell* e = c + npagecells;
	for (;;)
	{
		ASSERT(c!=e);
		if (c->page == p)
			return c;
		++c;
	}
}

RNewAllocator* RNewAllocator::FixedHeap(TAny* aBase, TInt aMaxLength, TInt aAlign, TBool aSingleThread)
/**
Creates a fixed length heap at a specified location.

On successful return from this function, aMaxLength bytes are committed by the chunk.
The heap cannot be extended.

@param aBase         A pointer to the location where the heap is to be constructed.
@param aMaxLength    The length of the heap. If the supplied value is less
                     than KMinHeapSize, it is discarded and the value KMinHeapSize
                     is used instead.
@param aAlign        The alignment of heap cells.
@param aSingleThread Indicates whether single threaded or not.

@return A pointer to the new heap, or NULL if the heap could not be created.

@panic USER 56 if aMaxLength is negative.
*/
//
// Force construction of the fixed memory.
//
	{

	__ASSERT_ALWAYS(aMaxLength>=0, ::Panic(ETHeapMaxLengthNegative));
	if (aMaxLength<KMinHeapSize)
		aMaxLength=KMinHeapSize;

	RNewAllocator* h = new(aBase) RNewAllocator(aMaxLength, aAlign, aSingleThread);

	if (!aSingleThread)
		{
		TInt r = h->iLock.CreateLocal();
		if (r!=KErrNone)
			return NULL;
		h->iHandles = (TInt*)&h->iLock;
		h->iHandleCount = 1;
		}
	return h;
	}

RNewAllocator* RNewAllocator::ChunkHeap(const TDesC* aName, TInt aMinLength, TInt aMaxLength, TInt aGrowBy, TInt aAlign, TBool aSingleThread)
/**
Creates a heap in a local or global chunk.

The chunk hosting the heap can be local or global.

A local chunk is one which is private to the process creating it and is not
intended for access by other user processes.
A global chunk is one which is visible to all processes.

The hosting chunk is local, if the pointer aName is NULL, otherwise
the hosting chunk is global and the descriptor *aName is assumed to contain
the name to be assigned to it.

Ownership of the host chunk is vested in the current process.

A minimum and a maximum size for the heap can be specified. On successful
return from this function, the size of the heap is at least aMinLength.
If subsequent requests for allocation of memory from the heap cannot be
satisfied by compressing the heap, the size of the heap is extended in
increments of aGrowBy until the request can be satisfied. Attempts to extend
the heap causes the size of the host chunk to be adjusted.

Note that the size of the heap cannot be adjusted by more than aMaxLength.

@param aName         If NULL, the function constructs a local chunk to host
                     the heap.
                     If not NULL, a pointer to a descriptor containing the name
                     to be assigned to the global chunk hosting the heap.
@param aMinLength    The minimum length of the heap.
@param aMaxLength    The maximum length to which the heap can grow.
                     If the supplied value is less than KMinHeapSize, then it
                     is discarded and the value KMinHeapSize used instead.
@param aGrowBy       The increments to the size of the host chunk. If a value is
                     not explicitly specified, the value KMinHeapGrowBy is taken
                     by default
@param aAlign        The alignment of heap cells.
@param aSingleThread Indicates whether single threaded or not.

@return A pointer to the new heap or NULL if the heap could not be created.

@panic USER 41 if aMinLength is greater than the supplied value of aMaxLength.
@panic USER 55 if aMinLength is negative.
@panic USER 56 if aMaxLength is negative.
*/
//
// Allocate a Chunk of the requested size and force construction.
//
	{

	__ASSERT_ALWAYS(aMinLength>=0, ::Panic(ETHeapMinLengthNegative));
	__ASSERT_ALWAYS(aMaxLength>=aMinLength, ::Panic(ETHeapCreateMaxLessThanMin));
	if (aMaxLength<KMinHeapSize)
		aMaxLength=KMinHeapSize;
	RChunk c;
	TInt r;
	if (aName)
		r = c.CreateDisconnectedGlobal(*aName, 0, 0, aMaxLength*2, aSingleThread ? EOwnerThread : EOwnerProcess);
	else
		r = c.CreateDisconnectedLocal(0, 0, aMaxLength*2, aSingleThread ? EOwnerThread : EOwnerProcess);
	if (r!=KErrNone)
		return NULL;

	RNewAllocator* h = ChunkHeap(c, aMinLength, aGrowBy, aMaxLength, aAlign, aSingleThread, UserHeap::EChunkHeapDuplicate);
	c.Close();
	return h;
	}

RNewAllocator* RNewAllocator::ChunkHeap(RChunk aChunk, TInt aMinLength, TInt aGrowBy, TInt aMaxLength, TInt aAlign, TBool aSingleThread, TUint32 aMode)
/**
Creates a heap in an existing chunk.

This function is intended to be used to create a heap in a user writable code
chunk as created by a call to RChunk::CreateLocalCode().
This type of heap can be used to hold code fragments from a JIT compiler.

The maximum length to which the heap can grow is the same as
the maximum size of the chunk.

@param aChunk        The chunk that will host the heap.
@param aMinLength    The minimum length of the heap.
@param aGrowBy       The increments to the size of the host chunk.
@param aMaxLength    The maximum length to which the heap can grow.
@param aAlign        The alignment of heap cells.
@param aSingleThread Indicates whether single threaded or not.
@param aMode         Flags controlling the reallocation. The only bit which has any
                     effect on reallocation is that defined by the enumeration
                     ENeverMove of the enum RAllocator::TReAllocMode.
                     If this is set, then any successful reallocation guarantees not
                     to have changed the start address of the cell.
                     By default, this parameter is zero.

@return A pointer to the new heap or NULL if the heap could not be created.
*/
//
// Construct a heap in an already existing chunk
//
	{

	return OffsetChunkHeap(aChunk, aMinLength, 0, aGrowBy, aMaxLength, aAlign, aSingleThread, aMode);
	}

RNewAllocator* RNewAllocator::OffsetChunkHeap(RChunk aChunk, TInt aMinLength, TInt aOffset, TInt aGrowBy, TInt aMaxLength, TInt aAlign, TBool aSingleThread, TUint32 aMode)
/**
Creates a heap in an existing chunk, offset from the beginning of the chunk.

This function is intended to be used to create a heap where a fixed amount of
additional data must be stored at a known location. The additional data can be
placed at the base address of the chunk, allowing it to be located without
depending on the internals of the heap structure.

The maximum length to which the heap can grow is the maximum size of the chunk,
minus the offset.

@param aChunk        The chunk that will host the heap.
@param aMinLength    The minimum length of the heap.
@param aOffset       The offset from the start of the chunk, to the start of the heap.
@param aGrowBy       The increments to the size of the host chunk.
@param aMaxLength    The maximum length to which the heap can grow.
@param aAlign        The alignment of heap cells.
@param aSingleThread Indicates whether single threaded or not.
@param aMode         Flags controlling the reallocation. The only bit which has any
                     effect on reallocation is that defined by the enumeration
                     ENeverMove of the enum RAllocator::TReAllocMode.
                     If this is set, then any successful reallocation guarantees not
                     to have changed the start address of the cell.
                     By default, this parameter is zero.

@return A pointer to the new heap or NULL if the heap could not be created.
*/
//
// Construct a heap in an already existing chunk
//
	{

	TInt page_size = malloc_getpagesize;
	if (!aAlign)
		aAlign = RNewAllocator::ECellAlignment;
	TInt maxLength = aChunk.MaxSize();
	TInt round_up = Max(aAlign, page_size);
	TInt min_cell = _ALIGN_UP(Max((TInt)RNewAllocator::EAllocCellSize, (TInt)RNewAllocator::EFreeCellSize), aAlign);
	aOffset = _ALIGN_UP(aOffset, 8);

#ifdef NO_RESERVE_MEMORY
#ifdef TRACING_HEAPS
	TKName chunk_name;
	aChunk.FullName(chunk_name);
	BTraceContextBig(BTrace::ETest1, 0xF, 0xFF, chunk_name.Ptr(), chunk_name.Size());

	TUint32 traceData[4];
	traceData[0] = aChunk.Handle();
	traceData[1] = aMinLength;
	traceData[2] = aMaxLength;
	traceData[3] = aAlign;
	BTraceContextN(BTrace::ETest1, 0xE, 0xEE, 0xEE, traceData, sizeof(traceData));
#endif
	//modifying the aMinLength because not all memory is the same in the new allocator. So it cannot reserve it properly
	if( aMinLength<aMaxLength)
		aMinLength = 0;
#endif

	if (aMaxLength && aMaxLength+aOffset<maxLength)
		maxLength = _ALIGN_UP(aMaxLength+aOffset, round_up);
	__ASSERT_ALWAYS(aMinLength>=0, ::Panic(ETHeapMinLengthNegative));
	__ASSERT_ALWAYS(maxLength>=aMinLength, ::Panic(ETHeapCreateMaxLessThanMin));
	aMinLength = _ALIGN_UP(Max(aMinLength, (TInt)sizeof(RNewAllocator) + min_cell) + aOffset, round_up);

	// the new allocator uses a disconnected chunk so must commit the initial allocation
	// with Commit() instead of Adjust()
	//	TInt r=aChunk.Adjust(aMinLength);
	//TInt r = aChunk.Commit(aOffset, aMinLength);

	aOffset = maxLength;
	//TInt MORE_CORE_OFFSET = maxLength/2;
	//TInt r = aChunk.Commit(MORE_CORE_OFFSET, aMinLength);
	TInt r = aChunk.Commit(aOffset, aMinLength);

	if (r!=KErrNone)
		return NULL;

	RNewAllocator* h = new (aChunk.Base() + aOffset) RNewAllocator(aChunk.Handle(), aOffset, aMinLength, maxLength, aGrowBy, aAlign, aSingleThread);
	//RNewAllocator* h = new (aChunk.Base() + MORE_CORE_OFFSET) RNewAllocator(aChunk.Handle(), aOffset, aMinLength, maxLength, aGrowBy, aAlign, aSingleThread);

	TBool duplicateLock = EFalse;
	if (!aSingleThread)
		{
		duplicateLock = aMode & UserHeap::EChunkHeapSwitchTo;
		if(h->iLock.CreateLocal(duplicateLock ? EOwnerThread : EOwnerProcess)!=KErrNone)
			{
			h->iChunkHandle = 0;
			return NULL;
			}
		}

	if (aMode & UserHeap::EChunkHeapSwitchTo)
		User::SwitchHeap(h);

	h->iHandles = &h->iChunkHandle;
	if (!aSingleThread)
		{
		// now change the thread-relative chunk/semaphore handles into process-relative handles
		h->iHandleCount = 2;
		if(duplicateLock)
			{
			RHandleBase s = h->iLock;
			r = h->iLock.Duplicate(RThread());
			s.Close();
			}
		if (r==KErrNone && (aMode & UserHeap::EChunkHeapDuplicate))
			{
			r = ((RChunk*)&h->iChunkHandle)->Duplicate(RThread());
			if (r!=KErrNone)
				h->iLock.Close(), h->iChunkHandle=0;
			}
		}
	else
		{
		h->iHandleCount = 1;
		if (aMode & UserHeap::EChunkHeapDuplicate)
			r = ((RChunk*)&h->iChunkHandle)->Duplicate(RThread(), EOwnerThread);
		}

	// return the heap address
	return (r==KErrNone) ? h : NULL;
	}


#define UserTestDebugMaskBit(bit) (TBool)(UserSvr::DebugMask(bit>>5) & (1<<(bit&31)))

#ifndef NO_NAMED_LOCAL_CHUNKS
//this class requires Symbian^3 for ElocalNamed

// Hack to get access to TChunkCreateInfo internals outside of the kernel
class TFakeChunkCreateInfo: public TChunkCreateInfo
	{
public:
	 void SetThreadNewAllocator(TInt aInitialSize, TInt aMaxSize, const TDesC& aName)
	 	{
		iType = TChunkCreate::ENormal | TChunkCreate::EDisconnected | TChunkCreate::EData;
		iMaxSize = aMaxSize * 2;

	 	iInitialBottom = 0;
	 	iInitialTop = aInitialSize;
	 	iAttributes = TChunkCreate::ELocalNamed;
	 	iName = &aName;
	 	iOwnerType = EOwnerThread;
	 	}
	};
#endif

#ifndef NO_NAMED_LOCAL_CHUNKS
_LIT(KLitDollarHeap,"$HEAP");
#endif
TInt RNewAllocator::CreateThreadHeap(SStdEpocThreadCreateInfo& aInfo, RNewAllocator*& aHeap, TInt aAlign, TBool aSingleThread)
/**
@internalComponent
*/
//
// Create a user-side heap
//
	{
	TInt page_size = malloc_getpagesize;
	TInt minLength = _ALIGN_UP(aInfo.iHeapInitialSize, page_size);
	TInt maxLength = Max(aInfo.iHeapMaxSize, minLength);
#ifdef TRACING_ALLOCS
	if (UserTestDebugMaskBit(96)) // 96 == KUSERHEAPTRACE in nk_trace.h
		aInfo.iFlags |= ETraceHeapAllocs;
#endif
	// Create the thread's heap chunk.
	RChunk c;
#ifndef NO_NAMED_LOCAL_CHUNKS
	TFakeChunkCreateInfo createInfo;
	createInfo.SetThreadNewAllocator(0, maxLength, KLitDollarHeap());	// Initialise with no memory committed.
	TInt r = c.Create(createInfo);
#else
	TInt r = c.CreateDisconnectedLocal(0, 0, maxLength * 2);
#endif
	if (r!=KErrNone)
		return r;
	aHeap = ChunkHeap(c, minLength, page_size, maxLength, aAlign, aSingleThread, UserHeap::EChunkHeapSwitchTo|UserHeap::EChunkHeapDuplicate);
	c.Close();
	if (!aHeap)
		return KErrNoMemory;
#ifdef TRACING_ALLOCS
	if (aInfo.iFlags & ETraceHeapAllocs)
		{
		aHeap->iFlags |= RAllocator::ETraceAllocs;
		BTraceContext8(BTrace::EHeap, BTrace::EHeapCreate,(TUint32)aHeap, RNewAllocator::EAllocCellSize);
		TInt handle = aHeap->ChunkHandle();
		TInt chunkId = ((RHandleBase&)handle).BTraceId();
		BTraceContext8(BTrace::EHeap, BTrace::EHeapChunkCreate, (TUint32)aHeap, chunkId);
		}
#endif
	return KErrNone;
	}

/*
 * \internal
 * Called from the qtmain.lib application wrapper.
 * Create a new heap as requested, but use the new allocator
 */
Q_CORE_EXPORT TInt qt_symbian_SetupThreadHeap(TBool /*aNotFirst*/, SStdEpocThreadCreateInfo& aInfo)
    {
    TInt r = KErrNone;
    if (!aInfo.iAllocator && aInfo.iHeapInitialSize>0)
        {
        // new heap required
        RNewAllocator* pH = NULL;
        r = RNewAllocator::CreateThreadHeap(aInfo, pH);
        }
    else if (aInfo.iAllocator)
        {
        // sharing a heap
        RAllocator* pA = aInfo.iAllocator;
        pA->Open();
        User::SwitchAllocator(pA);
        }
    return r;
    }

#else
/*
 * \internal
 * Called from the qtmain.lib application wrapper.
 * Create a new heap as requested, using the default system allocator
 */
Q_CORE_EXPORT TInt qt_symbian_SetupThreadHeap(TBool aNotFirst, SStdEpocThreadCreateInfo& aInfo)
{
    return UserHeap::SetupThreadHeap(aNotFirst, aInfo);
}
#endif

#ifndef __WINS__
#pragma pop
#endif