summaryrefslogtreecommitdiffstats
path: root/memwatch.c
blob: 2e7298a7d16f7627b5b559c93d54da3b1ed907ca (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
/*
** MEMWATCH.C
** Nonintrusive ANSI C memory leak / overwrite detection
** Copyright (C) 1992-2003 Johan Lindh
** All rights reserved.
** Version 2.71

	This file is part of MEMWATCH.

    MEMWATCH is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    MEMWATCH is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MEMWATCH; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

**
** 920810 JLI   [1.00]
** 920830 JLI   [1.10 double-free detection]
** 920912 JLI   [1.15 mwPuts, mwGrab/Drop, mwLimit]
** 921022 JLI   [1.20 ASSERT and VERIFY]
** 921105 JLI   [1.30 C++ support and TRACE]
** 921116 JLI   [1.40 mwSetOutFunc]
** 930215 JLI   [1.50 modified ASSERT/VERIFY]
** 930327 JLI   [1.51 better auto-init & PC-lint support]
** 930506 JLI   [1.55 MemWatch class, improved C++ support]
** 930507 JLI   [1.60 mwTest & CHECK()]
** 930809 JLI   [1.65 Abort/Retry/Ignore]
** 930820 JLI   [1.70 data dump when unfreed]
** 931016 JLI   [1.72 modified C++ new/delete handling]
** 931108 JLI   [1.77 mwSetAssertAction() & some small changes]
** 940110 JLI   [1.80 no-mans-land alloc/checking]
** 940328 JLI   [2.00 version 2.0 rewrite]
**              Improved NML (no-mans-land) support.
**              Improved performance (especially for free()ing!).
**              Support for 'read-only' buffers (checksums)
**              ^^ NOTE: I never did this... maybe I should?
**              FBI (free'd block info) tagged before freed blocks
**              Exporting of the mwCounter variable
**              mwBreakOut() localizes debugger support
**              Allocation statistics (global, per-module, per-line)
**              Self-repair ability with relinking
** 950913 JLI   [2.10 improved garbage handling]
** 951201 JLI   [2.11 improved auto-free in emergencies]
** 960125 JLI   [X.01 implemented auto-checking using mwAutoCheck()]
** 960514 JLI   [2.12 undefining of existing macros]
** 960515 JLI   [2.13 possibility to use default new() & delete()]
** 960516 JLI   [2.20 suppression of file flushing on unfreed msgs]
** 960516 JLI   [2.21 better support for using MEMWATCH with DLL's]
** 960710 JLI   [X.02 multiple logs and mwFlushNow()]
** 960801 JLI   [2.22 merged X.01 version with current]
** 960805 JLI   [2.30 mwIsXXXXAddr() to avoid unneeded GP's]
** 960805 JLI   [2.31 merged X.02 version with current]
** 961002 JLI   [2.32 support for realloc() + fixed STDERR bug]
** 961222 JLI   [2.40 added mwMark() & mwUnmark()]
** 970101 JLI   [2.41 added over/underflow checking after failed ASSERT/VERIFY]
** 970113 JLI   [2.42 added support for PC-Lint 7.00g]
** 970207 JLI   [2.43 added support for strdup()]
** 970209 JLI   [2.44 changed default filename to lowercase]
** 970405 JLI   [2.45 fixed bug related with atexit() and some C++ compilers]
** 970723 JLI   [2.46 added MW_ARI_NULLREAD flag]
** 970813 JLI   [2.47 stabilized marker handling]
** 980317 JLI   [2.48 ripped out C++ support; wasn't working good anyway]
** 980318 JLI   [2.50 improved self-repair facilities & SIGSEGV support]
** 980417 JLI	[2.51 more checks for invalid addresses]
** 980512 JLI	[2.52 moved MW_ARI_NULLREAD to occur before aborting]
** 990112 JLI	[2.53 added check for empty heap to mwIsOwned]
** 990217 JLI	[2.55 improved the emergency repairs diagnostics and NML]
** 990224 JLI	[2.56 changed ordering of members in structures]
** 990303 JLI	[2.57 first maybe-fixit-for-hpux test]
** 990516 JLI	[2.58 added 'static' to the definition of mwAutoInit]
** 990517 JLI	[2.59 fixed some high-sensitivity warnings]
** 990610 JLI	[2.60 fixed some more high-sensitivity warnings]
** 990715 JLI	[2.61 changed TRACE/ASSERT/VERIFY macro names]
** 991001 JLI	[2.62 added CHECK_BUFFER() and mwTestBuffer()]
** 991007 JLI	[2.63 first shot at a 64-bit compatible version]
** 991009 JLI	[2.64 undef's strdup() if defined, mwStrdup made const]
** 000704 JLI	[2.65 added some more detection for 64-bits]
** 010502 JLI   [2.66 incorporated some user fixes]
**              [mwRelink() could print out garbage pointer (thanks mac@phobos.ca)]
**				[added array destructor for C++ (thanks rdasilva@connecttel.com)]
**				[added mutex support (thanks rdasilva@connecttel.com)]
** 010531 JLI	[2.67 fix: mwMutexXXX() was declared even if MW_HAVE_MUTEX was not defined]
** 010619 JLI	[2.68 fix: mwRealloc() could leave the mutex locked]
** 020918 JLI	[2.69 changed to GPL, added C++ array allocation by Howard Cohen]
** 030212 JLI	[2.70 mwMalloc() bug for very large allocations (4GB on 32bits)]
** 030520 JLI	[2.71 added ULONG_LONG_MAX as a 64-bit detector (thanks Sami Salonen)]
*/

#define __MEMWATCH_C 1

#ifdef MW_NOCPP
#define MEMWATCH_NOCPP
#endif
#ifdef MW_STDIO
#define MEMWATCH_STDIO
#endif

/***********************************************************************
** Include files
***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include <time.h>
#include <limits.h>
#include "memwatch.h"

#ifndef toupper
#include <ctype.h>
#endif

#if defined(WIN32) || defined(__WIN32__)
#define MW_HAVE_MUTEX 1
#include <windows.h>
#endif

#if defined(MW_PTHREADS) || defined(HAVE_PTHREAD_H)
#define MW_HAVE_MUTEX 1
#include <pthread.h>
#endif

/***********************************************************************
** Defines & other weird stuff
***********************************************************************/

/*lint -save -e767 */
#define VERSION     "2.71"         /* the current version number */
#define CHKVAL(mw)  (0xFE0180L^(long)mw->count^(long)mw->size^(long)mw->line)
#define FLUSH()     mwFlush()
#define TESTS(f,l)  if(mwTestAlways) (void)mwTestNow(f,l,1)
#define PRECHK      0x01234567L
#define POSTCHK     0x76543210L
#define mwBUFFER_TO_MW(p) ( (mwData*) (void*) ( ((char*)p)-mwDataSize-mwOverflowZoneSize ) )
/*lint -restore */

#define MW_NML      0x0001

#ifdef _MSC_VER
#define COMMIT "c"  /* Microsoft C requires the 'c' to perform as desired */
#else
#define COMMIT ""   /* Normal ANSI */
#endif /* _MSC_VER */

#ifdef __cplusplus
#define CPPTEXT "++"
#else
#define CPPTEXT ""
#endif /* __cplusplus */

#ifdef MEMWATCH_STDIO
#define mwSTDERR stderr
#else
#define mwSTDERR mwLog
#endif

#ifdef MW_HAVE_MUTEX
#define MW_MUTEX_INIT()		mwMutexInit()
#define MW_MUTEX_TERM()		mwMutexTerm()
#define MW_MUTEX_LOCK()		mwMutexLock()
#define MW_MUTEX_UNLOCK()	mwMutexUnlock()
#else
#define MW_MUTEX_INIT()
#define MW_MUTEX_TERM()
#define MW_MUTEX_LOCK()
#define MW_MUTEX_UNLOCK()
#endif

/***********************************************************************
** If you really, really know what you're doing,
** you can predefine these things yourself.
***********************************************************************/

#ifndef mwBYTE_DEFINED
# if CHAR_BIT != 8
#  error need CHAR_BIT to be 8!
# else
typedef unsigned char mwBYTE;
#  define mwBYTE_DEFINED 1
# endif
#endif

#if defined(ULONGLONG_MAX) || defined(ULLONG_MAX) || defined(_UI64_MAX) || defined(ULONG_LONG_MAX)
# define mw64BIT 1
# define mwROUNDALLOC_DEFAULT 8
#else
# if UINT_MAX <= 0xFFFFUL
#  define mw16BIT 1
#  define mwROUNDALLOC_DEFAULT	2
# else
#  if ULONG_MAX > 0xFFFFFFFFUL
#   define mw64BIT 1
#   define mwROUNDALLOC_DEFAULT	8
#  else
#   define mw32BIT 1
#   define mwROUNDALLOC_DEFAULT	4
#  endif
# endif
#endif

/* mwROUNDALLOC is the number of bytes to */
/* round up to, to ensure that the end of */
/* the buffer is suitable for storage of */
/* any kind of object */
#ifndef mwROUNDALLOC
# define mwROUNDALLOC mwROUNDALLOC_DEFAULT
#endif

#ifndef mwDWORD_DEFINED
#if ULONG_MAX == 0xFFFFFFFFUL
typedef unsigned long mwDWORD;
#define mwDWORD_DEFINED "unsigned long"
#endif
#endif

#ifndef mwDWORD_DEFINED
#if UINT_MAX == 0xFFFFFFFFUL
typedef unsigned int mwDWORD;
#define mwDWORD_DEFINED "unsigned int"
#endif
#endif

#ifndef mwDWORD_DEFINED
#if USHRT_MAX == 0xFFFFFFFFUL
typedef unsigned short mwDWORD;
#define mwDWORD_DEFINED "unsigned short"
#endif
#endif

#ifndef mwBYTE_DEFINED
#error "can't find out the correct type for a 8 bit scalar"
#endif

#ifndef mwDWORD_DEFINED
#error "can't find out the correct type for a 32 bit scalar"
#endif

/***********************************************************************
** Typedefs & structures
***********************************************************************/

/* main data holding area, precedes actual allocation */
typedef struct mwData_ mwData;
struct mwData_ {
    mwData*     prev;   /* previous allocation in chain */
    mwData*     next;   /* next allocation in chain */
    const char* file;   /* file name where allocated */
    long        count;  /* action count */
    long        check;  /* integrity check value */
#if 0
    long        crc;    /* data crc value */
#endif
    size_t      size;   /* size of allocation */
    int         line;   /* line number where allocated */
    unsigned    flag;   /* flag word */
    };

/* statistics structure */
typedef struct mwStat_ mwStat;
struct mwStat_ {
    mwStat*     next;   /* next statistic buffer */
    const char* file;
    long        total;  /* total bytes allocated */
    long        num;    /* total number of allocations */
    long        max;    /* max allocated at one time */
    long        curr;   /* current allocations */
    int         line;
    };

/* grabbing structure, 1K in size */
typedef struct mwGrabData_ mwGrabData;
struct mwGrabData_ {
    mwGrabData* next;
    int         type;
    char        blob[ 1024 - sizeof(mwGrabData*) - sizeof(int) ];
    };

typedef struct mwMarker_ mwMarker;
struct mwMarker_ {
    void *host;
    char *text;
    mwMarker *next;
    int level;
    };

#if defined(WIN32) || defined(__WIN32__)
typedef HANDLE          mwMutex;
#endif

#if defined(MW_PTHREADS) || defined(HAVE_PTHREAD_H)
typedef pthread_mutex_t mwMutex;
#endif

/***********************************************************************
** Static variables
***********************************************************************/

static int      mwInited =      0;
static int      mwInfoWritten = 0;
static int      mwUseAtexit =   0;
static FILE*    mwLog =         NULL;
static int      mwFlushing =    0;
static int      mwStatLevel =   MW_STAT_DEFAULT;
static int      mwNML =         MW_NML_DEFAULT;
static int      mwFBI =         0;
static long     mwAllocLimit =  0L;
static int      mwUseLimit =    0;

static long     mwNumCurAlloc = 0L;
static mwData*  mwHead = 		NULL;
static mwData*  mwTail = 		NULL;
static int		mwDataSize =	0;
static unsigned char mwOverflowZoneTemplate[] = "mEmwAtch";
static int		mwOverflowZoneSize = mwROUNDALLOC;

static void     (*mwOutFunction)(int) = NULL;
static int      (*mwAriFunction)(const char*) = NULL;
static int      mwAriAction = MW_ARI_ABORT;

static char     mwPrintBuf[MW_TRACE_BUFFER+8];

static unsigned long mwCounter = 0L;
static long     mwErrors =      0L;

static int      mwTestFlags =   0;
static int      mwTestAlways =  0;

static FILE*    mwLogB1 =       NULL;
static int      mwFlushingB1 =  0;

static mwStat*  mwStatList = NULL;
static long     mwStatTotAlloc = 0L;
static long     mwStatMaxAlloc = 0L;
static long     mwStatNumAlloc = 0L;
static long     mwStatCurAlloc = 0L;
static long     mwNmlNumAlloc = 0L;
static long     mwNmlCurAlloc = 0L;

static mwGrabData* mwGrabList = NULL;
static long     mwGrabSize = 0L;

static void *   mwLastFree[MW_FREE_LIST];
static const char *mwLFfile[MW_FREE_LIST];
static int      mwLFline[MW_FREE_LIST];
static int      mwLFcur = 0;

static mwMarker* mwFirstMark = NULL;

static FILE*    mwLogB2 =       NULL;
static int      mwFlushingB2 =  0;

#ifdef MW_HAVE_MUTEX
static mwMutex	mwGlobalMutex;
#endif

/***********************************************************************
** Static function declarations
***********************************************************************/

static void     mwAutoInit( void );
static FILE*    mwLogR( void );
static void     mwLogW( FILE* );
static int      mwFlushR( void );
static void     mwFlushW( int );
static void     mwFlush( void );
static void     mwIncErr( void );
static void     mwUnlink( mwData*, const char* file, int line );
static int      mwRelink( mwData*, const char* file, int line );
static int      mwIsHeapOK( mwData *mw );
static int      mwIsOwned( mwData* mw, const char* file, int line );
static int      mwTestBuf( mwData* mw, const char* file, int line );
static void     mwDefaultOutFunc( int );
static void     mwWrite( const char* format, ... );
static void     mwLogFile( const char* name );
static size_t   mwFreeUp( size_t, int );
static const void *mwTestMem( const void *, unsigned, int );
static int      mwStrCmpI( const char *s1, const char *s2 );
static int      mwTestNow( const char *file, int line, int always_invoked );
static void     mwDropAll( void );
static const char *mwGrabType( int type );
static unsigned mwGrab_( unsigned kb, int type, int silent );
static unsigned mwDrop_( unsigned kb, int type, int silent );
static int      mwARI( const char* text );
static void     mwStatReport( void );
static mwStat*  mwStatGet( const char*, int, int );
static void     mwStatAlloc( size_t, const char*, int );
static void     mwStatFree( size_t, const char*, int );
static int		mwCheckOF( const void * p );
static void		mwWriteOF( void * p );
static char		mwDummy( char c );
#ifdef MW_HAVE_MUTEX
static void		mwMutexInit( void );
static void		mwMutexTerm( void );
static void		mwMutexLock( void );
static void		mwMutexUnlock( void );
#endif

/***********************************************************************
** System functions
***********************************************************************/

void mwInit( void ) {
    time_t tid;

    if( mwInited++ > 0 ) return;

	MW_MUTEX_INIT();

    /* start a log if none is running */
    if( mwLogR() == NULL ) mwLogFile( "memwatch.log" );
    if( mwLogR() == NULL ) {
        int i;
        char buf[32];
        /* oops, could not open it! */
        /* probably because it's already open */
        /* so we try some other names */
        for( i=1; i<100; i++ ) {
            sprintf( buf, "memwat%02d.log", i );
            mwLogFile( buf );
            if( mwLogR() != NULL ) break;
            }
        }

    /* initialize the statistics */
    mwStatList = NULL;
    mwStatTotAlloc = 0L;
    mwStatCurAlloc = 0L;
    mwStatMaxAlloc = 0L;
    mwStatNumAlloc = 0L;
	mwNmlCurAlloc = 0L;
	mwNmlNumAlloc = 0L;

	/* calculate the buffer size to use for a mwData */
	mwDataSize = sizeof(mwData);
	while( mwDataSize % mwROUNDALLOC ) mwDataSize ++;

    /* write informational header if needed */
    if( !mwInfoWritten ) {
        mwInfoWritten = 1;
        (void) time( &tid );
        mwWrite(
            "\n============="
            " MEMWATCH " VERSION " Copyright (C) 1992-1999 Johan Lindh "
            "=============\n");
        mwWrite( "\nStarted at %s\n", ctime( &tid ) );

/**************************************************************** Generic */
		mwWrite( "Modes: " );
#ifdef mwNew
        mwWrite( "C++ " );
#endif /* mwNew */
#ifdef __STDC__
        mwWrite( "__STDC__ " );
#endif /* __STDC__ */
#ifdef mw16BIT
		mwWrite( "16-bit " );
#endif
#ifdef mw32BIT
		mwWrite( "32-bit " );
#endif
#ifdef mw64BIT
		mwWrite( "64-bit " );
#endif
		mwWrite( "mwDWORD==(" mwDWORD_DEFINED ")\n" );
		mwWrite( "mwROUNDALLOC==%d sizeof(mwData)==%d mwDataSize==%d\n",
			mwROUNDALLOC, sizeof(mwData), mwDataSize );
/**************************************************************** Generic */

/************************************************************ Microsoft C */
#ifdef _MSC_VER
        mwWrite( "Compiled using Microsoft C" CPPTEXT
            " %d.%02d\n", _MSC_VER / 100, _MSC_VER % 100 );
#endif /* _MSC_VER */
/************************************************************ Microsoft C */

/************************************************************** Borland C */
#ifdef __BORLANDC__
        mwWrite( "Compiled using Borland C"
#ifdef __cplusplus
            "++ %d.%01d\n", __BCPLUSPLUS__/0x100, (__BCPLUSPLUS__%0x100)/0x10 );
#else
            " %d.%01d\n", __BORLANDC__/0x100, (__BORLANDC__%0x100)/0x10 );
#endif /* __cplusplus */
#endif /* __BORLANDC__ */
/************************************************************** Borland C */

/************************************************************** Watcom C */
#ifdef __WATCOMC__
        mwWrite( "Compiled using Watcom C %d.%02d ",
            __WATCOMC__/100, __WATCOMC__%100 );
#ifdef __FLAT__
        mwWrite( "(32-bit flat model)" );
#endif /* __FLAT__ */
        mwWrite( "\n" );
#endif /* __WATCOMC__ */
/************************************************************** Watcom C */

        mwWrite( "\n" );
        FLUSH();
        }

    if( mwUseAtexit ) (void) atexit( mwAbort );
    return;
    }

void mwAbort( void ) {
    mwData *mw;
    mwMarker *mrk;
    char *data;
    time_t tid;
    int c, i, j;
	int errors;

    tid = time( NULL );
    mwWrite( "\nStopped at %s\n", ctime( &tid) );

    if( !mwInited )
        mwWrite( "internal: mwAbort(): MEMWATCH not initialized!\n" );

    /* release the grab list */
    mwDropAll();

    /* report mwMarked items */
    while( mwFirstMark ) {
        mrk = mwFirstMark->next;
        mwWrite( "mark: %p: %s\n", mwFirstMark->host, mwFirstMark->text );
        free( mwFirstMark->text );
        free( mwFirstMark );
        mwFirstMark = mrk;
        mwErrors ++;
        }

    /* release all still allocated memory */
	errors = 0;
    while( mwHead != NULL && errors < 3 ) {
		if( !mwIsOwned(mwHead, __FILE__, __LINE__ ) ) {
			if( errors < 3 )
			{
				errors ++;
				mwWrite( "internal: NML/unfreed scan restarting\n" );
				FLUSH();
				mwHead = mwHead;
				continue;
			}
			mwWrite( "internal: NML/unfreed scan aborted, heap too damaged\n" );
			FLUSH();
			break;
			}
        mwFlushW(0);
        if( !(mwHead->flag & MW_NML) ) {
            mwErrors++;
            data = ((char*)mwHead)+mwDataSize;
            mwWrite( "unfreed: <%ld> %s(%d), %ld bytes at %p ",
                mwHead->count, mwHead->file, mwHead->line, (long)mwHead->size, data+mwOverflowZoneSize );
            if( mwCheckOF( data ) ) {
                mwWrite( "[underflowed] ");
                FLUSH();
                }
            if( mwCheckOF( (data+mwOverflowZoneSize+mwHead->size) ) ) {
                mwWrite( "[overflowed] ");
                FLUSH();
                }
            mwWrite( " \t{" );
            j = 16; if( mwHead->size < 16 ) j = (int) mwHead->size;
            for( i=0;i<16;i++ ) {
                if( i<j ) mwWrite( "%02X ",
                    (unsigned char) *(data+mwOverflowZoneSize+i) );
                else mwWrite( ".. " );
                }
            for( i=0;i<j;i++ ) {
                c = *(data+mwOverflowZoneSize+i);
                if( c < 32 || c > 126 ) c = '.';
                mwWrite( "%c", c );
                }
            mwWrite( "}\n" );
			mw = mwHead;
			mwUnlink( mw, __FILE__, __LINE__ );
            free( mw );
            }
        else {
            data = ((char*)mwHead) + mwDataSize + mwOverflowZoneSize;
            if( mwTestMem( data, mwHead->size, MW_VAL_NML ) ) {
                mwErrors++;
                mwWrite( "wild pointer: <%ld> NoMansLand %p alloc'd at %s(%d)\n",
                    mwHead->count, data + mwOverflowZoneSize, mwHead->file, mwHead->line );
                FLUSH();
                }
			mwNmlNumAlloc --;
			mwNmlCurAlloc -= mwHead->size;
			mw = mwHead;
			mwUnlink( mw, __FILE__, __LINE__ );
            free( mw );
            }
        }

	if( mwNmlNumAlloc ) mwWrite("internal: NoMansLand block counter %ld, not zero\n", mwNmlNumAlloc );
	if( mwNmlCurAlloc ) mwWrite("internal: NoMansLand byte counter %ld, not zero\n", mwNmlCurAlloc );

    /* report statistics */
    mwStatReport();
    FLUSH();

    mwInited = 0;
    mwHead = mwTail = NULL;
    if( mwErrors )
        fprintf(mwSTDERR,"MEMWATCH detected %ld anomalies\n",mwErrors);
    mwLogFile( NULL );
    mwErrors = 0;

    MW_MUTEX_TERM();

    }

void mwTerm( void ) {
    if( mwInited == 1 )
    {
        mwAbort();
        return;
    }
    if( !mwInited )
        mwWrite("internal: mwTerm(): MEMWATCH has not been started!\n");
    else
        mwInited --;
    }

void mwStatistics( int level )
{
    mwAutoInit();
    if( level<0 ) level=0;
    if( mwStatLevel != level )
    {
		mwWrite( "statistics: now collecting on a %s basis\n",
			level<1?"global":(level<2?"module":"line") );
	    mwStatLevel = level;
	}
}

void mwAutoCheck( int onoff ) {
    mwAutoInit();
    mwTestAlways = onoff;
    if( onoff ) mwTestFlags = MW_TEST_ALL;
    }

void mwSetOutFunc( void (*func)(int) ) {
    mwAutoInit();
    mwOutFunction = func;
    }

static void mwWriteOF( void *p )
{
	int i;
	unsigned char *ptr;
	ptr = (unsigned char*) p;
	for( i=0; i<mwOverflowZoneSize; i++ )
	{
		*(ptr+i) = mwOverflowZoneTemplate[i%8];
	}
	return;
}

static int mwCheckOF( const void *p )
{
	int i;
	const unsigned char *ptr;
	ptr = (const unsigned char *) p;
	for( i=0; i<mwOverflowZoneSize; i++ )
	{
		if( *(ptr+i) != mwOverflowZoneTemplate[i%8] )
			return 1; /* errors found */
	}
	return 0; /* no errors */
}

int mwTest( const char *file, int line, int items ) {
    mwAutoInit();
    mwTestFlags = items;
    return mwTestNow( file, line, 0 );
    }

/*
** Returns zero if there are no errors.
** Returns nonzero if there are errors.
*/
int mwTestBuffer( const char *file, int line, void *p ) {
    mwData* mw;

    mwAutoInit();

    /* do the quick ownership test */
    mw = (mwData*) mwBUFFER_TO_MW( p );

    if( mwIsOwned( mw, file, line ) ) {
        return mwTestBuf( mw, file, line );
		}
	return 1;
	}

void mwBreakOut( const char* cause ) {
    fprintf(mwSTDERR, "breakout: %s\n", cause);
    mwWrite("breakout: %s\n", cause );
    return;
    }

/*
** 981217 JLI: is it possible that ->next is not always set?
*/
void * mwMark( void *p, const char *desc, const char *file, unsigned line ) {
    mwMarker *mrk;
    unsigned n, isnew;
    char *buf;
    int tot, oflow = 0;
    char wherebuf[128];

    mwAutoInit();
    TESTS(NULL,0);

    if( desc == NULL ) desc = "unknown";
    if( file == NULL ) file = "unknown";

    tot = sprintf( wherebuf, "%.48s called from %s(%d)", desc, file, line );
    if( tot >= (int)sizeof(wherebuf) ) { wherebuf[sizeof(wherebuf)-1] = 0; oflow = 1; }

    if( p == NULL ) {
        mwWrite("mark: %s(%d), no mark for NULL:'%s' may be set\n", file, line, desc );
        return p;
        }

	if( mwFirstMark != NULL && !mwIsReadAddr( mwFirstMark, sizeof( mwMarker ) ) )
	{
		mwWrite("mark: %s(%d), mwFirstMark (%p) is trashed, can't mark for %s\n",
			file, line, mwFirstMark, desc );
		return p;
	}

    for( mrk=mwFirstMark; mrk; mrk=mrk->next )
	{
		if( mrk->next != NULL && !mwIsReadAddr( mrk->next, sizeof( mwMarker ) ) )
		{
			mwWrite("mark: %s(%d), mark(%p)->next(%p) is trashed, can't mark for %s\n",
				file, line, mrk, mrk->next, desc );
			return p;
		}
		if( mrk->host == p ) break;
	}

    if( mrk == NULL ) {
        isnew = 1;
        mrk = (mwMarker*) malloc( sizeof( mwMarker ) );
        if( mrk == NULL ) {
            mwWrite("mark: %s(%d), no mark for %p:'%s', out of memory\n", file, line, p, desc );
            return p;
            }
		mrk->next = NULL;
        n = 0;
        }
    else {
        isnew = 0;
        n = strlen( mrk->text );
        }

    n += strlen( wherebuf );
    buf = (char*) malloc( n+3 );
    if( buf == NULL ) {
        if( isnew ) free( mrk );
        mwWrite("mark: %s(%d), no mark for %p:'%s', out of memory\n", file, line, p, desc );
        return p;
        }

    if( isnew ) {
        memcpy( buf, wherebuf, n+1 );
        mrk->next = mwFirstMark;
        mrk->host = p;
        mrk->text = buf;
        mrk->level = 1;
        mwFirstMark = mrk;
        }
    else {
        strcpy( buf, mrk->text );
        strcat( buf, ", " );
        strcat( buf, wherebuf );
        free( mrk->text );
        mrk->text = buf;
        mrk->level ++;
        }

    if( oflow ) {
        mwIncErr();
        mwTrace( " [WARNING: OUTPUT BUFFER OVERFLOW - SYSTEM UNSTABLE]\n" );
        }
    return p;
    }

void* mwUnmark( void *p, const char *file, unsigned line ) {
    mwMarker *mrk, *prv;
    mrk = mwFirstMark;
    prv = NULL;
    while( mrk ) {
        if( mrk->host == p ) {
            if( mrk->level < 2 ) {
                if( prv ) prv->next = mrk->next;
                else mwFirstMark = mrk->next;
                free( mrk->text );
                free( mrk );
                return p;
                }
            mrk->level --;
            return p;
            }
        prv = mrk;
        mrk = mrk->next;
        }
    mwWrite("mark: %s(%d), no mark found for %p\n", file, line, p );
    return p;
    }


/***********************************************************************
** Abort/Retry/Ignore handlers
***********************************************************************/

static int mwARI( const char *estr ) {
    char inbuf[81];
    int c;
    fprintf(mwSTDERR, "\n%s\nMEMWATCH: Abort, Retry or Ignore? ", estr);
    (void) fgets(inbuf,sizeof(inbuf),stdin);
	for( c=0; inbuf[c] && inbuf[c] <= ' '; c++ ) ;
    c = inbuf[c];
    if( c == 'R' || c == 'r' ) {
        mwBreakOut( estr );
        return MW_ARI_RETRY;
        }
    if( c == 'I' || c == 'i' ) return MW_ARI_IGNORE;
    return MW_ARI_ABORT;
    }

/* standard ARI handler (exported) */
int mwAriHandler( const char *estr ) {
    mwAutoInit();
    return mwARI( estr );
    }

/* used to set the ARI function */
void mwSetAriFunc( int (*func)(const char *) ) {
    mwAutoInit();
    mwAriFunction = func;
    }

/***********************************************************************
** Allocation handlers
***********************************************************************/

void* mwMalloc( size_t size, const char* file, int line) {
    size_t needed;
    mwData *mw;
    char *ptr;
    void *p;

    mwAutoInit();

	MW_MUTEX_LOCK();

    TESTS(file,line);

    mwCounter ++;
    needed = mwDataSize + mwOverflowZoneSize*2 + size;
    if( needed < size )
    {
    	/* theoretical case: req size + mw overhead exceeded size_t limits */
    	return NULL;
    }

    /* if this allocation would violate the limit, fail it */
    if( mwUseLimit && ((long)size + mwStatCurAlloc > mwAllocLimit) ) {
        mwWrite( "limit fail: <%ld> %s(%d), %ld wanted %ld available\n",
            mwCounter, file, line, (long)size, mwAllocLimit - mwStatCurAlloc );
        mwIncErr();
        FLUSH();
		MW_MUTEX_UNLOCK();
        return NULL;
        }

    mw = (mwData*) malloc( needed );
    if( mw == NULL ) {
        if( mwFreeUp(needed,0) >= needed ) {
            mw = (mwData*) malloc(needed);
            if( mw == NULL ) {
                mwWrite( "internal: mwFreeUp(%u) reported success, but malloc() fails\n", needed );
                mwIncErr();
                FLUSH();
                }
            }
        if( mw == NULL ) {
            mwWrite( "fail: <%ld> %s(%d), %ld wanted %ld allocated\n",
                mwCounter, file, line, (long)size, mwStatCurAlloc );
            mwIncErr();
            FLUSH();
			MW_MUTEX_UNLOCK();
            return NULL;
            }
        }

    mw->count = mwCounter;
    mw->prev = NULL;
    mw->next = mwHead;
    mw->file = file;
    mw->size = size;
    mw->line = line;
    mw->flag = 0;
    mw->check = CHKVAL(mw);

    if( mwHead ) mwHead->prev = mw;
    mwHead = mw;
    if( mwTail == NULL ) mwTail = mw;

    ptr = ((char*)mw) + mwDataSize;
	mwWriteOF( ptr ); /* '*(long*)ptr = PRECHK;' */
    ptr += mwOverflowZoneSize;
    p = ptr;
    memset( ptr, MW_VAL_NEW, size );
    ptr += size;
    mwWriteOF( ptr ); /* '*(long*)ptr = POSTCHK;' */

    mwNumCurAlloc ++;
    mwStatCurAlloc += (long) size;
    mwStatTotAlloc += (long) size;
    if( mwStatCurAlloc > mwStatMaxAlloc )
        mwStatMaxAlloc = mwStatCurAlloc;
    mwStatNumAlloc ++;

    if( mwStatLevel ) mwStatAlloc( size, file, line );

	MW_MUTEX_UNLOCK();
    return p;
    }

void* mwRealloc( void *p, size_t size, const char* file, int line) {
    int oldUseLimit, i;
    mwData *mw;
    char *ptr;

    mwAutoInit();

    if( p == NULL ) return mwMalloc( size, file, line );
    if( size == 0 ) { mwFree( p, file, line ); return NULL; }

	MW_MUTEX_LOCK();

    /* do the quick ownership test */
    mw = (mwData*) mwBUFFER_TO_MW( p );
    if( mwIsOwned( mw, file, line ) ) {

		/* if the buffer is an NML, treat this as a double-free */
		if( mw->flag & MW_NML )
		{
            mwIncErr();
			if( *((unsigned char*)(mw)+mwDataSize+mwOverflowZoneSize) != MW_VAL_NML )
			{
				mwWrite( "internal: <%ld> %s(%d), no-mans-land MW-%p is corrupted\n",
					mwCounter, file, line, mw );
			}
			goto check_dbl_free;
		}

        /* if this allocation would violate the limit, fail it */
        if( mwUseLimit && ((long)size + mwStatCurAlloc - (long)mw->size > mwAllocLimit) ) {
            TESTS(file,line);
            mwCounter ++;
            mwWrite( "limit fail: <%ld> %s(%d), %ld wanted %ld available\n",
                mwCounter, file, line, (unsigned long)size - mw->size, mwAllocLimit - mwStatCurAlloc );
            mwIncErr();
            FLUSH();
			MW_MUTEX_UNLOCK();
            return NULL;
            }

        /* fake realloc operation */
        oldUseLimit = mwUseLimit;
        mwUseLimit = 0;
        ptr = (char*) mwMalloc( size, file, line );
        if( ptr != NULL ) {
            if( size < mw->size )
                memcpy( ptr, p, size );
            else
                memcpy( ptr, p, mw->size );
            mwFree( p, file, line );
            }
        mwUseLimit = oldUseLimit;
		MW_MUTEX_UNLOCK();
        return (void*) ptr;
        }

    /* Unknown pointer! */

    /* using free'd pointer? */
check_dbl_free:
    for(i=0;i<MW_FREE_LIST;i++) {
        if( mwLastFree[i] == p ) {
            mwIncErr();
            mwWrite( "realloc: <%ld> %s(%d), %p was"
                " freed from %s(%d)\n",
                mwCounter, file, line, p,
                mwLFfile[i], mwLFline[i] );
            FLUSH();
			MW_MUTEX_UNLOCK();
            return NULL;
            }
        }

    /* some weird pointer */
    mwIncErr();
    mwWrite( "realloc: <%ld> %s(%d), unknown pointer %p\n",
        mwCounter, file, line, p );
    FLUSH();
	MW_MUTEX_UNLOCK();
    return NULL;
    }

char *mwStrdup( const char* str, const char* file, int line ) {
    size_t len;
    char *newstring;

	MW_MUTEX_LOCK();

    if( str == NULL ) {
        mwIncErr();
        mwWrite( "strdup: <%ld> %s(%d), strdup(NULL) called\n",
            mwCounter, file, line );
        FLUSH();
		MW_MUTEX_UNLOCK();
        return NULL;
        }

    len = strlen( str ) + 1;
    newstring = (char*) mwMalloc( len, file, line );
    if( newstring != NULL ) memcpy( newstring, str, len );
	MW_MUTEX_UNLOCK();
    return newstring;
    }

void mwFree( void* p, const char* file, int line ) {
    int i;
    mwData* mw;
    char buffer[ sizeof(mwData) + (mwROUNDALLOC*3) + 64 ];

    /* this code is in support of C++ delete */
    if( file == NULL ) {
        mwFree_( p );
		MW_MUTEX_UNLOCK();
        return;
        }

    mwAutoInit();

	MW_MUTEX_LOCK();
    TESTS(file,line);
    mwCounter ++;

    /* on NULL free, write a warning and return */
    if( p == NULL ) {
        mwWrite( "NULL free: <%ld> %s(%d), NULL pointer free'd\n",
            mwCounter, file, line );
        FLUSH();
		MW_MUTEX_UNLOCK();
        return;
        }

    /* do the quick ownership test */
    mw = (mwData*) mwBUFFER_TO_MW( p );

    if( mwIsOwned( mw, file, line ) ) {
        (void) mwTestBuf( mw, file, line );

		/* if the buffer is an NML, treat this as a double-free */
		if( mw->flag & MW_NML )
		{
			if( *(((unsigned char*)mw)+mwDataSize+mwOverflowZoneSize) != MW_VAL_NML )
			{
				mwWrite( "internal: <%ld> %s(%d), no-mans-land MW-%p is corrupted\n",
					mwCounter, file, line, mw );
			}
			goto check_dbl_free;
		}

        /* update the statistics */
        mwNumCurAlloc --;
        mwStatCurAlloc -= (long) mw->size;
        if( mwStatLevel ) mwStatFree( mw->size, mw->file, mw->line );

        /* we should either free the allocation or keep it as NML */
        if( mwNML ) {
            mw->flag |= MW_NML;
			mwNmlNumAlloc ++;
			mwNmlCurAlloc += (long) mw->size;
            memset( ((char*)mw)+mwDataSize+mwOverflowZoneSize, MW_VAL_NML, mw->size );
            }
        else {
            /* unlink the allocation, and enter the post-free data */
            mwUnlink( mw, file, line );
            memset( mw, MW_VAL_DEL,
                mw->size + mwDataSize+mwOverflowZoneSize+mwOverflowZoneSize );
            if( mwFBI ) {
                memset( mw, '.', mwDataSize + mwOverflowZoneSize );
                sprintf( buffer, "FBI<%ld>%s(%d)", mwCounter, file, line );
                strncpy( (char*)(void*)mw, buffer, mwDataSize + mwOverflowZoneSize );
                }
            free( mw );
            }

        /* add the pointer to the last-free track */
        mwLFfile[ mwLFcur ] = file;
        mwLFline[ mwLFcur ] = line;
        mwLastFree[ mwLFcur++ ] = p;
        if( mwLFcur == MW_FREE_LIST ) mwLFcur = 0;

		MW_MUTEX_UNLOCK();
        return;
        }

    /* check for double-freeing */
check_dbl_free:
    for(i=0;i<MW_FREE_LIST;i++) {
        if( mwLastFree[i] == p ) {
            mwIncErr();
            mwWrite( "double-free: <%ld> %s(%d), %p was"
                " freed from %s(%d)\n",
                mwCounter, file, line, p,
                mwLFfile[i], mwLFline[i] );
            FLUSH();
			MW_MUTEX_UNLOCK();
            return;
            }
        }

    /* some weird pointer... block the free */
    mwIncErr();
    mwWrite( "WILD free: <%ld> %s(%d), unknown pointer %p\n",
        mwCounter, file, line, p );
    FLUSH();
	MW_MUTEX_UNLOCK();
    return;
    }

void* mwCalloc( size_t a, size_t b, const char *file, int line ) {
    void *p;
    size_t size = a * b;
    p = mwMalloc( size, file, line );
    if( p == NULL ) return NULL;
    memset( p, 0, size );
    return p;
    }

void mwFree_( void *p ) {
	MW_MUTEX_LOCK();
    TESTS(NULL,0);
	MW_MUTEX_UNLOCK();
    free(p);
    }

void* mwMalloc_( size_t size ) {
	MW_MUTEX_LOCK();
    TESTS(NULL,0);
	MW_MUTEX_UNLOCK();
    return malloc( size );
    }

void* mwRealloc_( void *p, size_t size ) {
	MW_MUTEX_LOCK();
    TESTS(NULL,0);
	MW_MUTEX_UNLOCK();
    return realloc( p, size );
    }

void* mwCalloc_( size_t a, size_t b ) {
	MW_MUTEX_LOCK();
    TESTS(NULL,0);
	MW_MUTEX_UNLOCK();
    return calloc( a, b );
    }

void mwFlushNow( void ) {
    if( mwLogR() ) fflush( mwLogR() );
    return;
    }

void mwDoFlush( int onoff ) {
    mwFlushW( onoff<1?0:onoff );
    if( onoff ) if( mwLogR() ) fflush( mwLogR() );
    return;
    }

void mwLimit( long lim ) {
    TESTS(NULL,0);
    mwWrite("limit: old limit = ");
    if( !mwAllocLimit ) mwWrite( "none" );
    else mwWrite( "%ld bytes", mwAllocLimit );
    mwWrite( ", new limit = ");
    if( !lim ) {
        mwWrite( "none\n" );
        mwUseLimit = 0;
        }
    else {
        mwWrite( "%ld bytes\n", lim );
        mwUseLimit = 1;
        }
    mwAllocLimit = lim;
    FLUSH();
    }

void mwSetAriAction( int action ) {
	MW_MUTEX_LOCK();
    TESTS(NULL,0);
    mwAriAction = action;
	MW_MUTEX_UNLOCK();
    return;
    }

int mwAssert( int exp, const char *exps, const char *fn, int ln ) {
    int i;
    char buffer[MW_TRACE_BUFFER+8];
    if( exp ) {
    	return 0;
    	}
    mwAutoInit();
	MW_MUTEX_LOCK();
    TESTS(fn,ln);
    mwIncErr();
    mwCounter++;
    mwWrite( "assert trap: <%ld> %s(%d), %s\n", mwCounter, fn, ln, exps );
    if( mwAriFunction != NULL ) {
        sprintf( buffer, "MEMWATCH: assert trap: %s(%d), %s", fn, ln, exps );
        i = (*mwAriFunction)(buffer);
		switch( i ) {
			case MW_ARI_IGNORE:
	           	mwWrite( "assert trap: <%ld> IGNORED - execution continues\n", mwCounter );
				MW_MUTEX_UNLOCK();
    	        return 0;
			case MW_ARI_RETRY:
            	mwWrite( "assert trap: <%ld> RETRY - executing again\n", mwCounter );
				MW_MUTEX_UNLOCK();
            	return 1;
			}
        }
    else {
        if( mwAriAction & MW_ARI_IGNORE ) {
            mwWrite( "assert trap: <%ld> AUTO IGNORED - execution continues\n", mwCounter );
			MW_MUTEX_UNLOCK();
            return 0;
            }
        fprintf(mwSTDERR,"\nMEMWATCH: assert trap: %s(%d), %s\n", fn, ln, exps );
        }

    FLUSH();
    (void) mwTestNow( fn, ln, 1 );
    FLUSH();

	if( mwAriAction & MW_ARI_NULLREAD ) {
		/* This is made in an attempt to kick in */
		/* any debuggers or OS stack traces */
	    FLUSH();
		/*lint -save -e413 */
		i = *((int*)NULL);
		mwDummy( (char)i );
		/*lint -restore */
		}

	MW_MUTEX_UNLOCK();
    exit(255);
    /* NOT REACHED - the return statement is in to keep */
    /* stupid compilers from squeaking about differing return modes. */
    /* Smart compilers instead say 'code unreachable...' */
    /*lint -save -e527 */
    return 0;
    /*lint -restore */
    }

int mwVerify( int exp, const char *exps, const char *fn, int ln ) {
    int i;
    char buffer[MW_TRACE_BUFFER+8];
    if( exp ) {
    	return 0;
    	}
    mwAutoInit();
	MW_MUTEX_LOCK();
    TESTS(fn,ln);
    mwIncErr();
    mwCounter++;
    mwWrite( "verify trap: <%ld> %s(%d), %s\n", mwCounter, fn, ln, exps );
    if( mwAriFunction != NULL ) {
        sprintf( buffer, "MEMWATCH: verify trap: %s(%d), %s", fn, ln, exps );
        i = (*mwAriFunction)(buffer);
        if( i == 0 ) {
            mwWrite( "verify trap: <%ld> IGNORED - execution continues\n", mwCounter );
			MW_MUTEX_UNLOCK();
            return 0;
            }
        if( i == 1 ) {
            mwWrite( "verify trap: <%ld> RETRY - executing again\n", mwCounter );
			MW_MUTEX_UNLOCK();
            return 1;
            }
        }
    else {
        if( mwAriAction & MW_ARI_NULLREAD ) {
            /* This is made in an attempt to kick in */
            /* any debuggers or OS stack traces */
		    FLUSH();
            /*lint -save -e413 */
            i = *((int*)NULL);
			mwDummy( (char)i );
            /*lint -restore */
            }
        if( mwAriAction & MW_ARI_IGNORE ) {
            mwWrite( "verify trap: <%ld> AUTO IGNORED - execution continues\n", mwCounter );
			MW_MUTEX_UNLOCK();
            return 0;
            }
        fprintf(mwSTDERR,"\nMEMWATCH: verify trap: %s(%d), %s\n", fn, ln, exps );
        }
    FLUSH();
    (void) mwTestNow( fn, ln, 1 );
    FLUSH();
	MW_MUTEX_UNLOCK();
	exit(255);
    /* NOT REACHED - the return statement is in to keep */
    /* stupid compilers from squeaking about differing return modes. */
    /* Smart compilers instead say 'code unreachable...' */
    /*lint -save -e527 */
    return 0;
    /*lint -restore */
    }

void mwTrace( const char *format, ... ) {
    int tot, oflow = 0;
    va_list mark;

    mwAutoInit();
	MW_MUTEX_LOCK();
    TESTS(NULL,0);
    if( mwOutFunction == NULL ) mwOutFunction = mwDefaultOutFunc;

    va_start( mark, format );
    tot = vsprintf( mwPrintBuf, format, mark );
    va_end( mark );
    if( tot >= MW_TRACE_BUFFER ) { mwPrintBuf[MW_TRACE_BUFFER] = 0; oflow = 1; }
    for(tot=0;mwPrintBuf[tot];tot++)
        (*mwOutFunction)( mwPrintBuf[tot] );
    if( oflow ) {
        mwIncErr();
        mwTrace( " [WARNING: OUTPUT BUFFER OVERFLOW - SYSTEM UNSTABLE]\n" );
        }

    FLUSH();
	MW_MUTEX_UNLOCK();
    }


/***********************************************************************
** Grab & Drop
***********************************************************************/

unsigned mwGrab( unsigned kb ) {
    TESTS(NULL,0);
    return mwGrab_( kb, MW_VAL_GRB, 0 );
    }

unsigned mwDrop( unsigned kb ) {
    TESTS(NULL,0);
    return mwDrop_( kb, MW_VAL_GRB, 0 );
    }

static void mwDropAll() {
    TESTS(NULL,0);
    (void) mwDrop_( 0, MW_VAL_GRB, 0 );
    (void) mwDrop_( 0, MW_VAL_NML, 0 );
    if( mwGrabList != NULL )
        mwWrite( "internal: the grab list is not empty after mwDropAll()\n");
    }

static const char *mwGrabType( int type ) {
    switch( type ) {
        case MW_VAL_GRB:
            return "grabbed";
        case MW_VAL_NML:
            return "no-mans-land";
        default:
            /* do nothing */
            ;
        }
    return "<unknown type>";
    }

static unsigned mwGrab_( unsigned kb, int type, int silent ) {
    unsigned i = kb;
    mwGrabData *gd;
    if( !kb ) i = kb = 65000U;

    for(;kb;kb--) {
        if( mwUseLimit &&
            (mwStatCurAlloc + mwGrabSize + (long)sizeof(mwGrabData) > mwAllocLimit) ) {
            if( !silent ) {
                mwWrite("grabbed: all allowed memory to %s (%u kb)\n",
                    mwGrabType(type), i-kb);
                FLUSH();
                }
            return i-kb;
            }
        gd = (mwGrabData*) malloc( sizeof(mwGrabData) );
        if( gd == NULL ) {
            if( !silent ) {
                mwWrite("grabbed: all available memory to %s (%u kb)\n",
                    mwGrabType(type), i-kb);
                FLUSH();
                }
            return i-kb;
            }
        mwGrabSize += (long) sizeof(mwGrabData);
        gd->next = mwGrabList;
        memset( gd->blob, type, sizeof(gd->blob) );
        gd->type = type;
        mwGrabList = gd;
        }
    if( !silent ) {
        mwWrite("grabbed: %u kilobytes of %s memory\n", i, mwGrabType(type) );
        FLUSH();
        }
    return i;
    }

static unsigned mwDrop_( unsigned kb, int type, int silent ) {
    unsigned i = kb;
    mwGrabData *gd,*tmp,*pr;
    const void *p;

    if( mwGrabList == NULL && kb == 0 ) return 0;
    if( !kb ) i = kb = 60000U;

    pr = NULL;
    gd = mwGrabList;
    for(;kb;) {
        if( gd == NULL ) {
            if( i-kb > 0 && !silent ) {
                mwWrite("dropped: all %s memory (%u kb)\n", mwGrabType(type), i-kb);
                FLUSH();
                }
            return i-kb;
            }
        if( gd->type == type ) {
            if( pr ) pr->next = gd->next;
            kb --;
            tmp = gd;
            if( mwGrabList == gd ) mwGrabList = gd->next;
            gd = gd->next;
            p = mwTestMem( tmp->blob, sizeof( tmp->blob ), type );
            if( p != NULL ) {
                mwWrite( "wild pointer: <%ld> %s memory hit at %p\n",
                    mwCounter, mwGrabType(type), p );
                FLUSH();
                }
            mwGrabSize -= (long) sizeof(mwGrabData);
            free( tmp );
            }
        else {
            pr = gd;
            gd = gd->next;
            }
        }
    if( !silent ) {
        mwWrite("dropped: %u kilobytes of %s memory\n", i, mwGrabType(type) );
        FLUSH();
        }
    return i;
    }

/***********************************************************************
** No-Mans-Land
***********************************************************************/

void mwNoMansLand( int level ) {
    mwAutoInit();
    TESTS(NULL,0);
    switch( level ) {
        case MW_NML_NONE:
            (void) mwDrop_( 0, MW_VAL_NML, 0 );
            break;
        case MW_NML_FREE:
            break;
        case MW_NML_ALL:
            (void) mwGrab_( 0, MW_VAL_NML, 0 );
            break;
        default:
            return;
        }
    mwNML = level;
    }

/***********************************************************************
** Static functions
***********************************************************************/

static void mwAutoInit( void )
{
    if( mwInited ) return;
    mwUseAtexit = 1;
    mwInit();
    return;
}

static FILE *mwLogR() {
    if( (mwLog == mwLogB1) && (mwLog == mwLogB2) ) return mwLog;
    if( mwLog == mwLogB1 ) mwLogB2 = mwLog;
    if( mwLog == mwLogB2 ) mwLogB1 = mwLog;
    if( mwLogB1 == mwLogB2 ) mwLog = mwLogB1;
    if( (mwLog == mwLogB1) && (mwLog == mwLogB2) ) {
        mwWrite("internal: log file handle damaged and recovered\n");
        FLUSH();
        return mwLog;
        }
    fprintf(mwSTDERR,"\nMEMWATCH: log file handle destroyed, using mwSTDERR\n" );
    mwLog = mwLogB1 = mwLogB2 = mwSTDERR;
    return mwSTDERR;
    }

static void mwLogW( FILE *p ) {
    mwLog = mwLogB1 = mwLogB2 = p;
    }

static int mwFlushR() {
    if( (mwFlushing == mwFlushingB1) && (mwFlushing == mwFlushingB2) ) return mwFlushing;
    if( mwFlushing == mwFlushingB1 ) mwFlushingB2 = mwFlushing;
    if( mwFlushing == mwFlushingB2 ) mwFlushingB1 = mwFlushing;
    if( mwFlushingB1 == mwFlushingB2 ) mwFlushing = mwFlushingB1;
    if( (mwFlushing == mwFlushingB1) && (mwFlushing == mwFlushingB2) ) {
        mwWrite("internal: flushing flag damaged and recovered\n");
        FLUSH();
        return mwFlushing;
        }
    mwWrite("internal: flushing flag destroyed, so set to true\n");
    mwFlushing = mwFlushingB1 = mwFlushingB2 = 1;
    return 1;
    }

static void mwFlushW( int n ) {
    mwFlushing = mwFlushingB1 = mwFlushingB2 = n;
    }

static void mwIncErr() {
    mwErrors++;
    mwFlushW( mwFlushR()+1 );
    FLUSH();
    }

static void mwFlush() {
    if( mwLogR() == NULL ) return;
#ifdef MW_FLUSH
    fflush( mwLogR() );
#else
    if( mwFlushR() ) fflush( mwLogR() );
#endif
    return;
    }

static void mwUnlink( mwData* mw, const char* file, int line ) {
    if( mw->prev == NULL ) {
        if( mwHead != mw )
            mwWrite( "internal: <%ld> %s(%d), MW-%p: link1 NULL, but not head\n",
                mwCounter, file, line, mw );
        mwHead = mw->next;
        }
    else {
        if( mw->prev->next != mw )
            mwWrite( "internal: <%ld> %s(%d), MW-%p: link1 failure\n",
                mwCounter, file, line, mw );
        else mw->prev->next = mw->next;
        }
    if( mw->next == NULL ) {
        if( mwTail != mw )
            mwWrite( "internal: <%ld> %s(%d), MW-%p: link2 NULL, but not tail\n",
                mwCounter, file, line, mw );
        mwTail = mw->prev;
        }
    else {
        if( mw->next->prev != mw )
            mwWrite( "internal: <%ld> %s(%d), MW-%p: link2 failure\n",
                mwCounter, file, line, mw );
        else mw->next->prev = mw->prev;
        }
    }

/*
** Relinking tries to repair a damaged mw block.
** Returns nonzero if it thinks it successfully
** repaired the heap chain.
*/
static int mwRelink( mwData* mw, const char* file, int line ) {
    int fails;
    mwData *mw1, *mw2;
    long count, size;
    mwStat *ms;

	if( file == NULL ) file = "unknown";

    if( mw == NULL ) {
        mwWrite("relink: cannot repair MW at NULL\n");
        FLUSH();
        goto emergency;
        }

    if( !mwIsSafeAddr(mw, mwDataSize) ) {
        mwWrite("relink: MW-%p is a garbage pointer\n", mw);
        FLUSH();
        goto emergency;
        }

    mwWrite("relink: <%ld> %s(%d) attempting to repair MW-%p...\n", mwCounter, file, line, mw );
    FLUSH();
    fails = 0;

    /* Repair from head */
    if( mwHead != mw ) {
        if( !mwIsSafeAddr( mwHead, mwDataSize ) ) {
            mwWrite("relink: failed for MW-%p; head pointer destroyed\n", mw );
            FLUSH();
            goto emergency;
            }
        for( mw1=mwHead; mw1; mw1=mw1->next ) {
            if( mw1->next == mw ) {
                mw->prev = mw1;
                break;
                }
            if( mw1->next &&
                ( !mwIsSafeAddr(mw1->next, mwDataSize ) || mw1->next->prev != mw1) ) {
                mwWrite("relink: failed for MW-%p; forward chain fragmented at MW-%p: 'next' is %p\n", mw, mw1, mw1->next );
                FLUSH();
                goto emergency;
                }
            }
        if( mw1 == NULL ) {
            mwWrite("relink: MW-%p not found in forward chain search\n", mw );
            FLUSH();
            fails ++;
            }
        }
	else
	{
		mwWrite( "relink: MW-%p is the head (first) allocation\n", mw );
		if( mw->prev != NULL )
		{
			mwWrite( "relink: MW-%p prev pointer is non-NULL, you have a wild pointer\n", mw );
			mw->prev = NULL;
		}
	}

    /* Repair from tail */
    if( mwTail != mw ) {
        if( !mwIsSafeAddr( mwTail, mwDataSize ) ) {
            mwWrite("relink: failed for MW-%p; tail pointer destroyed\n", mw );
            FLUSH();
            goto emergency;
            }
        for( mw1=mwTail; mw1; mw1=mw1->prev ) {
            if( mw1->prev == mw ) {
                mw->next = mw1;
                break;
                }
            if( mw1->prev && (!mwIsSafeAddr(mw1->prev, mwDataSize ) || mw1->prev->next != mw1) ) {
                mwWrite("relink: failed for MW-%p; reverse chain fragmented at MW-%p, 'prev' is %p\n", mw, mw1, mw1->prev );
                FLUSH();
                goto emergency;
                }
            }
        if( mw1 == NULL ) {
            mwWrite("relink: MW-%p not found in reverse chain search\n", mw );
            FLUSH();
            fails ++;
            }
        }
	else
	{
		mwWrite( "relink: MW-%p is the tail (last) allocation\n", mw );
		if( mw->next != NULL )
		{
			mwWrite( "relink: MW-%p next pointer is non-NULL, you have a wild pointer\n", mw );
			mw->next = NULL;
		}
	}

    if( fails > 1 ) {
        mwWrite("relink: heap appears intact, MW-%p probably garbage pointer\n", mw );
        FLUSH();
        goto verifyok;
        }

    /* restore MW info where possible */
    if( mwIsReadAddr( mw->file, 1 ) ) {
        ms = mwStatGet( mw->file, -1, 0 );
        if( ms == NULL ) mw->file = "<relinked>";
        }
    mw->check = CHKVAL(mw);
    goto verifyok;

    /* Emergency repair */
    emergency:

    if( mwHead == NULL && mwTail == NULL )
    {
        if( mwStatCurAlloc == 0 )
            mwWrite("relink: <%ld> %s(%d) heap is empty, nothing to repair\n", mwCounter, file, line );
        else
            mwWrite("relink: <%ld> %s(%d) heap damaged beyond repair\n", mwCounter, file, line );
        FLUSH();
        return 0;
    }

    mwWrite("relink: <%ld> %s(%d) attempting emergency repairs...\n", mwCounter, file, line );
    FLUSH();

	if( mwHead == NULL || mwTail == NULL )
	{
		if( mwHead == NULL ) mwWrite("relink: mwHead is NULL, but mwTail is %p\n", mwTail );
		else mwWrite("relink: mwTail is NULL, but mwHead is %p\n", mwHead );
	}

    mw1=NULL;
    if( mwHead != NULL )
	{
		if( !mwIsReadAddr( mwHead, mwDataSize ) || mwHead->check != CHKVAL(mwHead) )
		{
			mwWrite("relink: mwHead (MW-%p) is damaged, skipping forward scan\n", mwHead );
			mwHead = NULL;
			goto scan_reverse;
		}
		if( mwHead->prev != NULL )
		{
			mwWrite("relink: the mwHead pointer's 'prev' member is %p, not NULL\n", mwHead->prev );
		}
        for( mw1=mwHead; mw1; mw1=mw1->next )
		{
			if( mw1->next )
			{
				if( !mwIsReadAddr(mw1->next,mwDataSize) ||
					!mw1->next->check != CHKVAL(mw1) ||
					mw1->next->prev != mw1 )
				{
					mwWrite("relink: forward chain's last intact MW is MW-%p, %ld %sbytes at %s(%d)\n",
						mw1, mw1->size, (mw->flag & MW_NML)?"NoMansLand ":"", mw1->file, mw1->line );
					if( mwIsReadAddr(mw1->next,mwDataSize ) )
					{
						mwWrite("relink: forward chain's first damaged MW is MW-%p, %ld %sbytes at %s(%d)\n",
							mw1->next, mw1->size, (mw->flag & MW_NML)?"NoMansLand ":"",
							mwIsReadAddr(mw1->file,16)?mw1->file:"<garbage-pointer>", mw1->line );
					}
					else
					{
						mwWrite("relink: the 'next' pointer of this MW points to %p, which is out-of-legal-access\n",
							mw1->next );
					}
					break;
				}
			}
        }
	}


scan_reverse:
    mw2=NULL;
    if( mwTail != NULL )
	{
		if( !mwIsReadAddr(mwTail,mwDataSize) || mwTail->check != CHKVAL(mwTail) )
		{
			mwWrite("relink: mwTail (%p) is damaged, skipping reverse scan\n", mwTail );
			mwTail = NULL;
			goto analyze;
		}
		if( mwTail->next != NULL )
		{
			mwWrite("relink: the mwTail pointer's 'next' member is %p, not NULL\n", mwTail->next );
		}
        for( mw2=mwTail; mw2; mw2=mw2->prev )
		{
            if( mw2->prev )
			{
				if( !mwIsReadAddr(mw2->prev,mwDataSize) ||
					!mw2->prev->check != CHKVAL(mw2) ||
					mw2->prev->next != mw2 )
				{
					mwWrite("relink: reverse chain's last intact MW is MW-%p, %ld %sbytes at %s(%d)\n",
						mw2, mw2->size, (mw->flag & MW_NML)?"NoMansLand ":"", mw2->file, mw2->line );
					if( mwIsReadAddr(mw2->prev,mwDataSize ) )
					{
						mwWrite("relink: reverse chain's first damaged MW is MW-%p, %ld %sbytes at %s(%d)\n",
							mw2->prev, mw2->size, (mw->flag & MW_NML)?"NoMansLand ":"",
							mwIsReadAddr(mw2->file,16)?mw2->file:"<garbage-pointer>", mw2->line );
					}
					else
					{
						mwWrite("relink: the 'prev' pointer of this MW points to %p, which is out-of-legal-access\n",
							mw2->prev );
					}
					break;
				}
			}
        }
	}

analyze:
	if( mwHead == NULL && mwTail == NULL )
	{
        mwWrite("relink: both head and tail pointers damaged, aborting program\n");
        mwFlushW(1);
        FLUSH();
        abort();
	}
	if( mwHead == NULL )
	{
		mwHead = mw2;
		mwWrite("relink: heap truncated, MW-%p designated as new mwHead\n", mw2 );
		mw2->prev = NULL;
		mw1 = mw2 = NULL;
	}
	if( mwTail == NULL )
	{
		mwTail = mw1;
		mwWrite("relink: heap truncated, MW-%p designated as new mwTail\n", mw1 );
		mw1->next = NULL;
		mw1 = mw2 = NULL;
	}
    if( mw1 == NULL && mw2 == NULL &&
        mwHead->prev == NULL && mwTail->next == NULL ) {
        mwWrite("relink: verifying heap integrity...\n" );
        FLUSH();
        goto verifyok;
        }
    if( mw1 && mw2 && mw1 != mw2 ) {
        mw1->next = mw2;
        mw2->prev = mw1;
        mwWrite("relink: emergency repairs successful, assessing damage...\n");
        FLUSH();
        }
    else {
        mwWrite("relink: heap totally destroyed, aborting program\n");
        mwFlushW(1);
        FLUSH();
        abort();
        }

    /* Verify by checking that the number of active allocations */
    /* match the number of entries in the chain */
verifyok:
    if( !mwIsHeapOK( NULL ) ) {
        mwWrite("relink: heap verification FAILS - aborting program\n");
        mwFlushW(1);
        FLUSH();
        abort();
        }
    for( size=count=0, mw1=mwHead; mw1; mw1=mw1->next ) {
        count ++;
        size += (long) mw1->size;
        }
    if( count == mwNumCurAlloc ) {
        mwWrite("relink: successful, ");
        if( size == mwStatCurAlloc ) {
            mwWrite("no allocations lost\n");
            }
        else {
            if( mw != NULL ) {
                mwWrite("size information lost for MW-%p\n", mw);
                mw->size = 0;
                }
            }
        }
    else {
        mwWrite("relink: partial, %ld MW-blocks of %ld bytes lost\n",
			mwNmlNumAlloc+mwNumCurAlloc-count, mwNmlCurAlloc+mwStatCurAlloc-size );
        return 0;
        }

    return 1;
    }

/*
**  If mwData* is NULL:
**      Returns 0 if heap chain is broken.
**      Returns 1 if heap chain is intact.
**  If mwData* is not NULL:
**      Returns 0 if mwData* is missing or if chain is broken.
**      Returns 1 if chain is intact and mwData* is found.
*/
static int mwIsHeapOK( mwData *includes_mw ) {
    int found = 0;
    mwData *mw;

    for( mw = mwHead; mw; mw=mw->next ) {
        if( includes_mw == mw ) found++;
        if( !mwIsSafeAddr( mw, mwDataSize ) ) return 0;
        if( mw->prev ) {
            if( !mwIsSafeAddr( mw->prev, mwDataSize ) ) return 0;
            if( mw==mwHead || mw->prev->next != mw ) return 0;
            }
        if( mw->next ) {
            if( !mwIsSafeAddr( mw->next, mwDataSize ) ) return 0;
            if( mw==mwTail || mw->next->prev != mw ) return 0;
            }
        else if( mw!=mwTail ) return 0;
        }

    if( includes_mw != NULL && !found ) return 0;

    return 1;
    }

static int mwIsOwned( mwData* mw, const char *file, int line ) {
    int retv;
    mwStat *ms;

    /* see if the address is legal according to OS */
    if( !mwIsSafeAddr( mw, mwDataSize ) ) return 0;

    /* make sure we have _anything_ allocated */
    if( mwHead == NULL && mwTail == NULL && mwStatCurAlloc == 0 )
        return 0;

    /* calculate checksum */
    if( mw->check != CHKVAL(mw) ) {
        /* may be damaged checksum, see if block is in heap */
        if( mwIsHeapOK( mw ) ) {
            /* damaged checksum, repair it */
            mwWrite( "internal: <%ld> %s(%d), checksum for MW-%p is incorrect\n",
                mwCounter, file, line, mw );
            mwIncErr();
            if( mwIsReadAddr( mw->file, 1 ) ) {
                ms = mwStatGet( mw->file, -1, 0 );
                if( ms == NULL ) mw->file = "<relinked>";
                }
            else mw->file = "<unknown>";
            mw->size = 0;
            mw->check = CHKVAL(mw);
            return 1;
            }
        /* no, it's just some garbage data */
        return 0;
        }

	/* check that the non-NULL pointers are safe */
	if( mw->prev && !mwIsSafeAddr( mw->prev, mwDataSize ) ) mwRelink( mw, file, line );
	if( mw->next && !mwIsSafeAddr( mw->next, mwDataSize ) ) mwRelink( mw, file, line );

    /* safe address, checksum OK, proceed with heap checks */

    /* see if the block is in the heap */
    retv = 0;
    if( mw->prev ) { if( mw->prev->next == mw ) retv ++; }
    else { if( mwHead == mw ) retv++; }
    if( mw->next ) { if( mw->next->prev == mw ) retv ++; }
    else { if( mwTail == mw ) retv++; }
    if( mw->check == CHKVAL(mw) ) retv ++;
    if( retv > 2 ) return 1;

    /* block not in heap, check heap for corruption */

    if( !mwIsHeapOK( mw ) ) {
        if( mwRelink( mw, file, line ) )
            return 1;
        }

    /* unable to repair */
    mwWrite( "internal: <%ld> %s(%d), mwIsOwned fails for MW-%p\n",
       mwCounter, file, line, mw );
    mwIncErr();

    return 0;
    }

/*
** mwTestBuf:
**  Checks a buffers links and pre/postfixes.
**  Writes errors found to the log.
**  Returns zero if no errors found.
*/
static int mwTestBuf( mwData* mw, const char* file, int line ) {
    int retv = 0;
    char *p;

    if( file == NULL ) file = "unknown";

    if( !mwIsSafeAddr( mw, mwDataSize + mwOverflowZoneSize ) ) {
        mwWrite( "internal: <%ld> %s(%d): pointer MW-%p is invalid\n",
            mwCounter, file, line, mw );
        mwIncErr();
        return 2;
        }

    if( mw->check != CHKVAL(mw) ) {
        mwWrite( "internal: <%ld> %s(%d), info trashed; relinking\n",
            mwCounter, file, line );
        mwIncErr();
        if( !mwRelink( mw, file, line ) ) return 2;
        }

    if( mw->prev && mw->prev->next != mw ) {
        mwWrite( "internal: <%ld> %s(%d), buffer <%ld> %s(%d) link1 broken\n",
            mwCounter,file,line, (long)mw->size, mw->count, mw->file, mw->line );
        mwIncErr();
        if( !mwRelink( mw, file, line ) ) retv = 2;
        }
    if( mw->next && mw->next->prev != mw ) {
        mwWrite( "internal: <%ld> %s(%d), buffer <%ld> %s(%d) link2 broken\n",
            mwCounter,file,line, (long)mw->size, mw->count, mw->file, mw->line );
        mwIncErr();
        if( !mwRelink( mw, file, line ) ) retv = 2;
        }

    p = ((char*)mw) + mwDataSize;
    if( mwCheckOF( p ) ) {
        mwWrite( "underflow: <%ld> %s(%d), %ld bytes alloc'd at <%ld> %s(%d)\n",
            mwCounter,file,line, (long)mw->size, mw->count, mw->file, mw->line );
        mwIncErr();
        retv = 1;
        }
    p += mwOverflowZoneSize + mw->size;
    if( mwIsReadAddr( p, mwOverflowZoneSize ) && mwCheckOF( p ) ) {
        mwWrite( "overflow: <%ld> %s(%d), %ld bytes alloc'd at <%ld> %s(%d)\n",
            mwCounter,file,line, (long)mw->size, mw->count, mw->file, mw->line );
        mwIncErr();
        retv = 1;
        }

    return retv;
    }

static void mwDefaultOutFunc( int c ) {
    if( mwLogR() ) fputc( c, mwLogR() );
    }

static void mwWrite( const char *format, ... ) {
    int tot, oflow = 0;
    va_list mark;
    mwAutoInit();
    if( mwOutFunction == NULL ) mwOutFunction = mwDefaultOutFunc;
    va_start( mark, format );
    tot = vsprintf( mwPrintBuf, format, mark );
    va_end( mark );
    if( tot >= MW_TRACE_BUFFER ) { mwPrintBuf[MW_TRACE_BUFFER] = 0; oflow = 1; }
    for(tot=0;mwPrintBuf[tot];tot++)
        (*mwOutFunction)( mwPrintBuf[tot] );
    if( oflow ) {
        mwWrite( "\ninternal: mwWrite(): WARNING! OUTPUT EXCEEDED %u CHARS: SYSTEM UNSTABLE\n", MW_TRACE_BUFFER-1 );
        FLUSH();
        }
    return;
    }

static void mwLogFile( const char *name ) {
    time_t tid;
    (void) time( &tid );
    if( mwLogR() != NULL ) {
        fclose( mwLogR() );
        mwLogW( NULL );
        }
    if( name == NULL ) return;
    mwLogW( fopen( name, "a" COMMIT ) );
    if( mwLogR() == NULL )
        mwWrite( "logfile: failed to open/create file '%s'\n", name );
    }

/*
** Try to free NML memory until a contiguous allocation of
** 'needed' bytes can be satisfied. If this is not enough
** and the 'urgent' parameter is nonzero, grabbed memory is
** also freed.
*/
static size_t mwFreeUp( size_t needed, int urgent ) {
    void *p;
    mwData *mw, *mw2;
    char *data;

    /* free grabbed NML memory */
    for(;;) {
        if( mwDrop_( 1, MW_VAL_NML, 1 ) == 0 ) break;
        p = malloc( needed );
        if( p == NULL ) continue;
        free( p );
        return needed;
        }

    /* free normal NML memory */
    mw = mwHead;
    while( mw != NULL ) {
        if( !(mw->flag & MW_NML) ) mw = mw->next;
        else {
            data = ((char*)mw)+mwDataSize+mwOverflowZoneSize;
            if( mwTestMem( data, mw->size, MW_VAL_NML ) ) {
                mwIncErr();
                mwWrite( "wild pointer: <%ld> NoMansLand %p alloc'd at %s(%d)\n",
                    mw->count, data + mwOverflowZoneSize, mw->file, mw->line );
                }
            mw2 = mw->next;
            mwUnlink( mw, "mwFreeUp", 0 );
            free( mw );
            mw = mw2;
            p = malloc( needed );
            if( p == NULL ) continue;
            free( p );
            return needed;
            }
        }

    /* if not urgent (for internal purposes), fail */
    if( !urgent ) return 0;

    /* free grabbed memory */
    for(;;) {
        if( mwDrop_( 1, MW_VAL_GRB, 1 ) == 0 ) break;
        p = malloc( needed );
        if( p == NULL ) continue;
        free( p );
        return needed;
        }

    return 0;
    }

static const void * mwTestMem( const void *p, unsigned len, int c ) {
    const unsigned char *ptr;
    ptr = (const unsigned char *) p;
    while( len-- ) {
        if( *ptr != (unsigned char)c ) return (const void*)ptr;
        ptr ++;
        }
    return NULL;
    }

static int mwStrCmpI( const char *s1, const char *s2 ) {
    if( s1 == NULL || s2 == NULL ) return 0;
    while( *s1 ) {
        if( toupper(*s2) == toupper(*s1) ) { s1++; s2++; continue; }
        return 1;
        }
    return 0;
    }

#define AIPH() if( always_invoked ) { mwWrite("autocheck: <%ld> %s(%d) ", mwCounter, file, line ); always_invoked = 0; }

static int mwTestNow( const char *file, int line, int always_invoked ) {
    int retv = 0;
    mwData *mw;
    char *data;

    if( file && !always_invoked )
        mwWrite("check: <%ld> %s(%d), checking %s%s%s\n",
            mwCounter, file, line,
			(mwTestFlags & MW_TEST_CHAIN) ? "chain ": "",
		    (mwTestFlags & MW_TEST_ALLOC) ? "alloc ": "",
		    (mwTestFlags & MW_TEST_NML) ? "nomansland ": ""
			);

    if( mwTestFlags & MW_TEST_CHAIN ) {
        for( mw = mwHead; mw; mw=mw->next ) {
			if( !mwIsSafeAddr(mw, mwDataSize) ) {
				AIPH();
				mwWrite("check: heap corruption detected\n");
				mwIncErr();
				return retv + 1;
				}
			if( mw->prev ) {
				if( !mwIsSafeAddr(mw->prev, mwDataSize) ) {
					AIPH();
					mwWrite("check: heap corruption detected\n");
					mwIncErr();
					return retv + 1;
					}
				if( mw==mwHead || mw->prev->next != mw ) {
					AIPH();
					mwWrite("check: heap chain broken, prev link incorrect\n");
					mwIncErr();
					retv ++;
					}
				}
			if( mw->next ) {
				if( !mwIsSafeAddr(mw->next, mwDataSize) ) {
					AIPH();
					mwWrite("check: heap corruption detected\n");
					mwIncErr();
					return retv + 1;
					}
				if( mw==mwTail || mw->next->prev != mw ) {
					AIPH();
					mwWrite("check: heap chain broken, next link incorrect\n");
					mwIncErr();
					retv ++;
					}
				}
			else if( mw!=mwTail ) {
				AIPH();
				mwWrite("check: heap chain broken, tail incorrect\n");
				mwIncErr();
				retv ++;
				}
            }
        }
    if( mwTestFlags & MW_TEST_ALLOC ) {
        for( mw = mwHead; mw; mw=mw->next ) {
            if( mwTestBuf( mw, file, line ) ) retv ++;
            }
        }
    if( mwTestFlags & MW_TEST_NML ) {
        for( mw = mwHead; mw; mw=mw->next ) {
            if( (mw->flag & MW_NML) ) {
                data = ((char*)mw)+mwDataSize+mwOverflowZoneSize;
                if( mwTestMem( data, mw->size, MW_VAL_NML ) ) {
                    mwIncErr();
                    mwWrite( "wild pointer: <%ld> NoMansLand %p alloc'd at %s(%d)\n",
                        mw->count, data + mwOverflowZoneSize, mw->file, mw->line );
                    }
                }
            }
        }


	if( file && !always_invoked && !retv )
        mwWrite("check: <%ld> %s(%d), complete; no errors\n",
            mwCounter, file, line );
    return retv;
    }

/**********************************************************************
** Statistics
**********************************************************************/

static void mwStatReport()
{
    mwStat* ms, *ms2;
    const char *modname;
    int modnamelen;

    /* global statistics report */
    mwWrite( "\nMemory usage statistics (global):\n" );
    mwWrite( " N)umber of allocations made: %ld\n", mwStatNumAlloc );
    mwWrite( " L)argest memory usage      : %ld\n", mwStatMaxAlloc );
    mwWrite( " T)otal of all alloc() calls: %ld\n", mwStatTotAlloc );
    mwWrite( " U)nfreed bytes totals      : %ld\n", mwStatCurAlloc );
    FLUSH();

    if( mwStatLevel < 1 ) return;

    /* on a per-module basis */
    mwWrite( "\nMemory usage statistics (detailed):\n");
    mwWrite( " Module/Line                                Number   Largest  Total    Unfreed \n");
    for( ms=mwStatList; ms; ms=ms->next )
    {
        if( ms->line == -1 )
        {
			if( ms->file == NULL || !mwIsReadAddr(ms->file,22) ) modname = "<unknown>";
			else modname = ms->file;
			modnamelen = strlen(modname);
			if( modnamelen > 42 )
			{
				modname = modname + modnamelen - 42;
			}

            mwWrite(" %-42s %-8ld %-8ld %-8ld %-8ld\n",
            	modname, ms->num, ms->max, ms->total, ms->curr );
            if( ms->file && mwStatLevel > 1 )
            {
                for( ms2=mwStatList; ms2; ms2=ms2->next )
                {
                    if( ms2->line!=-1 && ms2->file!=NULL && !mwStrCmpI( ms2->file, ms->file ) )
					{
					mwWrite( "  %-8d                                  %-8ld %-8ld %-8ld %-8ld\n",
						ms2->line, ms2->num, ms2->max, ms2->total, ms2->curr );
					}
				}
			}
		}
	}
}

static mwStat* mwStatGet( const char *file, int line, int makenew ) {
    mwStat* ms;

    if( mwStatLevel < 2 ) line = -1;

    for( ms=mwStatList; ms!=NULL; ms=ms->next ) {
        if( line != ms->line ) continue;
        if( file==NULL ) {
            if( ms->file == NULL ) break;
            continue;
            }
        if( ms->file == NULL ) continue;
        if( !strcmp( ms->file, file ) ) break;
        }

    if( ms != NULL ) return ms;

    if( !makenew ) return NULL;

    ms = (mwStat*) malloc( sizeof(mwStat) );
    if( ms == NULL ) {
        if( mwFreeUp( sizeof(mwStat), 0 ) < sizeof(mwStat) ||
            (ms=(mwStat*)malloc(sizeof(mwStat))) == NULL ) {
            mwWrite("internal: memory low, statistics incomplete for '%s'\n", file );
            return NULL;
            }
        }
    ms->file = file;
    ms->line = line;
    ms->total = 0L;
    ms->max = 0L;
    ms->num = 0L;
    ms->curr = 0L;
    ms->next = mwStatList;
    mwStatList = ms;
    return ms;
    }

static void mwStatAlloc( size_t size, const char* file, int line ) {
    mwStat* ms;

    /* update the module statistics */
    ms = mwStatGet( file, -1, 1 );
    if( ms != NULL ) {
        ms->total += (long) size;
        ms->curr += (long) size;
        ms->num ++;
        if( ms->curr > ms->max ) ms->max = ms->curr;
        }

    /* update the line statistics */
    if( mwStatLevel > 1 && line != -1 && file ) {
        ms = mwStatGet( file, line, 1 );
        if( ms != NULL ) {
            ms->total += (long) size;
            ms->curr += (long) size;
            ms->num ++;
            if( ms->curr > ms->max ) ms->max = ms->curr;
            }
        }

    }

static void mwStatFree( size_t size, const char* file, int line ) {
    mwStat* ms;

    /* update the module statistics */
    ms = mwStatGet( file, -1, 1 );
    if( ms != NULL ) ms->curr -= (long) size;

    /* update the line statistics */
    if( mwStatLevel > 1 && line != -1 && file ) {
        ms = mwStatGet( file, line, 1 );
        if( ms != NULL ) ms->curr -= (long) size;
        }
    }

/***********************************************************************
** Safe memory checkers
**
** Using ifdefs, implement the operating-system specific mechanism
** of identifying a piece of memory as legal to access with read
** and write priviliges. Default: return nonzero for non-NULL pointers.
***********************************************************************/

static char mwDummy( char c )
{
	return c;
}

#ifndef MW_SAFEADDR
#ifdef WIN32
#define MW_SAFEADDR
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int mwIsReadAddr( const void *p, unsigned len )
{
    if( p == NULL ) return 0;
    if( IsBadReadPtr(p,len) ) return 0;
    return 1;
}
int mwIsSafeAddr( void *p, unsigned len )
{
    /* NOTE: For some reason, under Win95 the IsBad... */
    /* can return false for invalid pointers. */
    if( p == NULL ) return 0;
    if( IsBadReadPtr(p,len) || IsBadWritePtr(p,len) ) return 0;
    return 1;
}
#endif /* WIN32 */
#endif /* MW_SAFEADDR */

#ifndef MW_SAFEADDR
#ifdef SIGSEGV
#define MW_SAFEADDR

typedef void (*mwSignalHandlerPtr)( int );
mwSignalHandlerPtr mwOldSIGSEGV = (mwSignalHandlerPtr) 0;
jmp_buf mwSIGSEGVjump;
static void mwSIGSEGV( int n );

static void mwSIGSEGV( int n )
{
	n = n;
	longjmp( mwSIGSEGVjump, 1 );
}

int mwIsReadAddr( const void *p, unsigned len )
{
	const char *ptr;

    if( p == NULL ) return 0;
	if( !len ) return 1;

	/* set up to catch the SIGSEGV signal */
	mwOldSIGSEGV = signal( SIGSEGV, mwSIGSEGV );

	if( setjmp( mwSIGSEGVjump ) )
	{
		signal( SIGSEGV, mwOldSIGSEGV );
		return 0;
	}

	/* read all the bytes in the range */
	ptr = (const char *)p;
	ptr += len;

	/* the reason for this rather strange construct is that */
	/* we want to keep the number of used parameters and locals */
	/* to a minimum. if we use len for a counter gcc will complain */
	/* it may get clobbered by longjmp() at high warning levels. */
	/* it's a harmless warning, but this way we don't have to see it. */
	do
	{
		ptr --;
		if( *ptr == 0x7C ) (void) mwDummy( (char)0 );
	} while( (const void*) ptr != p );

	/* remove the handler */
	signal( SIGSEGV, mwOldSIGSEGV );

    return 1;
}
int mwIsSafeAddr( void *p, unsigned len )
{
	char *ptr;

	if( p == NULL ) return 0;
	if( !len ) return 1;

	/* set up to catch the SIGSEGV signal */
	mwOldSIGSEGV = signal( SIGSEGV, mwSIGSEGV );

	if( setjmp( mwSIGSEGVjump ) )
	{
		signal( SIGSEGV, mwOldSIGSEGV );
		return 0;
	}

	/* read and write-back all the bytes in the range */
	ptr = (char *)p;
	ptr += len;

	/* the reason for this rather strange construct is that */
	/* we want to keep the number of used parameters and locals */
	/* to a minimum. if we use len for a counter gcc will complain */
	/* it may get clobbered by longjmp() at high warning levels. */
	/* it's a harmless warning, but this way we don't have to see it. */
	do
	{
		ptr --;
		*ptr = mwDummy( *ptr );
	} while( (void*) ptr != p );

	/* remove the handler */
	signal( SIGSEGV, mwOldSIGSEGV );

    return 1;
}
#endif /* SIGSEGV */
#endif /* MW_SAFEADDR */

#ifndef MW_SAFEADDR
int mwIsReadAddr( const void *p, unsigned len )
{
    if( p == NULL ) return 0;
    if( len == 0 ) return 1;
    return 1;
}
int mwIsSafeAddr( void *p, unsigned len )
{
    if( p == NULL ) return 0;
    if( len == 0 ) return 1;
    return 1;
}
#endif

/**********************************************************************
** Mutex handling
**********************************************************************/

#if defined(WIN32) || defined(__WIN32__)

static void	mwMutexInit( void )
{
	mwGlobalMutex = CreateMutex( NULL, FALSE, NULL);
	return;
}

static void	mwMutexTerm( void )
{
	CloseHandle( mwGlobalMutex );
	return;
}

static void	mwMutexLock( void )
{
	if( WaitForSingleObject(mwGlobalMutex, 1000 ) == WAIT_TIMEOUT )
	{
		mwWrite( "mwMutexLock: timed out, possible deadlock\n" );
	}
	return;
}

static void	mwMutexUnlock( void )
{
	ReleaseMutex( mwGlobalMutex );
	return;
}

#endif

#if defined(MW_PTHREADS) || defined(HAVE_PTHREAD_H)

static void	mwMutexInit( void )
{
	pthread_mutex_init( &mwGlobalMutex, NULL );
	return;
}

static void	mwMutexTerm( void )
{
	pthread_mutex_destroy( &mwGlobalMutex );
	return;
}

static void	mwMutexLock( void )
{
	pthread_mutex_lock(&mwGlobalMutex);
	return;
}

static void	mwMutexUnlock( void )
{
	pthread_mutex_unlock(&mwGlobalMutex);
	return;
}

#endif

/**********************************************************************
** C++ new & delete
**********************************************************************/

#if 0 /* 980317: disabled C++ */

#ifdef __cplusplus
#ifndef MEMWATCH_NOCPP

int mwNCur = 0;
const char *mwNFile = NULL;
int mwNLine = 0;

class MemWatch {
public:
    MemWatch();
    ~MemWatch();
    };

MemWatch::MemWatch() {
    if( mwInited ) return;
    mwUseAtexit = 0;
    mwInit();
    }

MemWatch::~MemWatch() {
    if( mwUseAtexit ) return;
    mwTerm();
    }

/*
** This global new will catch all 'new' calls where MEMWATCH is
** not active.
*/
void* operator new( unsigned size ) {
    mwNCur = 0;
    return mwMalloc( size, "<unknown>", 0 );
    }

/*
** This is the new operator that's called when a module uses mwNew.
*/
void* operator new( unsigned size, const char *file, int line ) {
    mwNCur = 0;
    return mwMalloc( size, file, line );
    }

/*
** This is the new operator that's called when a module uses mwNew[].
** -- hjc 07/16/02
*/
void* operator new[] ( unsigned size, const char *file, int line ) {
    mwNCur = 0;
    return mwMalloc( size, file, line );
    }

/*
** Since this delete operator will recieve ALL delete's
** even those from within libraries, we must accept
** delete's before we've been initialized. Nor can we
** reliably check for wild free's if the mwNCur variable
** is not set.
*/
void operator delete( void *p ) {
    if( p == NULL ) return;
    if( !mwInited ) {
        free( p );
        return;
        }
    if( mwNCur ) {
        mwFree( p, mwNFile, mwNLine );
        mwNCur = 0;
        return;
        }
    mwFree_( p );
    }

void operator delete[]( void *p ) {
    if( p == NULL ) return;
    if( !mwInited ) {
        free( p );
        return;
        }
    if( mwNCur ) {
        mwFree( p, mwNFile, mwNLine );
        mwNCur = 0;
        return;
        }
    mwFree_( p );
    }

#endif /* MEMWATCH_NOCPP */
#endif /* __cplusplus */

#endif /* 980317: disabled C++ */

/* MEMWATCH.C */