zsh-workers
 help / color / mirror / code / Atom feed
ca9ab96ae6ef15cd7934a43419f0e631016cb3b5 blob 50072 bytes (raw)

   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
 
/*
 * mem.c - memory management
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 1992-1997 Paul Falstad
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Paul Falstad or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Paul Falstad and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Paul Falstad and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Paul Falstad and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

#include "zsh.mdh"
#include "mem.pro"

/*
	There are two ways to allocate memory in zsh.  The first way is
	to call zalloc/zshcalloc, which call malloc/calloc directly.  It
	is legal to call realloc() or free() on memory allocated this way.
	The second way is to call zhalloc/hcalloc, which allocates memory
	from one of the memory pools on the heap stack.  Such memory pools 
	will automatically created when the heap allocation routines are
	called.  To be sure that they are freed at appropriate times
	one should call pushheap() before one starts using heaps and
	popheap() after that (when the memory allocated on the heaps since
	the last pushheap() isn't needed anymore).
	pushheap() saves the states of all currently allocated heaps and
	popheap() resets them to the last state saved and destroys the
	information about that state.  If you called pushheap() and
	allocated some memory on the heaps and then come to a place where
	you don't need the allocated memory anymore but you still want
	to allocate memory on the heap, you should call freeheap().  This
	works like popheap(), only that it doesn't free the information
	about the heap states (i.e. the heaps are like after the call to
	pushheap() and you have to call popheap some time later).

	Memory allocated in this way does not have to be freed explicitly;
	it will all be freed when the pool is destroyed.  In fact,
	attempting to free this memory may result in a core dump.

	If possible, the heaps are allocated using mmap() so that the
	(*real*) heap isn't filled up with empty zsh heaps. If mmap()
	is not available and zsh's own allocator is used, we use a simple trick
	to avoid that: we allocate a large block of memory before allocating
	a heap pool, this memory is freed again immediately after the pool
	is allocated. If there are only small blocks on the free list this
	guarantees that the memory for the pool is at the end of the memory
	which means that we can give it back to the system when the pool is
	freed.

	hrealloc(char *p, size_t old, size_t new) is an optimisation
	with a similar interface to realloc().  Typically the new size
	will be larger than the old one, since there is no gain in
	shrinking the allocation (indeed, that will confused hrealloc()
	since it will forget that the unused space once belonged to this
	pointer).  However, new == 0 is a special case; then if we
	had to allocate a special heap for this memory it is freed at
	that point.
*/

#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MMAP) && defined(HAVE_MUNMAP)

#include <sys/mman.h>

#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
#define MAP_ANONYMOUS MAP_ANON
#endif

#if defined(MAP_ANONYMOUS) && defined(MAP_PRIVATE)

#define USE_MMAP 1
#define MMAP_FLAGS (MAP_ANONYMOUS | MAP_PRIVATE)

#endif
#endif

#ifdef ZSH_MEM_WARNING
# ifndef DEBUG
#  define DEBUG 1
# endif
#endif

#if defined(ZSH_MEM) && defined(ZSH_MEM_DEBUG)

static int h_m[1025], h_push, h_pop, h_free;

#endif

/* Make sure we align to the longest fundamental type. */
union mem_align {
    zlong l;
    double d;
};

#define H_ISIZE  sizeof(union mem_align)
#define HEAPSIZE (16384 - H_ISIZE)
/* Memory available for user data in default arena size */
#define HEAP_ARENA_SIZE (HEAPSIZE - sizeof(struct heap))
#define HEAPFREE (16384 - H_ISIZE)

/* Memory available for user data in heap h */
#define ARENA_SIZEOF(h) ((h)->size - sizeof(struct heap))

/* list of zsh heaps */

static Heap heaps;

/* a heap with free space, not always correct (it will be the last heap
 * if that was newly allocated but it may also be another one) */

static Heap fheap;

/**/
#ifdef ZSH_HEAP_DEBUG
/*
 * The heap ID we'll allocate next.
 *
 * We'll avoid using 0 as that means zero-initialised memory
 * containing a heap ID is (correctly) marked as invalid.
 */
static Heapid next_heap_id = (Heapid)1;

/*
 * The ID of the heap from which we last allocated heap memory.
 * In theory, since we carefully avoid allocating heap memory during
 * interrupts, after any call to zhalloc() or wrappers this should
 * be the ID of the heap containing the memory just returned.
 */
/**/
mod_export Heapid last_heap_id;

/*
 * Stack of heaps saved by new_heaps().
 * Assumes old_heaps() will come along and restore it later
 * (outputs an error if old_heaps() is called out of sequence).
 */
LinkList heaps_saved;

/*
 * Debugging verbosity.  This must be set from a debugger.
 * An 'or' of bits from the enum heap_debug_verbosity.
 */
volatile int heap_debug_verbosity;

/*
 * Generate a heap identifier that's unique up to unsigned integer wrap.
 *
 * For the purposes of debugging we won't bother trying to make a
 * heap_id globally unique, which would require checking all existing
 * heaps every time we create an ID and still wouldn't do what we
 * ideally want, which is to make sure the IDs of valid heaps are
 * different from the IDs of no-longer-valid heaps.  Given that,
 * we'll just assume that if we haven't tracked the problem when the
 * ID wraps we're out of luck.  We could change the type to a long long
 * if we wanted more room
 */

static Heapid
new_heap_id(void)
{
    return next_heap_id++;
}

/**/
#endif

/* Use new heaps from now on. This returns the old heap-list. */

/**/
mod_export Heap
new_heaps(void)
{
    Heap h;

    queue_signals();
    h = heaps;

    fheap = heaps = NULL;
    unqueue_signals();

#ifdef ZSH_HEAP_DEBUG
    if (heap_debug_verbosity & HDV_NEW) {
	fprintf(stderr, "HEAP DEBUG: heap " HEAPID_FMT
		" saved, new heaps created.\n", h->heap_id);
    }
    if (!heaps_saved)
	heaps_saved = znewlinklist();
    zpushnode(heaps_saved, h);
#endif
    return h;
}

/* Re-install the old heaps again, freeing the new ones. */

/**/
mod_export void
old_heaps(Heap old)
{
    Heap h, n;

    queue_signals();
    for (h = heaps; h; h = n) {
	n = h->next;
	DPUTS(h->sp, "BUG: old_heaps() with pushed heaps");
#ifdef ZSH_HEAP_DEBUG
	if (heap_debug_verbosity & HDV_FREE) {
	    fprintf(stderr, "HEAP DEBUG: heap " HEAPID_FMT
		    "freed in old_heaps().\n", h->heap_id);
	}
#endif
#ifdef USE_MMAP
	munmap((void *) h, h->size);
#else
	zfree(h, HEAPSIZE);
#endif
#ifdef ZSH_VALGRIND
	VALGRIND_DESTROY_MEMPOOL((char *)h);
#endif
    }
    heaps = old;
#ifdef ZSH_HEAP_DEBUG
    if (heap_debug_verbosity & HDV_OLD) {
	fprintf(stderr, "HEAP DEBUG: heap " HEAPID_FMT
		"restored.\n", heaps->heap_id);
    }
    {
	Heap myold = heaps_saved ? getlinknode(heaps_saved) : NULL;
	if (old != myold)
	{
	    fprintf(stderr, "HEAP DEBUG: invalid old heap " HEAPID_FMT
		    ", expecting " HEAPID_FMT ".\n", old->heap_id,
		    myold->heap_id);
	}
    }
#endif
    fheap = NULL;
    unqueue_signals();
}

/* Temporarily switch to other heaps (or back again). */

/**/
mod_export Heap
switch_heaps(Heap new)
{
    Heap h;

    queue_signals();
    h = heaps;

#ifdef ZSH_HEAP_DEBUG
    if (heap_debug_verbosity & HDV_SWITCH) {
	fprintf(stderr, "HEAP DEBUG: heap temporarily switched from "
		HEAPID_FMT " to " HEAPID_FMT ".\n", h->heap_id, new->heap_id);
    }
#endif
    heaps = new;
    fheap = NULL;
    unqueue_signals();

    return h;
}

/* save states of zsh heaps */

/**/
mod_export void
pushheap(void)
{
    Heap h;
    Heapstack hs;

    queue_signals();

#if defined(ZSH_MEM) && defined(ZSH_MEM_DEBUG)
    h_push++;
#endif

    for (h = heaps; h; h = h->next) {
	DPUTS(!h->used && h->next, "BUG: empty heap");
	hs = (Heapstack) zalloc(sizeof(*hs));
	hs->next = h->sp;
	h->sp = hs;
	hs->used = h->used;
#ifdef ZSH_HEAP_DEBUG
	hs->heap_id = h->heap_id;
	h->heap_id = new_heap_id();
	if (heap_debug_verbosity & HDV_PUSH) {
	    fprintf(stderr, "HEAP DEBUG: heap " HEAPID_FMT " pushed, new id is "
		    HEAPID_FMT ".\n",
		    hs->heap_id, h->heap_id);
	}
#endif
    }
    unqueue_signals();
}

/* reset heaps to previous state */

/**/
mod_export void
freeheap(void)
{
    Heap h, hn, hl = NULL;

    queue_signals();

#if defined(ZSH_MEM) && defined(ZSH_MEM_DEBUG)
    h_free++;
#endif

    /*
     * When pushheap() is called, it sweeps over the entire heaps list of
     * arenas and marks every one of them with the amount of free space in
     * that arena at that moment.  zhalloc() is then allowed to grab bits
     * out of any of those arenas that have free space.
     *
     * Whenever fheap is NULL here, the loop below sweeps back over the
     * entire heap list again, resetting the free space in every arena to
     * the amount stashed by pushheap() and finding the arena with the most
     * free space to optimize zhalloc()'s next search.  When there's a lot
     * of stuff already on the heap, this is an enormous amount of work,
     * and performance goes to hell.
     *
     * Therefore, we defer freeing the most recently allocated arena until
     * we reach popheap().  This may fail to reclaim some space in earlier
     * arenas.
     *
     * However, if the arena to which fheap points is unused, we want to
     * free it, so we have no choice but to do the sweep for a new fheap.
     */
    if (fheap && !fheap->sp)
       fheap = NULL;   /* We used to do this unconditionally */
    /*
     * In other cases, either fheap is already correct, or it has never
     * been set and this loop will do it, or it'll be reset from scratch
     * on the next popheap().  So all that's needed here is to pick up
     * the scan wherever the last pass [or the last popheap()] left off.
     */
    for (h = (fheap ? fheap : heaps); h; h = hn) {
	hn = h->next;
	if (h->sp) {
#ifdef ZSH_MEM_DEBUG
#ifdef ZSH_VALGRIND
	    VALGRIND_MAKE_MEM_UNDEFINED((char *)arena(h) + h->sp->used,
					h->used - h->sp->used);
#endif
	    memset(arena(h) + h->sp->used, 0xff, h->used - h->sp->used);
#endif
	    h->used = h->sp->used;
	    if (!fheap) {
		if (h->used < ARENA_SIZEOF(h))
		    fheap = h;
	    } else if (ARENA_SIZEOF(h) - h->used >
		       ARENA_SIZEOF(fheap) - fheap->used)
		fheap = h;
	    hl = h;
#ifdef ZSH_HEAP_DEBUG
	    /*
	     * As the free makes the heap invalid, give it a new
	     * identifier.  We're not popping it, so don't use
	     * the one in the heap stack.
	     */
	    {
		Heapid new_id = new_heap_id();
		if (heap_debug_verbosity & HDV_FREE) {
		    fprintf(stderr, "HEAP DEBUG: heap " HEAPID_FMT
			    " freed, new id is " HEAPID_FMT ".\n",
			    h->heap_id, new_id);
		}
		h->heap_id = new_id;
	    }
#endif
#ifdef ZSH_VALGRIND
	    VALGRIND_MEMPOOL_TRIM((char *)h, (char *)arena(h), h->used);
#endif
	} else {
	    if (fheap == h)
		fheap = NULL;
	    if (h->next) {
		/* We want to cut this out of the arena list if we can */
		if (h == heaps)
		    hl = heaps = h->next;
		else if (hl && hl->next == h)
		    hl->next = h->next;
		else {
		    DPUTS(hl, "hl->next != h when freeing");
		    hl = h;
		    continue;
		}
		h->next = NULL;
	    } else {
		/* Leave an empty arena at the end until popped */
		h->used = 0;
		fheap = hl = h;
		break;
	    }
#ifdef USE_MMAP
	    munmap((void *) h, h->size);
#else
	    zfree(h, HEAPSIZE);
#endif
#ifdef ZSH_VALGRIND
	    VALGRIND_DESTROY_MEMPOOL((char *)h);
#endif
	}
    }
    if (hl)
	hl->next = NULL;
    else
	heaps = fheap = NULL;

    unqueue_signals();
}

/* reset heap to previous state and destroy state information */

/**/
mod_export void
popheap(void)
{
    Heap h, hn, hl = NULL;
    Heapstack hs;

    queue_signals();

#if defined(ZSH_MEM) && defined(ZSH_MEM_DEBUG)
    h_pop++;
#endif

    fheap = NULL;
    for (h = heaps; h; h = hn) {
	hn = h->next;
	if ((hs = h->sp)) {
	    h->sp = hs->next;
#ifdef ZSH_MEM_DEBUG
#ifdef ZSH_VALGRIND
	    VALGRIND_MAKE_MEM_UNDEFINED((char *)arena(h) + hs->used,
					h->used - hs->used);
#endif
	    memset(arena(h) + hs->used, 0xff, h->used - hs->used);
#endif
	    h->used = hs->used;
#ifdef ZSH_HEAP_DEBUG
	    if (heap_debug_verbosity & HDV_POP) {
		fprintf(stderr, "HEAP DEBUG: heap " HEAPID_FMT
			" popped, old heap was " HEAPID_FMT ".\n",
			h->heap_id, hs->heap_id);
	    }
	    h->heap_id = hs->heap_id;
#endif
#ifdef ZSH_VALGRIND
	    VALGRIND_MEMPOOL_TRIM((char *)h, (char *)arena(h), h->used);
#endif
	    if (!fheap) {
		if (h->used < ARENA_SIZEOF(h))
		    fheap = h;
	    } else if (ARENA_SIZEOF(h) - h->used >
		       ARENA_SIZEOF(fheap) - fheap->used)
		fheap = h;
	    zfree(hs, sizeof(*hs));

	    hl = h;
	} else {
	    if (h->next) {
		/* We want to cut this out of the arena list if we can */
		if (h == heaps)
		    hl = heaps = h->next;
		else if (hl && hl->next == h)
		    hl->next = h->next;
		else {
		    DPUTS(hl, "hl->next != h when popping");
		    hl = h;
		    continue;
		}
		h->next = NULL;
	    }
#ifdef USE_MMAP
	    munmap((void *) h, h->size);
#else
	    zfree(h, HEAPSIZE);
#endif
#ifdef ZSH_VALGRIND
	    VALGRIND_DESTROY_MEMPOOL((char *)h);
#endif
	}
    }
    if (hl)
	hl->next = NULL;
    else
	heaps = NULL;

    unqueue_signals();
}

#ifdef USE_MMAP
/*
 * Utility function to allocate a heap area of at least *n bytes.
 * *n will be rounded up to the next page boundary.
 */
static Heap
mmap_heap_alloc(size_t *n)
{
    Heap h;
    static size_t pgsz = 0;

    if (!pgsz) {

#ifdef _SC_PAGESIZE
	pgsz = sysconf(_SC_PAGESIZE);     /* SVR4 */
#else
# ifdef _SC_PAGE_SIZE
	pgsz = sysconf(_SC_PAGE_SIZE);    /* HPUX */
# else
	pgsz = getpagesize();
# endif
#endif

	pgsz--;
    }
    *n = (*n + pgsz) & ~pgsz;
    h = (Heap) mmap(NULL, *n, PROT_READ | PROT_WRITE,
		    MMAP_FLAGS, -1, 0);
    if (h == ((Heap) -1)) {
	zerr("fatal error: out of heap memory");
	exit(1);
    }

    return h;
}
#endif

/* check whether a pointer is within a memory pool */

/**/
mod_export void *
zheapptr(void *p)
{
    Heap h;
    queue_signals();
    for (h = heaps; h; h = h->next)
	if ((char *)p >= arena(h) &&
	    (char *)p + H_ISIZE < arena(h) + ARENA_SIZEOF(h))
	    break;
    unqueue_signals();
    return (h ? p : 0);
}

/* allocate memory from the current memory pool */

/**/
mod_export void *
zhalloc(size_t size)
{
    Heap h, hp = NULL;
    size_t n;
#ifdef ZSH_VALGRIND
    size_t req_size = size;

    if (size == 0)
	return NULL;
#endif

    size = (size + H_ISIZE - 1) & ~(H_ISIZE - 1);

    queue_signals();

#if defined(ZSH_MEM) && defined(ZSH_MEM_DEBUG)
    h_m[size < (1024 * H_ISIZE) ? (size / H_ISIZE) : 1024]++;
#endif

    /* find a heap with enough free space */

    /*
     * This previously assigned:
     *   h = ((fheap && ARENA_SIZEOF(fheap) >= (size + fheap->used))
     *	      ? fheap : heaps);
     * but we think that nothing upstream of fheap has more free space,
     * so why start over at heaps just because fheap has too little?
     */
    for (h = (fheap ? fheap : heaps); h; h = h->next) {
	hp = h;
	if (ARENA_SIZEOF(h) >= (n = size + h->used)) {
	    void *ret;

	    h->used = n;
	    ret = arena(h) + n - size;
	    unqueue_signals();
#ifdef ZSH_HEAP_DEBUG
	    last_heap_id = h->heap_id;
	    if (heap_debug_verbosity & HDV_ALLOC) {
		fprintf(stderr, "HEAP DEBUG: allocated memory from heap "
			HEAPID_FMT ".\n", h->heap_id);
	    }
#endif
#ifdef ZSH_VALGRIND
	    VALGRIND_MEMPOOL_ALLOC((char *)h, (char *)ret, req_size);
#endif
	    return ret;
	}
    }
    {
        /* not found, allocate new heap */
#if defined(ZSH_MEM) && !defined(USE_MMAP)
	static int called = 0;
	void *foo = called ? (void *)malloc(HEAPFREE) : NULL;
            /* tricky, see above */
#endif

	n = HEAP_ARENA_SIZE > size ? HEAPSIZE : size + sizeof(*h);

#ifdef USE_MMAP
	h = mmap_heap_alloc(&n);
#else
	h = (Heap) zalloc(n);
#endif

#if defined(ZSH_MEM) && !defined(USE_MMAP)
	if (called)
	    zfree(foo, HEAPFREE);
	called = 1;
#endif

	h->size = n;
	h->used = size;
	h->next = NULL;
	h->sp = NULL;
#ifdef ZSH_HEAP_DEBUG
	h->heap_id = new_heap_id();
	if (heap_debug_verbosity & HDV_CREATE) {
	    fprintf(stderr, "HEAP DEBUG: create new heap " HEAPID_FMT ".\n",
		    h->heap_id);
	}
#endif
#ifdef ZSH_VALGRIND
	VALGRIND_CREATE_MEMPOOL((char *)h, 0, 0);
	VALGRIND_MAKE_MEM_NOACCESS((char *)arena(h),
				   n - ((char *)arena(h)-(char *)h));
	VALGRIND_MEMPOOL_ALLOC((char *)h, (char *)arena(h), req_size);
#endif

	DPUTS(hp && hp->next, "failed to find end of chain in zhalloc");
	if (hp)
	    hp->next = h;
	else
	    heaps = h;
	fheap = h;

	unqueue_signals();
#ifdef ZSH_HEAP_DEBUG
	last_heap_id = h->heap_id;
	if (heap_debug_verbosity & HDV_ALLOC) {
	    fprintf(stderr, "HEAP DEBUG: allocated memory from heap "
		    HEAPID_FMT ".\n", h->heap_id);
	}
#endif
	return arena(h);
    }
}

/**/
mod_export void *
hrealloc(char *p, size_t old, size_t new)
{
    Heap h, ph;

#ifdef ZSH_VALGRIND
    size_t new_req = new;
#endif

    old = (old + H_ISIZE - 1) & ~(H_ISIZE - 1);
    new = (new + H_ISIZE - 1) & ~(H_ISIZE - 1);

    if (old == new)
	return p;
    if (!old && !p)
#ifdef ZSH_VALGRIND
	return zhalloc(new_req);
#else
	return zhalloc(new);
#endif

    /* find the heap with p */

    queue_signals();
    for (h = heaps, ph = NULL; h; ph = h, h = h->next)
	if (p >= arena(h) && p < arena(h) + ARENA_SIZEOF(h))
	    break;

    DPUTS(!h, "BUG: hrealloc() called for non-heap memory.");
    DPUTS(h->sp && arena(h) + h->sp->used > p,
	  "BUG: hrealloc() wants to realloc pushed memory");

    /*
     * If the end of the old chunk is before the used pointer,
     * more memory has been zhalloc'ed afterwards.
     * We can't tell if that's still in use, obviously, since
     * that's the whole point of heap memory.
     * We have no choice other than to grab some more memory
     * somewhere else and copy in the old stuff.
     */
    if (p + old < arena(h) + h->used) {
	if (new > old) {
#ifdef ZSH_VALGRIND
	    char *ptr = (char *) zhalloc(new_req);
#else
	    char *ptr = (char *) zhalloc(new);
#endif
	    memcpy(ptr, p, old);
#ifdef ZSH_MEM_DEBUG
	    memset(p, 0xff, old);
#endif
#ifdef ZSH_VALGRIND
	    VALGRIND_MEMPOOL_FREE((char *)h, (char *)p);
	    /*
	     * zhalloc() marked h,ptr,new as an allocation so we don't
	     * need to do that here.
	     */
#endif
	    unqueue_signals();
	    return ptr;
	} else {
#ifdef ZSH_VALGRIND
	    VALGRIND_MEMPOOL_FREE((char *)h, (char *)p);
	    if (p) {
		VALGRIND_MEMPOOL_ALLOC((char *)h, (char *)p,
				       new_req);
		VALGRIND_MAKE_MEM_DEFINED((char *)h, (char *)p);
	    }
#endif
	    unqueue_signals();
	    return new ? p : NULL;
	}
    }

    DPUTS(p + old != arena(h) + h->used, "BUG: hrealloc more than allocated");

    /*
     * We now know there's nothing afterwards in the heap, now see if
     * there's nothing before.  Then we can reallocate the whole thing.
     * Otherwise, we need to keep the stuff at the start of the heap,
     * then allocate a new one too; this is handled below.  (This will
     * guarantee we occupy a full heap next time round, provided we
     * don't use the heap for anything else.)
     */
    if (p == arena(h)) {
#ifdef ZSH_HEAP_DEBUG
	Heapid heap_id = h->heap_id;
#endif
	/*
	 * Zero new seems to be a special case saying we've finished
	 * with the specially reallocated memory, see scanner() in glob.c.
	 */
	if (!new) {
	    if (ph)
		ph->next = h->next;
	    else
		heaps = h->next;
	    fheap = NULL;
#ifdef USE_MMAP
	    munmap((void *) h, h->size);
#else
	    zfree(h, HEAPSIZE);
#endif
#ifdef ZSH_VALGRIND
	    VALGRIND_DESTROY_MEMPOOL((char *)h);
#endif
	    unqueue_signals();
	    return NULL;
	}
	if (new > ARENA_SIZEOF(h)) {
	    Heap hnew;
	    /*
	     * Not enough memory in this heap.  Allocate a new
	     * one of sufficient size.
	     *
	     * To avoid this happening too often, allocate
	     * chunks in multiples of HEAPSIZE.
	     * (Historical note:  there didn't used to be any
	     * point in this since we didn't consistently record
	     * the allocated size of the heap, but now we do.)
	     */
	    size_t n = (new + sizeof(*h) + HEAPSIZE);
	    n -= n % HEAPSIZE;
	    fheap = NULL;

#ifdef USE_MMAP
	    {
		/*
		 * I don't know any easy portable way of requesting
		 * a mmap'd segment be extended, so simply allocate
		 * a new one and copy.
		 */
		hnew = mmap_heap_alloc(&n);
		/* Copy the entire heap, header (with next pointer) included */
		memcpy(hnew, h, h->size);
		munmap((void *)h, h->size);
	    }
#else
	    hnew = (Heap) realloc(h, n);
#endif
#ifdef ZSH_VALGRIND
	    VALGRIND_MEMPOOL_FREE((char *)h, p);
	    VALGRIND_DESTROY_MEMPOOL((char *)h);
	    VALGRIND_CREATE_MEMPOOL((char *)hnew, 0, 0);
	    VALGRIND_MEMPOOL_ALLOC((char *)hnew, (char *)arena(hnew),
				   new_req);
	    VALGRIND_MAKE_MEM_DEFINED((char *)hnew, (char *)arena(hnew));
#endif
	    h = hnew;

	    h->size = n;
	    if (ph)
		ph->next = h;
	    else
		heaps = h;
	}
#ifdef ZSH_VALGRIND
	else {
	    VALGRIND_MEMPOOL_FREE((char *)h, (char *)p);
	    VALGRIND_MEMPOOL_ALLOC((char *)h, (char *)p, new_req);
	    VALGRIND_MAKE_MEM_DEFINED((char *)h, (char *)p);
	}
#endif
	h->used = new;
#ifdef ZSH_HEAP_DEBUG
	h->heap_id = heap_id;
#endif
	unqueue_signals();
	return arena(h);
    }
#ifndef USE_MMAP
    DPUTS(h->used > ARENA_SIZEOF(h), "BUG: hrealloc at invalid address");
#endif
    if (h->used + (new - old) <= ARENA_SIZEOF(h)) {
	h->used += new - old;
	unqueue_signals();
#ifdef ZSH_VALGRIND
	VALGRIND_MEMPOOL_FREE((char *)h, (char *)p);
	VALGRIND_MEMPOOL_ALLOC((char *)h, (char *)p, new_req);
	VALGRIND_MAKE_MEM_DEFINED((char *)h, (char *)p);
#endif
	return p;
    } else {
	char *t = zhalloc(new);
	memcpy(t, p, old > new ? new : old);
	h->used -= old;
#ifdef ZSH_MEM_DEBUG
	memset(p, 0xff, old);
#endif
#ifdef ZSH_VALGRIND
	VALGRIND_MEMPOOL_FREE((char *)h, (char *)p);
	/* t already marked as allocated by zhalloc() */
#endif
	unqueue_signals();
	return t;
    }
}

/**/
#ifdef ZSH_HEAP_DEBUG
/*
 * Check if heap_id is the identifier of a currently valid heap,
 * including any heap buried on the stack, or of permanent memory.
 * Return 0 if so, else 1.
 *
 * This gets confused by use of switch_heaps().  That's because so do I.
 */

/**/
mod_export int
memory_validate(Heapid heap_id)
{
    Heap h;
    Heapstack hs;
    LinkNode node;

    if (heap_id == HEAPID_PERMANENT)
	return 0;

    queue_signals();
    for (h = heaps; h; h = h->next) {
	if (h->heap_id == heap_id)
	    return 0;
	for (hs = heaps->sp; hs; hs = hs->next) {
	    if (hs->heap_id == heap_id)
		return 0;
	}
    }

    if (heaps_saved) {
	for (node = firstnode(heaps_saved); node; incnode(node)) {
	    for (h = (Heap)getdata(node); h; h = h->next) {
		if (h->heap_id == heap_id)
		    return 0;
		for (hs = heaps->sp; hs; hs = hs->next) {
		    if (hs->heap_id == heap_id)
			return 0;
		}
	    }
	}
    }

    return 1;
}
/**/
#endif

/* allocate memory from the current memory pool and clear it */

/**/
mod_export void *
hcalloc(size_t size)
{
    void *ptr;

    ptr = zhalloc(size);
    memset(ptr, 0, size);
    return ptr;
}

/* allocate permanent memory */

/**/
mod_export void *
zalloc(size_t size)
{
    void *ptr;

    if (!size)
	size = 1;
    queue_signals();
    if (!(ptr = (void *) malloc(size))) {
	zerr("fatal error: out of memory");
	exit(1);
    }
    unqueue_signals();

    return ptr;
}

/**/
mod_export void *
zshcalloc(size_t size)
{
    void *ptr;

    if (!size)
	size = 1;
    queue_signals();
    if (!(ptr = (void *) malloc(size))) {
	zerr("fatal error: out of memory");
	exit(1);
    }
    unqueue_signals();
    memset(ptr, 0, size);

    return ptr;
}

/* This front-end to realloc is used to make sure we have a realloc *
 * that conforms to POSIX realloc.  Older realloc's can fail if     *
 * passed a NULL pointer, but POSIX realloc should handle this.  A  *
 * better solution would be for configure to check if realloc is    *
 * POSIX compliant, but I'm not sure how to do that.                */

/**/
mod_export void *
zrealloc(void *ptr, size_t size)
{
    queue_signals();
    if (ptr) {
	if (size) {
	    /* Do normal realloc */
	    if (!(ptr = (void *) realloc(ptr, size))) {
		zerr("fatal error: out of memory");
		exit(1);
	    }
	    unqueue_signals();
	    return ptr;
	}
	else
	    /* If ptr is not NULL, but size is zero, *
	     * then object pointed to is freed.      */
	    free(ptr);

	ptr = NULL;
    } else {
	/* If ptr is NULL, then behave like malloc */
        if (!(ptr = (void *) malloc(size))) {
            zerr("fatal error: out of memory");
            exit(1);
        }
    }
    unqueue_signals();

    return ptr;
}

/**/
#ifdef ZSH_MEM

/*
   Below is a simple segment oriented memory allocator for systems on
   which it is better than the system's one. Memory is given in blocks
   aligned to an integer multiple of sizeof(union mem_align), which will
   probably be 64-bit as it is the longer of zlong or double. Each block is
   preceded by a header which contains the length of the data part (in
   bytes). In allocated blocks only this field of the structure m_hdr is
   senseful. In free blocks the second field (next) is a pointer to the next
   free segment on the free list.

   On top of this simple allocator there is a second allocator for small
   chunks of data. It should be both faster and less space-consuming than
   using the normal segment mechanism for such blocks.
   For the first M_NSMALL-1 possible sizes memory is allocated in arrays
   that can hold M_SNUM blocks. Each array is stored in one segment of the
   main allocator. In these segments the third field of the header structure
   (free) contains a pointer to the first free block in the array. The
   last field (used) gives the number of already used blocks in the array.

   If the macro name ZSH_MEM_DEBUG is defined, some information about the memory
   usage is stored. This information can than be viewed by calling the
   builtin `mem' (which is only available if ZSH_MEM_DEBUG is set).

   If ZSH_MEM_WARNING is defined, error messages are printed in case of errors.

   If ZSH_SECURE_FREE is defined, free() checks if the given address is really
   one that was returned by malloc(), it ignores it if it wasn't (printing
   an error message if ZSH_MEM_WARNING is also defined).
*/
#if !defined(__hpux) && !defined(DGUX) && !defined(__osf__)
# if defined(_BSD)
#  ifndef HAVE_BRK_PROTO
   extern int brk _((caddr_t));
#  endif
#  ifndef HAVE_SBRK_PROTO
   extern caddr_t sbrk _((int));
#  endif
# else
#  ifndef HAVE_BRK_PROTO
   extern int brk _((void *));
#  endif
#  ifndef HAVE_SBRK_PROTO
   extern void *sbrk _((int));
#  endif
# endif
#endif

#if defined(_BSD) && !defined(STDC_HEADERS)
# define FREE_RET_T   int
# define FREE_ARG_T   char *
# define FREE_DO_RET
# define MALLOC_RET_T char *
# define MALLOC_ARG_T size_t
#else
# define FREE_RET_T   void
# define FREE_ARG_T   void *
# define MALLOC_RET_T void *
# define MALLOC_ARG_T size_t
#endif

/* structure for building free list in blocks holding small blocks */

struct m_shdr {
    struct m_shdr *next;	/* next one on free list */
#ifdef PAD_64_BIT
    /* dummy to make this 64-bit aligned */
    struct m_shdr *dummy;
#endif
};

struct m_hdr {
    zlong len;			/* length of memory block */
#if defined(PAD_64_BIT) && !defined(ZSH_64_BIT_TYPE)
    /* either 1 or 2 zlong's, whichever makes up 64 bits. */
    zlong dummy1;
#endif
    struct m_hdr *next;		/* if free: next on free list
				   if block of small blocks: next one with
				                 small blocks of same size*/
    struct m_shdr *free;	/* if block of small blocks: free list */
    zlong used;			/* if block of small blocks: number of used
				                                     blocks */
#if defined(PAD_64_BIT) && !defined(ZSH_64_BIT_TYPE)
    zlong dummy2;
#endif
};


/* alignment for memory blocks */

#define M_ALIGN (sizeof(union mem_align))

/* length of memory header, length of first field of memory header and
   minimal size of a block left free (if we allocate memory and take a
   block from the free list that is larger than needed, it must have at
   least M_MIN extra bytes to be splitted; if it has, the rest is put on
   the free list) */

#define M_HSIZE (sizeof(struct m_hdr))
#if defined(PAD_64_BIT) && !defined(ZSH_64_BIT_TYPE)
# define M_ISIZE (2*sizeof(zlong))
#else
# define M_ISIZE (sizeof(zlong))
#endif
#define M_MIN   (2 * M_ISIZE)

/* M_FREE  is the number of bytes that have to be free before memory is
 *         given back to the system
 * M_KEEP  is the number of bytes that will be kept when memory is given
 *         back; note that this has to be less than M_FREE
 * M_ALLOC is the number of extra bytes to request from the system */

#define M_FREE  32768
#define M_KEEP  16384
#define M_ALLOC M_KEEP

/* a pointer to the last free block, a pointer to the free list (the blocks
   on this list are kept in order - lowest address first) */

static struct m_hdr *m_lfree, *m_free;

/* system's pagesize */

static long m_pgsz = 0;

/* the highest and the lowest valid memory addresses, kept for fast validity
   checks in free() and to find out if and when we can give memory back to
   the system */

static char *m_high, *m_low;

/* Management of blocks for small blocks:
   Such blocks are kept in lists (one list for each of the sizes that are
   allocated in such blocks).  The lists are stored in the m_small array.
   M_SIDX() calculates the index into this array for a given size.  M_SNUM
   is the size (in small blocks) of such blocks.  M_SLEN() calculates the
   size of the small blocks held in a memory block, given a pointer to the
   header of it.  M_SBLEN() gives the size of a memory block that can hold
   an array of small blocks, given the size of these small blocks.  M_BSLEN()
   calculates the size of the small blocks held in a memory block, given the
   length of that block (including the header of the memory block.  M_NSMALL
   is the number of possible block sizes that small blocks should be used
   for. */


#define M_SIDX(S)  ((S) / M_ISIZE)
#define M_SNUM     128
#define M_SLEN(M)  ((M)->len / M_SNUM)
#if defined(PAD_64_BIT) && !defined(ZSH_64_BIT_TYPE)
/* Include the dummy in the alignment */
#define M_SBLEN(S) ((S) * M_SNUM + sizeof(struct m_shdr *) +  \
		    2*sizeof(zlong) + sizeof(struct m_hdr *))
#define M_BSLEN(S) (((S) - sizeof(struct m_shdr *) -  \
		     2*sizeof(zlong) - sizeof(struct m_hdr *)) / M_SNUM)
#else
#define M_SBLEN(S) ((S) * M_SNUM + sizeof(struct m_shdr *) +  \
		    sizeof(zlong) + sizeof(struct m_hdr *))
#define M_BSLEN(S) (((S) - sizeof(struct m_shdr *) -  \
		     sizeof(zlong) - sizeof(struct m_hdr *)) / M_SNUM)
#endif
#define M_NSMALL    8

static struct m_hdr *m_small[M_NSMALL];

#ifdef ZSH_MEM_DEBUG

static int m_s = 0, m_b = 0;
static int m_m[1025], m_f[1025];

static struct m_hdr *m_l;

#endif /* ZSH_MEM_DEBUG */

MALLOC_RET_T
malloc(MALLOC_ARG_T size)
{
    struct m_hdr *m, *mp, *mt;
    long n, s, os = 0;
#ifndef USE_MMAP
    struct heap *h, *hp, *hf = NULL, *hfp = NULL;
#endif

    /* some systems want malloc to return the highest valid address plus one
       if it is called with an argument of zero.
    
       TODO: really?  Suppose we allocate more memory, so
       that this is now in bounds, then a more rational application
       that thinks it can free() anything it malloc'ed, even
       of zero length, calls free for it?  Aren't we in big
       trouble?  Wouldn't it be safer just to allocate some
       memory anyway?

       If the above comment is really correct, then at least
       we need to check in free() if we're freeing memory
       at m_high.
    */

    if (!size)
#if 1
	size = 1;
#else
	return (MALLOC_RET_T) m_high;
#endif

    queue_signals();  /* just queue signals rather than handling them */

    /* first call, get page size */

    if (!m_pgsz) {

#ifdef _SC_PAGESIZE
	m_pgsz = sysconf(_SC_PAGESIZE);     /* SVR4 */
#else
# ifdef _SC_PAGE_SIZE
	m_pgsz = sysconf(_SC_PAGE_SIZE);    /* HPUX */
# else
	m_pgsz = getpagesize();
# endif
#endif

	m_free = m_lfree = NULL;
    }
    size = (size + M_ALIGN - 1) & ~(M_ALIGN - 1);

    /* Do we need a small block? */

    if ((s = M_SIDX(size)) && s < M_NSMALL) {
	/* yep, find a memory block with free small blocks of the
	   appropriate size (if we find it in this list, this means that
	   it has room for at least one more small block) */
	for (mp = NULL, m = m_small[s]; m && !m->free; mp = m, m = m->next);

	if (m) {
	    /* we found one */
	    struct m_shdr *sh = m->free;

	    m->free = sh->next;
	    m->used++;

	    /* if all small blocks in this block are allocated, the block is 
	       put at the end of the list blocks with small blocks of this
	       size (i.e., we try to keep blocks with free blocks at the
	       beginning of the list, to make the search faster) */

	    if (m->used == M_SNUM && m->next) {
		for (mt = m; mt->next; mt = mt->next);

		mt->next = m;
		if (mp)
		    mp->next = m->next;
		else
		    m_small[s] = m->next;
		m->next = NULL;
	    }
#ifdef ZSH_MEM_DEBUG
	    m_m[size / M_ISIZE]++;
#endif

	    unqueue_signals();
	    return (MALLOC_RET_T) sh;
	}
	/* we still want a small block but there were no block with a free
	   small block of the requested size; so we use the real allocation
	   routine to allocate a block for small blocks of this size */
	os = size;
	size = M_SBLEN(size);
    } else
	s = 0;

    /* search the free list for an block of at least the requested size */
    for (mp = NULL, m = m_free; m && m->len < size; mp = m, m = m->next);

#ifndef USE_MMAP

    /* if there is an empty zsh heap at a lower address we steal it and take
       the memory from it, putting the rest on the free list (remember
       that the blocks on the free list are ordered) */

    for (hp = NULL, h = heaps; h; hp = h, h = h->next)
	if (!h->used &&
	    (!hf || h < hf) &&
	    (!m || ((char *)m) > ((char *)h)))
	    hf = h, hfp = hp;

    if (hf) {
	/* we found such a heap */
	Heapstack hso, hsn;

	/* delete structures on the list holding the heap states */
	for (hso = hf->sp; hso; hso = hsn) {
	    hsn = hso->next;
	    zfree(hso, sizeof(*hso));
	}
	/* take it from the list of heaps */
	if (hfp)
	    hfp->next = hf->next;
	else
	    heaps = hf->next;
	/* now we simply free it and than search the free list again */
	zfree(hf, HEAPSIZE);

	for (mp = NULL, m = m_free; m && m->len < size; mp = m, m = m->next);
    }
#endif
    if (!m) {
	long nal;
	/* no matching free block was found, we have to request new
	   memory from the system */
	n = (size + M_HSIZE + M_ALLOC + m_pgsz - 1) & ~(m_pgsz - 1);

	if (((char *)(m = (struct m_hdr *)sbrk(n))) == ((char *)-1)) {
	    DPUTS1(1, "MEM: allocation error at sbrk, size %L.", n);
	    unqueue_signals();
	    return NULL;
	}
	if ((nal = ((long)(char *)m) & (M_ALIGN-1))) {
	    if ((char *)sbrk(M_ALIGN - nal) == (char *)-1) {
		DPUTS(1, "MEM: allocation error at sbrk.");
		unqueue_signals();
		return NULL;
	    }
	    m = (struct m_hdr *) ((char *)m + (M_ALIGN - nal));
	}
	/* set m_low, for the check in free() */
	if (!m_low)
	    m_low = (char *)m;

#ifdef ZSH_MEM_DEBUG
	m_s += n;

	if (!m_l)
	    m_l = m;
#endif

	/* save new highest address */
	m_high = ((char *)m) + n;

	/* initialize header */
	m->len = n - M_ISIZE;
	m->next = NULL;

	/* put it on the free list and set m_lfree pointing to it */
	if ((mp = m_lfree))
	    m_lfree->next = m;
	m_lfree = m;
    }
    if ((n = m->len - size) > M_MIN) {
	/* the block we want to use has more than M_MIN bytes plus the
	   number of bytes that were requested; we split it in two and
	   leave the rest on the free list */
	struct m_hdr *mtt = (struct m_hdr *)(((char *)m) + M_ISIZE + size);

	mtt->len = n - M_ISIZE;
	mtt->next = m->next;

	m->len = size;

	/* put the rest on the list */
	if (m_lfree == m)
	    m_lfree = mtt;

	if (mp)
	    mp->next = mtt;
	else
	    m_free = mtt;
    } else if (mp) {
	/* the block we found wasn't the first one on the free list */
	if (m == m_lfree)
	    m_lfree = mp;
	mp->next = m->next;
    } else {
	/* it was the first one */
	m_free = m->next;
	if (m == m_lfree)
	    m_lfree = m_free;
    }

    if (s) {
	/* we are allocating a block that should hold small blocks */
	struct m_shdr *sh, *shn;

	/* build the free list in this block and set `used' filed */
	m->free = sh = (struct m_shdr *)(((char *)m) +
					 sizeof(struct m_hdr) + os);

	for (n = M_SNUM - 2; n--; sh = shn)
	    shn = sh->next = sh + s;
	sh->next = NULL;

	m->used = 1;

	/* put the block on the list of blocks holding small blocks if
	   this size */
	m->next = m_small[s];
	m_small[s] = m;

#ifdef ZSH_MEM_DEBUG
	m_m[os / M_ISIZE]++;
#endif

	unqueue_signals();
	return (MALLOC_RET_T) (((char *)m) + sizeof(struct m_hdr));
    }
#ifdef ZSH_MEM_DEBUG
    m_m[m->len < (1024 * M_ISIZE) ? (m->len / M_ISIZE) : 1024]++;
#endif

    unqueue_signals();
    return (MALLOC_RET_T) & m->next;
}

/* this is an internal free(); the second argument may, but need not hold
   the size of the block the first argument is pointing to; if it is the
   right size of this block, freeing it will be faster, though; the value
   0 for this parameter means: `don't know' */

/**/
mod_export void
zfree(void *p, int sz)
{
    struct m_hdr *m = (struct m_hdr *)(((char *)p) - M_ISIZE), *mp, *mt = NULL;
    int i;
# ifdef DEBUG
    int osz = sz;
# endif

#ifdef ZSH_SECURE_FREE
    sz = 0;
#else
    sz = (sz + M_ALIGN - 1) & ~(M_ALIGN - 1);
#endif

    if (!p)
	return;

    /* first a simple check if the given address is valid */
    if (((char *)p) < m_low || ((char *)p) > m_high ||
	((long)p) & (M_ALIGN - 1)) {
	DPUTS(1, "BUG: attempt to free storage at invalid address");
	return;
    }

    queue_signals();

  fr_rec:

    if ((i = sz / M_ISIZE) < M_NSMALL || !sz)
	/* if the given sizes says that it is a small block, find the
	   memory block holding it; we search all blocks with blocks
	   of at least the given size; if the size parameter is zero,
	   this means, that all blocks are searched */
	for (; i < M_NSMALL; i++) {
	    for (mp = NULL, mt = m_small[i];
		 mt && (((char *)mt) > ((char *)p) ||
			(((char *)mt) + mt->len) < ((char *)p));
		 mp = mt, mt = mt->next);

	    if (mt) {
		/* we found the block holding the small block */
		struct m_shdr *sh = (struct m_shdr *)p;

#ifdef ZSH_SECURE_FREE
		struct m_shdr *sh2;

		/* check if the given address is equal to the address of
		   the first small block plus an integer multiple of the
		   block size */
		if ((((char *)p) - (((char *)mt) + sizeof(struct m_hdr))) %
		    M_BSLEN(mt->len)) {

		    DPUTS(1, "BUG: attempt to free storage at invalid address");
		    unqueue_signals();
		    return;
		}
		/* check, if the address is on the (block-intern) free list */
		for (sh2 = mt->free; sh2; sh2 = sh2->next)
		    if (((char *)p) == ((char *)sh2)) {

			DPUTS(1, "BUG: attempt to free already free storage");
			unqueue_signals();
			return;
		    }
#endif
		DPUTS(M_BSLEN(mt->len) < osz,
		      "BUG: attempt to free more than allocated.");

#ifdef ZSH_MEM_DEBUG
		m_f[M_BSLEN(mt->len) / M_ISIZE]++;
		memset(sh, 0xff, M_BSLEN(mt->len));
#endif

		/* put the block onto the free list */
		sh->next = mt->free;
		mt->free = sh;

		if (--mt->used) {
		    /* if there are still used blocks in this block, we
		       put it at the beginning of the list with blocks
		       holding small blocks of the same size (since we
		       know that there is at least one free block in it,
		       this will make allocation of small blocks faster;
		       it also guarantees that long living memory blocks
		       are preferred over younger ones */
		    if (mp) {
			mp->next = mt->next;
			mt->next = m_small[i];
			m_small[i] = mt;
		    }
		    unqueue_signals();
		    return;
		}
		/* if there are no more used small blocks in this
		   block, we free the whole block */
		if (mp)
		    mp->next = mt->next;
		else
		    m_small[i] = mt->next;

		m = mt;
		p = (void *) & m->next;

		break;
	    } else if (sz) {
		/* if we didn't find a block and a size was given, try it
		   again as if no size were given */
		sz = 0;
		goto fr_rec;
	    }
	}
#ifdef ZSH_MEM_DEBUG
    if (!mt)
	m_f[m->len < (1024 * M_ISIZE) ? (m->len / M_ISIZE) : 1024]++;
#endif

#ifdef ZSH_SECURE_FREE
    /* search all memory blocks, if one of them is at the given address */
    for (mt = (struct m_hdr *)m_low;
	 ((char *)mt) < m_high;
	 mt = (struct m_hdr *)(((char *)mt) + M_ISIZE + mt->len))
	if (((char *)p) == ((char *)&mt->next))
	    break;

    /* no block was found at the given address */
    if (((char *)mt) >= m_high) {
	DPUTS(1, "BUG: attempt to free storage at invalid address");
	unqueue_signals();
	return;
    }
#endif

    /* see if the block is on the free list */
    for (mp = NULL, mt = m_free; mt && mt < m; mp = mt, mt = mt->next);

    if (m == mt) {
	/* it is, ouch! */
	DPUTS(1, "BUG: attempt to free already free storage");
	unqueue_signals();
	return;
    }
    DPUTS(m->len < osz, "BUG: attempt to free more than allocated");
#ifdef ZSH_MEM_DEBUG
    memset(p, 0xff, m->len);
#endif
    if (mt && ((char *)mt) == (((char *)m) + M_ISIZE + m->len)) {
	/* the block after the one we are freeing is free, we put them
	   together */
	m->len += mt->len + M_ISIZE;
	m->next = mt->next;

	if (mt == m_lfree)
	    m_lfree = m;
    } else
	m->next = mt;

    if (mp && ((char *)m) == (((char *)mp) + M_ISIZE + mp->len)) {
	/* the block before the one we are freeing is free, we put them
	   together */
	mp->len += m->len + M_ISIZE;
	mp->next = m->next;

	if (m == m_lfree)
	    m_lfree = mp;
    } else if (mp)
	/* otherwise, we just put it on the free list */
	mp->next = m;
    else {
	m_free = m;
	if (!m_lfree)
	    m_lfree = m_free;
    }

    /* if the block we have just freed was at the end of the process heap
       and now there is more than one page size of memory, we can give
       it back to the system (and we do it ;-) */
    if ((((char *)m_lfree) + M_ISIZE + m_lfree->len) == m_high &&
	m_lfree->len >= m_pgsz + M_MIN + M_FREE) {
	long n = (m_lfree->len - M_MIN - M_KEEP) & ~(m_pgsz - 1);

	m_lfree->len -= n;
#ifdef HAVE_BRK
	if (brk(m_high -= n) == -1) {
#else
	m_high -= n;
	if (sbrk(-n) == (void *)-1) {
#endif /* HAVE_BRK */
	    DPUTS(1, "MEM: allocation error at brk.");
	}

#ifdef ZSH_MEM_DEBUG
	m_b += n;
#endif
    }
    unqueue_signals();
}

FREE_RET_T
free(FREE_ARG_T p)
{
    zfree(p, 0);		/* 0 means: size is unknown */

#ifdef FREE_DO_RET
    return 0;
#endif
}

/* this one is for strings (and only strings, real strings, real C strings,
   those that have a zero byte at the end) */

/**/
mod_export void
zsfree(char *p)
{
    if (p)
	zfree(p, strlen(p) + 1);
}

MALLOC_RET_T
realloc(MALLOC_RET_T p, MALLOC_ARG_T size)
{
    struct m_hdr *m = (struct m_hdr *)(((char *)p) - M_ISIZE), *mt;
    char *r;
    int i, l = 0;

    /* some system..., see above */
    if (!p && size)
	return (MALLOC_RET_T) malloc(size);
    /* and some systems even do this... */
    if (!p || !size)
	return (MALLOC_RET_T) p;

    queue_signals();  /* just queue signals caught rather than handling them */

    /* check if we are reallocating a small block, if we do, we have
       to compute the size of the block from the sort of block it is in */
    for (i = 0; i < M_NSMALL; i++) {
	for (mt = m_small[i];
	     mt && (((char *)mt) > ((char *)p) ||
		    (((char *)mt) + mt->len) < ((char *)p));
	     mt = mt->next);

	if (mt) {
	    l = M_BSLEN(mt->len);
	    break;
	}
    }
    if (!l)
	/* otherwise the size of the block is in the memory just before
	   the given address */
	l = m->len;

    /* now allocate the new block, copy the old contents, and free the
       old block */
    r = malloc(size);
    memcpy(r, (char *)p, (size > l) ? l : size);
    free(p);

    unqueue_signals();
    return (MALLOC_RET_T) r;
}

MALLOC_RET_T
calloc(MALLOC_ARG_T n, MALLOC_ARG_T size)
{
    long l;
    char *r;

    if (!(l = n * size))
	return (MALLOC_RET_T) m_high;

    r = malloc(l);

    memset(r, 0, l);

    return (MALLOC_RET_T) r;
}

#ifdef ZSH_MEM_DEBUG

/**/
int
bin_mem(char *name, char **argv, Options ops, int func)
{
    int i, ii, fi, ui, j;
    struct m_hdr *m, *mf, *ms;
    char *b, *c, buf[40];
    long u = 0, f = 0, to, cu;

    queue_signals();
    if (OPT_ISSET(ops,'v')) {
	printf("The lower and the upper addresses of the heap. Diff gives\n");
	printf("the difference between them, i.e. the size of the heap.\n\n");
    }
    printf("low mem %ld\t high mem %ld\t diff %ld\n",
	   (long)m_l, (long)m_high, (long)(m_high - ((char *)m_l)));

    if (OPT_ISSET(ops,'v')) {
	printf("\nThe number of bytes that were allocated using sbrk() and\n");
	printf("the number of bytes that were given back to the system\n");
	printf("via brk().\n");
    }
    printf("\nsbrk %d\tbrk %d\n", m_s, m_b);

    if (OPT_ISSET(ops,'v')) {
	printf("\nInformation about the sizes that were allocated or freed.\n");
	printf("For each size that were used the number of mallocs and\n");
	printf("frees is shown. Diff gives the difference between these\n");
	printf("values, i.e. the number of blocks of that size that is\n");
	printf("currently allocated. Total is the product of size and diff,\n");
	printf("i.e. the number of bytes that are allocated for blocks of\n");
	printf("this size. The last field gives the accumulated number of\n");
	printf("bytes for all sizes.\n");
    }
    printf("\nsize\tmalloc\tfree\tdiff\ttotal\tcum\n");
    for (i = 0, cu = 0; i < 1024; i++)
	if (m_m[i] || m_f[i]) {
	    to = (long) i * M_ISIZE * (m_m[i] - m_f[i]);
	    printf("%ld\t%d\t%d\t%d\t%ld\t%ld\n",
		   (long)i * M_ISIZE, m_m[i], m_f[i], m_m[i] - m_f[i],
		   to, (cu += to));
	}

    if (m_m[i] || m_f[i])
	printf("big\t%d\t%d\t%d\n", m_m[i], m_f[i], m_m[i] - m_f[i]);

    if (OPT_ISSET(ops,'v')) {
	printf("\nThe list of memory blocks. For each block the following\n");
	printf("information is shown:\n\n");
	printf("num\tthe number of this block\n");
	printf("tnum\tlike num but counted separately for used and free\n");
	printf("\tblocks\n");
	printf("addr\tthe address of this block\n");
	printf("len\tthe length of the block\n");
	printf("state\tthe state of this block, this can be:\n");
	printf("\t  used\tthis block is used for one big block\n");
	printf("\t  free\tthis block is free\n");
	printf("\t  small\tthis block is used for an array of small blocks\n");
	printf("cum\tthe accumulated sizes of the blocks, counted\n");
	printf("\tseparately for used and free blocks\n");
	printf("\nFor blocks holding small blocks the number of free\n");
	printf("blocks, the number of used blocks and the size of the\n");
	printf("blocks is shown. For otherwise used blocks the first few\n");
	printf("bytes are shown as an ASCII dump.\n");
    }
    printf("\nblock list:\nnum\ttnum\taddr\t\tlen\tstate\tcum\n");
    for (m = m_l, mf = m_free, ii = fi = ui = 1; ((char *)m) < m_high;
	 m = (struct m_hdr *)(((char *)m) + M_ISIZE + m->len), ii++) {
	for (j = 0, ms = NULL; j < M_NSMALL && !ms; j++)
	    for (ms = m_small[j]; ms; ms = ms->next)
		if (ms == m)
		    break;

	if (m == mf)
	    buf[0] = '\0';
	else if (m == ms)
	    sprintf(buf, "%ld %ld %ld", (long)(M_SNUM - ms->used),
		    (long)ms->used,
		    (long)(m->len - sizeof(struct m_hdr)) / M_SNUM + 1);

	else {
	    for (i = 0, b = buf, c = (char *)&m->next; i < 20 && i < m->len;
		 i++, c++)
		*b++ = (*c >= ' ' && *c < 127) ? *c : '.';
	    *b = '\0';
	}

	printf("%d\t%d\t%ld\t%ld\t%s\t%ld\t%s\n", ii,
	       (m == mf) ? fi++ : ui++,
	       (long)m, (long)m->len,
	       (m == mf) ? "free" : ((m == ms) ? "small" : "used"),
	       (m == mf) ? (f += m->len) : (u += m->len),
	       buf);

	if (m == mf)
	    mf = mf->next;
    }

    if (OPT_ISSET(ops,'v')) {
	printf("\nHere is some information about the small blocks used.\n");
	printf("For each size the arrays with the number of free and the\n");
	printf("number of used blocks are shown.\n");
    }
    printf("\nsmall blocks:\nsize\tblocks (free/used)\n");

    for (i = 0; i < M_NSMALL; i++)
	if (m_small[i]) {
	    printf("%ld\t", (long)i * M_ISIZE);

	    for (ii = 0, m = m_small[i]; m; m = m->next) {
		printf("(%ld/%ld) ", (long)(M_SNUM - m->used),
		       (long)m->used);
		if (!((++ii) & 7))
		    printf("\n\t");
	    }
	    putchar('\n');
	}
    if (OPT_ISSET(ops,'v')) {
	printf("\n\nBelow is some information about the allocation\n");
	printf("behaviour of the zsh heaps. First the number of times\n");
	printf("pushheap(), popheap(), and freeheap() were called.\n");
    }
    printf("\nzsh heaps:\n\n");

    printf("push %d\tpop %d\tfree %d\n\n", h_push, h_pop, h_free);

    if (OPT_ISSET(ops,'v')) {
	printf("\nThe next list shows for several sizes the number of times\n");
	printf("memory of this size were taken from heaps.\n\n");
    }
    printf("size\tmalloc\ttotal\n");
    for (i = 0; i < 1024; i++)
	if (h_m[i])
	    printf("%ld\t%d\t%ld\n", (long)i * H_ISIZE, h_m[i],
		   (long)i * H_ISIZE * h_m[i]);
    if (h_m[1024])
	printf("big\t%d\n", h_m[1024]);

    unqueue_signals();
    return 0;
}

#endif

/**/
#else				/* not ZSH_MEM */

/**/
mod_export void
zfree(void *p, UNUSED(int sz))
{
    if (p)
	free(p);
}

/**/
mod_export void
zsfree(char *p)
{
    if (p)
	free(p);
}

/**/
#endif
debug log:

solving ca9ab96 ...
found ca9ab96 in https://inbox.vuxu.org/zsh-workers/151024123943.ZM30236@torch.brasslantern.com/
found 68bd767 in https://git.vuxu.org/mirror/zsh/
preparing index
index prepared:
100644 68bd76740335fe71c1010fbae4f9fb7da5068833	Src/mem.c

applying [1/1] https://inbox.vuxu.org/zsh-workers/151024123943.ZM30236@torch.brasslantern.com/
diff --git a/Src/mem.c b/Src/mem.c
index 68bd767..ca9ab96 100644

Checking patch Src/mem.c...
Applied patch Src/mem.c cleanly.

index at:
100644 ca9ab96ae6ef15cd7934a43419f0e631016cb3b5	Src/mem.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).