home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / binutils-1.8.x.tar.gz / binutils-1.8.x.tar / binutils / malloc.c < prev    next >
C/C++ Source or Header  |  1991-11-27  |  21KB  |  842 lines

  1. /* dynamic memory allocation for GNU.
  2.    Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22.  
  23. /*
  24.  * @(#)nmalloc.c 1 (Caltech) 2/21/82
  25.  *
  26.  *    U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
  27.  *
  28.  *    Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
  29.  *
  30.  * This is a very fast storage allocator.  It allocates blocks of a small 
  31.  * number of different sizes, and keeps free lists of each size.  Blocks
  32.  * that don't exactly fit are passed up to the next larger size.  In this 
  33.  * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
  34.  * This is designed for use in a program that uses vast quantities of
  35.  * memory, but bombs when it runs out.  To make it a little better, it
  36.  * warns the user when he starts to get near the end.
  37.  *
  38.  * June 84, ACT: modified rcheck code to check the range given to malloc,
  39.  * rather than the range determined by the 2-power used.
  40.  *
  41.  * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
  42.  * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
  43.  * You should call malloc_init to reinitialize after loading dumped Emacs.
  44.  * Call malloc_stats to get info on memory stats if MSTATS turned on.
  45.  * realloc knows how to return same block given, just changing its size,
  46.  * if the power of 2 is correct.
  47.  */
  48.  
  49. #ifdef WYSE
  50. #include <sys/bsd2usg.h>
  51. #endif /* WYSE */
  52.  
  53. /*
  54.  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
  55.  * smallest allocatable block is 8 bytes.  The overhead information will
  56.  * go in the first int of the block, and the returned pointer will point
  57.  * to the second.
  58.  *
  59. #ifdef MSTATS
  60.  * nmalloc[i] is the difference between the number of mallocs and frees
  61.  * for a given block size.
  62. #endif MSTATS
  63.  */
  64.  
  65. #ifdef emacs
  66. /* config.h specifies which kind of system this is.  */
  67. #include "config.h"
  68. #include <signal.h>
  69. #else
  70.  
  71. /* Determine which kind of system this is.  */
  72. #include <signal.h>
  73. #ifndef SIGTSTP
  74. #ifndef VMS
  75. #ifndef USG
  76. #define USG
  77. #endif
  78. #endif /* not VMS */
  79. #else /* SIGTSTP */
  80. #ifdef SIGIO
  81. #define BSD4_2
  82. #endif /* SIGIO */
  83. #endif /* SIGTSTP */
  84.  
  85. #endif /* not emacs */
  86.  
  87. /* Define getpagesize () if the system does not.  */
  88. #include "getpagesize.h"
  89.  
  90. #ifdef BSD
  91. #ifdef BSD4_1
  92. #include <sys/vlimit.h>        /* warn the user when near the end */
  93. #else /* if 4.2 or newer */
  94. #include <sys/time.h>
  95. #include <sys/resource.h>
  96. #endif /* if 4.2 or newer */
  97. #endif
  98.  
  99. #ifdef VMS
  100. #include "vlimit.h"
  101. #endif
  102.  
  103. extern char *start_of_data ();
  104.  
  105. #ifdef BSD
  106. #ifndef DATA_SEG_BITS
  107. #define start_of_data() &etext
  108. #endif
  109. #endif
  110.  
  111. #ifndef emacs
  112. #define start_of_data() &etext
  113. #endif
  114.  
  115. #define ISALLOC ((char) 0xf7)    /* magic byte that implies allocation */
  116. #define ISFREE ((char) 0x54)    /* magic byte that implies free block */
  117.                 /* this is for error checking only */
  118. #define ISMEMALIGN ((char) 0xd6)  /* Stored before the value returned by
  119.                      memalign, with the rest of the word
  120.                      being the distance to the true
  121.                      beginning of the block.  */
  122.  
  123. extern char etext;
  124.  
  125. /* These two are for user programs to look at, when they are interested.  */
  126.  
  127. unsigned int malloc_sbrk_used;       /* amount of data space used now */
  128. unsigned int malloc_sbrk_unused;     /* amount more we can have */
  129.  
  130. /* start of data space; can be changed by calling init_malloc */
  131. static char *data_space_start;
  132.  
  133. #ifdef MSTATS
  134. static int nmalloc[30];
  135. static int nmal, nfre;
  136. #endif /* MSTATS */
  137.  
  138. /* If range checking is not turned on, all we have is a flag indicating
  139.    whether memory is allocated, an index in nextf[], and a size field; to
  140.    realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
  141.    on whether the former can hold the exact size (given the value of
  142.    'index').  If range checking is on, we always need to know how much space
  143.    is allocated, so the 'size' field is never used. */
  144.  
  145. struct mhead {
  146.     char     mh_alloc;    /* ISALLOC or ISFREE */
  147.     char     mh_index;    /* index in nextf[] */
  148. /* Remainder are valid only when block is allocated */
  149.     unsigned short mh_size;    /* size, if < 0x10000 */
  150. #ifdef rcheck
  151.     unsigned mh_nbytes;    /* number of bytes allocated */
  152.     int      mh_magic4;    /* should be == MAGIC4 */
  153. #endif /* rcheck */
  154. };
  155.  
  156. /* Access free-list pointer of a block.
  157.   It is stored at block + 4.
  158.   This is not a field in the mhead structure
  159.   because we want sizeof (struct mhead)
  160.   to describe the overhead for when the block is in use,
  161.   and we do not want the free-list pointer to count in that.  */
  162.  
  163. #define CHAIN(a) \
  164.   (*(struct mhead **) (sizeof (char *) + (char *) (a)))
  165.  
  166. #ifdef rcheck
  167.  
  168. /* To implement range checking, we write magic values in at the beginning and
  169.    end of each allocated block, and make sure they are undisturbed whenever a
  170.    free or a realloc occurs. */
  171. /* Written in each of the 4 bytes following the block's real space */
  172. #define MAGIC1 0x55
  173. /* Written in the 4 bytes before the block's real space */
  174. #define MAGIC4 0x55555555
  175. #define ASSERT(p) if (!(p)) botch("p"); else
  176. #define EXTRA  4        /* 4 bytes extra for MAGIC1s */
  177. #else
  178. #define ASSERT(p) if (!(p)) abort (); else
  179. #define EXTRA  0
  180. #endif /* rcheck */
  181.  
  182.  
  183. /* nextf[i] is free list of blocks of size 2**(i + 3)  */
  184.  
  185. static struct mhead *nextf[30];
  186.  
  187. /* busy[i] is nonzero while allocation of block size i is in progress.  */
  188.  
  189. static char busy[30];
  190.  
  191. /* Number of bytes of writable memory we can expect to be able to get */
  192. static unsigned int lim_data;
  193.  
  194. /* Level number of warnings already issued.
  195.   0 -- no warnings issued.
  196.   1 -- 75% warning already issued.
  197.   2 -- 85% warning already issued.
  198. */
  199. static int warnlevel;
  200.  
  201. /* Function to call to issue a warning;
  202.    0 means don't issue them.  */
  203. static void (*warnfunction) ();
  204.  
  205. /* nonzero once initial bunch of free blocks made */
  206. static int gotpool;
  207.  
  208. char *_malloc_base;
  209.  
  210. static void getpool ();
  211.  
  212. /* Cause reinitialization based on job parameters;
  213.   also declare where the end of pure storage is. */
  214. void
  215. malloc_init (start, warnfun)
  216.      char *start;
  217.      void (*warnfun) ();
  218. {
  219.   if (start)
  220.     data_space_start = start;
  221.   lim_data = 0;
  222.   warnlevel = 0;
  223.   warnfunction = warnfun;
  224. }
  225.  
  226. /* Return the maximum size to which MEM can be realloc'd
  227.    without actually requiring copying.  */
  228.  
  229. int
  230. malloc_usable_size (mem)
  231.      char *mem;
  232. {
  233.   struct mhead *p
  234.     = (struct mhead *) (mem - ((sizeof (struct mhead) + 7) & ~7));
  235.   int blocksize = 8 << p->mh_index;
  236.  
  237.   return blocksize - sizeof (struct mhead) - EXTRA;
  238. }
  239.  
  240. static void
  241. morecore (nu)            /* ask system for more memory */
  242.      register int nu;        /* size index to get more of  */
  243. {
  244.   char *sbrk ();
  245.   register char *cp;
  246.   register int nblks;
  247.   register unsigned int siz;
  248.   int oldmask;
  249.  
  250. #ifdef BSD
  251. #ifndef BSD4_1
  252.   int newmask = -1;
  253.   /* Blocking these signals interferes with debugging, at least on BSD on
  254.      the HP 9000/300.  */
  255. #ifdef SIGTRAP
  256.   newmask &= ~(1 << SIGTRAP);
  257. #endif
  258. #ifdef SIGILL
  259.   newmask &= ~(1 << SIGILL);
  260. #endif
  261. #ifdef SIGTSTP
  262.   newmask &= ~(1 << SIGTSTP);
  263. #endif
  264. #ifdef SIGSTOP
  265.   newmask &= ~(1 << SIGSTOP);
  266. #endif
  267.   oldmask = sigsetmask (newmask);
  268. #endif
  269. #endif
  270.  
  271.   if (!data_space_start)
  272.     {
  273.       data_space_start = start_of_data ();
  274.     }
  275.  
  276.   if (lim_data == 0)
  277.     get_lim_data ();
  278.  
  279.  /* On initial startup, get two blocks of each size up to 1k bytes */
  280.   if (!gotpool)
  281.     { getpool (); getpool (); gotpool = 1; }
  282.  
  283.   /* Find current end of memory and issue warning if getting near max */
  284.  
  285. #ifndef VMS
  286.   /* Maximum virtual memory on VMS is difficult to calculate since it
  287.    * depends on several dynmacially changing things. Also, alignment
  288.    * isn't that important. That is why much of the code here is ifdef'ed
  289.    * out for VMS systems.
  290.    */
  291.   cp = sbrk (0);
  292.   siz = cp - data_space_start;
  293.  
  294.   if (warnfunction)
  295.     switch (warnlevel)
  296.       {
  297.       case 0: 
  298.     if (siz > (lim_data / 4) * 3)
  299.       {
  300.         warnlevel++;
  301.         (*warnfunction) ("Warning: past 75% of memory limit");
  302.       }
  303.     break;
  304.       case 1: 
  305.     if (siz > (lim_data / 20) * 17)
  306.       {
  307.         warnlevel++;
  308.         (*warnfunction) ("Warning: past 85% of memory limit");
  309.       }
  310.     break;
  311.       case 2: 
  312.     if (siz > (lim_data / 20) * 19)
  313.       {
  314.         warnlevel++;
  315.         (*warnfunction) ("Warning: past 95% of memory limit");
  316.       }
  317.     break;
  318.       }
  319.  
  320.   if ((int) cp & 0x3ff)    /* land on 1K boundaries */
  321.     sbrk (1024 - ((int) cp & 0x3ff));
  322. #endif /* not VMS */
  323.  
  324.  /* Take at least 2k, and figure out how many blocks of the desired size
  325.     we're about to get */
  326.   nblks = 1;
  327.   if ((siz = nu) < 8)
  328.     nblks = 1 << ((siz = 8) - nu);
  329.  
  330.   if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
  331.     {
  332. #ifdef BSD
  333. #ifndef BSD4_1
  334.       sigsetmask (oldmask);
  335. #endif
  336. #endif
  337.       return;            /* no more room! */
  338.     }
  339.   malloc_sbrk_used = siz;
  340.   malloc_sbrk_unused = lim_data - siz;
  341.  
  342. #ifndef VMS
  343.   if ((int) cp & 7)
  344.     {        /* shouldn't happen, but just in case */
  345.       cp = (char *) (((int) cp + 8) & ~7);
  346.       nblks--;
  347.     }
  348. #endif /* not VMS */
  349.  
  350.  /* save new header and link the nblks blocks together */
  351.   nextf[nu] = (struct mhead *) cp;
  352.   siz = 1 << (nu + 3);
  353.   while (1)
  354.     {
  355.       ((struct mhead *) cp) -> mh_alloc = ISFREE;
  356.       ((struct mhead *) cp) -> mh_index = nu;
  357.       if (--nblks <= 0) break;
  358.       CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
  359.       cp += siz;
  360.     }
  361.   CHAIN ((struct mhead *) cp) = 0;
  362.  
  363. #ifdef BSD
  364. #ifndef BSD4_1
  365.   sigsetmask (oldmask);
  366. #endif
  367. #endif
  368. }
  369.  
  370. static void
  371. getpool ()
  372. {
  373.   register int nu;
  374.   char * sbrk ();
  375.   register char *cp = sbrk (0);
  376.  
  377.   if ((int) cp & 0x3ff)    /* land on 1K boundaries */
  378.     sbrk (1024 - ((int) cp & 0x3ff));
  379.  
  380.   /* Record address of start of space allocated by malloc.  */
  381.   if (_malloc_base == 0)
  382.     _malloc_base = cp;
  383.  
  384.   /* Get 2k of storage */
  385.  
  386.   cp = sbrk (04000);
  387.   if (cp == (char *) -1)
  388.     return;
  389.  
  390.   /* Divide it into an initial 8-word block
  391.      plus one block of size 2**nu for nu = 3 ... 10.  */
  392.  
  393.   CHAIN (cp) = nextf[0];
  394.   nextf[0] = (struct mhead *) cp;
  395.   ((struct mhead *) cp) -> mh_alloc = ISFREE;
  396.   ((struct mhead *) cp) -> mh_index = 0;
  397.   cp += 8;
  398.  
  399.   for (nu = 0; nu < 7; nu++)
  400.     {
  401.       CHAIN (cp) = nextf[nu];
  402.       nextf[nu] = (struct mhead *) cp;
  403.       ((struct mhead *) cp) -> mh_alloc = ISFREE;
  404.       ((struct mhead *) cp) -> mh_index = nu;
  405.       cp += 8 << nu;
  406.     }
  407. }
  408.  
  409. char *
  410. malloc (n)        /* get a block */
  411.      unsigned n;
  412. {
  413.   register struct mhead *p;
  414.   register unsigned int nbytes;
  415.   register int nunits = 0;
  416.  
  417.   /* Figure out how many bytes are required, rounding up to the nearest
  418.      multiple of 8, then figure out which nestf[] area to use.
  419.      Both the beginning of the header and the beginning of the
  420.      block should be on an eight byte boundary.  */
  421.   nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
  422.   {
  423.     register unsigned int   shiftr = (nbytes - 1) >> 2;
  424.  
  425.     while (shiftr >>= 1)
  426.       nunits++;
  427.   }
  428.  
  429.   /* In case this is reentrant use of malloc from signal handler,
  430.      pick a block size that no other malloc level is currently
  431.      trying to allocate.  That's the easiest harmless way not to
  432.      interfere with the other level of execution.  */
  433.   while (busy[nunits]) nunits++;
  434.   busy[nunits] = 1;
  435.  
  436.   /* If there are no blocks of the appropriate size, go get some */
  437.   /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
  438.   if (nextf[nunits] == 0)
  439.     morecore (nunits);
  440.  
  441.   /* Get one block off the list, and set the new list head */
  442.   if ((p = nextf[nunits]) == 0)
  443.     {
  444.       busy[nunits] = 0;
  445.       return 0;
  446.     }
  447.   nextf[nunits] = CHAIN (p);
  448.   busy[nunits] = 0;
  449.  
  450.   /* Check for free block clobbered */
  451.   /* If not for this check, we would gobble a clobbered free chain ptr */
  452.   /* and bomb out on the NEXT allocate of this size block */
  453.   if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
  454. #ifdef rcheck
  455.     botch ("block on free list clobbered");
  456. #else /* not rcheck */
  457.     abort ();
  458. #endif /* not rcheck */
  459.  
  460.   /* Fill in the info, and if range checking, set up the magic numbers */
  461.   p -> mh_alloc = ISALLOC;
  462. #ifdef rcheck
  463.   p -> mh_nbytes = n;
  464.   p -> mh_magic4 = MAGIC4;
  465.   {
  466.     /* Get the location n after the beginning of the user's space.  */
  467.     register char *m = (char *) p + ((sizeof *p + 7) & ~7) + n;
  468.  
  469.     *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
  470.   }
  471. #else /* not rcheck */
  472.   p -> mh_size = n;
  473. #endif /* not rcheck */
  474. #ifdef MSTATS
  475.   nmalloc[nunits]++;
  476.   nmal++;
  477. #endif /* MSTATS */
  478.   return (char *) p + ((sizeof *p + 7) & ~7);
  479. }
  480.  
  481. free (mem)
  482.      char *mem;
  483. {
  484.   register struct mhead *p;
  485.   {
  486.     register char *ap = mem;
  487.  
  488.     if (ap == 0)
  489.       return;
  490.  
  491.     p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
  492.     if (p -> mh_alloc == ISMEMALIGN)
  493.       {
  494.     ap -= p->mh_size;
  495.     p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
  496.       }
  497.  
  498. #ifndef rcheck
  499.     if (p -> mh_alloc != ISALLOC)
  500.       abort ();
  501.  
  502. #else rcheck
  503.     if (p -> mh_alloc != ISALLOC)
  504.       {
  505.     if (p -> mh_alloc == ISFREE)
  506.       botch ("free: Called with already freed block argument\n");
  507.     else
  508.       botch ("free: Called with bad argument\n");
  509.       }
  510.  
  511.     ASSERT (p -> mh_magic4 == MAGIC4);
  512.     ap += p -> mh_nbytes;
  513.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
  514.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap   == MAGIC1);
  515. #endif /* rcheck */
  516.   }
  517.   {
  518.     register int nunits = p -> mh_index;
  519.  
  520.     ASSERT (nunits <= 29);
  521.     p -> mh_alloc = ISFREE;
  522.  
  523.     /* Protect against signal handlers calling malloc.  */
  524.     busy[nunits] = 1;
  525.     /* Put this block on the free list.  */
  526.     CHAIN (p) = nextf[nunits];
  527.     nextf[nunits] = p;
  528.     busy[nunits] = 0;
  529.  
  530. #ifdef MSTATS
  531.     nmalloc[nunits]--;
  532.     nfre++;
  533. #endif /* MSTATS */
  534.   }
  535. }
  536.  
  537. char *
  538. realloc (mem, n)
  539.      char *mem;
  540.      register unsigned n;
  541. {
  542.   register struct mhead *p;
  543.   register unsigned int tocopy;
  544.   register unsigned int nbytes;
  545.   register int nunits;
  546.  
  547.   if (mem == 0)
  548.     return malloc (n);
  549.   p = (struct mhead *) (mem - ((sizeof *p + 7) & ~7));
  550.   nunits = p -> mh_index;
  551.   ASSERT (p -> mh_alloc == ISALLOC);
  552. #ifdef rcheck
  553.   ASSERT (p -> mh_magic4 == MAGIC4);
  554.   {
  555.     register char *m = mem + (tocopy = p -> mh_nbytes);
  556.     ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
  557.     ASSERT (*m++ == MAGIC1); ASSERT (*m   == MAGIC1);
  558.   }
  559. #else /* not rcheck */
  560.   if (p -> mh_index >= 13)
  561.     tocopy = (1 << (p -> mh_index + 3)) - ((sizeof *p + 7) & ~7);
  562.   else
  563.     tocopy = p -> mh_size;
  564. #endif /* not rcheck */
  565.  
  566.   /* See if desired size rounds to same power of 2 as actual size. */
  567.   nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
  568.  
  569.   /* If ok, use the same block, just marking its size as changed.  */
  570.   if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
  571.     {
  572. #ifdef rcheck
  573.       register char *m = mem + tocopy;
  574.       *m++ = 0;  *m++ = 0;  *m++ = 0;  *m++ = 0;
  575.       p-> mh_nbytes = n;
  576.       m = mem + n;
  577.       *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;
  578. #else /* not rcheck */
  579.       p -> mh_size = n;
  580. #endif /* not rcheck */
  581.       return mem;
  582.     }
  583.  
  584.   if (n < tocopy)
  585.     tocopy = n;
  586.   {
  587.     register char *new;
  588.  
  589.     if ((new = malloc (n)) == 0)
  590.       return 0;
  591.     bcopy (mem, new, tocopy);
  592.     free (mem);
  593.     return new;
  594.   }
  595. }
  596.  
  597. /* This is in case something linked with Emacs calls calloc.  */
  598.  
  599. char *
  600. calloc (num, size)
  601.      unsigned num, size;
  602. {
  603.   register char *mem;
  604.  
  605.   num *= size;
  606.   mem = malloc (num);
  607.   if (mem != 0)
  608.     bzero (mem, num);
  609.   return mem;
  610. }
  611.  
  612. #ifndef VMS
  613.  
  614. char *
  615. memalign (alignment, size)
  616.      unsigned alignment, size;
  617. {
  618.   register char *ptr = malloc (size + alignment);
  619.   register char *aligned;
  620.   register struct mhead *p;
  621.  
  622.   if (ptr == 0)
  623.     return 0;
  624.   /* If entire block has the desired alignment, just accept it.  */
  625.   if (((int) ptr & (alignment - 1)) == 0)
  626.     return ptr;
  627.   /* Otherwise, get address of byte in the block that has that alignment.  */
  628.   aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
  629.  
  630.   /* Store a suitable indication of how to free the block,
  631.      so that free can find the true beginning of it.  */
  632.   p = (struct mhead *) (aligned - ((7 + sizeof (struct mhead)) & ~7));
  633.   p -> mh_size = aligned - ptr;
  634.   p -> mh_alloc = ISMEMALIGN;
  635.   return aligned;
  636. }
  637.  
  638. #ifndef HPUX
  639. /* This runs into trouble with getpagesize on HPUX.
  640.    Patching out seems cleaner than the ugly fix needed.  */
  641. char *
  642. valloc (size)
  643. {
  644.   return memalign (getpagesize (), size);
  645. }
  646. #endif /* not HPUX */
  647. #endif /* not VMS */
  648.  
  649. #ifdef MSTATS
  650. /* Return statistics describing allocation of blocks of size 2**n. */
  651.  
  652. struct mstats_value
  653.   {
  654.     int blocksize;
  655.     int nfree;
  656.     int nused;
  657.   };
  658.  
  659. struct mstats_value
  660. malloc_stats (size)
  661.      int size;
  662. {
  663.   struct mstats_value v;
  664.   register int i;
  665.   register struct mhead *p;
  666.  
  667.   v.nfree = 0;
  668.  
  669.   if (size < 0 || size >= 30)
  670.     {
  671.       v.blocksize = 0;
  672.       v.nused = 0;
  673.       return v;
  674.     }
  675.  
  676.   v.blocksize = 1 << (size + 3);
  677.   v.nused = nmalloc[size];
  678.  
  679.   for (p = nextf[size]; p; p = CHAIN (p))
  680.     v.nfree++;
  681.  
  682.   return v;
  683. }
  684. int
  685. malloc_mem_used ()
  686. {
  687.   int i;
  688.   int size_used;
  689.  
  690.   size_used = 0;
  691.   
  692.   for (i = 0; i < 30; i++)
  693.     {
  694.       int allocation_size = 1 << (i + 3);
  695.       struct mhead *p;
  696.       
  697.       size_used += nmalloc[i] * allocation_size;
  698.     }
  699.  
  700.   return size_used;
  701. }
  702.  
  703. int 
  704. malloc_mem_free ()
  705. {
  706.   int i;
  707.   int size_unused;
  708.  
  709.   size_unused = 0;
  710.   
  711.   for (i = 0; i < 30; i++)
  712.     {
  713.       int allocation_size = 1 << (i + 3);
  714.       struct mhead *p;
  715.       
  716.       for (p = nextf[i]; p ; p = CHAIN (p))
  717.     size_unused += allocation_size;
  718.     }
  719.  
  720.   return size_unused;
  721. }
  722. #endif /* MSTATS */
  723.  
  724. /*
  725.  *    This function returns the total number of bytes that the process
  726.  *    will be allowed to allocate via the sbrk(2) system call.  On
  727.  *    BSD systems this is the total space allocatable to stack and
  728.  *    data.  On USG systems this is the data space only.
  729.  */
  730.  
  731. #ifdef USG
  732.  
  733. get_lim_data ()
  734. {
  735.   extern long ulimit ();
  736.     
  737. #ifdef ULIMIT_BREAK_VALUE
  738.   lim_data = ULIMIT_BREAK_VALUE;
  739. #else
  740.   lim_data = ulimit (3, 0);
  741. #endif
  742.  
  743.   lim_data -= (long) data_space_start;
  744. }
  745.  
  746. #else /* not USG */
  747. #if defined (BSD4_1) || defined (VMS)
  748.  
  749. get_lim_data ()
  750. {
  751.   lim_data = vlimit (LIM_DATA, -1);
  752. }
  753.  
  754. #else /* not BSD4_1 and not VMS */
  755.  
  756. get_lim_data ()
  757. {
  758.   struct rlimit XXrlimit;
  759.  
  760.   getrlimit (RLIMIT_DATA, &XXrlimit);
  761. #ifdef RLIM_INFINITY
  762.   lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
  763. #else
  764.   lim_data = XXrlimit.rlim_cur;    /* soft limit */
  765. #endif
  766. }
  767.  
  768. #endif /* not BSD4_1 and not VMS */
  769. #endif /* not USG */
  770.  
  771. #ifdef VMS
  772. /* There is a problem when dumping and restoring things on VMS. Calls
  773.  * to SBRK don't necessarily result in contiguous allocation. Dumping
  774.  * doesn't work when it isn't. Therefore, we make the initial
  775.  * allocation contiguous by allocating a big chunk, and do SBRKs from
  776.  * there. Once Emacs has dumped there is no reason to continue
  777.  * contiguous allocation, malloc doesn't depend on it.
  778.  *
  779.  * There is a further problem of using brk and sbrk while using VMS C
  780.  * run time library routines malloc, calloc, etc. The documentation
  781.  * says that this is a no-no, although I'm not sure why this would be
  782.  * a problem. In any case, we remove the necessity to call brk and
  783.  * sbrk, by calling calloc (to assure zero filled data) rather than
  784.  * sbrk.
  785.  *
  786.  * VMS_ALLOCATION_SIZE is the size of the allocation array. This
  787.  * should be larger than the malloc size before dumping. Making this
  788.  * too large will result in the startup procedure slowing down since
  789.  * it will require more space and time to map it in.
  790.  *
  791.  * The value for VMS_ALLOCATION_SIZE in the following define was determined
  792.  * by running emacs linked (and a large allocation) with the debugger and
  793.  * looking to see how much storage was used. The allocation was 201 pages,
  794.  * so I rounded it up to a power of two.
  795.  */
  796. #ifndef VMS_ALLOCATION_SIZE
  797. #define VMS_ALLOCATION_SIZE    (512*256)
  798. #endif
  799.  
  800. /* Use VMS RTL definitions */
  801. #undef sbrk
  802. #undef brk
  803. #undef malloc
  804. int vms_out_initial = 0;
  805. char vms_initial_buffer[VMS_ALLOCATION_SIZE];
  806. static char *vms_current_brk = &vms_initial_buffer;
  807. static char *vms_end_brk = &vms_initial_buffer[VMS_ALLOCATION_SIZE-1];
  808.  
  809. #include <stdio.h>
  810.  
  811. char *
  812. sys_sbrk (incr)
  813.      int incr;
  814. {
  815.   char *sbrk(), *temp, *ptr;
  816.  
  817.   if (vms_out_initial)
  818.     {
  819.       /* out of initial allocation... */
  820.       if (!(temp = malloc (incr)))
  821.     temp = (char *) -1;
  822.     }
  823.   else
  824.     {
  825.       /* otherwise, go out of our area */
  826.       ptr = vms_current_brk + incr; /* new current_brk */
  827.       if (ptr <= vms_end_brk)
  828.     {
  829.       temp = vms_current_brk;
  830.       vms_current_brk = ptr;
  831.     }
  832.       else
  833.     {
  834.       vms_out_initial = 1;    /* mark as out of initial allocation */
  835.       if (!(temp = malloc (incr)))
  836.         temp = (char *) -1;
  837.     }
  838.     }
  839.   return temp;
  840. }
  841. #endif /* VMS */
  842.