home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / libg++ / src / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  33.2 KB  |  1,250 lines

  1. /* 
  2. Copyright (C) 1989 Free Software Foundation
  3.     written by Doug Lea (dl@oswego.edu)
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17.  
  18.  
  19.  
  20. #ifndef NO_LIBGXX_MALLOC   /* ignore whole file otherwise */
  21.  
  22. /* compile with -DMALLOC_STATS to collect statistics */
  23. /* collecting statistics slows down malloc by at least 15% */
  24.  
  25. #ifdef MALLOC_STATS
  26. #define UPDATE_STATS(ARGS) {ARGS;}
  27. #else
  28. #define UPDATE_STATS(ARGS)
  29. #endif
  30.  
  31. /* History
  32.  
  33.  
  34.    Tue Jan 16 04:54:27 1990  Doug Lea  (dl at g.oswego.edu)
  35.  
  36.      version 1 released in libg++
  37.  
  38.    Sun Jan 21 05:52:47 1990  Doug Lea  (dl at g.oswego.edu)
  39.  
  40.      bins are now own struct for, sanity.
  41.  
  42.      new victim search strategy: scan up and consolidate.
  43.      Both faster and less fragmentation.
  44.  
  45.      refined when to scan bins for consolidation, via consollink, etc.
  46.  
  47.      realloc: always try to expand chunk, avoiding some fragmentation.
  48.  
  49.      changed a few inlines into macros
  50.  
  51.      hardwired SBRK_UNIT to 4096 for uniformity across systems
  52.  
  53.    Tue Mar 20 14:18:23 1990  Doug Lea  (dl at g.oswego.edu)
  54.  
  55.      calloc and cfree now correctly parameterized.
  56.  
  57.    Sun Apr  1 10:00:48 1990  Doug Lea  (dl at g.oswego.edu)
  58.  
  59.      added memalign and valloc.
  60.  
  61.    Sun Jun 24 05:46:48 1990  Doug Lea  (dl at g.oswego.edu)
  62.  
  63.      #include gepagesize.h only ifndef sun
  64.      cache pagesize after first call
  65.  
  66.    Wed Jul 25 08:35:19 1990  Doug Lea  (dl at g.oswego.edu)
  67.  
  68.      No longer rely on a `designated victim':
  69.  
  70.        1. It sometimes caused splits of large chunks
  71.           when smaller ones would do, leading to
  72.           bad worst-case fragmentation.
  73.  
  74.        2. Scanning through the av array fast anyway,
  75.           so the overhead isn't worth it.
  76.  
  77.      To compensate, several other minor changes:
  78.  
  79.        1. Unusable chunks are checked for consolidation during
  80.           searches inside bins, better distributing chunks
  81.           across bins.
  82.  
  83.        2. Chunks are returned when found in malloc_find_space,
  84.            rather than finishing cleaning everything up, to
  85.            avoid wasted iterations due to (1).
  86.  
  87.    Sun Dec 15 08:50:37 1991  Doug Lea  (dl at g.oswego.edu)
  88.  
  89.        unsigned int => size_t
  90.  
  91. */
  92.  
  93. /* 
  94.   A version of malloc/free/realloc tuned for C++ applications.
  95.  
  96.   Here's what you probably want to know first:
  97.  
  98.   In various tests, this appears to be about as fast as,
  99.   and usually substantially less memory-wasteful than BSD/GNUemacs malloc.
  100.  
  101.   Generally, it is slower (by perhaps 20%) than bsd-style malloc
  102.   only when bsd malloc would waste a great deal of space in 
  103.   fragmented blocks, which this malloc recovers; or when, by
  104.   chance or design, nearly all requests are near the bsd malloc
  105.   power-of-2 allocation bin boundaries, and as many chunks are
  106.   used as are allocated. 
  107.  
  108.   It uses more space than bsd malloc only when, again by chance
  109.   or design, only bsdmalloc bin-sized requests are malloced, or when
  110.   little dynamic space is malloced, since this malloc may grab larger
  111.   chunks from the system at a time than bsd.
  112.  
  113.   In other words, this malloc seems generally superior to bsd
  114.   except perhaps for programs that are specially tuned to
  115.   deal with bsdmalloc's characteristics. But even here, the
  116.   performance differences are slight.
  117.  
  118.  
  119.   This malloc, like any other, is a compromised design. 
  120.  
  121.  
  122.   Chunks of memory are maintained using a `boundary tag' method as
  123.   described in e.g., Knuth or Standish.  This means that the size of
  124.   the chunk is stored both in the front of the chunk and at the end.
  125.   This makes consolidating fragmented chunks into bigger chunks very fast.
  126.   The size field is also used to hold bits representing whether a
  127.   chunk is free or in use.
  128.  
  129.   Malloced chunks have space overhead of 8 bytes: The preceding
  130.   and trailing size fields. When they are freed, the list pointer
  131.   fields are also needed.
  132.  
  133.   Available chunks are kept in doubly linked lists. The lists are
  134.   maintained in an array of bins using a power-of-two method, except
  135.   that instead of 32 bins (one for each 1 << i), there are 128: each
  136.   power of two is split in quarters. The use of very fine bin sizes 
  137.   closely approximates the use of one bin per actually used size,
  138.   without necessitating the overhead of locating such bins. It is
  139.   especially desirable in common C++ applications where large numbers
  140.   of identically-sized blocks are malloced/freed in some dynamic
  141.   manner, and then later are all freed. The finer bin sizes make
  142.   finding blocks fast, with little wasted overallocation. The
  143.   consolidation methods ensure that once the collection of blocks is
  144.   no longer useful, fragments are gathered into bigger chunks awaiting new
  145.   roles.
  146.  
  147.   The bins av[i] serve as heads of the lists. Bins contain a dummy
  148.   header for the chunk lists, and a `dirty' field used to indicate
  149.   whether the list may need to be scanned for consolidation.
  150.  
  151.   On allocation, the bin corresponding to the request size is
  152.   scanned, and if there is a chunk with size >= requested, it
  153.   is split, if too big, and used. Chunks on the list which are
  154.   too small are examined for consolidation during this traversal.
  155.  
  156.   If no chunk exists in the list bigger bins are scanned in search of
  157.   a victim.
  158.  
  159.   If no victim can be found, then smaller bins are examined for
  160.   consolidation in order to construct a victim.
  161.  
  162.   Finally, if consolidation fails to come up with a usable chunk,
  163.   more space is obtained from the system.
  164.  
  165.   After a split, the remainder is placed on
  166.   the back of the appropriate bin list. (All freed chunks are placed
  167.   on fronts of lists. All remaindered or consolidated chunks are
  168.   placed on the rear. Correspondingly, searching within a bin
  169.   starts at the front, but finding victims is from the back. All
  170.   of this approximates  the  effect of having 2 kinds of lists per 
  171.   bin: returned chunks vs unallocated chunks, but without the overhead 
  172.   of maintaining 2 lists.)
  173.  
  174.   Deallocation (free) consists only of placing the chunk on
  175.   a list.
  176.  
  177.   Reallocation proceeds in the usual way. If a chunk can be extended,
  178.   it is, else a malloc-copy-free sequence is taken.
  179.  
  180.   memalign requests more than enough space from malloc, finds a
  181.   spot within that chunk that meets the alignment request, and
  182.   then possibly frees the leading and trailing space. Overreliance
  183.   on memalign is a sure way to fragment space.
  184.  
  185.  
  186.   Some other implementation matters:
  187.  
  188.   8 byte alignment is currently hardwired into the design. Calling
  189.   memalign will return a chunk that is both 8-byte aligned, and
  190.   meets the requested alignment.
  191.  
  192.   The basic overhead of a used chunk is 8 bytes: 4 at the front and
  193.   4 at the end.
  194.  
  195.   When a chunk is free, 8 additional bytes are needed for free list
  196.   pointers. Thus, the minimum allocatable size is 16 bytes.
  197.  
  198.   The existence of front and back overhead permits some reasonably
  199.   effective fence-bashing checks: The front and back fields must
  200.   be identical. This is checked only within free() and realloc().
  201.   The checks are fast enough to be made non-optional.
  202.  
  203.   The overwriting of parts of freed memory with the freelist pointers
  204.   can also be very effective (albeit in an annoying way) in helping 
  205.   users track down dangling pointers.
  206.  
  207.   User overwriting of freed space will often result in crashes
  208.   within malloc or free.
  209.   
  210.   These routines are also tuned to C++ in that free(0) is a noop and
  211.   a failed malloc automatically calls (*new_handler)(). 
  212.  
  213.   malloc(0) returns a pointer to something of the minimum allocatable size.
  214.  
  215.   Additional memory is gathered from the system (via sbrk) in a
  216.   way that allows chunks obtained across different sbrk calls to
  217.   be consolidated, but does not require contiguous memory: Thus,
  218.   it should be safe to intersperse mallocs with other sbrk calls.
  219.  
  220.   This malloc is NOT designed to work in multiprocessing applications.
  221.   No semaphores or other concurrency control are provided to ensure
  222.   that multiple malloc or free calls don't run at the same time,
  223.   which could be disasterous.
  224.  
  225.   VERY heavy use of inlines is made, for clarity. If this malloc
  226.   is ported via a compiler without inlining capabilities, all
  227.   inlines should be transformed into macros -- making them non-inline
  228.   makes malloc at least twice as slow.
  229.  
  230.  
  231. */
  232.  
  233.  
  234. /* preliminaries */
  235.  
  236. #include <stddef.h>   /* for size_t */
  237.  
  238. #ifdef __cplusplus
  239. #include <stdio.h>
  240. #else
  241. #include "//usr/include/stdio.h"  /* needed for error reporting */
  242. #endif
  243.  
  244. #ifdef __cplusplus
  245. extern "C" {
  246. #endif
  247.  
  248. #ifdef _G_SYSV
  249. extern void*     memset(void*, int, int);
  250. extern void*     memcpy(void*,  const void*, int);
  251. inline void      bzero(void* s, int l) { memset(s, 0, l); }
  252. #else
  253. extern void      bzero(void*, size_t      );
  254. #endif
  255.  
  256. extern void      bcopy(void*, void*, size_t      );
  257.  
  258. extern void*     sbrk(size_t      );
  259.  
  260.  
  261. #ifdef __GNUC__
  262. extern volatile void abort();
  263. #else
  264. extern void abort();
  265. #endif
  266.  
  267. #ifdef __cplusplus
  268. };  /* end of extern "C" */
  269. #endif
  270.  
  271.  
  272. /* A good multiple to call sbrk with */
  273.  
  274. #define SBRK_UNIT 4096 
  275.  
  276.  
  277.  
  278. /* how to die on detected error */
  279.  
  280. #ifdef __GNUC__
  281. static volatile void malloc_user_error()
  282. #else
  283. static void malloc_user_error()
  284. #endif
  285. {
  286.   fputs("malloc/free/realloc: clobbered space detected\n", stderr); abort();
  287. }
  288.  
  289.  
  290.  
  291. /*  Basic overhead for each malloc'ed chunk */
  292.  
  293.  
  294. struct malloc_chunk
  295. {
  296.   size_t               size;     /* Size in bytes, including overhead. */
  297.                                  /* Or'ed with INUSE if in use. */
  298.  
  299.   struct malloc_chunk* fd;       /* double links -- used only if free. */
  300.   struct malloc_chunk* bk;
  301.  
  302. };
  303.  
  304. typedef struct malloc_chunk* mchunkptr;
  305.  
  306. struct malloc_bin
  307. {
  308.   struct malloc_chunk hd;        /* dummy list header */
  309.   size_t              dirty;     /* True if maybe consolidatable */
  310.                                  /* Wasting a word here makes */
  311.                                  /* sizeof(bin) a power of 2, */
  312.                                  /* which makes size2bin() faster */
  313. };
  314.  
  315. typedef struct malloc_bin* mbinptr;
  316.  
  317.  
  318. /*  sizes, alignments */
  319.  
  320.  
  321. #define SIZE_SZ                   (sizeof(size_t      ))
  322. #define MALLOC_MIN_OVERHEAD       (SIZE_SZ + SIZE_SZ)
  323. #define MALLOC_ALIGN_MASK         (MALLOC_MIN_OVERHEAD - 1)
  324.  
  325. #define MINSIZE (sizeof(struct malloc_chunk) + SIZE_SZ) /* MUST == 16! */
  326.  
  327.  
  328. /* pad request bytes into a usable size */
  329.  
  330. static inline size_t       request2size(size_t       request)
  331. {
  332.   return  (request == 0) ?  MINSIZE : 
  333.     ((request + MALLOC_MIN_OVERHEAD + MALLOC_ALIGN_MASK) 
  334.       & ~(MALLOC_ALIGN_MASK));
  335. }
  336.  
  337.  
  338. static inline int aligned_OK(void* m)  
  339. {
  340.   return ((size_t      )(m) & (MALLOC_ALIGN_MASK)) == 0;
  341. }
  342.  
  343.  
  344. /* size field or'd with INUSE when in use */
  345. #define INUSE  0x1
  346.  
  347.  
  348.  
  349. /* the bins, initialized to have null double linked lists */
  350.  
  351. #define MAXBIN 120   /* 1 more than needed for 32 bit addresses */
  352.  
  353. #define FIRSTBIN (&(av[0])) 
  354.  
  355. static struct malloc_bin  av[MAXBIN] = 
  356. {
  357.   { { 0, &(av[0].hd),  &(av[0].hd) }, 0 },
  358.   { { 0, &(av[1].hd),  &(av[1].hd) }, 0 },
  359.   { { 0, &(av[2].hd),  &(av[2].hd) }, 0 },
  360.   { { 0, &(av[3].hd),  &(av[3].hd) }, 0 },
  361.   { { 0, &(av[4].hd),  &(av[4].hd) }, 0 },
  362.   { { 0, &(av[5].hd),  &(av[5].hd) }, 0 },
  363.   { { 0, &(av[6].hd),  &(av[6].hd) }, 0 },
  364.   { { 0, &(av[7].hd),  &(av[7].hd) }, 0 },
  365.   { { 0, &(av[8].hd),  &(av[8].hd) }, 0 },
  366.   { { 0, &(av[9].hd),  &(av[9].hd) }, 0 },
  367.  
  368.   { { 0, &(av[10].hd), &(av[10].hd) }, 0 },
  369.   { { 0, &(av[11].hd), &(av[11].hd) }, 0 },
  370.   { { 0, &(av[12].hd), &(av[12].hd) }, 0 },
  371.   { { 0, &(av[13].hd), &(av[13].hd) }, 0 },
  372.   { { 0, &(av[14].hd), &(av[14].hd) }, 0 },
  373.   { { 0, &(av[15].hd), &(av[15].hd) }, 0 },
  374.   { { 0, &(av[16].hd), &(av[16].hd) }, 0 },
  375.   { { 0, &(av[17].hd), &(av[17].hd) }, 0 },
  376.   { { 0, &(av[18].hd), &(av[18].hd) }, 0 },
  377.   { { 0, &(av[19].hd), &(av[19].hd) }, 0 },
  378.  
  379.   { { 0, &(av[20].hd), &(av[20].hd) }, 0 },
  380.   { { 0, &(av[21].hd), &(av[21].hd) }, 0 },
  381.   { { 0, &(av[22].hd), &(av[22].hd) }, 0 },
  382.   { { 0, &(av[23].hd), &(av[23].hd) }, 0 },
  383.   { { 0, &(av[24].hd), &(av[24].hd) }, 0 },
  384.   { { 0, &(av[25].hd), &(av[25].hd) }, 0 },
  385.   { { 0, &(av[26].hd), &(av[26].hd) }, 0 },
  386.   { { 0, &(av[27].hd), &(av[27].hd) }, 0 },
  387.   { { 0, &(av[28].hd), &(av[28].hd) }, 0 },
  388.   { { 0, &(av[29].hd), &(av[29].hd) }, 0 },
  389.  
  390.   { { 0, &(av[30].hd), &(av[30].hd) }, 0 },
  391.   { { 0, &(av[31].hd), &(av[31].hd) }, 0 },
  392.   { { 0, &(av[32].hd), &(av[32].hd) }, 0 },
  393.   { { 0, &(av[33].hd), &(av[33].hd) }, 0 },
  394.   { { 0, &(av[34].hd), &(av[34].hd) }, 0 },
  395.   { { 0, &(av[35].hd), &(av[35].hd) }, 0 },
  396.   { { 0, &(av[36].hd), &(av[36].hd) }, 0 },
  397.   { { 0, &(av[37].hd), &(av[37].hd) }, 0 },
  398.   { { 0, &(av[38].hd), &(av[38].hd) }, 0 },
  399.   { { 0, &(av[39].hd), &(av[39].hd) }, 0 },
  400.  
  401.   { { 0, &(av[40].hd), &(av[40].hd) }, 0 },
  402.   { { 0, &(av[41].hd), &(av[41].hd) }, 0 },
  403.   { { 0, &(av[42].hd), &(av[42].hd) }, 0 },
  404.   { { 0, &(av[43].hd), &(av[43].hd) }, 0 },
  405.   { { 0, &(av[44].hd), &(av[44].hd) }, 0 },
  406.   { { 0, &(av[45].hd), &(av[45].hd) }, 0 },
  407.   { { 0, &(av[46].hd), &(av[46].hd) }, 0 },
  408.   { { 0, &(av[47].hd), &(av[47].hd) }, 0 },
  409.   { { 0, &(av[48].hd), &(av[48].hd) }, 0 },
  410.   { { 0, &(av[49].hd), &(av[49].hd) }, 0 },
  411.  
  412.   { { 0, &(av[50].hd), &(av[50].hd) }, 0 },
  413.   { { 0, &(av[51].hd), &(av[51].hd) }, 0 },
  414.   { { 0, &(av[52].hd), &(av[52].hd) }, 0 },
  415.   { { 0, &(av[53].hd), &(av[53].hd) }, 0 },
  416.   { { 0, &(av[54].hd), &(av[54].hd) }, 0 },
  417.   { { 0, &(av[55].hd), &(av[55].hd) }, 0 },
  418.   { { 0, &(av[56].hd), &(av[56].hd) }, 0 },
  419.   { { 0, &(av[57].hd), &(av[57].hd) }, 0 },
  420.   { { 0, &(av[58].hd), &(av[58].hd) }, 0 },
  421.   { { 0, &(av[59].hd), &(av[59].hd) }, 0 },
  422.  
  423.   { { 0, &(av[60].hd), &(av[60].hd) }, 0 },
  424.   { { 0, &(av[61].hd), &(av[61].hd) }, 0 },
  425.   { { 0, &(av[62].hd), &(av[62].hd) }, 0 },
  426.   { { 0, &(av[63].hd), &(av[63].hd) }, 0 },
  427.   { { 0, &(av[64].hd), &(av[64].hd) }, 0 },
  428.   { { 0, &(av[65].hd), &(av[65].hd) }, 0 },
  429.   { { 0, &(av[66].hd), &(av[66].hd) }, 0 },
  430.   { { 0, &(av[67].hd), &(av[67].hd) }, 0 },
  431.   { { 0, &(av[68].hd), &(av[68].hd) }, 0 },
  432.   { { 0, &(av[69].hd), &(av[69].hd) }, 0 },
  433.  
  434.   { { 0, &(av[70].hd), &(av[70].hd) }, 0 },
  435.   { { 0, &(av[71].hd), &(av[71].hd) }, 0 },
  436.   { { 0, &(av[72].hd), &(av[72].hd) }, 0 },
  437.   { { 0, &(av[73].hd), &(av[73].hd) }, 0 },
  438.   { { 0, &(av[74].hd), &(av[74].hd) }, 0 },
  439.   { { 0, &(av[75].hd), &(av[75].hd) }, 0 },
  440.   { { 0, &(av[76].hd), &(av[76].hd) }, 0 },
  441.   { { 0, &(av[77].hd), &(av[77].hd) }, 0 },
  442.   { { 0, &(av[78].hd), &(av[78].hd) }, 0 },
  443.   { { 0, &(av[79].hd), &(av[79].hd) }, 0 },
  444.  
  445.   { { 0, &(av[80].hd), &(av[80].hd) }, 0 },
  446.   { { 0, &(av[81].hd), &(av[81].hd) }, 0 },
  447.   { { 0, &(av[82].hd), &(av[82].hd) }, 0 },
  448.   { { 0, &(av[83].hd), &(av[83].hd) }, 0 },
  449.   { { 0, &(av[84].hd), &(av[84].hd) }, 0 },
  450.   { { 0, &(av[85].hd), &(av[85].hd) }, 0 },
  451.   { { 0, &(av[86].hd), &(av[86].hd) }, 0 },
  452.   { { 0, &(av[87].hd), &(av[87].hd) }, 0 },
  453.   { { 0, &(av[88].hd), &(av[88].hd) }, 0 },
  454.   { { 0, &(av[89].hd), &(av[89].hd) }, 0 },
  455.  
  456.   { { 0, &(av[90].hd), &(av[90].hd) }, 0 },
  457.   { { 0, &(av[91].hd), &(av[91].hd) }, 0 },
  458.   { { 0, &(av[92].hd), &(av[92].hd) }, 0 },
  459.   { { 0, &(av[93].hd), &(av[93].hd) }, 0 },
  460.   { { 0, &(av[94].hd), &(av[94].hd) }, 0 },
  461.   { { 0, &(av[95].hd), &(av[95].hd) }, 0 },
  462.   { { 0, &(av[96].hd), &(av[96].hd) }, 0 },
  463.   { { 0, &(av[97].hd), &(av[97].hd) }, 0 },
  464.   { { 0, &(av[98].hd), &(av[98].hd) }, 0 },
  465.   { { 0, &(av[99].hd), &(av[99].hd) }, 0 },
  466.  
  467.   { { 0, &(av[100].hd), &(av[100].hd) }, 0 },
  468.   { { 0, &(av[101].hd), &(av[101].hd) }, 0 },
  469.   { { 0, &(av[102].hd), &(av[102].hd) }, 0 },
  470.   { { 0, &(av[103].hd), &(av[103].hd) }, 0 },
  471.   { { 0, &(av[104].hd), &(av[104].hd) }, 0 },
  472.   { { 0, &(av[105].hd), &(av[105].hd) }, 0 },
  473.   { { 0, &(av[106].hd), &(av[106].hd) }, 0 },
  474.   { { 0, &(av[107].hd), &(av[107].hd) }, 0 },
  475.   { { 0, &(av[108].hd), &(av[108].hd) }, 0 },
  476.   { { 0, &(av[109].hd), &(av[109].hd) }, 0 },
  477.  
  478.   { { 0, &(av[110].hd), &(av[110].hd) }, 0 },
  479.   { { 0, &(av[111].hd), &(av[111].hd) }, 0 },
  480.   { { 0, &(av[112].hd), &(av[112].hd) }, 0 },
  481.   { { 0, &(av[113].hd), &(av[113].hd) }, 0 },
  482.   { { 0, &(av[114].hd), &(av[114].hd) }, 0 },
  483.   { { 0, &(av[115].hd), &(av[115].hd) }, 0 },
  484.   { { 0, &(av[116].hd), &(av[116].hd) }, 0 },
  485.   { { 0, &(av[117].hd), &(av[117].hd) }, 0 },
  486.   { { 0, &(av[118].hd), &(av[118].hd) }, 0 },
  487.   { { 0, &(av[119].hd), &(av[119].hd) }, 0 }
  488. };
  489.  
  490. /*
  491.   indexing into bins
  492. */
  493.  
  494. static inline mbinptr size2bin(size_t       sz)
  495. {
  496.   mbinptr b = av;
  497.   while (sz >= (MINSIZE * 2)) { b += 4; sz >>= 1; } /* find power of 2 */
  498.   b += (sz - MINSIZE) >> 2;                         /* find quadrant */
  499.   return b;
  500. }
  501.  
  502.  
  503.  
  504. /* counts maintained if MALLOC_STATS defined */
  505.  
  506. #ifdef MALLOC_STATS
  507.  
  508. static size_t       sbrked_mem;
  509. static size_t       requested_mem;
  510. static size_t       malloced_mem;
  511. static size_t       freed_mem;
  512. static size_t       max_used_mem;
  513.  
  514. static size_t       n_sbrks;
  515. static size_t       n_mallocs;
  516. static size_t       n_frees;
  517. static size_t       n_reallocs;
  518. static size_t       n_reallocs_with_copy;
  519. static size_t       n_avail;
  520. static size_t       max_inuse;
  521.  
  522. static size_t       n_malloc_chunks;
  523. static size_t       n_malloc_bins;
  524.  
  525. static size_t       n_split;
  526. static size_t       n_consol;
  527.  
  528.  
  529. static void do_malloc_stats(const mchunkptr p)
  530. {
  531.   ++n_mallocs;
  532.   if ((n_mallocs-n_frees) > max_inuse)
  533.     max_inuse = n_mallocs - n_frees;
  534.   malloced_mem += (p->size & ~(INUSE));
  535.   if (malloced_mem - freed_mem > max_used_mem)
  536.     max_used_mem = malloced_mem - freed_mem;
  537. }
  538.  
  539. static void do_free_stats(const mchunkptr p)
  540. {
  541.   ++n_frees;
  542.   freed_mem += (p->size & ~(INUSE));
  543. }        
  544.  
  545. #endif
  546.  
  547.  
  548.  
  549. /* Utilities needed below for memalign */
  550. /* This is redundant with libg++ support, but not if used stand-alone */
  551.  
  552. static size_t       gcd(size_t       a, size_t       b)
  553. {
  554.   size_t       tmp;
  555.   
  556.   if (b > a)
  557.   {
  558.     tmp = a; a = b; b = tmp;
  559.   }
  560.   for(;;)
  561.   {
  562.     if (b == 0)
  563.       return a;
  564.     else if (b == 1)
  565.       return b;
  566.     else
  567.     {
  568.       tmp = b;
  569.       b = a % b;
  570.       a = tmp;
  571.     }
  572.   }
  573. }
  574.  
  575. static inline size_t       lcm(size_t       x, size_t       y)
  576. {
  577.   return x / gcd(x, y) * y;
  578. }
  579.  
  580.  
  581.  
  582. /* maintaining INUSE via size field */
  583.  
  584.  
  585. #define inuse(p)       ((p)->size & INUSE)
  586. #define set_inuse(p)   ((p)->size |= INUSE)
  587. #define clear_inuse(b) ((p)->size &= ~INUSE)
  588.  
  589.   
  590. /* operations on  malloc_chunk addresses */
  591.  
  592.  
  593. /* return ptr to next physical malloc_chunk */
  594.  
  595. #define next_chunk(p) ((mchunkptr)((char*)(p) + (p)->size))
  596.  
  597. /* return ptr to previous physical malloc_chunk */
  598.  
  599. #define prev_chunk(p) ((mchunkptr)((char*)(p)-((((int*)(p))[-1]) & ~(INUSE))))
  600.  
  601. /* place size at front and back of chunk */
  602.  
  603.  
  604. static inline void set_size(mchunkptr p, size_t       sz)
  605. {
  606.   p->size = *((int*)((char*)(p) + sz - SIZE_SZ)) = sz;
  607. }
  608.  
  609.  
  610.  
  611.  
  612. /* conversion from malloc headers to user pointers, and back */
  613.  
  614. static inline void* chunk2mem(mchunkptr p) 
  615.   set_inuse(p);
  616.   return (void*)((char*)(p) + SIZE_SZ); 
  617. }
  618.  
  619. static inline mchunkptr mem2chunk(void* mem) 
  620.   mchunkptr p = (mchunkptr)((char*)(mem) - SIZE_SZ); 
  621.  
  622.   /* a quick sanity check */
  623.   size_t       sz = p->size & ~(INUSE);
  624.   if (p->size == sz || sz != *((int*)((char*)(p) + sz - SIZE_SZ)))
  625.     malloc_user_error();
  626.  
  627.   p->size = sz;   /* clears INUSE */
  628.   return p;
  629. }
  630.  
  631.  
  632.  
  633. /* maintaining bins & pointers */
  634.  
  635.  
  636. /* maximum bin actually used */
  637.  
  638. static mbinptr malloc_maxbin = FIRSTBIN;
  639.  
  640.  
  641. /* operations on lists inside bins */
  642.  
  643.  
  644. /* take a chunk off a list */
  645.  
  646. static inline void unlink(mchunkptr p)
  647. {
  648.   mchunkptr b = p->bk;
  649.   mchunkptr f = p->fd;
  650.  
  651.   f->bk = b;  b->fd = f;
  652.  
  653.   UPDATE_STATS (--n_avail);
  654. }
  655.  
  656.  
  657.  
  658. /* split a chunk and place on the back of a list */
  659.  
  660. static inline void split(mchunkptr p, size_t       offset)
  661. {
  662.   size_t       room = p->size - offset;
  663.   if (room >= MINSIZE)
  664.   {
  665.     mbinptr   bn = size2bin(room);                  /* new bin */
  666.     mchunkptr h  = &(bn->hd);                       /* its head */
  667.     mchunkptr b  = h->bk;                           /* old back element */
  668.     mchunkptr t = (mchunkptr)((char*)(p) + offset); /* remaindered chunk */
  669.     
  670.     /* set size */
  671.     t->size = *((int*)((char*)(t) + room    - SIZE_SZ)) = room;
  672.  
  673.     /* link up */
  674.     t->bk = b;  t->fd = h;  h->bk = b->fd = t;
  675.     
  676.     /* adjust maxbin (h == b means was empty) */
  677.     if (h == b && bn > malloc_maxbin) malloc_maxbin = bn; 
  678.  
  679.     /* adjust size of chunk to be returned */
  680.     p->size = *((int*)((char*)(p) + offset  - SIZE_SZ)) = offset;
  681.  
  682.     UPDATE_STATS ((++n_split, ++n_avail));
  683.   }
  684. }
  685.  
  686.  
  687.  
  688. /* place a consolidated chunk on the back of a list */
  689. /* like above, except no split */
  690.  
  691. static inline void consollink(mchunkptr p)
  692. {
  693.   mbinptr   bn = size2bin(p->size);
  694.   mchunkptr h  = &(bn->hd);
  695.   mchunkptr b  = h->bk;
  696.  
  697.   p->bk = b;  p->fd = h;  h->bk = b->fd = p;
  698.  
  699.   if (h == b && bn > malloc_maxbin) malloc_maxbin = bn; 
  700.  
  701.   UPDATE_STATS(++n_avail);
  702. }
  703.  
  704.  
  705. /* place a freed chunk on the front of a list */
  706.  
  707. static inline void frontlink(mchunkptr p)
  708. {
  709.   mbinptr   bn = size2bin(p->size);
  710.   mchunkptr h  = &(bn->hd);
  711.   mchunkptr f  = h->fd;
  712.  
  713.   p->bk = h;  p->fd = f;  f->bk = h->fd = p;
  714.  
  715.   if (h == f && bn > malloc_maxbin) malloc_maxbin = bn;  
  716.  
  717.   bn->dirty = 1;
  718.  
  719.   UPDATE_STATS(++n_avail);
  720. }
  721.  
  722.  
  723.  
  724. /* Dealing with sbrk */
  725.  
  726.  
  727. /* To link consecutive sbrk regions when possible */
  728.  
  729. static int* last_sbrk_end;
  730.  
  731.  
  732. static mchunkptr malloc_from_sys(unsigned nb)
  733. {
  734.   mchunkptr p;
  735.   size_t       sbrk_size;
  736.   int* ip;
  737.   
  738.   /* Minimally, we need to pad with enough space */
  739.   /* to place dummy size/use fields to ends if needed */
  740.  
  741.   sbrk_size = ((nb + SBRK_UNIT - 1 + SIZE_SZ + SIZE_SZ) 
  742.                / SBRK_UNIT) * SBRK_UNIT;
  743.  
  744.   ip = (int*)(sbrk(sbrk_size));
  745.   if ((char*)ip == (char*)(-1)) /* sbrk returns -1 on failure */
  746.   {
  747.     return 0;
  748.   }
  749.  
  750.   UPDATE_STATS ((++n_sbrks, sbrked_mem += sbrk_size));
  751.  
  752.  
  753.   if (last_sbrk_end != &ip[-1]) 
  754.   {                             
  755.     /* It's either first time through or someone else called sbrk. */
  756.     /* Arrange end-markers at front & back */
  757.  
  758.     /* Shouldn't be necessary, but better to be safe */
  759.     while (!aligned_OK(ip)) { ++ip; sbrk_size -= SIZE_SZ; }
  760.  
  761.  
  762.     /* Mark the front as in use to prevent merging. */
  763.     /* Note we can get away with only 1 word, not MINSIZE overhead here */
  764.  
  765.     *ip++ = SIZE_SZ | INUSE;
  766.     
  767.     p = (mchunkptr)ip;
  768.     set_size(p,sbrk_size - (SIZE_SZ + SIZE_SZ)); 
  769.     
  770.   }
  771.   else 
  772.   {
  773.     mchunkptr l;  
  774.  
  775.     /* We can safely make the header start at end of prev sbrked chunk. */
  776.     /* We will still have space left at the end from a previous call */
  777.     /* to place the end marker, below */
  778.  
  779.     p = (mchunkptr)(last_sbrk_end);
  780.     set_size(p, sbrk_size);
  781.  
  782.  
  783.     /* Even better, maybe we can merge with last fragment: */
  784.  
  785.     l = prev_chunk(p);
  786.     if (!inuse(l))  
  787.     {
  788.       unlink(l);
  789.       set_size(l, p->size + l->size);
  790.       p = l;
  791.     }
  792.  
  793.   }
  794.  
  795.   /* mark the end of sbrked space as in use to prevent merging */
  796.  
  797.   last_sbrk_end = (int*)((char*)p + p->size);
  798.   *last_sbrk_end = SIZE_SZ | INUSE;
  799.  
  800.   UPDATE_STATS((++n_avail, ++n_malloc_chunks));
  801.  
  802.   /* make it safe to unlink in malloc */
  803.   UPDATE_STATS(++n_avail);
  804.   p->fd = p->bk = p;
  805.  
  806.   return p;
  807. }
  808.  
  809.  
  810.  
  811. /* Consolidate dirty bins. */
  812. /* Stop if found a chunk big enough to satisfy current malloc request */
  813.  
  814. /* (It requires much less bookkeeping to consolidate entire bins */
  815. /* at once than to keep records of which chunks might be */
  816. /* consolidatable. So long as the lists are short, which we */
  817. /* try to ensure via small bin ranges, there is little wasted effort.) */
  818.  
  819. static mchunkptr malloc_find_space(size_t       nb)
  820. {
  821.   mbinptr b;
  822.  
  823.   /* first, re-adjust max used bin */
  824.  
  825.   while (malloc_maxbin >= FIRSTBIN && 
  826.          malloc_maxbin->hd.bk == &(malloc_maxbin->hd))
  827.   {
  828.     malloc_maxbin->dirty = 0;
  829.     --malloc_maxbin;
  830.   }
  831.  
  832.   for (b = malloc_maxbin; b >= FIRSTBIN; --b)
  833.   {
  834.     UPDATE_STATS(++n_malloc_bins);
  835.  
  836.     if (b->dirty)
  837.     {
  838.       mchunkptr h = &(b->hd);         /* head of list */
  839.       mchunkptr p = h->fd;            /* chunk traverser */
  840.  
  841.       while (p != h)
  842.       {
  843.         mchunkptr nextp = p->fd;       /* save, in case of relinks */
  844.         int consolidated = 0;          /* only unlink/relink if consolidated */
  845.  
  846.         mchunkptr t;
  847.  
  848.         UPDATE_STATS(++n_malloc_chunks);
  849.  
  850.         while (!inuse(t = prev_chunk(p))) /* consolidate backward */
  851.         {
  852.           if (!consolidated) { consolidated = 1; unlink(p); }
  853.           if (t == nextp) nextp = t->fd;
  854.           unlink(t);
  855.           set_size(t, t->size + p->size);
  856.           p = t;
  857.           UPDATE_STATS (++n_consol);
  858.         }
  859.         
  860.         while (!inuse(t = next_chunk(p))) /* consolidate forward */
  861.         {
  862.           if (!consolidated) { consolidated = 1; unlink(p); }
  863.           if (t == nextp) nextp = t->fd;
  864.           unlink(t);
  865.           set_size(p, p->size + t->size);
  866.           UPDATE_STATS (++n_consol);
  867.         }
  868.  
  869.        if (consolidated)
  870.        {
  871.           if (p->size >= nb)
  872.           {
  873.             /* make it safe to unlink in malloc */
  874.             UPDATE_STATS(++n_avail);
  875.             p->fd = p->bk = p;
  876.             return p;
  877.           }
  878.           else
  879.             consollink(p);
  880.         }
  881.  
  882.         p = nextp;
  883.  
  884.       }
  885.  
  886.       b->dirty = 0;
  887.  
  888.     }
  889.   }
  890.  
  891.   /* nothing available - sbrk some more */
  892.  
  893.   return malloc_from_sys(nb);
  894. }
  895.  
  896.  
  897.  
  898. /*   Finally, the user-level functions  */
  899.  
  900. void* malloc(size_t       bytes)
  901. {
  902.   size_t       nb  = request2size(bytes);  /* padded request size */
  903.   mbinptr      b   = size2bin(nb);         /* corresponding bin */
  904.   mchunkptr    hd  = &(b->hd);             /* head of its list */
  905.   mchunkptr    p   = hd->fd;               /* chunk traverser */
  906.  
  907.   UPDATE_STATS((requested_mem+=bytes, ++n_malloc_bins));
  908.  
  909.   /* Try a (near) exact match in own bin */
  910.   /* clean out unusable but consolidatable chunks in bin while traversing */
  911.  
  912.   while (p != hd)
  913.   {
  914.     UPDATE_STATS(++n_malloc_chunks);
  915.     if (p->size >= nb)
  916.       goto found;
  917.     else    /* try to consolidate; same code as malloc_find_space */
  918.     {
  919.       mchunkptr nextp = p->fd;       /* save, in case of relinks */
  920.       int consolidated = 0;          /* only unlink/relink if consolidated */
  921.       
  922.       mchunkptr t;
  923.  
  924.       while (!inuse(t = prev_chunk(p))) /* consolidate backward */
  925.       {
  926.         if (!consolidated) { consolidated = 1; unlink(p); }
  927.         if (t == nextp) nextp = t->fd;
  928.         unlink(t);
  929.         set_size(t, t->size + p->size);
  930.         p = t;
  931.         UPDATE_STATS (++n_consol);
  932.       }
  933.       
  934.       while (!inuse(t = next_chunk(p))) /* consolidate forward */
  935.       {
  936.         if (!consolidated) { consolidated = 1; unlink(p); }
  937.         if (t == nextp) nextp = t->fd;
  938.         unlink(t);
  939.         set_size(p, p->size + t->size);
  940.         UPDATE_STATS (++n_consol);
  941.       }
  942.       
  943.       if (consolidated)
  944.       {
  945.         if (p->size >= nb)
  946.         {
  947.           /* make it safe to unlink again below */
  948.           UPDATE_STATS(++n_avail);
  949.           p->fd = p->bk = p;
  950.           goto found;
  951.         }
  952.         else
  953.           consollink(p);
  954.       }
  955.  
  956.       p = nextp;
  957.  
  958.     }
  959.   }
  960.  
  961.   b->dirty = 0; /* true if got here */
  962.  
  963.   /*  Scan bigger bins for a victim */
  964.  
  965.   while (++b <= malloc_maxbin)
  966.   {
  967.     UPDATE_STATS(++n_malloc_bins);
  968.     if ((p = b->hd.bk) != &(b->hd))    /* no need to check size */
  969.       goto found;
  970.   }
  971.  
  972.   /* Consolidate or sbrk */
  973.  
  974.   p = malloc_find_space(nb);
  975.  
  976.   if (p == 0) return 0; /* allocation failure */
  977.  
  978.  found:   /* Use what we found */
  979.  
  980.   unlink(p);
  981.   split(p, nb); 
  982.   UPDATE_STATS(do_malloc_stats(p));
  983.   return chunk2mem(p);
  984. }
  985.  
  986.  
  987.  
  988.  
  989. void free(void* mem)
  990. {
  991.   if (mem != 0)
  992.   {
  993.     mchunkptr p = mem2chunk(mem);
  994.     UPDATE_STATS(do_free_stats(p));
  995.     frontlink(p);
  996.   }
  997. }
  998.  
  999.  
  1000. void* calloc(size_t       n, size_t       elem_size)
  1001. {
  1002.   size_t       sz = n * elem_size;
  1003.   void* p = malloc(sz);
  1004.   bzero(p, sz);
  1005.   return p;
  1006. };
  1007.  
  1008. /* This is here for compatibility with older systems */
  1009. void cfree(void *mem)
  1010. {
  1011.   free(mem);
  1012. }
  1013.  
  1014.  
  1015. size_t       malloc_usable_size(void* mem)
  1016. {
  1017.   if (mem == 0)
  1018.     return 0;
  1019.   else
  1020.   {
  1021.     mchunkptr p = (mchunkptr)((char*)(mem) - SIZE_SZ); 
  1022.     size_t       sz = p->size & ~(INUSE);
  1023.     if (p->size == sz || sz != *((int*)((char*)(p) + sz - SIZE_SZ)))
  1024.       return 0;
  1025.     else
  1026.       return sz - MALLOC_MIN_OVERHEAD;
  1027.   }
  1028. }
  1029.  
  1030.  
  1031.  
  1032. void* realloc(void* mem, size_t       bytes)
  1033. {
  1034.   if (mem == 0) 
  1035.     return malloc(bytes);
  1036.   else
  1037.   {
  1038.     size_t       nb      = request2size(bytes);
  1039.     mchunkptr    p       = mem2chunk(mem);
  1040.     size_t       oldsize = p->size;
  1041.     int          room;
  1042.     mchunkptr    nxt;
  1043.  
  1044.     UPDATE_STATS((++n_reallocs, requested_mem += bytes-oldsize));
  1045.     
  1046.     /* try to expand (even if already big enough), to clean up chunk */
  1047.  
  1048.     while (!inuse(nxt = next_chunk(p)))
  1049.     {
  1050.       UPDATE_STATS ((malloced_mem += nxt->size, ++n_consol));
  1051.       unlink(nxt);
  1052.       set_size(p, p->size + nxt->size);
  1053.     }
  1054.  
  1055.     room = p->size - nb;
  1056.     if (room >= 0)
  1057.     {
  1058.       split(p, nb);
  1059.       UPDATE_STATS(malloced_mem -= room);
  1060.       return chunk2mem(p);
  1061.     }
  1062.     else /* do the obvious */
  1063.     {
  1064.       void* newmem;
  1065.       set_inuse(p);    /* don't let malloc consolidate us yet! */
  1066.       newmem = malloc(nb);
  1067.       bcopy(mem, newmem, oldsize - SIZE_SZ);
  1068.       free(mem);
  1069.       UPDATE_STATS(++n_reallocs_with_copy);
  1070.       return newmem;
  1071.     }
  1072.   }
  1073. }
  1074.  
  1075.  
  1076.  
  1077. /* return a pointer to space with at least the alignment requested */
  1078.  
  1079. void* memalign(size_t       alignment, size_t       bytes)
  1080. {
  1081.   mchunkptr p;
  1082.   size_t       nb = request2size(bytes);
  1083.  
  1084.   /* find an alignment that both we and the user can live with: */
  1085.   /* least common multiple guarantees mutual happiness */
  1086.   size_t       align = lcm(alignment, MALLOC_MIN_OVERHEAD);
  1087.   size_t       mask = align - 1;
  1088.  
  1089.   /* call malloc with worst case padding to hit alignment; */
  1090.   /* we will give back extra */
  1091.  
  1092.   size_t       req = nb + align + MINSIZE;
  1093.   void* m = malloc(req);
  1094.  
  1095.   if (m == 0) return m;
  1096.  
  1097.   p = mem2chunk(m);
  1098.  
  1099.   /* keep statistics on track */
  1100.  
  1101.   UPDATE_STATS(--n_mallocs);
  1102.   UPDATE_STATS(malloced_mem -= p->size);
  1103.   UPDATE_STATS(requested_mem -= req);
  1104.   UPDATE_STATS(requested_mem += bytes);
  1105.  
  1106.   if (((int)(m) & (mask)) != 0) /* misaligned */
  1107.   {
  1108.  
  1109.     /* find an aligned spot inside chunk */
  1110.  
  1111.     mchunkptr ap = (mchunkptr)(( ((int)(m) + mask) & -align) - SIZE_SZ);
  1112.  
  1113.     size_t       gap = (size_t      )(ap) - (size_t      )(p);
  1114.     size_t       room;
  1115.  
  1116.     /* we need to give back leading space in a chunk of at least MINSIZE */
  1117.  
  1118.     if (gap < MINSIZE)
  1119.     {
  1120.       /* This works since align >= MINSIZE */
  1121.       /* and we've malloc'd enough total room */
  1122.  
  1123.       ap = (mchunkptr)( (int)(ap) + align );
  1124.       gap += align;    
  1125.     }
  1126.  
  1127.     if (gap + nb > p->size) /* can't happen unless chunk sizes corrupted */
  1128.       malloc_user_error();
  1129.  
  1130.     room = p->size - gap;
  1131.  
  1132.     /* give back leader */
  1133.     set_size(p, gap);
  1134.     consollink(p);
  1135.  
  1136.     /* use the rest */
  1137.     p = ap;
  1138.     set_size(p, room);
  1139.   }
  1140.  
  1141.   /* also give back spare room at the end */
  1142.  
  1143.   split(p, nb); 
  1144.   UPDATE_STATS(do_malloc_stats(p));
  1145.   return chunk2mem(p);
  1146.  
  1147. }
  1148.  
  1149. #ifndef sun
  1150. #include <getpagesize.h>
  1151. #endif
  1152.  
  1153. static size_t       malloc_pagesize = 0;
  1154.  
  1155. void* valloc(size_t       bytes)
  1156. {
  1157.   if (malloc_pagesize == 0) malloc_pagesize = getpagesize();
  1158.   return memalign (malloc_pagesize, bytes);
  1159. }
  1160.     
  1161.  
  1162. void malloc_stats()
  1163. {
  1164. #ifndef MALLOC_STATS
  1165. }
  1166. #else
  1167.   int i;
  1168.   mchunkptr p;
  1169.   double nm = (double)(n_mallocs + n_reallocs);
  1170.  
  1171.   fprintf(stderr, "\nmalloc statistics\n\n");
  1172.  
  1173.   if (n_mallocs != 0)
  1174.   fprintf(stderr, "requests  = %10u total size = %10u\tave = %10u\n",
  1175.           n_mallocs, requested_mem, requested_mem/n_mallocs);
  1176.  
  1177.   if (n_mallocs != 0)
  1178.   fprintf(stderr, "mallocs   = %10u total size = %10u\tave = %10u\n",
  1179.           n_mallocs, malloced_mem, malloced_mem/n_mallocs);
  1180.   
  1181.   if (n_frees != 0)
  1182.   fprintf(stderr, "frees     = %10u total size = %10u\tave = %10u\n",
  1183.           n_frees, freed_mem, freed_mem/n_frees);
  1184.   
  1185.   if (n_mallocs-n_frees != 0)
  1186.   fprintf(stderr, "in use    = %10u total size = %10u\tave = %10u\n",
  1187.           n_mallocs-n_frees, malloced_mem-freed_mem, 
  1188.           (malloced_mem-freed_mem) / (n_mallocs-n_frees));
  1189.  
  1190.   if (max_inuse != 0)
  1191.   fprintf(stderr, "max in use= %10u total size = %10u\tave = %10u\n",
  1192.           max_inuse, max_used_mem, max_used_mem / max_inuse);
  1193.   
  1194.   if (n_avail != 0)
  1195.   fprintf(stderr, "available = %10u total size = %10u\tave = %10u\n",
  1196.           n_avail, sbrked_mem - (malloced_mem-freed_mem), 
  1197.           (sbrked_mem - (malloced_mem-freed_mem)) / n_avail);
  1198.  
  1199.   if (n_sbrks != 0)
  1200.   fprintf(stderr, "sbrks     = %10u total size = %10u\tave = %10u\n\n",
  1201.           n_sbrks, sbrked_mem, sbrked_mem/ n_sbrks);
  1202.  
  1203.   if (n_reallocs != 0)
  1204.   fprintf(stderr, "reallocs  = %10u with copy  = %10u\n\n",
  1205.           n_reallocs, n_reallocs_with_copy);
  1206.  
  1207.  
  1208.   if (nm != 0)
  1209.   {
  1210.     fprintf(stderr, "chunks scanned per malloc = %6.3f\n", 
  1211.             n_malloc_chunks / nm);
  1212.     fprintf(stderr, "bins scanned per malloc   = %6.3f\n", 
  1213.             n_malloc_bins / nm);
  1214.     fprintf(stderr, "splits per malloc         = %6.3f\n", 
  1215.             n_split / nm);
  1216.     fprintf(stderr, "consolidations per malloc = %6.3f\n", 
  1217.             n_consol / nm);
  1218.   }
  1219.  
  1220.   fprintf(stderr, "\nfree chunks:\n");
  1221.   for (i = 0; i < MAXBIN; ++i)
  1222.   {
  1223.     p = av[i].hd.fd;
  1224.     if (p != &(av[i].hd))
  1225.     {
  1226.       size_t       count = 1;
  1227.       size_t       sz = p->size;
  1228.       for (p = p->fd; p != &(av[i].hd); p = p->fd)
  1229.       {
  1230.         if (p->size == sz)
  1231.           ++count;
  1232.         else
  1233.         {
  1234.           fprintf(stderr, "\tsize = %10u count = %5u\n", sz, count);
  1235.           count = 1;
  1236.           sz = p->size;
  1237.         }
  1238.       }
  1239.  
  1240.       fprintf(stderr, "\tsize = %10u count = %5u\n", sz, count);
  1241.  
  1242.     }
  1243.   }
  1244. }
  1245. #endif /* MALLOC_STATS */
  1246.  
  1247. #endif /* NO_LIBGXX_MALLOC */
  1248.