home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / gmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-10  |  39.3 KB  |  1,413 lines

  1. /* DO NOT EDIT THIS FILE -- it is automagically generated.  -*- C -*- */
  2.  
  3. /* Get the configuration files if we're being compiled for Emacs.  */
  4. #ifdef emacs
  5. #include "config.h"
  6. #endif
  7.  
  8. #define SKELETON
  9.  
  10. #if    defined(__GNU_LIBRARY__) || __STDC__ || defined(USG)
  11. #include <string.h>
  12. #define memmove            memcpy
  13. #else
  14. #define    memset(s, zero, n)    bzero ((s), (n))
  15. #define    memcpy(d, s, n)        bcopy ((s), (d), (n))
  16. #define memmove(d, s, n)    bcopy ((s), (d), (n))
  17. #endif
  18.  
  19. #define _MALLOC_INTERNAL
  20.  
  21. /* The malloc headers and source files from the C library follow here.  */
  22. /* Declarations for `malloc' and friends.
  23.    Copyright 1990, 1991 Free Software Foundation
  24.           Written May 1989 by Mike Haertel.
  25.  
  26. The GNU C Library is free software; you can redistribute it and/or
  27. modify it under the terms of the GNU Library General Public License as
  28. published by the Free Software Foundation; either version 2 of the
  29. License, or (at your option) any later version.
  30.  
  31. The GNU C Library is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  34. Library General Public License for more details.
  35.  
  36. You should have received a copy of the GNU Library General Public
  37. License along with the GNU C Library; see the file COPYING.LIB.  If
  38. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  39. Cambridge, MA 02139, USA.
  40.  
  41.    The author may be reached (Email) at the address mike@ai.mit.edu,
  42.    or (US mail) as Mike Haertel c/o Free Software Foundation. */
  43.  
  44. #ifndef _MALLOC_H
  45.  
  46. #define _MALLOC_H    1
  47.  
  48. /*  */
  49.  
  50. #ifndef    NULL
  51. #define    NULL    0
  52. #endif
  53.  
  54. #ifdef    __STDC__
  55. #include <stddef.h>
  56. #else
  57. #undef    size_t
  58. #define    size_t    unsigned int
  59. #endif
  60.  
  61.  
  62. /* Allocate SIZE bytes of memory.  */
  63. extern void *malloc ();
  64. /* Re-allocate the previously allocated block
  65.    in char *, making the new block SIZE bytes long.  */
  66. extern void *realloc ();
  67. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
  68. extern void *calloc ();
  69. /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
  70. extern void free ();
  71.  
  72. /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
  73. extern void *memalign ();
  74.  
  75. /* Allocate SIZE bytes on a page boundary.  */
  76. extern void *valloc ();
  77.  
  78.  
  79. #ifdef _MALLOC_INTERNAL
  80.  
  81. #if    defined(__GNU_LIBRARY__) || defined(__STDC__)
  82. #include <limits.h>
  83. #else
  84. #define    CHAR_BIT    8
  85. #endif
  86.  
  87. /* The allocator divides the heap into blocks of fixed size; large
  88.    requests receive one or more whole blocks, and small requests
  89.    receive a fragment of a block.  Fragment sizes are powers of two,
  90.    and all fragments of a block are the same size.  When all the
  91.    fragments in a block have been freed, the block itself is freed.  */
  92. #define INT_BIT        (CHAR_BIT * sizeof(int))
  93. #define BLOCKLOG    (INT_BIT > 16 ? 12 : 9)
  94. #define BLOCKSIZE    (1 << BLOCKLOG)
  95. #define BLOCKIFY(SIZE)    (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  96.  
  97. /* Determine the amount of memory spanned by the initial heap table
  98.    (not an absolute limit).  */
  99. #define HEAP        (INT_BIT > 16 ? 4194304 : 65536)
  100.  
  101. /* Number of contiguous free blocks allowed to build up at the end of
  102.    memory before they will be returned to the system.  */
  103. #define FINAL_FREE_BLOCKS    8
  104.  
  105. /* Where to start searching the free list when looking for new memory.
  106.    The two possible values are 0 and _heapindex.  Starting at 0 seems
  107.    to reduce total memory usage, while starting at _heapindex seems to
  108.    run faster.  */
  109. #define MALLOC_SEARCH_START    _heapindex
  110.  
  111. /* Data structure giving per-block information.  */
  112. typedef union
  113. {
  114.   /* Heap information for a busy block.  */
  115.   struct
  116.   {
  117.     /* Zero for a large block, or positive giving the
  118.        logarithm to the base two of the fragment size.  */
  119.     int type;
  120.     union
  121.     {
  122.       struct
  123.       {
  124.     size_t nfree;        /* Free fragments in a fragmented block.  */
  125.     size_t first;        /* First free fragment of the block.  */
  126.       } frag;
  127.       /* Size (in blocks) of a large cluster.  */
  128.       size_t size;
  129.     } info;
  130.   } busy;
  131.   /* Heap information for a free block (that may be the first of
  132.      a free cluster).  */
  133.   struct
  134.   {
  135.     size_t size;        /* Size (in blocks) of a free cluster.  */
  136.     size_t next;        /* Index of next free cluster.  */
  137.     size_t prev;        /* Index of previous free cluster.  */
  138.   } free;
  139. } malloc_info;
  140.  
  141. /* Pointer to first block of the heap.  */
  142. extern char *_heapbase;
  143.  
  144. /* Table indexed by block number giving per-block information.  */
  145. extern malloc_info *_heapinfo;
  146.  
  147. /* Address to block number and vice versa.  */
  148. #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  149. /* The following line MUST be split!  m4 will not process it otherwise.  */
  150. #define ADDRESS(B) \
  151.   ((char *) (((B) - 1) * BLOCKSIZE + _heapbase))
  152.  
  153. /* Current search index for the heap table.  */
  154. extern size_t _heapindex;
  155.  
  156. /* Limit of valid info table indices.  */
  157. extern size_t _heaplimit;
  158.  
  159. /* Doubly linked lists of free fragments.  */
  160. struct list
  161. {
  162.   struct list *next;
  163.   struct list *prev;
  164. };
  165.  
  166. /* Free list headers for each fragment size.  */
  167. extern struct list _fraghead[];
  168.  
  169. /* List of blocks allocated with `memalign' (or `valloc').  */
  170. struct alignlist
  171. {
  172.   struct alignlist *next;
  173.   char *aligned;        /* The address that memaligned returned.  */
  174.   char *exact;            /* The address that malloc returned.  */
  175. };
  176. extern struct alignlist *_aligned_blocks;
  177.  
  178. /* Instrumentation.  */
  179. extern size_t _chunks_used;
  180. extern size_t _bytes_used;
  181. extern size_t _chunks_free;
  182. extern size_t _bytes_free;
  183.  
  184. /* Internal version of `free' used in `morecore'. */
  185. extern void __free ();
  186.  
  187. #endif /* _MALLOC_INTERNAL.  */
  188.  
  189. /* Underlying allocation function; successive calls should
  190.    return contiguous pieces of memory.  */
  191. extern char *(*__morecore) ();
  192.  
  193. /* Default value of `__morecore'.  */
  194. extern char *__default_morecore ();
  195.  
  196. /* Nonzero if `malloc' has been called and done its initialization.  */
  197. extern int __malloc_initialized;
  198.  
  199. /* Hooks for debugging versions.  */
  200. extern void (*__free_hook) ();
  201. extern char *(*__malloc_hook) ();
  202. extern char *(*__realloc_hook) ();
  203.  
  204. /* Activate a standard collection of debugging hooks.  */
  205. extern void mcheck ();
  206.  
  207. /* Statistics available to the user.  */
  208. struct mstats
  209. {
  210.   size_t bytes_total;        /* Total size of the heap. */
  211.   size_t chunks_used;        /* Chunks allocated by the user. */
  212.   size_t bytes_used;        /* Byte total of user-allocated chunks. */
  213.   size_t chunks_free;        /* Chunks in the free list. */
  214.   size_t bytes_free;        /* Byte total of chunks in the free list. */
  215. };
  216.  
  217. /* Pick up the current statistics. */
  218. extern struct mstats mstats ();
  219.  
  220. #endif /* malloc.h  */
  221. /* Memory allocator `malloc'.
  222.    Copyright 1990, 1991 Free Software Foundation
  223.           Written May 1989 by Mike Haertel.
  224.  
  225. The GNU C Library is free software; you can redistribute it and/or
  226. modify it under the terms of the GNU Library General Public License as
  227. published by the Free Software Foundation; either version 2 of the
  228. License, or (at your option) any later version.
  229.  
  230. The GNU C Library is distributed in the hope that it will be useful,
  231. but WITHOUT ANY WARRANTY; without even the implied warranty of
  232. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  233. Library General Public License for more details.
  234.  
  235. You should have received a copy of the GNU Library General Public
  236. License along with the GNU C Library; see the file COPYING.LIB.  If
  237. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  238. Cambridge, MA 02139, USA.
  239.  
  240.    The author may be reached (Email) at the address mike@ai.mit.edu,
  241.    or (US mail) as Mike Haertel c/o Free Software Foundation. */
  242.  
  243. /*  */
  244.  
  245. #define _MALLOC_INTERNAL
  246.  
  247. /*
  248. #if    defined(__GNU_LIBRARY__) || __STDC__ || defined(USG)
  249. #include <string.h>
  250. #else
  251. #define    memset(s, zero, n)    bzero ((s), (n))
  252. #define    memcpy(d, s, n)        bcopy ((s), (d), (n))
  253. #endif
  254. */
  255.  
  256. /* How to really get more memory.  */
  257. char *(*__morecore) ()= __default_morecore;
  258.  
  259. /* Debugging hook for `malloc'.  */
  260. char *(*__malloc_hook) ();
  261.  
  262. /* Pointer to the base of the first block.  */
  263. char *_heapbase;
  264.  
  265. /* Block information table.  Allocated with align/__free (not malloc/free).  */
  266. malloc_info *_heapinfo;
  267.  
  268. /* Number of info entries.  */
  269. static size_t heapsize;
  270.  
  271. /* Search index in the info table.  */
  272. size_t _heapindex;
  273.  
  274. /* Limit of valid info table indices.  */
  275. size_t _heaplimit;
  276.  
  277. /* Free lists for each fragment size.  */
  278. struct list _fraghead[BLOCKLOG];
  279.  
  280. /* Instrumentation.  */
  281. size_t _chunks_used;
  282. size_t _bytes_used;
  283. size_t _chunks_free;
  284. size_t _bytes_free;
  285.  
  286. /* Are you experienced?  */
  287. int __malloc_initialized;
  288.  
  289. /* Aligned allocation.  */
  290. static char *
  291. align (size)
  292.      size_t size;
  293. {
  294.   char *result;
  295.   unsigned long int adj;
  296.  
  297.   result = (*__morecore) (size);
  298.   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  299.                         (char *) NULL)) % BLOCKSIZE;
  300.   if (adj != 0)
  301.     {
  302.       adj = BLOCKSIZE - adj;
  303.       (void) (*__morecore) (adj);
  304.       result = (char *) result + adj;
  305.     }
  306.   return result;
  307. }
  308.  
  309. /* Set everything up and remember that we have.  */
  310. static int
  311. initialize ()
  312. {
  313.   heapsize = HEAP / BLOCKSIZE;
  314.   _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
  315.   if (_heapinfo == NULL)
  316.     return 0;
  317.   memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
  318.   _heapinfo[0].free.size = 0;
  319.   _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
  320.   _heapindex = 0;
  321.   _heapbase = (char *) _heapinfo;
  322.   __malloc_initialized = 1;
  323.   return 1;
  324. }
  325.  
  326. /* Get neatly aligned memory, initializing or
  327.    growing the heap info table as necessary. */
  328. static char *
  329. morecore (size)
  330.      size_t size;
  331. {
  332.   char *result;
  333.   malloc_info *newinfo, *oldinfo;
  334.   size_t newsize;
  335.  
  336.   result = align (size);
  337.   if (result == NULL)
  338.     return NULL;
  339.  
  340.   /* Check if we need to grow the info table.  */
  341.   if ((size_t) BLOCK ((char *) result + size) > heapsize)
  342.     {
  343.       newsize = heapsize;
  344.       while ((size_t) BLOCK ((char *) result + size) > newsize)
  345.     newsize *= 2;
  346.       newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
  347.       if (newinfo == NULL)
  348.     {
  349.       (*__morecore) (-size);
  350.       return NULL;
  351.     }
  352.       memset (newinfo, 0, newsize * sizeof (malloc_info));
  353.       memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
  354.       oldinfo = _heapinfo;
  355.       newinfo[BLOCK (oldinfo)].busy.type = 0;
  356.       newinfo[BLOCK (oldinfo)].busy.info.size
  357.     = BLOCKIFY (heapsize * sizeof (malloc_info));
  358.       _heapinfo = newinfo;
  359.       __free (oldinfo);
  360.       heapsize = newsize;
  361.     }
  362.  
  363.   _heaplimit = BLOCK ((char *) result + size);
  364.   return result;
  365. }
  366.  
  367. /* Allocate memory from the heap.  */
  368. void *
  369. malloc (size)
  370.      size_t size;
  371. {
  372.   char *result;
  373.   size_t block, blocks, lastblocks, start;
  374.   register size_t i;
  375.   struct list *next;
  376.  
  377. #ifdef HAVE_X_WINDOWS
  378.   /* there is at least one Xt bug where calloc(n,x) is blindly called
  379.      where n can be 0, and yet if 0 is returned, Xt barfs */
  380.   if (size == 0)
  381.     size = sizeof (struct list);
  382. #else
  383.   if (size == 0)
  384.     return NULL;
  385. #endif
  386.  
  387.   if (__malloc_hook != NULL)
  388.     return (*__malloc_hook) (size);
  389.  
  390.   if (!__malloc_initialized)
  391.     if (!initialize ())
  392.       return NULL;
  393.  
  394. #ifdef SUNOS_LOCALTIME_BUG
  395.   /* fix for localtime() writing 9 bytes problem */
  396.   if (size < 16)
  397.     size = 16;
  398. #endif
  399.  
  400.   if (size < sizeof (struct list))
  401.     size = sizeof (struct list);
  402.  
  403.   /* Determine the allocation policy based on the request size.  */
  404.   if (size <= BLOCKSIZE / 2)
  405.     {
  406.       /* Small allocation to receive a fragment of a block.
  407.          Determine the logarithm to base two of the fragment size. */
  408.       register size_t log = 1;
  409.       --size;
  410.       while ((size /= 2) != 0)
  411.     ++log;
  412.  
  413.       /* Look in the fragment lists for a
  414.          free fragment of the desired size. */
  415.       next = _fraghead[log].next;
  416.       if (next != NULL)
  417.     {
  418.       /* There are free fragments of this size.
  419.              Pop a fragment out of the fragment list and return it.
  420.              Update the block's nfree and first counters. */
  421.       result = (char *) next;
  422.       next->prev->next = next->next;
  423.       if (next->next != NULL)
  424.         next->next->prev = next->prev;
  425.       block = BLOCK (result);
  426.       if (--_heapinfo[block].busy.info.frag.nfree != 0)
  427.         _heapinfo[block].busy.info.frag.first = (unsigned long int)
  428.           ((unsigned long int) ((char *) next->next - (char *) NULL)
  429.            % BLOCKSIZE) >> log;
  430.  
  431.       /* Update the statistics.  */
  432.       ++_chunks_used;
  433.       _bytes_used += 1 << log;
  434.       --_chunks_free;
  435.       _bytes_free -= 1 << log;
  436.     }
  437.       else
  438.     {
  439.       /* No free fragments of the desired size, so get a new block
  440.              and break it into fragments, returning the first.  */
  441.       result = malloc (BLOCKSIZE);
  442.       if (result == NULL)
  443.         return NULL;
  444.  
  445.       /* Link all fragments but the first into the free list.  */
  446.       for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i)
  447.         {
  448.           next = (struct list *) ((char *) result + (i << log));
  449.           next->next = _fraghead[log].next;
  450.           next->prev = &_fraghead[log];
  451.           next->prev->next = next;
  452.           if (next->next != NULL)
  453.         next->next->prev = next;
  454.         }
  455.  
  456.       /* Initialize the nfree and first counters for this block.  */
  457.       block = BLOCK (result);
  458.       _heapinfo[block].busy.type = log;
  459.       _heapinfo[block].busy.info.frag.nfree = i - 1;
  460.       _heapinfo[block].busy.info.frag.first = i - 1;
  461.  
  462.       _chunks_free += (BLOCKSIZE >> log) - 1;
  463.       _bytes_free += BLOCKSIZE - (1 << log);
  464.       _bytes_used -= BLOCKSIZE - (1 << log);
  465.     }
  466.     }
  467.   else
  468.     {
  469.       /* Large allocation to receive one or more blocks.
  470.          Search the free list in a circle starting at the last place visited.
  471.          If we loop completely around without finding a large enough
  472.          space we will have to get more memory from the system.  */
  473.       blocks = BLOCKIFY (size);
  474.       start = block = MALLOC_SEARCH_START;
  475.       while (_heapinfo[block].free.size < blocks)
  476.     {
  477.       block = _heapinfo[block].free.next;
  478.       if (block == start)
  479.         {
  480.           /* Need to get more from the system.  Check to see if
  481.                  the new core will be contiguous with the final free
  482.                  block; if so we don't need to get as much.  */
  483.           block = _heapinfo[0].free.prev;
  484.           lastblocks = _heapinfo[block].free.size;
  485.           if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
  486.           (*__morecore) (0) == ADDRESS (block + lastblocks) &&
  487.           (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
  488.         {
  489.           _heapinfo[block].free.size = blocks;
  490.           _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
  491.           continue;
  492.         }
  493.           result = morecore (blocks * BLOCKSIZE);
  494.           if (result == NULL)
  495.         return NULL;
  496.           block = BLOCK (result);
  497.           _heapinfo[block].busy.type = 0;
  498.           _heapinfo[block].busy.info.size = blocks;
  499.           ++_chunks_used;
  500.           _bytes_used += blocks * BLOCKSIZE;
  501.           return result;
  502.         }
  503.     }
  504.  
  505.       /* At this point we have found a suitable free list entry.
  506.          Figure out how to remove what we need from the list. */
  507.       result = ADDRESS (block);
  508.       if (_heapinfo[block].free.size > blocks)
  509.     {
  510.       /* The block we found has a bit left over,
  511.              so relink the tail end back into the free list. */
  512.       _heapinfo[block + blocks].free.size
  513.         = _heapinfo[block].free.size - blocks;
  514.       _heapinfo[block + blocks].free.next
  515.         = _heapinfo[block].free.next;
  516.       _heapinfo[block + blocks].free.prev
  517.         = _heapinfo[block].free.prev;
  518.       _heapinfo[_heapinfo[block].free.prev].free.next
  519.         = _heapinfo[_heapinfo[block].free.next].free.prev
  520.               = _heapindex = block + blocks;
  521.     }
  522.       else
  523.     {
  524.       /* The block exactly matches our requirements,
  525.              so just remove it from the list. */
  526.       _heapinfo[_heapinfo[block].free.next].free.prev
  527.         = _heapinfo[block].free.prev;
  528.       _heapinfo[_heapinfo[block].free.prev].free.next
  529.         = _heapindex = _heapinfo[block].free.next;
  530.       --_chunks_free;
  531.     }
  532.  
  533.       _heapinfo[block].busy.type = 0;
  534.       _heapinfo[block].busy.info.size = blocks;
  535.       ++_chunks_used;
  536.       _bytes_used += blocks * BLOCKSIZE;
  537.       _bytes_free -= blocks * BLOCKSIZE;
  538.     }
  539.  
  540.   return result;
  541. }
  542. /* Free a block of memory allocated by `malloc'.
  543.    Copyright 1990, 1991 Free Software Foundation
  544.           Written May 1989 by Mike Haertel.
  545.  
  546. The GNU C Library is free software; you can redistribute it and/or
  547. modify it under the terms of the GNU Library General Public License as
  548. published by the Free Software Foundation; either version 2 of the
  549. License, or (at your option) any later version.
  550.  
  551. The GNU C Library is distributed in the hope that it will be useful,
  552. but WITHOUT ANY WARRANTY; without even the implied warranty of
  553. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  554. Library General Public License for more details.
  555.  
  556. You should have received a copy of the GNU Library General Public
  557. License along with the GNU C Library; see the file COPYING.LIB.  If
  558. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  559. Cambridge, MA 02139, USA.
  560.  
  561.    The author may be reached (Email) at the address mike@ai.mit.edu,
  562.    or (US mail) as Mike Haertel c/o Free Software Foundation. */
  563.  
  564. /*  */
  565.  
  566. #define _MALLOC_INTERNAL
  567.  
  568. /* Debugging hook for free.  */
  569. void (*__free_hook) ();
  570.  
  571. /* List of blocks allocated by memalign.  */
  572. struct alignlist *_aligned_blocks = NULL;
  573.  
  574. /* Return memory to the heap.
  575.    Like `free' but don't call a __free_hook if there is one.  */
  576. void
  577. __free (ptr)
  578.      char *ptr;
  579. {
  580.   int type;
  581.   size_t block, blocks;
  582.   register size_t i;
  583.   struct list *prev, *next;
  584.  
  585.   block = BLOCK (ptr);
  586.  
  587.   type = _heapinfo[block].busy.type;
  588.   switch (type)
  589.     {
  590.     case 0:
  591.       /* Get as many statistics as early as we can.  */
  592.       --_chunks_used;
  593.       _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
  594.       _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
  595.  
  596.       /* Find the free cluster previous to this one in the free list.
  597.      Start searching at the last block referenced; this may benefit
  598.      programs with locality of allocation.  */
  599.       i = _heapindex;
  600.       if (i > block)
  601.     while (i > block)
  602.       i = _heapinfo[i].free.prev;
  603.       else
  604.     {
  605.       do
  606.         i = _heapinfo[i].free.next;
  607.       while (i > 0 && i < block);
  608.       i = _heapinfo[i].free.prev;
  609.     }
  610.  
  611.       /* Determine how to link this block into the free list.  */
  612.       if (block == i + _heapinfo[i].free.size)
  613.     {
  614.       /* Coalesce this block with its predecessor.  */
  615.       _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
  616.       block = i;
  617.     }
  618.       else
  619.     {
  620.       /* Really link this block back into the free list.  */
  621.       _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
  622.       _heapinfo[block].free.next = _heapinfo[i].free.next;
  623.       _heapinfo[block].free.prev = i;
  624.       _heapinfo[i].free.next = block;
  625.       _heapinfo[_heapinfo[block].free.next].free.prev = block;
  626.       ++_chunks_free;
  627.     }
  628.  
  629.       /* Now that the block is linked in, see if we can coalesce it
  630.      with its successor (by deleting its successor from the list
  631.      and adding in its size).  */
  632.       if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
  633.     {
  634.       _heapinfo[block].free.size
  635.         += _heapinfo[_heapinfo[block].free.next].free.size;
  636.       _heapinfo[block].free.next
  637.         = _heapinfo[_heapinfo[block].free.next].free.next;
  638.       _heapinfo[_heapinfo[block].free.next].free.prev = block;
  639.       --_chunks_free;
  640.     }
  641.  
  642.       /* Now see if we can return stuff to the system.  */
  643.       blocks = _heapinfo[block].free.size;
  644. #ifndef PURIFY
  645.       if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
  646.       && (*__morecore) (0) == ADDRESS (block + blocks))
  647.     {
  648.       register size_t bytes = blocks * BLOCKSIZE;
  649.       _heaplimit -= blocks;
  650.       (*__morecore) (-bytes);
  651.       _heapinfo[_heapinfo[block].free.prev].free.next
  652.         = _heapinfo[block].free.next;
  653.       _heapinfo[_heapinfo[block].free.next].free.prev
  654.         = _heapinfo[block].free.prev;
  655.       block = _heapinfo[block].free.prev;
  656.       --_chunks_free;
  657.       _bytes_free -= bytes;
  658.     }
  659. #endif
  660.  
  661.       /* Set the next search to begin at this block.  */
  662.       _heapindex = block;
  663.       break;
  664.  
  665.     default:
  666.       /* Do some of the statistics.  */
  667.       --_chunks_used;
  668.       _bytes_used -= 1 << type;
  669.       ++_chunks_free;
  670.       _bytes_free += 1 << type;
  671.  
  672.       /* Get the address of the first free fragment in this block.  */
  673.       prev = (struct list *) ((char *) ADDRESS (block) +
  674.                (_heapinfo[block].busy.info.frag.first << type));
  675.  
  676.       if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
  677.     {
  678.       /* If all fragments of this block are free, remove them
  679.          from the fragment list and free the whole block.  */
  680.       next = prev;
  681.       for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i)
  682.         next = next->next;
  683.       prev->prev->next = next;
  684.       if (next != NULL)
  685.         next->prev = prev->prev;
  686.       _heapinfo[block].busy.type = 0;
  687.       _heapinfo[block].busy.info.size = 1;
  688.  
  689.       /* Keep the statistics accurate.  */
  690.       ++_chunks_used;
  691.       _bytes_used += BLOCKSIZE;
  692.       _chunks_free -= BLOCKSIZE >> type;
  693.       _bytes_free -= BLOCKSIZE;
  694.  
  695.       free (ADDRESS (block));
  696.     }
  697.       else if (_heapinfo[block].busy.info.frag.nfree != 0)
  698.     {
  699.       /* If some fragments of this block are free, link this
  700.          fragment into the fragment list after the first free
  701.          fragment of this block. */
  702.       next = (struct list *) ptr;
  703.       next->next = prev->next;
  704.       next->prev = prev;
  705.       prev->next = next;
  706.       if (next->next != NULL)
  707.         next->next->prev = next;
  708.       ++_heapinfo[block].busy.info.frag.nfree;
  709.     }
  710.       else
  711.     {
  712.       /* No fragments of this block are free, so link this
  713.          fragment into the fragment list and announce that
  714.          it is the first free fragment of this block. */
  715.       prev = (struct list *) ptr;
  716.       _heapinfo[block].busy.info.frag.nfree = 1;
  717.       _heapinfo[block].busy.info.frag.first = (unsigned long int)
  718.         ((unsigned long int) ((char *) ptr - (char *) NULL)
  719.          % BLOCKSIZE >> type);
  720.       prev->next = _fraghead[type].next;
  721.       prev->prev = &_fraghead[type];
  722.       prev->prev->next = prev;
  723.       if (prev->next != NULL)
  724.         prev->next->prev = prev;
  725.     }
  726.       break;
  727.     }
  728. }
  729.  
  730. /* Return memory to the heap.  */
  731. void
  732. free (ptr)
  733.      void *ptr;
  734. {
  735.   register struct alignlist *l;
  736.  
  737.   if (ptr == NULL)
  738.     return;
  739.  
  740.   for (l = _aligned_blocks; l != NULL; l = l->next)
  741.     if (l->aligned == ptr)
  742.       {
  743.     l->aligned = NULL;    /* Mark the slot in the list as free.  */
  744.     ptr = l->exact;
  745.     break;
  746.       }
  747.  
  748.   if (__free_hook != NULL)
  749.     (*__free_hook) (ptr);
  750.   else
  751.     __free (ptr);
  752. }
  753. /* Change the size of a block allocated by `malloc'.
  754.    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
  755.              Written May 1989 by Mike Haertel.
  756.  
  757. The GNU C Library is free software; you can redistribute it and/or
  758. modify it under the terms of the GNU Library General Public License as
  759. published by the Free Software Foundation; either version 2 of the
  760. License, or (at your option) any later version.
  761.  
  762. The GNU C Library is distributed in the hope that it will be useful,
  763. but WITHOUT ANY WARRANTY; without even the implied warranty of
  764. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  765. Library General Public License for more details.
  766.  
  767. You should have received a copy of the GNU Library General Public
  768. License along with the GNU C Library; see the file COPYING.LIB.  If
  769. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  770. Cambridge, MA 02139, USA.
  771.  
  772.    The author may be reached (Email) at the address mike@ai.mit.edu,
  773.    or (US mail) as Mike Haertel c/o Free Software Foundation. */
  774.  
  775. /*  */
  776.  
  777. /*
  778. #if    defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG)
  779. #include <string.h>
  780. #else
  781. #define    memcpy(d, s, n)        bcopy ((s), (d), (n))
  782. #endif
  783. */
  784.  
  785. #define _MALLOC_INTERNAL
  786.  
  787. #define min(A, B) ((A) < (B) ? (A) : (B))
  788.  
  789. /* Debugging hook for realloc.  */
  790. char *(*__realloc_hook) ();
  791.  
  792. /* Resize the given region to the new size, returning a pointer
  793.    to the (possibly moved) region.  This is optimized for speed;
  794.    some benchmarks seem to indicate that greater compactness is
  795.    achieved by unconditionally allocating and copying to a
  796.    new region.  This module has incestuous knowledge of the
  797.    internals of both free and malloc. */
  798. void *
  799. realloc (ptr, size)
  800.      void *ptr;
  801.      size_t size;
  802. {
  803.   char *result;
  804.   int type;
  805.   size_t block, blocks, oldlimit;
  806.  
  807.   if (size == 0)
  808.     {
  809.       free (ptr);
  810.       return malloc (0);
  811.     }
  812.   else if (ptr == NULL)
  813.     return malloc (size);
  814.  
  815.   if (__realloc_hook != NULL)
  816.     return (*__realloc_hook) (ptr, size);
  817.  
  818.   block = BLOCK (ptr);
  819.  
  820.   type = _heapinfo[block].busy.type;
  821.   switch (type)
  822.     {
  823.     case 0:
  824.       /* Maybe reallocate a large block to a small fragment.  */
  825.       if (size <= BLOCKSIZE / 2)
  826.     {
  827.       result = malloc (size);
  828.       if (result != NULL)
  829.         {
  830.           memcpy (result, ptr, size);
  831.           free (ptr);
  832.           return result;
  833.         }
  834.     }
  835.  
  836.       /* The new size is a large allocation as well;
  837.      see if we can hold it in place. */
  838.       blocks = BLOCKIFY (size);
  839.       if (blocks < _heapinfo[block].busy.info.size)
  840.     {
  841.       /* The new size is smaller; return
  842.          excess memory to the free list. */
  843.       _heapinfo[block + blocks].busy.type = 0;
  844.       _heapinfo[block + blocks].busy.info.size
  845.         = _heapinfo[block].busy.info.size - blocks;
  846.       _heapinfo[block].busy.info.size = blocks;
  847.       free (ADDRESS (block + blocks));
  848.       result = ptr;
  849.     }
  850.       else if (blocks == _heapinfo[block].busy.info.size)
  851.     /* No size change necessary.  */
  852.     result = ptr;
  853.       else
  854.     {
  855.       /* Won't fit, so allocate a new region that will.
  856.          Free the old region first in case there is sufficient
  857.          adjacent free space to grow without moving. */
  858.       blocks = _heapinfo[block].busy.info.size;
  859.       /* Prevent free from actually returning memory to the system.  */
  860.       oldlimit = _heaplimit;
  861.       _heaplimit = 0;
  862.       free (ptr);
  863.       _heaplimit = oldlimit;
  864.       result = malloc (size);
  865.       if (result == NULL)
  866.         {
  867.           (void) malloc (blocks * BLOCKSIZE);
  868.           return NULL;
  869.         }
  870.       if (ptr != result)
  871.         memmove (result, ptr, blocks * BLOCKSIZE);
  872.     }
  873.       break;
  874.  
  875.     default:
  876.       /* Old size is a fragment; type is logarithm
  877.      to base two of the fragment size.  */
  878.       if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
  879.     /* The new size is the same kind of fragment.  */
  880.     result = ptr;
  881.       else
  882.     {
  883.       /* The new size is different; allocate a new space,
  884.          and copy the lesser of the new size and the old. */
  885.       result = malloc (size);
  886.       if (result == NULL)
  887.         return NULL;
  888.       memcpy (result, ptr, min (size, (size_t) 1 << type));
  889.       free (ptr);
  890.     }
  891.       break;
  892.     }
  893.  
  894.   return result;
  895. }
  896. /* Copyright (C) 1991 Free Software Foundation, Inc.
  897. This file is part of the GNU C Library.
  898.  
  899. The GNU C Library is free software; you can redistribute it and/or
  900. modify it under the terms of the GNU Library General Public License as
  901. published by the Free Software Foundation; either version 2 of the
  902. License, or (at your option) any later version.
  903.  
  904. The GNU C Library is distributed in the hope that it will be useful,
  905. but WITHOUT ANY WARRANTY; without even the implied warranty of
  906. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  907. Library General Public License for more details.
  908.  
  909. You should have received a copy of the GNU Library General Public
  910. License along with the GNU C Library; see the file COPYING.LIB.  If
  911. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  912. Cambridge, MA 02139, USA.  */
  913.  
  914. /*  */
  915.  
  916. /*
  917. #if    defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG)
  918. #include <string.h>
  919. #else
  920. #define    memset(s, zero, n)    bzero ((s), (n))
  921. #endif
  922. */
  923.  
  924. /* Allocate an array of NMEMB elements each SIZE bytes long.
  925.    The entire array is initialized to zeros.  */
  926. void *
  927. calloc (nmemb, size)
  928.      register size_t nmemb;
  929.      register size_t size;
  930. {
  931.   register char *result = malloc (nmemb * size);
  932.  
  933.   if (result != NULL)
  934.     (void) memset (result, 0, nmemb * size);
  935.  
  936.   return result;
  937. }
  938. /* Copyright (C) 1991 Free Software Foundation, Inc.
  939. This file is part of the GNU C Library.
  940.  
  941. The GNU C Library is free software; you can redistribute it and/or modify
  942. it under the terms of the GNU General Public License as published by
  943. the Free Software Foundation; either version 2, or (at your option)
  944. any later version.
  945.  
  946. The GNU C Library is distributed in the hope that it will be useful,
  947. but WITHOUT ANY WARRANTY; without even the implied warranty of
  948. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  949. GNU General Public License for more details.
  950.  
  951. You should have received a copy of the GNU General Public License
  952. along with the GNU C Library; see the file COPYING.  If not, write to
  953. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  954.  
  955. /*  */
  956.  
  957. #define    _MALLOC_INTERNAL
  958.  
  959. #ifndef    __GNU_LIBRARY__
  960. #define    __sbrk    sbrk
  961. #endif
  962.  
  963. extern char *__sbrk ();
  964.  
  965. /* Allocate INCREMENT more bytes of data space,
  966.    and return the start of data space, or NULL on errors.
  967.    If INCREMENT is negative, shrink data space.  */
  968. char *
  969. __default_morecore (increment)
  970.      long int increment;
  971. {
  972.   char *result = __sbrk ((int) increment);
  973.   if (result == (char *) -1)
  974.     return NULL;
  975.   return result;
  976. }
  977. /* Allocate memory on a page boundary.
  978.    Copyright (C) 1991 Free Software Foundation, Inc.
  979.  
  980. The GNU C Library is free software; you can redistribute it and/or
  981. modify it under the terms of the GNU Library General Public License as
  982. published by the Free Software Foundation; either version 2 of the
  983. License, or (at your option) any later version.
  984.  
  985. The GNU C Library is distributed in the hope that it will be useful,
  986. but WITHOUT ANY WARRANTY; without even the implied warranty of
  987. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  988. Library General Public License for more details.
  989.  
  990. You should have received a copy of the GNU Library General Public
  991. License along with the GNU C Library; see the file COPYING.LIB.  If
  992. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  993. Cambridge, MA 02139, USA.  */
  994.  
  995. #ifdef    __GNU_LIBRARY__
  996. extern size_t __getpagesize ();
  997. #else
  998.  
  999. #ifdef BSD
  1000. #ifndef BSD4_1
  1001. #define HAVE_GETPAGESIZE
  1002. #endif
  1003. #endif
  1004.  
  1005. #ifdef USG5_4
  1006. #include <sys/unistd.h>
  1007.  
  1008. #define __getpagesize() sysconf(_SC_PAGESIZE);
  1009.  
  1010. #else
  1011.  
  1012. #ifdef HAVE_GETPAGESIZE
  1013. #define __getpagesize() getpagesize()
  1014. extern int getpagesize();
  1015. #else
  1016.  
  1017. #include <sys/param.h>
  1018.  
  1019. #ifdef EXEC_PAGESIZE
  1020. #define __getpagesize() EXEC_PAGESIZE
  1021. #else
  1022. #ifdef NBPG
  1023. #define __getpagesize() NBPG * CLSIZE
  1024. #ifndef CLSIZE
  1025. #define CLSIZE 1
  1026. #endif /* no CLSIZE */
  1027. #else /* no NBPG */
  1028. #define __getpagesize() NBPC
  1029. #endif /* no NBPG */
  1030. #endif /* no EXEC_PAGESIZE */
  1031.  
  1032. #endif /* not HAVE_GETPAGESIZE */
  1033.  
  1034. #endif
  1035.  
  1036. #endif /* HAVE_SYSCONF */
  1037.  
  1038. static size_t pagesize;
  1039.  
  1040. void *
  1041. valloc (size)
  1042.      size_t size;
  1043. {
  1044.   if (pagesize == 0)
  1045.     pagesize = __getpagesize ();
  1046.  
  1047.   return memalign (pagesize, size);
  1048. }
  1049. /* Copyright (C) 1991 Free Software Foundation, Inc.
  1050. This file is part of the GNU C Library.
  1051.  
  1052. The GNU C Library is free software; you can redistribute it and/or
  1053. modify it under the terms of the GNU Library General Public License as
  1054. published by the Free Software Foundation; either version 2 of the
  1055. License, or (at your option) any later version.
  1056.  
  1057. The GNU C Library is distributed in the hope that it will be useful,
  1058. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1059. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1060. Library General Public License for more details.
  1061.  
  1062. You should have received a copy of the GNU Library General Public
  1063. License along with the GNU C Library; see the file COPYING.LIB.  If
  1064. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  1065. Cambridge, MA 02139, USA.  */
  1066.  
  1067. /*  */
  1068.  
  1069. #define _MALLOC_INTERNAL
  1070.  
  1071. void *
  1072. memalign (alignment, size)
  1073.      size_t alignment;
  1074.      size_t size;
  1075. {
  1076.   char *result;
  1077.   unsigned long int adj;
  1078.  
  1079.   size = ((size + alignment - 1) / alignment) * alignment;
  1080.  
  1081.   result = malloc (size);
  1082.   if (result == NULL)
  1083.     return NULL;
  1084.   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  1085.                         (char *) NULL)) % alignment;
  1086.   if (adj != 0)
  1087.     {
  1088.       struct alignlist *l;
  1089.       for (l = _aligned_blocks; l != NULL; l = l->next)
  1090.     if (l->aligned == NULL)
  1091.       /* This slot is free.  Use it.  */
  1092.       break;
  1093.       if (l == NULL)
  1094.     {
  1095.       l = (struct alignlist *) malloc (sizeof (struct alignlist));
  1096.       if (l == NULL)
  1097.         {
  1098.           free (result);
  1099.           return NULL;
  1100.         }
  1101.     }
  1102.       l->exact = result;
  1103.       result = l->aligned = (char *) result + alignment - adj;
  1104.       l->next = _aligned_blocks;
  1105.       _aligned_blocks = l;
  1106.     }
  1107.  
  1108.   return result;
  1109. }
  1110. /* Standard debugging hooks for `malloc'.
  1111.    Copyright 1990, 1991 Free Software Foundation
  1112.    Written May 1989 by Mike Haertel.
  1113.  
  1114. The GNU C Library is free software; you can redistribute it and/or
  1115. modify it under the terms of the GNU Library General Public License as
  1116. published by the Free Software Foundation; either version 2 of the
  1117. License, or (at your option) any later version.
  1118.  
  1119. The GNU C Library is distributed in the hope that it will be useful,
  1120. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1121. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1122. Library General Public License for more details.
  1123.  
  1124. You should have received a copy of the GNU Library General Public
  1125. License along with the GNU C Library; see the file COPYING.LIB.  If
  1126. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  1127. Cambridge, MA 02139, USA.
  1128.  
  1129.  The author may be reached (Email) at the address mike@ai.mit.edu,
  1130.  or (US mail) as Mike Haertel c/o Free Software Foundation. */
  1131.  
  1132. /*  */
  1133.  
  1134.  
  1135. /* Old hook values.  */
  1136. static void (*old_free_hook) ();
  1137. static char *(*old_malloc_hook) ();
  1138. static char *(*old_realloc_hook) ();
  1139.  
  1140. /* Function to call when something awful happens.  */
  1141. extern void abort ();
  1142. static void (*abortfunc) ()= (void (*)()) abort;
  1143.  
  1144. /* Arbitrary magical numbers.  */
  1145. #define MAGICWORD    0xfedabeeb
  1146. #define MAGICBYTE    ((char) 0xd7)
  1147.  
  1148. struct hdr
  1149. {
  1150.   size_t size;            /* Exact size requested by user.  */
  1151.   unsigned long int magic;    /* Magic number to check header integrity.  */
  1152. };
  1153.  
  1154. static void
  1155. checkhdr (hdr)
  1156.      struct hdr *hdr;
  1157. {
  1158.   if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
  1159.     (*abortfunc) ();
  1160. }
  1161.  
  1162. static void
  1163. freehook (ptr)
  1164.      char *ptr;
  1165. {
  1166.   struct hdr *hdr = ((struct hdr *) ptr) - 1;
  1167.   checkhdr (hdr);
  1168.   hdr->magic = 0;
  1169.   __free_hook = old_free_hook;
  1170.   free (hdr);
  1171.   __free_hook = freehook;
  1172. }
  1173.  
  1174. static char *
  1175. mallochook (size)
  1176.      size_t size;
  1177. {
  1178.   struct hdr *hdr;
  1179.  
  1180.   __malloc_hook = old_malloc_hook;
  1181.   hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
  1182.   __malloc_hook = mallochook;
  1183.   if (hdr == NULL)
  1184.     return NULL;
  1185.  
  1186.   hdr->size = size;
  1187.   hdr->magic = MAGICWORD;
  1188.   ((char *) &hdr[1])[size] = MAGICBYTE;
  1189.   return (char *) (hdr + 1);
  1190. }
  1191.  
  1192. static char *
  1193. reallochook (ptr, size)
  1194.      char *ptr;
  1195.      size_t size;
  1196. {
  1197.   struct hdr *hdr = ((struct hdr *) ptr) - 1;
  1198.  
  1199.   checkhdr (hdr);
  1200.   __free_hook = old_free_hook;
  1201.   __malloc_hook = old_malloc_hook;
  1202.   __realloc_hook = old_realloc_hook;
  1203.   hdr = (struct hdr *) realloc ((char *) hdr, sizeof (struct hdr) + size + 1);
  1204.   __free_hook = freehook;
  1205.   __malloc_hook = mallochook;
  1206.   __realloc_hook = reallochook;
  1207.   if (hdr == NULL)
  1208.     return NULL;
  1209.  
  1210.   hdr->size = size;
  1211.   ((char *) &hdr[1])[size] = MAGICBYTE;
  1212.   return (char *) (hdr + 1);
  1213. }
  1214.  
  1215. void
  1216. mcheck (func)
  1217.      void (*func) ();
  1218. {
  1219.   static int mcheck_used = 0;
  1220.  
  1221.   if (func != NULL)
  1222.     abortfunc = func;
  1223.  
  1224.   /* These hooks may not be safely inserted if malloc is already in use.  */
  1225.   if (!__malloc_initialized && !mcheck_used)
  1226.     {
  1227.       old_free_hook = __free_hook;
  1228.       __free_hook = freehook;
  1229.       old_malloc_hook = __malloc_hook;
  1230.       __malloc_hook = mallochook;
  1231.       old_realloc_hook = __realloc_hook;
  1232.       __realloc_hook = reallochook;
  1233.       mcheck_used = 1;
  1234.     }
  1235. }
  1236. /* More debugging hooks for `malloc'.
  1237.    Copyright 1991 Free Software Foundation
  1238.           Written April 2, 1991 by John Gilmore of Cygnus Support
  1239.           Based on mcheck.c by Mike Haertel.
  1240.  
  1241. This program is free software; you can redistribute it and/or modify
  1242. it under the terms of the GNU General Public License as published by
  1243. the Free Software Foundation; either version 2 of the License, or
  1244. (at your option) any later version.
  1245.  
  1246. This program is distributed in the hope that it will be useful,
  1247. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1248. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1249. GNU General Public License for more details.
  1250.  
  1251. You should have received a copy of the GNU General Public License
  1252. along with this program; if not, write to the Free Software
  1253. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  1254.  
  1255. /*  */
  1256.  
  1257. #include <stdio.h>
  1258.  
  1259. #ifndef    __GNU_LIBRARY__
  1260. extern char *getenv ();
  1261. #endif
  1262.  
  1263. static FILE *mallstream;
  1264. static char mallenv[]= "MALLOC_TRACE";
  1265. static char mallbuf[BUFSIZ];    /* Buffer for the output.  */
  1266.  
  1267. /* Address to breakpoint on accesses to... */
  1268. char *mallwatch;
  1269.  
  1270. #if 0
  1271. /* Old hook values.  */
  1272. static void (*old_free_hook) ();
  1273. static char *(*old_malloc_hook) ();
  1274. static char *(*old_realloc_hook) ();
  1275. #endif
  1276.  
  1277. /* This function is called when the block being alloc'd, realloc'd, or
  1278.    freed has an address matching the variable "mallwatch".  In a debugger,
  1279.    set "mallwatch" to the address of interest, then put a breakpoint on
  1280.    tr_break.  */
  1281.  
  1282. void
  1283. tr_break ()
  1284. {
  1285. }
  1286.  
  1287. static void
  1288. tr_freehook (ptr)
  1289.      char *ptr;
  1290. {
  1291.   fprintf (mallstream, "- %p\n", ptr);    /* Be sure to print it first.  */
  1292.   if (ptr == mallwatch)
  1293.     tr_break ();
  1294.   __free_hook = old_free_hook;
  1295.   free (ptr);
  1296.   __free_hook = tr_freehook;
  1297. }
  1298.  
  1299. static char *
  1300. tr_mallochook (size)
  1301.      size_t size;
  1302. {
  1303.   char *hdr;
  1304.  
  1305.   __malloc_hook = old_malloc_hook;
  1306.   hdr = (char *) malloc (size);
  1307.   __malloc_hook = tr_mallochook;
  1308.  
  1309.   /* We could be printing a NULL here; that's OK.  */
  1310.   fprintf (mallstream, "+ %p %x\n", hdr, size);
  1311.  
  1312.   if (hdr == mallwatch)
  1313.     tr_break ();
  1314.  
  1315.   return hdr;
  1316. }
  1317.  
  1318. static char *
  1319. tr_reallochook (ptr, size)
  1320.      char *ptr;
  1321.      size_t size;
  1322. {
  1323.   char *hdr;
  1324.  
  1325.   if (ptr == mallwatch)
  1326.     tr_break ();
  1327.  
  1328.   __free_hook = old_free_hook;
  1329.   __malloc_hook = old_malloc_hook;
  1330.   __realloc_hook = old_realloc_hook;
  1331.   hdr = (char *) realloc (ptr, size);
  1332.   __free_hook = tr_freehook;
  1333.   __malloc_hook = tr_mallochook;
  1334.   __realloc_hook = tr_reallochook;
  1335.   if (hdr == NULL)
  1336.     /* Failed realloc.  */
  1337.     fprintf (mallstream, "! %p %x\n", ptr, size);
  1338.   else
  1339.     fprintf (mallstream, "< %p\n> %p %x\n", ptr, hdr, size);
  1340.  
  1341.   if (hdr == mallwatch)
  1342.     tr_break ();
  1343.  
  1344.   return hdr;
  1345. }
  1346.  
  1347. /* We enable tracing if either the environment variable MALLOC_TRACE
  1348.    is set, or if the variable mallwatch has been patched to an address
  1349.    that the debugging user wants us to stop on.  When patching mallwatch,
  1350.    don't forget to set a breakpoint on tr_break!  */
  1351.  
  1352. void
  1353. mtrace ()
  1354. {
  1355.   char *mallfile;
  1356.  
  1357.   mallfile = getenv (mallenv);
  1358.   if (mallfile != NULL || mallwatch != NULL)
  1359.     {
  1360.       mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
  1361.       if (mallstream != NULL)
  1362.     {
  1363.       /* Be sure it doesn't malloc its buffer!  */
  1364.       setbuf (mallstream, mallbuf);
  1365.       fprintf (mallstream, "= Start\n");
  1366.       old_free_hook = __free_hook;
  1367.       __free_hook = tr_freehook;
  1368.       old_malloc_hook = __malloc_hook;
  1369.       __malloc_hook = tr_mallochook;
  1370.       old_realloc_hook = __realloc_hook;
  1371.       __realloc_hook = tr_reallochook;
  1372.     }
  1373.     }
  1374. }
  1375. /* Access the statistics maintained by `malloc'.
  1376.    Copyright 1990, 1991 Free Software Foundation
  1377.           Written May 1989 by Mike Haertel.
  1378.  
  1379. The GNU C Library is free software; you can redistribute it and/or
  1380. modify it under the terms of the GNU Library General Public License as
  1381. published by the Free Software Foundation; either version 2 of the
  1382. License, or (at your option) any later version.
  1383.  
  1384. The GNU C Library is distributed in the hope that it will be useful,
  1385. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1386. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1387. Library General Public License for more details.
  1388.  
  1389. You should have received a copy of the GNU Library General Public
  1390. License along with the GNU C Library; see the file COPYING.LIB.  If
  1391. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  1392. Cambridge, MA 02139, USA.
  1393.  
  1394.    The author may be reached (Email) at the address mike@ai.mit.edu,
  1395.    or (US mail) as Mike Haertel c/o Free Software Foundation. */
  1396.  
  1397. /*  */
  1398.  
  1399. #define _MALLOC_INTERNAL
  1400.  
  1401. struct mstats
  1402. mstats ()
  1403. {
  1404.   struct mstats result;
  1405.  
  1406.   result.bytes_total = (char *) (*__morecore) (0) - _heapbase;
  1407.   result.chunks_used = _chunks_used;
  1408.   result.bytes_used = _bytes_used;
  1409.   result.chunks_free = _chunks_free;
  1410.   result.bytes_free = _bytes_free;
  1411.   return result;
  1412. }
  1413.