~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

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
/** Zone memory management. -*- Mode: ObjC -*-
   Copyright (C) 1997,1998 Free Software Foundation, Inc.

   Written by: Yoo C. Chung <wacko@laplace.snu.ac.kr>
   Date: January 1997
   Rewrite by: Richard Frith-Macdonald <richard@brainstrom.co.uk>

   This file is part of the GNUstep Base Library.

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

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

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02111 USA.

   <title>NSZone class reference</title>
   $Date: 2012-01-09 01:28:27 -0700 (Mon, 09 Jan 2012) $ $Revision: 34468 $
*/

/*  Design goals:

    - Allocation and deallocation should be reasonably efficient.
    - We want to catch code that writes outside it's permitted area.

 */


/* Actual design:

   - The default zone uses malloc() and friends.  We assume that
   they're thread safe and that they return NULL if we're out of
   memory (glibc malloc does this, what about other mallocs? FIXME).

    - The OpenStep spec says that when a zone is recycled, any memory in
   use is returned to the default zone.
   Since, in general, we have no control over the system malloc, we can't
   possibly do this.  Instead, we move the recycled zone to a list of
   'dead' zones, and as soon as all memory used in it is released, we
   destroy it and remove it from that list.  In the meantime, we release
   any blocks of memory we can (ie those that don't contain unfreed chunks).

   - For freeable zones, a small linear buffer is used for
   deallocating and allocating.  Anything that can't go into the
   buffer then uses a more general purpose segregated fit algorithm
   after flushing the buffer.

   - For memory chunks in freeable zones, the pointer to the chunk is
   preceded by the a chunk header which contains the size of the chunk
   (plus a couple of flags) and a pointer to the end of the memory
   requested.  This adds 8 bytes for freeable zones, which is usually
   what we need for alignment purposes anyway (assuming we're on a
   32 bit machine).  The granularity for allocation of chunks is quite
   large - a chunk must be big enough to hold the chunk header plus a
   couple of pointers and an unsigned size value.
   The actual memory allocated will be the size of the chunk header plus
   the size of memory requested plus one (a guard byte), all rounded up
   to a multiple of the granularity.

   - For nonfreeable zones, worst-like fit is used.  This is OK since
   we don't have to worry about memory fragmentation. */

/* Other information:

   - This uses some GCC specific extensions.  But since the library is
   supposed to compile on GCC 2.7.2.1 (patched) or higher, and the
   only other Objective-C compiler I know of (other than NeXT's, which
   is based on GCC as far as I know) is the StepStone compiler, which
   I haven't the foggiest idea why anyone would prefer it to GCC ;),
   it should be OK.

   - These functions should be thread safe, but I haven't really
   tested them extensively in multithreaded cases. */


/* Define to turn off NSAssertions. */
#define NS_BLOCK_ASSERTIONS 1

#define IN_NSZONE_M 1

#import "common.h"
#include <stddef.h>
#include <string.h>
#import "Foundation/NSException.h"
#import "Foundation/NSLock.h"
#import "GSPrivate.h"
#import "GSPThread.h"

/**
 * Try to get more memory - the normal process has failed.
 * If we can't do anything, just return a null pointer.
 * Try to do some logging if possible.
 */
void *
GSOutOfMemory(NSUInteger size, BOOL retry)
{
  fprintf(stderr, "GSOutOfMemory ... wanting %"PRIuPTR" bytes.\n", size);
  return 0;
}

/* Default zone functions for default zone. */
static void* default_malloc (NSZone *zone, size_t size);
static void* default_realloc (NSZone *zone, void *ptr, size_t size);
static void default_free (NSZone *zone, void *ptr);
static void default_recycle (NSZone *zone);
static BOOL default_check (NSZone *zone);
static BOOL default_lookup (NSZone *zone, void *ptr);
static struct NSZoneStats default_stats (NSZone *zone);

static void*
default_malloc (NSZone *zone, size_t size)
{
  void *mem;

  mem = malloc(size);
  if (mem == NULL)
    [NSException raise: NSMallocException
                 format: @"Default zone has run out of memory"];
  return mem;
}

static void*
default_realloc (NSZone *zone, void *ptr, size_t size)
{
  void *mem;

  if (size == 0)
    {
      free(ptr);
      return NULL;
    }
  if (ptr == 0)
    {
      mem = malloc(size);
      if (mem == NULL)
	[NSException raise: NSMallocException
		     format: @"Default zone has run out of memory"];
      return mem;
    }
  mem = realloc(ptr, size);
  if (mem == NULL)
    [NSException raise: NSMallocException
                 format: @"Default zone has run out of memory"];
  return mem;
}

static void
default_free (NSZone *zone, void *ptr)
{
  free(ptr);
}

static void
default_recycle (NSZone *zone)
{
  /* Recycle the default zone?  Thou hast got to be kiddin'. */
  [NSException raise: NSGenericException
              format: @"Trying to recycle default zone"];
}

static BOOL
default_check (NSZone *zone)
{
  /* We can't check memory managed by malloc(). */
  [NSException raise: NSGenericException
	      format: @"No checking for default zone"];
  return NO;
}

static BOOL
default_lookup (NSZone *zone, void *ptr)
{
  /* Assume all memory is in default zone. */
  return YES;
}

static struct NSZoneStats
default_stats (NSZone *zone)
{
  struct NSZoneStats dummy = {0,0,0,0,0};

  /* We can't obtain statistics from the memory managed by malloc(). */
  [NSException raise: NSGenericException
	      format: @"No statistics for default zone"];
  return dummy;
}

static NSZone default_zone =
{
  default_malloc, default_realloc, default_free, default_recycle,
  default_check, default_lookup, default_stats, 0, @"default", 0
};

/*
 * For backward compatibility.
 */
NSZone	*__nszone_private_hidden_default_zone = &default_zone;



void
NSSetZoneName (NSZone *zone, NSString *name)
{
  if (!zone)
    zone = NSDefaultMallocZone();
  [gnustep_global_lock lock];
  name = [name copy];
  if (zone->name != nil)
    [zone->name release];
  zone->name = name;
  [gnustep_global_lock unlock];
}

NSString*
NSZoneName (NSZone *zone)
{
  if (!zone)
    zone = NSDefaultMallocZone();
  return zone->name;
}
#if __OBJC_GC__

#include <objc/objc-auto.h>

__strong void *
NSAllocateCollectable(NSUInteger size, NSUInteger options)
{
  if (!objc_collecting_enabled())
    {
      return calloc(1, size);
    }
  id obj = objc_gc_allocate_collectable(size, 
             ((options & NSScannedOption) == NSScannedOption));
  if ((options & NSCollectorDisabledOption) == NSCollectorDisabledOption)
    {
      obj = objc_gc_retain(obj);
    }
  return obj;
}

__strong void *
NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options)
{
  if (!objc_collecting_enabled())
    {
      return realloc(ptr, size);
    }
  return objc_gc_reallocate_collectable(ptr, size, 
             ((options & NSScannedOption) == NSScannedOption));
}

id NSMakeCollectable(id obj)
{
  if (objc_collecting_enabled())
    {
      objc_gc_release(obj);
    }
  return obj;
}

NSZone*
NSCreateZone (NSUInteger start, NSUInteger gran, BOOL canFree)
{
  return &default_zone;
}

NSZone*
NSDefaultMallocZone (void)
{
  return &default_zone;
}

NSZone*
NSZoneFromPointer (void *ptr)
{
  return &default_zone;
}

void
NSRecycleZone (NSZone *zone) { }

BOOL
NSZoneCheck (NSZone *zone)
{
  return YES;
}

struct
NSZoneStats NSZoneStats (NSZone *zone)
{
  struct NSZoneStats stats = { 0 };
  return stats;
}

void
GSMakeWeakPointer(Class theClass, const char *iVarName) { }

BOOL
GSAssignZeroingWeakPointer(void **destination, void *source)
{
  objc_assign_weak(source, (id*)destination);
  return YES;
}

void*
NSZoneMalloc (NSZone *zone, NSUInteger size)
{
  return NSZoneCalloc(zone, 1, size);
}

void*
NSZoneCalloc (NSZone *zone, NSUInteger elems, NSUInteger bytes)
{
  if (objc_collecting_enabled())
    {
      // FIXME: Overflow checking
      size_t size = elems * bytes;
      return objc_gc_allocate_collectable(size, YES);
    }
  return calloc(elems, bytes);
}

void*
NSZoneRealloc (NSZone *zone, void *ptr, NSUInteger size)
{
  if (objc_collecting_enabled())
    {
      return objc_gc_reallocate_collectable(ptr, size, YES);
    }
  return realloc(ptr, size);
}

void NSZoneFree (NSZone *zone, void *ptr) { }

#elif	GS_WITH_GC

#if	defined(DEBUG)
#define	GC_DEBUG	1
#endif	/* DEBUG */

#include <gc/gc.h>

/*
 * Dummy zones used with garbage collection.
 * The 'atomic' zone is for memory that will be assumed not to contain
 * pointers for garbage collection purposes.
 */
static NSZone atomic_zone =
{
  0, 0, 0, 0, 0, 0, 0, 0, @"default", 0
};

void *
NSAllocateCollectable(NSUInteger size, NSUInteger options)
{
  void	*ptr;

  if (options & NSScannedOption)
    {
      if (options & NSCollectorDisabledOption)
	{
          ptr = (void*)GC_MALLOC_UNCOLLECTABLE(size);
	  memset(ptr, '\0', size);
	}
      else
	{
          ptr = (void*)GC_MALLOC_IGNORE_OFF_PAGE(size);
	  memset(ptr, '\0', size);
	}
    }
  else
    {
      if (options & NSCollectorDisabledOption)
	{
	  ptr = (void*)calloc(1, size);
	}
      else
	{
	  ptr = (void*)GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(size);
	  memset(ptr, '\0', size);
	}
    }
  return ptr;
}

void *
NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options)
{
  if (ptr == 0)
    {
      ptr = NSAllocateCollectable(size, options);
    }
  else if (size > 0 && GC_base(ptr) == 0)
    {
      /* This is not collectable memory, so we don't know how big it is.
       * that means we must use realloc first, to get a known size, and
       * then, if needed, allocate a chunk the right size and copy to it.
       */
      ptr = realloc(ptr, size);
      if (ptr != 0)
	{
	  if ((options & NSScannedOption)
	    || !(options & NSCollectorDisabledOption))
	    {
	      void	*tmp = NSAllocateCollectable(size, options);

	      memcpy(tmp, ptr, size);
	      free(ptr);
	      ptr = tmp;
	    }
	}
    }
  else
    {
      void	*tmp = NSAllocateCollectable(size, options);
      unsigned	length = GC_size(ptr);

      if (length > size)
	{
	  length = size;
	}
      if (tmp != 0 && length > 0)
	{
          memcpy(tmp, ptr, length);
	}
      GC_FREE(ptr);
      ptr = tmp;
    }
  return ptr;
}

NSZone*
NSCreateZone (NSUInteger start, NSUInteger gran, BOOL canFree)
{
  NSLog(@" *** Creating a zone while running GC is ignored.");
  return &default_zone;
}

NSZone*
NSDefaultMallocZone (void)
{
  return &default_zone;
}

NSZone*
GSAtomicMallocZone (void)
{
  return &atomic_zone;
}

NSZone*
NSZoneFromPointer (void *ptr)
{
  return &default_zone;
}

void
NSRecycleZone (NSZone *zone)
{
}

BOOL
NSZoneCheck (NSZone *zone)
{
  return YES;
}

struct
NSZoneStats NSZoneStats (NSZone *zone)
{
  struct NSZoneStats stats = { 0 };
  return stats;
}

void
GSMakeWeakPointer(Class theClass, const char *iVarName)
{
  class_ivar_set_gcinvisible(theClass, iVarName, YES);
}

BOOL
GSAssignZeroingWeakPointer(void **destination, void *source)
{
  if (destination == 0)
    {
      return NO;	// Bad destination pointer
    }
  if (*destination == source)
    {
      return YES;	// Already assigned.
    }
  if (source != 0 && GC_base(source) == 0)
    {
      return NO;	// Source is not garbage collectable.
    }
  if (*destination != 0)
    {
      GC_unregister_disappearing_link(destination);
    }
  *destination = source;
  if (source != 0)
    {
      GC_general_register_disappearing_link(destination, source);
    }
  return YES;
}

void*
NSZoneMalloc (NSZone *zone, NSUInteger size)
{
  return NSZoneCalloc(zone, 1, size);
}

void*
NSZoneCalloc (NSZone *zone, NSUInteger elems, NSUInteger bytes)
{
  size_t	size = elems * bytes;
  void		*ptr;

  if (zone == &atomic_zone)
    {
      ptr = (void*)GC_MALLOC_ATOMIC(size);
      memset(ptr, '\0', size);
    }
  else
    {
      ptr = (void*)malloc(size);
      if (ptr == 0)
	{
          ptr = GSOutOfMemory(size, NO);
	}
      if (ptr != 0)
	{
	  memset(ptr, '\0', size);
	}
    }
  return ptr;
}

void*
NSZoneRealloc (NSZone *zone, void *ptr, NSUInteger size)
{
  if (GC_base(ptr) != 0)
    {
      ptr = GC_REALLOC(ptr, size);
    }
  else
    {
      ptr = realloc(ptr, size);
    }
  return ptr;
}

void
NSZoneFree (NSZone *zone, void *ptr)
{
  if (GC_base(ptr) != 0)
    {
      GC_FREE(ptr);
    }
  else
    {
      free(ptr);
    }
}

BOOL
GSPrivateIsCollectable(const void *ptr)
{
  if (GC_base((void*)ptr) == 0) return NO;
  else return YES;
}

#else	/* GS_WITH_GC */


/* Alignment */
#ifdef ALIGN
#undef ALIGN
#endif
#define ALIGN ((__alignof__(double) < 8) ? 8 : __alignof__(double))
#define MINGRAN 256 /* Minimum granularity. */
#define DEFBLOCK 16384 /* Default granularity. */
#define BUFFER 4 /* Buffer size.  FIXME?: Is this a reasonable optimum. */
#define MAX_SEG 16 /* Segregated list size. */
#define FBSZ sizeof(ff_block)
#define NBSZ sizeof(nf_chunk)

/* Information bits in size. */
#define INUSE 0x01 /* Current chunk in use. */
#define PREVUSE 0x02 /* Previous chunk in use. */
#define LIVE 0x04

/* Bits to mask off to get size. */
#define SIZE_BITS (INUSE | PREVUSE | LIVE)

#define	NF_HEAD sizeof(nf_block)

typedef struct _ffree_free_link ff_link;
typedef struct _nfree_block_struct nf_block;
typedef struct _ffree_block_struct ff_block;
typedef struct _ffree_zone_struct ffree_zone;
typedef struct _nfree_zone_struct nfree_zone;


/* Header for blocks in nonfreeable zones. */
struct _nfree_block_unpadded
{
  struct _nfree_block_struct *next;
  size_t size; // Size of block
  size_t top; // Position of next memory chunk to allocate
};
#define	NFBPAD	sizeof(struct _nfree_block_unpadded)

struct _nfree_block_struct
{
  struct _nfree_block_struct *next;
  size_t size; // Size of block
  size_t top; // Position of next memory chunk to allocate
  char	padding[ALIGN - ((NFBPAD % ALIGN) ? (NFBPAD % ALIGN) : ALIGN)];
};

struct _ffree_block_unpadded {
  size_t	size;
  struct _ffree_block_struct *next;
};
#define	FFCPAD	sizeof(struct _ffree_block_unpadded)

/* Header for blocks and chunks in freeable zones. */
struct _ffree_block_struct
{
  size_t size;
  struct _ffree_block_struct *next;
  char	padding[ALIGN - ((FFCPAD % ALIGN) ? (FFCPAD % ALIGN) : ALIGN)];
};

struct _ffree_free_link_unpadded
{
  size_t	size;
  ff_link	*prev;
  ff_link	*next;
  size_t	back;	/* Back link at end of 'dead' block.	*/
};
#define	FFDPAD	sizeof(struct _ffree_free_link_unpadded)

struct _ffree_free_link
{
  size_t	size;
  ff_link	*prev;
  ff_link	*next;
  size_t	back;
  char	padding[ALIGN - ((FFDPAD % ALIGN) ? (FFDPAD % ALIGN) : ALIGN)];
};

/* NSZone structure for freeable zones. */
struct _ffree_zone_struct
{
  NSZone common;
  pthread_mutex_t lock;
  ff_block *blocks; // Linked list of blocks
  ff_link *segheadlist[MAX_SEG]; // Segregated list, holds heads
  ff_link *segtaillist[MAX_SEG]; // Segregated list, holds tails
  size_t bufsize; // Buffer size
  size_t size_buf[BUFFER]; // Buffer holding sizes
  ff_block *ptr_buf[BUFFER]; // Buffer holding pointers to chunks
};

/* Rounds up N to nearest multiple of BASE. */
static inline size_t
roundupto (size_t n, size_t base)
{
  size_t a = (n/base)*base;

  return (n-a)? (a+base): n;
}

/*
 *	Minimum chunk size for freeable zones.
 *	Need room for basic chunk header, next and prev pointers for
 *	free-list, and a reverse pointer (size_t) to go at the end of the
 *	chunk while it is waiting to be consolidated with other chunks.
 */
#define MINCHUNK sizeof(ff_link)

#define CLTOSZ(n) ((n)*MINCHUNK) /* Converts classes to sizes. */

static inline void*
chunkToPointer(ff_block *chunk)
{
  return (void*)(&chunk[1]);
}

static inline ff_block*
pointerToChunk(void* ptr)
{
  return &(((ff_block*)ptr)[-1]);
}

static inline size_t
chunkIsLive(ff_block* ptr)
{
  return ptr->size & LIVE;
}

static inline size_t
chunkIsInUse(ff_block* ptr)
{
  return ptr->size & INUSE;
}

static inline size_t
chunkIsPrevInUse(ff_block* ptr)
{
  return ptr->size & PREVUSE;
}

static inline size_t
chunkSize(ff_block* ptr)
{
  return ptr->size & ~SIZE_BITS;
}

static inline void
chunkClrInUse(ff_block* ptr)
{
  ptr->size &= ~INUSE;
}

static inline size_t
chunkClrLive(ff_block* ptr)
{
  return ptr->size &= ~LIVE;
}

static inline void
chunkClrPrevInUse(ff_block* ptr)
{
  ptr->size &= ~PREVUSE;
}

static inline void
chunkSetInUse(ff_block* ptr)
{
  ptr->size |= INUSE;
}

static inline size_t
chunkSetLive(ff_block* ptr)
{
  return ptr->size |= LIVE;
}

static inline void
chunkSetPrevInUse(ff_block* ptr)
{
  ptr->size |= PREVUSE;
}

static inline void
chunkSetSize(ff_block* ptr, size_t size)
{
  ptr->size = size;
}

static inline ff_block*
chunkNext(ff_block *ptr)
{
  return (ff_block*) ((void*)ptr+chunkSize(ptr));
}

static inline void
chunkMakeLink(ff_block *ptr)
{
  NSAssert(!chunkIsInUse(ptr), NSInternalInconsistencyException);
  NSAssert(!chunkIsLive(ptr), NSInternalInconsistencyException);
  (&(chunkNext(ptr)->size))[-1] = chunkSize(ptr);
}

static inline ff_block*
chunkChop(ff_block *ptr, size_t size)
{
  ff_block	*remainder;
  size_t	left = chunkSize(ptr)-size;

  NSAssert((chunkSize(ptr) % MINCHUNK) == 0, NSInternalInconsistencyException);
  NSAssert(chunkSize(ptr) > size, NSInternalInconsistencyException);
  remainder = (ff_block*)((void*)ptr+size);
  chunkSetSize(remainder, left | PREVUSE);
  chunkMakeLink(remainder);
  chunkSetSize(ptr, size | chunkIsPrevInUse(ptr) | INUSE);
  return remainder;
}

static inline ff_block*
chunkPrev(ff_block *ptr)
{
  size_t	offset;
  ff_block	*prev;

  NSAssert(!chunkIsPrevInUse(ptr), NSInternalInconsistencyException);
  offset = (&(ptr->size))[-1];
  NSAssert(offset > 0 && (offset % MINCHUNK) == 0,
    NSInternalInconsistencyException);
  prev = (ff_block*)((void*)ptr-offset);
  NSAssert(chunkSize(prev) == offset, NSInternalInconsistencyException);
  NSAssert(!chunkIsInUse(prev), NSInternalInconsistencyException);
  return prev;
}

/* NSZone structure for nonfreeable zones. */
struct _nfree_zone_struct
{
  NSZone common;
  pthread_mutex_t lock;
  /* Linked list of blocks in decreasing order of free space,
     except maybe for the first block. */
  nf_block *blocks;
  size_t use;
};

/* Memory management functions for freeable zones. */
static void* fmalloc (NSZone *zone, size_t size);
static void* frealloc (NSZone *zone, void *ptr, size_t size);
static void ffree (NSZone *zone, void *ptr);
static void frecycle (NSZone *zone);
static BOOL fcheck (NSZone *zone);
static BOOL flookup (NSZone *zone, void *ptr);
static struct NSZoneStats fstats (NSZone *zone);

static inline size_t segindex (size_t size);
static ff_block* get_chunk (ffree_zone *zone, size_t size);
static void take_chunk (ffree_zone *zone, ff_block *chunk);
static void put_chunk (ffree_zone *zone, ff_block *chunk);
static inline void add_buf (ffree_zone *zone, ff_block *chunk);
static void flush_buf (ffree_zone *zone);

/* Memory management functions for nonfreeable zones. */
static void* nmalloc (NSZone *zone, size_t size);
static void nrecycle (NSZone *zone);
static void* nrealloc (NSZone *zone, void *ptr, size_t size);
static void nfree (NSZone *zone, void *ptr);
static BOOL ncheck (NSZone *zone);
static BOOL nlookup (NSZone *zone, void *ptr);
static struct NSZoneStats nstats (NSZone *zone);

/* Memory management functions for recycled zones. */
static void* rmalloc (NSZone *zone, size_t size);
static void rrecycle (NSZone *zone);
static void* rrealloc (NSZone *zone, void *ptr, size_t size);
static void rffree (NSZone *zone, void *ptr);
static void rnfree (NSZone *zone, void *ptr);

/*
 *	Lists of zones to be used to determine if a pointer is in a zone.
 */
static NSZone	*zone_list = 0;

static inline void
destroy_zone(NSZone* zone)
{
  if (zone_list == zone)
    zone_list = zone->next;
  else
    {
      NSZone *ptr = zone_list;

      while (ptr->next != zone)
	ptr = ptr->next;
      if (ptr)
        ptr->next = zone->next;
    }
  free((void*)zone);
}

/* Search the buffer to see if there is any memory chunks large enough
   to satisfy request using first fit.  If the memory chunk found has
   a size exactly equal to the one requested, remove it from the buffer
   and return it.  If not, cut off a chunk that does match the size
   and return it.  If there is no chunk large enough in the buffer,
   get a chunk from the general purpose allocator that uses segregated
   fit.  Since a chunk in the buffer is not freed in the general purpose
   allocator, the headers are as if it is still in use. */
static void*
fmalloc (NSZone *zone, size_t size)
{
  size_t i = 0;
  size_t chunksize = roundupto(size+FBSZ+1, MINCHUNK);
  ffree_zone *zptr = (ffree_zone*)zone;
  size_t bufsize;
  size_t *size_buf = zptr->size_buf;
  ff_block **ptr_buf = zptr->ptr_buf;
  ff_block *chunkhead;
  void *result;

  pthread_mutex_lock(&(zptr->lock));
  bufsize = zptr->bufsize;
  while ((i < bufsize) && (chunksize > size_buf[i]))
    i++;
  if (i < bufsize)
    /* Use memory chunk in buffer. */
    {
      if (size_buf[i] == chunksize)
        /* Exact fit. */
        {
          zptr->bufsize--;
          bufsize = zptr->bufsize;
          chunkhead = ptr_buf[i];
          size_buf[i] = size_buf[bufsize];
          ptr_buf[i] = ptr_buf[bufsize];

          NSAssert(chunkIsInUse(chunkhead), NSInternalInconsistencyException);
          NSAssert((chunkSize(chunkhead) % MINCHUNK) == 0,
	    NSInternalInconsistencyException);
        }
      else
        {
          /*
	   *	Break off chunk leaving remainder marked as in use since it
	   *	stays in this buffer rather than on a free-list.
	   */
          chunkhead = ptr_buf[i];
          size_buf[i] -= chunksize;
          ptr_buf[i] = chunkChop(chunkhead, chunksize);
	  chunkSetInUse(ptr_buf[i]);
        }
    }
  else
    /* Get memory from segregate fit allocator. */
    {
      flush_buf(zptr);
      chunkhead = get_chunk(zptr, chunksize);
      if (chunkhead == NULL)
        {
          pthread_mutex_unlock(&(zptr->lock));
          if (zone->name != nil)
            [NSException raise: NSMallocException
                        format: @"Zone %@ has run out of memory", zone->name];
          else
            [NSException raise: NSMallocException
                        format: @"Out of memory"];
        }

      NSAssert(chunkIsInUse(chunkhead), NSInternalInconsistencyException);
      NSAssert(chunkIsPrevInUse(chunkNext(chunkhead)),
	NSInternalInconsistencyException);
      NSAssert((chunkSize(chunkhead) % MINCHUNK) == 0,
	NSInternalInconsistencyException);
    }
  chunkhead->next = (ff_block*)(chunkToPointer(chunkhead)+size);
  *((char*)chunkhead->next) = (char)42;
  chunkSetLive(chunkhead);
  result = chunkToPointer(chunkhead);
  pthread_mutex_unlock(&(zptr->lock));
  return result;
}

/* If PTR == NULL, then it's the same as ordinary memory allocation.
   If a smaller size than it originally had is requested, shrink the
   chunk.  If a larger size is requested, check if there is enough
   space after it.  If there isn't enough space, get a new chunk and
   move it there, releasing the original.  The space before the chunk
   should also be checked, but I'll leave this to a later date. */
static void*
frealloc (NSZone *zone, void *ptr, size_t size)
{
  size_t realsize;
  size_t chunksize = roundupto(size+FBSZ+1, MINCHUNK);
  ffree_zone *zptr = (ffree_zone*)zone;
  ff_block *chunkhead, *slack;
  void *result;

  NSAssert(ptr == NULL || NSZoneFromPointer(ptr) == zone,
    NSInternalInconsistencyException);
  if (ptr == NULL)
    return fmalloc(zone, size);
  chunkhead = pointerToChunk(ptr);
  pthread_mutex_lock(&(zptr->lock));
  realsize = chunkSize(chunkhead);

  NSAssert(chunkIsInUse(chunkhead), NSInternalInconsistencyException);
  NSAssert((realsize % MINCHUNK) == 0, NSInternalInconsistencyException);

  chunkClrLive(chunkhead);
  if (chunksize < realsize)
    {
      /*
       *	Chop tail off existing memory chunk and tell the next chunk
       *	after it that it is no longer in use.  Then put it in the
       *	buffer to be added to the free list later (we can't add it
       *	immediately 'cos we might invalidate the rule that there
       *	must not be two adjacent unused chunks).
       */
      slack = chunkChop(chunkhead, chunksize);
      chunkSetInUse(slack);
      add_buf(zptr, slack);
    }
  else if (chunksize > realsize)
    {
      size_t nextsize;
      ff_block *nextchunk, *farchunk;

      nextchunk = chunkNext(chunkhead);
      nextsize = chunkSize(nextchunk);

      NSAssert((nextsize % MINCHUNK) == 0, NSInternalInconsistencyException);

      if (!chunkIsInUse(nextchunk) && (nextsize+realsize >= chunksize))
        /* Expand to next chunk. */
        {
          take_chunk(zptr, nextchunk);
          if (nextsize+realsize == chunksize)
            {
              farchunk = chunkNext(nextchunk);
              chunkSetPrevInUse(farchunk);
            }
          else
            {
	      chunkSetSize(chunkhead, nextsize+realsize);
	      slack = chunkChop(chunkhead, chunksize);
              put_chunk(zptr, slack);
            }
	  chunkSetSize(chunkhead, chunksize |
		chunkIsPrevInUse(chunkhead) | INUSE);
        }
      else
        /* Get new chunk and copy. */
        {
          ff_block *newchunk;

          newchunk = get_chunk(zptr, chunksize);
          if (newchunk == NULL)
            {
              pthread_mutex_unlock(&(zptr->lock));
              if (zone->name != nil)
                [NSException raise: NSMallocException
                            format: @"Zone %@ has run out of memory",
                             zone->name];
              else
                [NSException raise: NSMallocException
                            format: @"Out of memory"];
            }
          memcpy((void*)(&newchunk[1]), (void*)(&chunkhead[1]), realsize-FBSZ);
          add_buf(zptr, chunkhead);
          chunkhead = newchunk;
        }
    }
  chunkhead->next = (ff_block*)(chunkToPointer(chunkhead)+size);
  *((char*)chunkhead->next) = (char)42;
  chunkSetLive(chunkhead);
  result = chunkToPointer(chunkhead);
  pthread_mutex_unlock(&(zptr->lock));
  return result;
}

/* Frees memory chunk by simply adding it to the buffer. */
static void
ffree (NSZone *zone, void *ptr)
{
  ff_block *chunk;
  NSAssert(NSZoneFromPointer(ptr) == zone, NSInternalInconsistencyException);
  pthread_mutex_lock(&(((ffree_zone*)zone)->lock));
  chunk = pointerToChunk(ptr);
  if (chunkIsLive(chunk) == 0)
    [NSException raise: NSMallocException
	        format: @"Attempt to free freed memory"];
  NSAssert(*((char*)chunk->next) == (char)42, NSInternalInconsistencyException);
  add_buf((ffree_zone*)zone, chunk);
  pthread_mutex_unlock(&(((ffree_zone*)zone)->lock));
}

static BOOL
frecycle1(NSZone *zone)
{
  ffree_zone *zptr = (ffree_zone*)zone;
  ff_block *block;
  ff_block *nextblock;

  pthread_mutex_lock(&(zptr->lock));
  flush_buf(zptr);
  block = zptr->blocks;
  while (block != NULL)
    {
      ff_block	*tmp = &block[1];
      nextblock = block->next;
      if (chunkIsInUse(tmp) == 0 && chunkNext(tmp) == chunkNext(block))
	{
	  if (zptr->blocks == block)
	    zptr->blocks = block->next;
	  else
	    {
	      tmp = zptr->blocks;
	      while (tmp->next != block)
		tmp = tmp->next;
	      tmp->next = block->next;
	    }
          free((void*)block);
	}
      block = nextblock;
    }
  pthread_mutex_unlock(&(zptr->lock));
  if (zptr->blocks == 0)
    {
      pthread_mutex_destroy(&(zptr->lock));
      return YES;
    }
  return NO;
}

/* Recycle the zone. */
static void
frecycle (NSZone *zone)
{
  [gnustep_global_lock lock];
  if (zone->name != nil)
    {
      NSString *name = zone->name;
      zone->name = nil;
      [name release];
    }
  if (frecycle1(zone) == YES)
    destroy_zone(zone);
  else
    {
      zone->malloc = rmalloc;
      zone->realloc = rrealloc;
      zone->free = rffree;
      zone->recycle = rrecycle;
    }
  [gnustep_global_lock unlock];
}

static void
rffree (NSZone *zone, void *ptr)
{
  ffree(zone, ptr);
  [gnustep_global_lock lock];
  if (frecycle1(zone))
    destroy_zone(zone);
  [gnustep_global_lock unlock];
}


/* Check integrity of a freeable zone.  Doesn't have to be
   particularly efficient. */
static BOOL
fcheck (NSZone *zone)
{
  size_t i;
  ffree_zone *zptr = (ffree_zone*)zone;
  ff_block *block;

  pthread_mutex_lock(&(zptr->lock));
  /* Check integrity of each block the zone owns. */
  block = zptr->blocks;
  while (block != NULL)
    {
      ff_block *blockstart = &block[1];
      ff_block *blockend = chunkNext(block);
      ff_block *nextchunk = blockstart;

      if (blockend->next != block)
	goto inconsistent;
      if (!chunkIsPrevInUse(blockstart))
	goto inconsistent;

      while (nextchunk < blockend)
        {
          ff_block *chunk = nextchunk;
          size_t chunksize;

	  chunksize = chunkSize(chunk);
	  if ((chunksize % ALIGN) != 0)
	    goto inconsistent;
	  nextchunk = chunkNext(chunk);

          if (chunkIsInUse(chunk))
            /* Check whether this is a valid used chunk. */
            {
              if (!chunkIsPrevInUse(nextchunk))
                goto inconsistent;
	      if (chunkIsLive(chunk))
		{
	          if (chunk->next < &chunk[1] || chunk->next > nextchunk)
                    goto inconsistent;
	          if (*(char*)chunk->next != (char)42)
                    goto inconsistent;
		}
            }
          else
            /* Check whether this is a valid free chunk. */
            {
              if (chunkIsPrevInUse(nextchunk))
                goto inconsistent;
              if (!chunkIsInUse(nextchunk))
                goto inconsistent;
	      if (chunkIsLive(chunk))
                goto inconsistent;
            }
	  if (chunk != blockstart && chunkIsPrevInUse(chunk) == 0)
	    {
	      ff_block *prev = chunkPrev(chunk);

	      if (chunkNext(prev) != chunk)
		goto inconsistent;
	    }
        }
      /* Check whether the block ends properly. */
      if (nextchunk != blockend)
	goto inconsistent;
      if (chunkSize(blockend) != 0)
        goto inconsistent;
      if (chunkIsInUse(blockend) == 0)
        goto inconsistent;

      block = block->next;
    }
  /* Check the integrity of the segregated list. */
  for (i = 0; i < MAX_SEG; i++)
    {
      ff_link	*chunk = zptr->segheadlist[i];

      while (chunk != NULL)
        {
          ff_link *nextchunk;

          nextchunk = chunk->next;
          /* Isn't this one ugly if statement? */
          if (chunkIsInUse((ff_block*)chunk)
              || (segindex(chunkSize((ff_block*)chunk)) != i)
              || ((nextchunk != NULL) && (chunk != nextchunk->prev))
              || ((nextchunk == NULL) && (chunk != zptr->segtaillist[i])))
            goto inconsistent;
          chunk = nextchunk;
        }
    }
  /* Check the buffer. */
  if (zptr->bufsize > BUFFER)
    goto inconsistent;
  for (i = 0; i < zptr->bufsize; i++)
    {
      ff_block *chunk = zptr->ptr_buf[i];
      if ((zptr->size_buf[i] != chunkSize(chunk)) || !chunkIsInUse(chunk))
        goto inconsistent;
    }
  pthread_mutex_unlock(&(zptr->lock));
  return YES;

inconsistent: // Jump here if an inconsistency was found.
  pthread_mutex_unlock(&(zptr->lock));
  return NO;
}

static BOOL
flookup (NSZone *zone, void *ptr)
{
  ffree_zone	*zptr = (ffree_zone*)zone;
  ff_block	*block;
  BOOL		found = NO;

  pthread_mutex_lock(&(zptr->lock));
  for (block = zptr->blocks; block != NULL; block = block->next)
    {
      if (ptr >= (void*)block && ptr < (void*)chunkNext(block))
	{
	  found = YES;
	  break;
	}
    }
  pthread_mutex_unlock(&(zptr->lock));
  return found;
}

/* Obtain statistics about the zone.  Doesn't have to be particularly
   efficient. */
static struct NSZoneStats
fstats (NSZone *zone)
{
  size_t i;
  struct NSZoneStats stats;
  ffree_zone *zptr = (ffree_zone*)zone;
  ff_block *block;

  stats.bytes_total = 0;
  stats.chunks_used = 0;
  stats.bytes_used = 0;
  stats.chunks_free = 0;
  stats.bytes_free = 0;
  pthread_mutex_lock(&(zptr->lock));
  block = zptr->blocks;
  /* Go through each block. */
  while (block != NULL)
    {
      ff_block *blockend = chunkNext(block);
      ff_block *chunk = &block[1];

      stats.bytes_total += chunkSize(block);
      while (chunk < blockend)
        {
          size_t chunksize = chunkSize(chunk);

          if (chunkIsInUse(chunk))
            {
              stats.chunks_used++;
              stats.bytes_used += chunksize;
            }
          else
            {
              stats.chunks_free++;
              stats.bytes_free += chunksize;
            }
          chunk = chunkNext(chunk);
        }
      block = block->next;
    }
  /* Go through buffer. */
  for (i = 0; i < zptr->bufsize; i++)
    {
      stats.chunks_used--;
      stats.chunks_free++;
      stats.bytes_used -= zptr->size_buf[i];
      stats.bytes_free += zptr->size_buf[i];
    }
  pthread_mutex_unlock(&(zptr->lock));
  /* Remove overhead. */
  stats.bytes_used -= FBSZ*stats.chunks_used;
  return stats;
}

/* Calculate which segregation class a certain size should be in.
   FIXME: Optimize code and find a more optimum distribution. */
static inline size_t
segindex (size_t size)
{
  NSAssert(size%MINCHUNK == 0, NSInternalInconsistencyException);

  if (size < CLTOSZ(8))
    return size/MINCHUNK;
  else if (size < CLTOSZ(16))
    return 7;
  else if (size < CLTOSZ(32))
    return 8;
  else if (size < CLTOSZ(64))
    return 9;
  else if (size < CLTOSZ(128))
    return 10;
  else if (size < CLTOSZ(256))
    return 11;
  else if (size < CLTOSZ(512))
    return 12;
  else if (size < CLTOSZ(1024))
    return 13;
  else if (size < CLTOSZ(2048))
    return 14;
  else
    return 15;
}

/* Look through the segregated list with first fit to find a memory
   chunk.  If one is not found, get more memory. */
static ff_block*
get_chunk (ffree_zone *zone, size_t size)
{
  size_t class = segindex(size);
  ff_block *chunk;
  ff_link *link = zone->segheadlist[class];

  NSAssert(size%MINCHUNK == 0, NSInternalInconsistencyException);

  while ((link != NULL) && (chunkSize((ff_block*)link) < size))
    link = link->next;
  if (link == NULL)
    /* Get more memory. */
    {
      class++;
      while ((class < MAX_SEG) && (zone->segheadlist[class] == NULL))
        class++;
      if (class == MAX_SEG)
        /* Absolutely no memory in segregated list. */
        {
          size_t blocksize;
          ff_block *block;

          blocksize = roundupto(size, zone->common.gran);
          block = malloc(blocksize+2*FBSZ);
          if (block == NULL)
            return NULL;

	  /*
	   *	Set up the new block header and add to blocks list.
	   */
          block->size = blocksize+FBSZ;	/* Point to block trailer.	*/
          block->next = zone->blocks;
          zone->blocks = block;
	  /*
	   *	Set up the block trailer.
	   */
          chunk = chunkNext(block);
	  chunk->next = block;		/* Point back to block head.	*/
	  /*
	   *	Now set up block contents.
	   */
          if (size < blocksize)
            {
	      chunkSetSize(chunk, INUSE);	/* Tailer size is zero.	*/
              chunk = &block[1];
	      chunkSetSize(chunk, size | PREVUSE | INUSE);
	      chunk = chunkNext(chunk);
	      chunkSetSize(chunk, (block->size-FBSZ-size) | PREVUSE);
              put_chunk(zone, chunk);
              chunk = &block[1];
            }
          else
	    {
	      chunkSetSize(chunk, PREVUSE | INUSE);
              chunk = &block[1];
	      chunkSetSize(chunk, size | PREVUSE | INUSE);
	    }
        }
      else
        {
          ff_block *slack;

          NSAssert(class < MAX_SEG, NSInternalInconsistencyException);

          chunk = (ff_block*)zone->segheadlist[class];

          NSAssert(!chunkIsInUse(chunk), NSInternalInconsistencyException);
          NSAssert(size < chunkSize(chunk), NSInternalInconsistencyException);
          NSAssert((chunkSize(chunk) % MINCHUNK) == 0,
	    NSInternalInconsistencyException);

          take_chunk(zone, chunk);
	  slack = chunkChop(chunk, size);
          put_chunk(zone, slack);
        }
    }
  else
    {
      size_t chunksize;

      chunk = (ff_block*)link;
      chunksize = chunkSize(chunk);

      NSAssert((chunksize % MINCHUNK) == 0, NSInternalInconsistencyException);
      NSAssert(!chunkIsInUse(chunk), NSInternalInconsistencyException);
      NSAssert(chunkIsPrevInUse(chunk), NSInternalInconsistencyException);
      NSAssert(chunkIsInUse(chunkNext(chunk)),
	NSInternalInconsistencyException);

      take_chunk(zone, chunk);
      if (chunksize > size)
        {
          ff_block *slack;

          slack = chunkChop(chunk, size);
          put_chunk(zone, slack);
        }
      else
        {
          ff_block *nextchunk = chunkNext(chunk);

          NSAssert(!chunkIsInUse(chunk), NSInternalInconsistencyException);
          NSAssert(!chunkIsPrevInUse(nextchunk),
	    NSInternalInconsistencyException);
          NSAssert(chunksize == size, NSInternalInconsistencyException);
	  chunkSetInUse(chunk);
	  chunkSetPrevInUse(nextchunk);
        }
    }
  NSAssert(chunkIsInUse(chunk), NSInternalInconsistencyException);
  NSAssert(chunkIsPrevInUse(chunkNext(chunk)),
    NSInternalInconsistencyException);
  return chunk;
}

/* Take the given chunk out of the free list.  No headers are set. */
static void
take_chunk (ffree_zone *zone, ff_block *chunk)
{
  size_t size = chunkSize(chunk);
  size_t class = segindex(size);
  ff_link *otherlink;
  ff_link *links = (ff_link*)chunk;

  NSAssert((size % MINCHUNK) == 0, NSInternalInconsistencyException);
  NSAssert(!chunkIsInUse(chunk), NSInternalInconsistencyException);

  if (links->prev == NULL)
    zone->segheadlist[class] = links->next;
  else
    {
      otherlink = links->prev;
      otherlink->next = links->next;
    }
  if (links->next == NULL)
    zone->segtaillist[class] = links->prev;
  else
    {
      otherlink = links->next;
      otherlink->prev = links->prev;
    }
}

/*
 *	Add the given chunk to the segregated list.  The header to the
 *	chunk must be set appropriately, but the tailer is set here.
 *	NB.  The chunk must NOT be in use, and the adjacent chunks within
 *	its memory block MUST be in use - the memory coalescing done in
 *	flush_buf() depends on this rule.
 */
static void
put_chunk (ffree_zone *zone, ff_block *chunk)
{
  size_t size = chunkSize(chunk);
  size_t class = segindex(size);
  ff_link *links = (ff_link*)chunk;

  NSAssert((chunkSize(chunk) % MINCHUNK) == 0,
    NSInternalInconsistencyException);
  NSAssert(!chunkIsInUse(chunk), NSInternalInconsistencyException);
  NSAssert(chunkIsPrevInUse(chunk), NSInternalInconsistencyException);
  NSAssert(chunkIsInUse(chunkNext(chunk)), NSInternalInconsistencyException);

  chunkMakeLink(chunk);
  if (zone->segtaillist[class] == NULL)
    {
      NSAssert(zone->segheadlist[class] == NULL,
	NSInternalInconsistencyException);

      zone->segheadlist[class] = zone->segtaillist[class] = links;
      links->prev = links->next = NULL;
    }
  else
    {
      ff_link *prevlink = zone->segtaillist[class];

      NSAssert(zone->segheadlist[class] != NULL,
	NSInternalInconsistencyException);

      links->next = NULL;
      links->prev = prevlink;
      prevlink->next = links;
      zone->segtaillist[class] = links;
    }
}

/* Add the given pointer to the buffer.  If the buffer becomes full,
   flush it.  The given pointer must always be one that points to used
   memory (i.e. chunks with headers that declare them as used). */
static inline void
add_buf (ffree_zone *zone, ff_block *chunk)
{
  size_t bufsize = zone->bufsize;

  NSAssert(bufsize < BUFFER, NSInternalInconsistencyException);
  NSAssert(chunkIsInUse(chunk), NSInternalInconsistencyException);
  NSAssert((chunkSize(chunk) % MINCHUNK) == 0,
    NSInternalInconsistencyException);
  NSAssert(chunkSize(chunk) >= MINCHUNK, NSInternalInconsistencyException);

  zone->bufsize++;
  zone->size_buf[bufsize] = chunkSize(chunk);
  zone->ptr_buf[bufsize] = chunk;
  chunkClrLive(chunk);
  if (bufsize == BUFFER-1)
    flush_buf(zone);
}

/* Flush buffers.  All coalescing is done here. */
static void
flush_buf (ffree_zone *zone)
{
  size_t i, size;
  size_t bufsize = zone->bufsize;
  ff_block *chunk, *nextchunk;
  size_t *size_buf = zone->size_buf;
  ff_block **ptr_buf = zone->ptr_buf;

  NSAssert(bufsize <= BUFFER, NSInternalInconsistencyException);

  for (i = 0; i < bufsize; i++)
    {
      size = size_buf[i];
      chunk = ptr_buf[i];

      NSAssert(chunkSize(chunk) == size, NSInternalInconsistencyException);
      NSAssert(chunkIsInUse(chunk), NSInternalInconsistencyException);

      nextchunk = chunkNext(chunk);
      if (!chunkIsPrevInUse(chunk))
        /* Coalesce with previous chunk. */
        {
	  chunk = chunkPrev(chunk);
	  NSAssert(!chunkIsInUse(chunk), NSInternalInconsistencyException);
	  NSAssert(chunkIsPrevInUse(chunk), NSInternalInconsistencyException);
          size += chunkSize(chunk);
          take_chunk(zone, chunk);
        }
      if (!chunkIsInUse(nextchunk))
        /* Coalesce with next chunk. */
        {
          size_t nextsize = chunkSize(nextchunk);

	  NSAssert(chunkIsPrevInUse(nextchunk),
	    NSInternalInconsistencyException);
          NSAssert((nextsize % MINCHUNK) == 0,
	    NSInternalInconsistencyException);
          size += nextsize;
          take_chunk(zone, nextchunk);
	  nextchunk = chunkNext(nextchunk);
        }
      chunkSetSize(chunk, size | PREVUSE);
      put_chunk(zone, chunk);
      chunkClrPrevInUse(nextchunk);
      NSAssert(chunkNext(chunk) == nextchunk, NSInternalInconsistencyException);
      NSAssert(chunkPrev(nextchunk) == chunk, NSInternalInconsistencyException);
      NSAssert((chunkSize(chunk) % MINCHUNK) == 0,
	NSInternalInconsistencyException);
      NSAssert(!chunkIsInUse(chunk), NSInternalInconsistencyException);
      NSAssert(chunkIsPrevInUse(chunk), NSInternalInconsistencyException);
      NSAssert(chunkIsInUse(nextchunk), NSInternalInconsistencyException);
      NSAssert(!chunkIsPrevInUse(nextchunk), NSInternalInconsistencyException);
    }
  zone->bufsize = 0;
}

/* If the first block in block list has enough space, use that space.
   Otherwise, sort the block list in decreasing free space order (only
   the first block needs to be put in its appropriate place since
   the rest of the list is already sorted).  Then check if the first
   block has enough space for the request.  If it does, use it.  If it
   doesn't, get more memory from the default zone, since none of the
   other blocks in the block list could have enough memory. */
static void*
nmalloc (NSZone *zone, size_t size)
{
  nfree_zone *zptr = (nfree_zone*)zone;
  size_t chunksize = roundupto(size, ALIGN);
  size_t freesize;
  void *chunkhead;
  nf_block *block;
  size_t top;

  pthread_mutex_lock(&(zptr->lock));
  block = zptr->blocks;
  top = block->top;
  freesize = block->size-top;
  if (freesize >= chunksize)
    {
      chunkhead = (void*)(block)+top;
      block->top += chunksize;
    }
  else
    {
      nf_block *preblock;

      /* First, get the block list in decreasing free size order. */
      preblock = NULL;
      while ((block->next != NULL)
        && (freesize < block->next->size-block->next->top))
        {
          preblock = block;
          block = block->next;
        }
      if (preblock != NULL)
        {
          preblock->next = zptr->blocks;
          zptr->blocks = zptr->blocks->next;
          preblock->next->next = block;
        }
      if (zptr->blocks->size-zptr->blocks->top < chunksize)
        /* Get new block. */
        {
          size_t blocksize = roundupto(chunksize+NF_HEAD, zone->gran);

          block = malloc(blocksize);
          if (block == NULL)
            {
              pthread_mutex_unlock(&(zptr->lock));
              if (zone->name != nil)
                [NSException raise: NSMallocException
                            format: @"Zone %@ has run out of memory",
                             zone->name];
              else
                [NSException raise: NSMallocException
                            format: @"Out of memory"];
            }
          block->next = zptr->blocks;
          block->size = blocksize;
          block->top = NF_HEAD;
          zptr->blocks = block;
        }
      chunkhead = (void*)block+block->top;
      block->top += chunksize;
    }
  zptr->use++;
  pthread_mutex_unlock(&(zptr->lock));
  return chunkhead;
}

/* Return the blocks to the default zone, then deallocate mutex, and
   then release zone name if it exists. */
static BOOL
nrecycle1 (NSZone *zone)
{
  nfree_zone *zptr = (nfree_zone*)zone;

  pthread_mutex_lock(&(zptr->lock));
  if (zptr->use == 0)
    {
      nf_block *nextblock;
      nf_block *block = zptr->blocks;

      while (block != NULL)
	{
	  nextblock = block->next;
	  free(block);
	  block = nextblock;
	}
      zptr->blocks = 0;
    }
  pthread_mutex_unlock(&(zptr->lock));
  if (zptr->blocks == 0)
    {
      pthread_mutex_destroy(&(zptr->lock));
      return YES;
    }
  return NO;
}

/* Recycle the zone. */
static void
nrecycle (NSZone *zone)
{
  [gnustep_global_lock lock];
  if (zone->name != nil)
    {
      NSString *name = zone->name;
      zone->name = nil;
      [name release];
    }
  if (nrecycle1(zone) == YES)
    destroy_zone(zone);
  else
    {
      zone->malloc = rmalloc;
      zone->realloc = rrealloc;
      zone->free = rnfree;
      zone->recycle = rrecycle;
    }
  [gnustep_global_lock unlock];
}

static void*
nrealloc (NSZone *zone, void *ptr, size_t size)
{
  nfree_zone *zptr = (nfree_zone*)zone;
  void *tmp = nmalloc(zone, size);

  if (ptr != 0)
    {
      pthread_mutex_lock(&(zptr->lock));
      if (tmp)
	{
	  nf_block *block;
	  size_t old = 0;

	  for (block = zptr->blocks; block != NULL; block = block->next) {
	    if (ptr >= (void*)block && ptr < ((void*)block)+block->size) {
		old = ((void*)block)+block->size - ptr;
		break;
	    }
	  }
	  if (old > 0)
	    {
	      if (size < old)
		old = size;
	      memcpy(tmp, ptr, old);
	    }
	}
      zptr->use--;
      pthread_mutex_unlock(&(zptr->lock));
    }
  return tmp;
}

/*
 *	The OpenStep spec says we don't release memory - but we have to do
 *	some minimal bookkeeping so that, when the zone is recycled, we can
 *	determine if all the allocated memory has been freed.  Until it is
 *	all freed, we can't actually destroy the zone!
 */
static void
nfree (NSZone *zone, void *ptr)
{
  nfree_zone *zptr = (nfree_zone*)zone;

  pthread_mutex_lock(&(zptr->lock));
  zptr->use--;
  pthread_mutex_unlock(&(zptr->lock));
}

static void
rnfree (NSZone *zone, void *ptr)
{
  nfree_zone *zptr = (nfree_zone*)zone;

  nfree(zone, ptr);
  if (zptr->use == 0)
    {
      [gnustep_global_lock lock];
      nrecycle1(zone);
      destroy_zone(zone);
      [gnustep_global_lock unlock];
    }
}

/* Check integrity of a nonfreeable zone.  Doesn't have to
   particularly efficient. */
static BOOL
ncheck (NSZone *zone)
{
  nfree_zone *zptr = (nfree_zone*)zone;
  nf_block *block;

  pthread_mutex_lock(&(zptr->lock));
  block = zptr->blocks;
  while (block != NULL)
    {
      if (block->size < block->top)
        {
          pthread_mutex_unlock(&(zptr->lock));
          return NO;
        }
      block = block->next;
    }
  /* FIXME: Do more checking? */
  pthread_mutex_unlock(&(zptr->lock));
  return YES;
}

static BOOL
nlookup (NSZone *zone, void *ptr)
{
  nfree_zone *zptr = (nfree_zone*)zone;
  nf_block *block;
  BOOL found = NO;

  pthread_mutex_lock(&(zptr->lock));
  for (block = zptr->blocks; block != NULL; block = block->next)
    {
      if (ptr >= (void*)block &&  ptr < ((void*)block)+block->size)
	{
	  found = YES;
	  break;
	}
    }
  pthread_mutex_unlock(&(zptr->lock));
  return found;
}

/* Return statistics for a nonfreeable zone.  Doesn't have to
   particularly efficient. */
static struct NSZoneStats
nstats (NSZone *zone)
{
  struct NSZoneStats stats;
  nfree_zone *zptr = (nfree_zone*)zone;
  nf_block *block;

  stats.bytes_total = 0;
  stats.chunks_used = 0;
  stats.bytes_used = 0;
  stats.chunks_free = 0;
  stats.bytes_free = 0;
  pthread_mutex_lock(&(zptr->lock));
  block = zptr->blocks;
  while (block != NULL)
    {
      size_t *chunk;

      stats.bytes_total += block->size;
      chunk = (void*)block+NF_HEAD;
      while ((void*)chunk < (void*)block+block->top)
        {
          stats.chunks_used++;
          stats.bytes_used += *chunk;
          chunk = (void*)chunk+(*chunk);
        }
      if (block->size != block->top)
        {
          stats.chunks_free++;
          stats.bytes_free += block->size-block->top;
        }
      block = block->next;
    }
  pthread_mutex_unlock(&(zptr->lock));
  return stats;
}


static void*
rmalloc (NSZone *zone, size_t size)
{
  [NSException raise: NSMallocException
	      format: @"Attempt to malloc memory in recycled zone"];
  return 0;
}

static void
rrecycle (NSZone *zone)
{
  [NSException raise: NSMallocException
	      format: @"Attempt to recycle a recycled zone"];
}

static void*
rrealloc (NSZone *zone, void *ptr, size_t size)
{
  [NSException raise: NSMallocException
	      format: @"Attempt to realloc memory in recycled zone"];
  return 0;
}

static void rnfree (NSZone *zone, void *ptr);

GS_DECLARE NSZone*
NSZoneFromPointer(void *ptr)
{
  NSZone	*zone;

  if (ptr == 0) return 0;
  if (zone_list == 0) return &default_zone;

  /*
   *	See if we can find the zone in our list of all zones.
   */
  [gnustep_global_lock lock];
  for (zone = zone_list; zone != 0; zone = zone->next)
    {
      if ((zone->lookup)(zone, ptr) == YES)
	{
	  break;
	}
    }
  [gnustep_global_lock unlock];
  return (zone == 0) ? &default_zone : zone;
}

NSZone*
NSCreateZone (NSUInteger start, NSUInteger gran, BOOL canFree)
{
  size_t i, startsize, granularity;
  NSZone *newZone;

  if (start > 0)
    startsize = roundupto(start, roundupto(MINGRAN, MINCHUNK));
  else
    startsize = roundupto(MINGRAN, MINCHUNK);
  if (gran > 0)
    granularity = roundupto(gran, roundupto(MINGRAN, MINCHUNK));
  else
    granularity = roundupto(MINGRAN, MINCHUNK);
  if (canFree)
    {
      ffree_zone *zone;
      ff_block *block;
      ff_block *chunk;
      ff_block *tailer;

      zone = malloc(sizeof(ffree_zone));
      if (zone == NULL)
        [NSException raise: NSMallocException
                     format: @"No memory to create zone"];
      zone->common.malloc = fmalloc;
      zone->common.realloc = frealloc;
      zone->common.free = ffree;
      zone->common.recycle = frecycle;
      zone->common.check = fcheck;
      zone->common.lookup = flookup;
      zone->common.stats = fstats;
      zone->common.gran = granularity;
      zone->common.name = nil;
      GS_INIT_RECURSIVE_MUTEX(zone->lock);
      for (i = 0; i < MAX_SEG; i++)
        {
          zone->segheadlist[i] = NULL;
          zone->segtaillist[i] = NULL;
        }
      zone->bufsize = 0;
      zone->blocks = malloc(startsize + 2*FBSZ);
      if (zone->blocks == NULL)
        {
          pthread_mutex_destroy(&(zone->lock));
          free(zone);
          [NSException raise: NSMallocException
                       format: @"No memory to create zone"];
        }
      /*
       *	Set up block header.
       */
      block = zone->blocks;
      block->next = NULL;		/* Point to next block.		*/
      block->size = startsize+FBSZ;	/* Point to first chunk.	*/
      /*
       *	Set up block trailer.
       */
      tailer = chunkNext(block);
      chunkSetSize(tailer, PREVUSE|INUSE);
      tailer->next = block;		/* Point back to block start.	*/
      /*
       *	Set up the block as a single chunk and put it in the
       *	buffer for quick allocation.
       */
      chunk = &block[1];
      chunkSetSize(chunk, (block->size-FBSZ) | PREVUSE|INUSE);
      add_buf(zone, chunk);

      newZone = (NSZone*)zone;
    }
  else
    {
      nf_block *block;
      nfree_zone *zone;

      zone = malloc(sizeof(nfree_zone));
      if (zone == NULL)
        [NSException raise: NSMallocException
                     format: @"No memory to create zone"];
      zone->common.malloc = nmalloc;
      zone->common.realloc = nrealloc;
      zone->common.free = nfree;
      zone->common.recycle = nrecycle;
      zone->common.check = ncheck;
      zone->common.lookup = nlookup;
      zone->common.stats = nstats;
      zone->common.gran = granularity;
      zone->common.name = nil;
      GS_INIT_RECURSIVE_MUTEX(zone->lock);
      zone->blocks = malloc(startsize);
      zone->use = 0;
      if (zone->blocks == NULL)
        {
          pthread_mutex_destroy(&(zone->lock));
          free(zone);
          [NSException raise: NSMallocException
                       format: @"No memory to create zone"];
        }

      block = zone->blocks;
      block->next = NULL;
      block->size = startsize;
      block->top = NF_HEAD;
      newZone = (NSZone*)zone;
    }

  [gnustep_global_lock lock];
  newZone->next = zone_list;
  zone_list = newZone;
  [gnustep_global_lock unlock];

  return newZone;
}

void*
NSZoneCalloc (NSZone *zone, NSUInteger elems, NSUInteger bytes)
{
  return memset(NSZoneMalloc(zone, elems*bytes), 0, elems*bytes);
}

void *
NSAllocateCollectable(NSUInteger size, NSUInteger options)
{
  return NSZoneCalloc(NSDefaultMallocZone(), 1, size);
}

void *
NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options)
{
  return NSZoneRealloc(0, ptr, size);
}

NSZone*
NSDefaultMallocZone (void)
{
  return &default_zone;
}

NSZone*
GSAtomicMallocZone (void)
{
  return &default_zone;
}

void
GSMakeWeakPointer(Class theClass, const char *iVarName)
{
  return;
}

BOOL
GSAssignZeroingWeakPointer(void **destination, void *source)
{
  if (destination == 0)
    {
      return NO;
    }
  *destination = source;
  return YES;
}

void*
NSZoneMalloc (NSZone *zone, NSUInteger size)
{
  if (!zone)
    zone = NSDefaultMallocZone();
  return (zone->malloc)(zone, size);
}

void* 
NSZoneRealloc (NSZone *zone, void *ptr, NSUInteger size)
{
  if (!zone)
    zone = NSDefaultMallocZone();
  return (zone->realloc)(zone, ptr, size);
}

void
NSRecycleZone (NSZone *zone)
{
  if (!zone)
    zone = NSDefaultMallocZone();
  (zone->recycle)(zone);
}

void
NSZoneFree (NSZone *zone, void *ptr)
{
  if (!zone)
    zone = NSDefaultMallocZone();
  (zone->free)(zone, ptr);
}

BOOL
NSZoneCheck (NSZone *zone)
{
  if (!zone)
    zone = NSDefaultMallocZone();
  return (zone->check)(zone);
}

struct NSZoneStats
NSZoneStats (NSZone *zone)
{
  if (!zone)
    zone = NSDefaultMallocZone();
  return (zone->stats)(zone);
}

BOOL
GSPrivateIsCollectable(const void *ptr)
{
  return NO;
}

#endif	/* GS_WITH_GC */