[comp.sources.atari.st] v02i078: gcclib2 -- Patches to GCC library part07/07

koreth%panarthea.ebay@sun.com (Steven Grimm) (08/21/89)

Submitted-by: uunet.UU.NET!unido!sbsvax!roeder (Edgar Roeder)
Posting-number: Volume 2, Issue 78
Archive-name: gcclib2/part07



#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 7 (of 7)."
# Contents:  malloc.c
# Wrapped by roeder@sbsvax on Wed Aug 16 22:03:05 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'malloc.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'malloc.c'\"
else
echo shar: Extracting \"'malloc.c'\" \(21197 characters\)
sed "s/^X//" >'malloc.c' <<'END_OF_FILE'
X/* Dynamic memory allocation for GNU.
X   Copyright (C) 1985 Richard M. Stallman,
X    based mostly on the public domain work of others.
X
XThis program is distributed in the hope that it will be useful,
Xbut without any warranty.  No author or distributor
Xaccepts responsibility to anyone for the consequences of using it
Xor for whether it serves any particular purpose or works at all,
Xunless he says so in writing.
X
X   Permission is granted to anyone to distribute verbatim copies
X   of this program's source code as received, in any medium, provided that
X   the copyright notice, the nonwarraty notice above
X   and this permission notice are preserved,
X   and that the distributor grants the recipient all rights
X   for further redistribution as permitted by this notice,
X   and informs him of these rights.
X
X   Permission is granted to distribute modified versions of this
X   program's source code, or of portions of it, under the above
X   conditions, plus the conditions that all changed files carry
X   prominent notices stating who last changed them and that the
X   derived material, including anything packaged together with it and
X   conceptually functioning as a modification of it rather than an
X   application of it, is in its entirety subject to a permission
X   notice identical to this one.
X
X   Permission is granted to distribute this program (verbatim or
X   as modified) in compiled or executable form, provided verbatim
X   redistribution is permitted as stated above for source code, and
X    A.  it is accompanied by the corresponding machine-readable
X      source code, under the above conditions, or
X    B.  it is accompanied by a written offer, with no time limit,
X      to distribute the corresponding machine-readable source code,
X      under the above conditions, to any one, in return for reimbursement
X      of the cost of distribution.   Verbatim redistribution of the
X      written offer must be permitted.  Or,
X    C.  it is distributed by someone who received only the
X      compiled or executable form, and is accompanied by a copy of the
X      written offer of source code which he received along with it.
X
X   Permission is granted to distribute this program (verbatim or as modified)
X   in executable form as part of a larger system provided that the source
X   code for this program, including any modifications used,
X   is also distributed or offered as stated in the preceding paragraph.
X
XIn other words, you are welcome to use, share and improve this program.
XYou are forbidden to forbid anyone else to use, share and improve
Xwhat you give them.   Help stamp out software-hoarding!  */
X
X
X/*
X * @(#)nmalloc.c 1 (Caltech) 2/21/82
X *
X *	U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
X *
X *	Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
X *
X * This is a very fast storage allocator.  It allocates blocks of a small 
X * number of different sizes, and keeps free lists of each size.  Blocks
X * that don't exactly fit are passed up to the next larger size.  In this 
X * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
X * This is designed for use in a program that uses vast quantities of
X * memory, but bombs when it runs out.  To make it a little better, it
X * warns the user when he starts to get near the end.
X *
X * June 84, ACT: modified rcheck code to check the range given to malloc,
X * rather than the range determined by the 2-power used.
X *
X * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
X * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
X * You should call malloc_init to reinitialize after loading dumped Emacs.
X * Call malloc_stats to get info on memory stats if MSTATS turned on.
X * realloc knows how to return same block given, just changing its size,
X * if the power of 2 is correct.
X */
X
X/*
X * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
X * smallest allocatable block is 8 bytes.  The overhead information will
X * go in the first long of the block, and the returned pointer will point
X * to the second.
X *
X#ifdef MSTATS
X * nmalloc[i] is the difference between the number of mallocs and frees
X * for a given block size.
X#endif /* MSTATS */
X
X#define BUCKETS	43	/* this is sufficient for 16 MByte */
X
X#ifdef emacs
X#include "config.h"
X#endif /* emacs */
X
X#ifdef atarist
X#define atari
X#endif
X#ifndef atari
X#include <sys/param.h>
X#endif
X
X/* Determine which kind of system this is.  */
X#include <signal.h>
X#ifndef SIGTSTP
X#define USG
X#else /* SIGTSTP */
X#ifdef SIGIO
X#define BSD42
X#endif /* SIGIO */
X#endif /* SIGTSTP */
X
X#ifdef atari
X#include <osbind.h>
X#include <stdio.h>
X#define USG
X#define EXEC_PAGESIZE	256
X#define malloc_warning	botch
X#define botch	Bconws
X#endif
X#ifdef __GNUC__
X#define gnu_malloc	malloc
X#define gnu_free	free
X#define gnu_realloc	realloc
X#define gnu_bcopy	bcopy
X#define lsbrk		sbrk
X#ifdef atari
X#define sbrk		(char *) Malloc
X#endif
X#endif
X
X#ifndef BSD42
X#ifndef USG
X#include <sys/vlimit.h>		/* warn the user when near the end */
X#endif
X#else /* if BSD42 */
X#include <sys/time.h>
X#include <sys/resource.h>
X#endif /* BSD42 */
X
X
X#if defined (BSD4_1) || defined (USG)
X#ifdef EXEC_PAGESIZE
X#define getpagesize() EXEC_PAGESIZE
X#else
X#ifdef NBPG
X#define getpagesize() NBPG * CLSIZE
X#ifndef CLSIZE
X#define CLSIZE 1
X#endif /* no CLSIZE */
X#else /* no NBPG */
X#define getpagesize() NBPC
X#endif /* no NBPG */
X#endif /* no EXEC_PAGESIZE */
X#endif /* BSD4_1 or USG */
X
X#define ISALLOC ((unsigned char) 0xf7)	/* magic byte that implies allocation */
X#define ISFREE ((unsigned char) 0x54)	/* magic byte that implies free block */
X					/* this is for error checking only */
X#define ISMEMALIGN ((unsigned char) 0xd6)	/* Stored before the value
X						   returned by memalign, with
X						   the rest of the word being
X						   the distance to the true
X						   beginning of the block.  */
X
Xextern char etext;
Xextern char *start_of_data ();
X
X/* These two are for user programs to look at, when they are interested.  */
X
Xlong malloc_sbrk_used;       /* amount of data space used now */
Xlong malloc_sbrk_unused;     /* amount more we can have */
X
X/* start of data space; can be changed by calling init_malloc */
Xstatic char *data_space_start;
X
X#ifdef MSTATS
Xstatic int nmalloc[BUCKETS];
Xstatic int nmaxalloc[BUCKETS];
Xstatic int nmal, nfre;
X#endif /* MSTATS */
X
X/* If range checking is not turned on, all we have is a flag indicating
X   whether memory is allocated, an index in nextf[], and a size field; to
X   realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
X   on whether the former can hold the exact size (given the value of
X   'index').  If range checking is on, we always need to know how much space
X   is allocated, so the 'size' field is never used. */
X
Xstruct mhead {
X	unsigned char	mh_alloc;	/* ISALLOC or ISFREE */
X	unsigned char	mh_index;	/* index in nextf[] */
X/* Remainder are valid only when block is allocated */
X	unsigned short	mh_size;	/* size, if < 0x10000 */
X#ifdef rcheck
X	unsigned long	mh_nbytes;	/* number of bytes allocated */
X	long		mh_magic4;	/* should be == MAGIC4 */
X#endif /* rcheck */
X};
X
X/* Access free-list pointer of a block.
X  It is stored at block + 4.
X  This is not a field in the mhead structure
X  because we want sizeof (struct mhead)
X  to describe the overhead for when the block is in use,
X  and we do not want the free-list pointer to count in that.  */
X
X#define CHAIN(a) \
X  (*(struct mhead **) (sizeof (char *) + (char *) (a)))
X
X#ifdef rcheck
X
X/* To implement range checking, we write magic values in at the beginning and
X   end of each allocated block, and make sure they are undisturbed whenever a
X   free or a realloc occurs. */
X/* Written in each of the 4 bytes following the block's real space */
X#define MAGIC1 ((unsigned char) 0x55)
X/* Written in the 4 bytes before the block's real space */
X#define MAGIC4 0x55555555L
X#ifdef __GNUC__
X#define ASSERT(p) if (!(p)) botch(#p "\n"); else
X#else
X#define ASSERT(p) if (!(p)) botch("p\n"); else
X#endif
X#define EXTRA  4		/* 4 bytes extra for MAGIC1s */
X#else
X#define ASSERT(p)
X#define EXTRA  0
X#endif /* rcheck */
X
X#define BUCKETSIZE(b)	((1L << ((b >> 1) + 2)) * (2 + b % 2))
X#define MIN_ALLOC	20	/* bucket with 8kB */
X
X/* nextf[i] is free list of blocks of size 2**(i + 3)  */
X
Xstatic struct mhead *nextf[BUCKETS];
X
X/* Number of bytes of writable memory we can expect to be able to get */
Xstatic long lim_data;
X/* Level number of warnings already issued.
X  0 -- no warnings issued.
X  1 -- 75% warning already issued.
X  2 -- 85% warning already issued.
X*/
Xstatic int warnlevel;
X
X/* nonzero once initial bunch of free blocks made */
Xstatic int	gotpool;
Xstatic long	poolsize;
X
X/* Cause reinitialization based on job parameters;
X  also declare where the end of pure storage is. */
Xmalloc_init (start)
X     char *start;
X{
X  register int	i;
X
X  data_space_start = start;
X  lim_data = 0L;
X  warnlevel = 0;
X  poolsize = 0;
X  for(i = 0; i < MIN_ALLOC; i++) poolsize += BUCKETSIZE(i);
X}
X
Xstatic	getpool();
X
Xstatic
Xmorecore (nu)			/* ask system for more memory */
X     register int nu;		/* size index to get more of  */
X{
X#ifndef __GNUC__
X  char *sbrk ();
X  char *lsbrk ();
X#endif
X  register char *cp;
X  register long siz;
X  register long block_size;
X  register long cur_size;
X
X  if (!data_space_start)
X    {
X#if defined(USG) && defined (emacs)
X      data_space_start = start_of_data ();
X#else /* not USG, or not Emacs */
X#ifdef atari
X#ifdef __GNUC__
X      malloc_init((char *) 8);	/* dummy */
X#else
X      data_space_start = sbrk(0);
X#endif
X#else
X      data_space_start = &etext;
X#endif atari
X#endif /* not USG, or not Emacs */
X    }
X
X#ifndef atari
X  if (lim_data == 0L)
X#endif
X    get_lim_data ();
X
X /* On initial startup, get two blocks of each size up to 1k bytes */
X  if (!gotpool) {
X    getpool (), getpool (), gotpool = 1;
X  }
X
X /* Find current end of memory and issue warning if getting near max */
X
X  malloc_sbrk_unused = lim_data - malloc_sbrk_used;
X
X  switch (warnlevel)
X    {
X    case 0: 
X      if (malloc_sbrk_used > (lim_data / 4) * 3)
X	{
X	  warnlevel++;
X	  malloc_warning ("\nWarning: past 75% of memory limit\n");
X	}
X      break;
X    case 1: 
X      if (malloc_sbrk_used > (lim_data / 20) * 17)
X	{
X	  warnlevel++;
X	  malloc_warning ("\nWarning: past 85% of memory limit\n");
X	}
X      break;
X    case 2: 
X      if (malloc_sbrk_used > (lim_data / 20) * 19)
X	{
X	  warnlevel++;
X	  malloc_warning ("\nWarning: past 95% of memory limit\n");
X	}
X      break;
X    }
X
X  if(nextf[nu]) return;	/* perhaps we got what we wanted in getpool() */
X
X#ifdef ALLIGN_ON_K_BOUNDARY
X  cp = sbrk(0);
X  if ((int) cp & 0x3ff)	/* land on 1K boundaries */
X    sbrk (1024 - ((int) cp & 0x3ff));
X#endif /* ALLIGN_ON_K_BOUNDARY */
X
X /* Take at least 2k, and figure out how many blocks of the desired size
X    we're about to get */
X  for(siz = nu; siz <= MIN_ALLOC; siz++) {
X    if(nextf[siz]) {
X      cp = (char *) nextf[siz];
X      nextf[siz] = CHAIN(cp);
X      cur_size = BUCKETSIZE(siz);
X      goto insert_block;
X    }
X  }
X
X#ifdef __GNUC__
X  if (!(cp = (char *) Malloc (cur_size = BUCKETSIZE(siz))))
X#else
X  if ((cp = lsbrk (cur_size = BUCKETSIZE(siz))) == (char *) -1)
X#endif
X    return;			/* no more room! */
X  malloc_sbrk_used += BUCKETSIZE(siz);
X
X /* save new header and link the blocks together */
Xinsert_block:
X  block_size = BUCKETSIZE(nu);
X  while (siz >= 0)
X    {
X      CHAIN ((struct mhead *) cp) = (struct mhead *) nextf[nu];
X      nextf[nu] = (struct mhead *) cp;
X      ((struct mhead *) cp) -> mh_alloc = ISFREE;
X      ((struct mhead *) cp) -> mh_index = nu;
X      if((cur_size -= block_size) <= 0) break;
X      cp += block_size;
X      while((block_size = BUCKETSIZE(siz)) > cur_size) if(!(siz--)) break;
X      nu = siz;
X    }
X}
X
Xstatic
Xgetpool ()
X{
X  register int nu;
X  register char *cp;
X
X#ifdef ALLIGN_ON_K_BOUNDARY
X  cp = sbrk (0);
X  if ((int) cp & 0x3ff)	/* land on 1K boundaries */
X    sbrk (1024 - ((int) cp & 0x3ff));
X#endif /* ALLIGN_ON_K_BOUNDARY */
X
X  /* Get 2k of storage */
X
X  cp = sbrk (poolsize);
X#ifdef __GNUC__
X  if (!cp)
X#else
X  if (cp == (char *) -1)
X#endif
X    return;
X  malloc_sbrk_used += poolsize;
X
X  /* Divide it into an initial 8-word block
X     plus one block of size 2**nu for nu = 3 ... 10.  */
X
X#ifdef notdef
X  CHAIN (cp) = nextf[0];
X  nextf[0] = (struct mhead *) cp;
X  ((struct mhead *) cp) -> mh_alloc = ISFREE;
X  ((struct mhead *) cp) -> mh_index = 0;
X  cp += 8;
X#endif
X
X  for (nu = 0; nu < MIN_ALLOC; nu++)
X    {
X      CHAIN (cp) = nextf[nu];
X      nextf[nu] = (struct mhead *) cp;
X      ((struct mhead *) cp) -> mh_alloc = ISFREE;
X      ((struct mhead *) cp) -> mh_index = nu;
X      cp += BUCKETSIZE(nu);
X    }
X}
X
Xchar *
Xgnu_malloc (n)		/* get a block */
X     unsigned long n;
X{
X  register struct mhead *p;
X  register unsigned long nbytes;
X  register int nunits = 0;
X
X  /* Figure out how many bytes are required, rounding up to the nearest
X     multiple of 4, then figure out which nextf[] area to use */
X  nbytes = (n + sizeof *p + EXTRA + 3) & ~3L;
X  while(BUCKETSIZE(nunits) < nbytes) nunits++;
X
X  /* If there are no blocks of the appropriate size, go get some */
X  /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
X  /* Below MIN_ALLOC morecore() splits up a larger block if there is one. */
X  /* If this can't be done, a block of BUCKETSIZE(MIN_ALLOC) is Malloc()'ed */
X  /* from the system. Above MIN_ALLOC we allocate the memory from the system */
X  /* and only if this fails we try to use a larger block (not splitting up). */
X  if (nextf[nunits] == 0L) {
X    morecore (nunits);
X    if (nextf[nunits] == 0L) {
X      /* try to find a bigger block to use */
X      register int	i;
X      for (i = nunits + 1; i < BUCKETS; i++) if(nextf[i]) break;
X      if(i < BUCKETS) nunits = i;
X    }
X  }
X
X  /* Get one block off the list, and set the new list head */
X  if ((p = nextf[nunits]) == 0L) {
X#ifdef MSTATS
X      printf("\nmalloc(%ld) [bucket #%d] failed with %ld bytes free (%d%% of %ld)\n",
X		nbytes,nunits,Malloc(-1L),(int)(malloc_sbrk_unused * 100 / lim_data),
X		lim_data);
X      printf("nextf @ %06lx\n",nextf);
X      memory_statistic();
X      abort();
X#endif /* MSTATS */
X      return 0L;
X    }
X  nextf[nunits] = CHAIN (p);
X
X  /* Check for free block clobbered */
X  /* If not for this check, we would gobble a clobbered free chain ptr */
X  /* and bomb out on the NEXT allocate of this size block */
X  if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
X#ifdef rcheck
X    botch ("block on free list clobbered");
X#else /* not rcheck */
X    abort ();
X#endif /* not rcheck */
X
X  /* Fill in the info, and if range checking, set up the magic numbers */
X  p -> mh_alloc = ISALLOC;
X#ifdef rcheck
X  p -> mh_nbytes = n;
X  p -> mh_magic4 = MAGIC4;
X  {
X    register char  *m = (char *) (p + 1) + n;
X
X    *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
X  }
X#else /* not rcheck */
X  p -> mh_size = n;
X#endif /* not rcheck */
X#ifdef MSTATS
X  nmalloc[nunits]++;
X  if(nmaxalloc[nunits] < nmalloc[nunits]) nmaxalloc[nunits] = nmalloc[nunits];
X  nmal++;
X#endif /* MSTATS */
X  return (char *) (p + 1);
X}
X
Xgnu_free (mem)
X     char *mem;
X{
X  register struct mhead *p;
X  {
X    register char *ap = mem;
X
X    if (ap == 0L)
X      return;
X
X    p = (struct mhead *) ap;
X    p--;
X    if (p -> mh_alloc == ISMEMALIGN)
X      {
X	ap -= p->mh_size;
X	p = (struct mhead *) ap;
X	p--;
X      }
X
X    if (p -> mh_alloc != ISALLOC)
X      abort ();
X
X#ifdef rcheck
X    ASSERT (p -> mh_magic4 == MAGIC4);
X    ap += p -> mh_nbytes;
X    ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
X    ASSERT (*ap++ == MAGIC1); ASSERT (*ap   == MAGIC1);
X#endif /* rcheck */
X  }
X  {
X    register int nunits = p -> mh_index;
X
X    ASSERT (nunits < BUCKETS);
X    p -> mh_alloc = ISFREE;
X    CHAIN (p) = nextf[nunits];
X    nextf[nunits] = p;
X#ifdef MSTATS
X    nmalloc[nunits]--;
X    nfre++;
X#endif /* MSTATS */
X  }
X}
X
Xchar *
Xgnu_realloc (mem, n)
X     char *mem;
X     register unsigned long n;
X{
X  register struct mhead *p;
X  register unsigned long tocopy;
X  register long nbytes;
X  register int nunits;
X
X  if ((p = (struct mhead *) mem) == (struct mhead *) 0)
X    return gnu_malloc (n);
X  p--;
X  nunits = p -> mh_index;
X  ASSERT (p -> mh_alloc == ISALLOC);
X#ifdef rcheck
X  ASSERT (p -> mh_magic4 == MAGIC4);
X  {
X    register char *m = mem + (tocopy = p -> mh_nbytes);
X#ifdef TRACE
X    if(*m != MAGIC1) {
X      printf("m = %08lX\tp = %08lX\n",m,p);
X      printf("p->mh_alloc\t= %02x\n      index\t= %02x\n      size\t= %d\n      nbytes\t= %ld\n      magic\t= %08X\n",
X	      (int) p->mh_alloc,(int) p->mh_index,p->mh_size,p->mh_nbytes,p->mh_magic4);
X      printf("m[0] = %02x\tm[1] = %02x\tm[2] = %02x\tm[3] = %02x\n",(int)m[0],
X		(int)m[1],(int)m[2],(int)m[3]);
X    }
X#endif /* TRACE */
X    ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
X    ASSERT (*m++ == MAGIC1); ASSERT (*m   == MAGIC1);
X  }
X#else /* not rcheck */
X  if (p -> mh_index >= 13)
X    tocopy = BUCKETSIZE(p -> mh_index) - sizeof *p;
X  else
X    tocopy = p -> mh_size;
X#endif /* not rcheck */
X
X  /* See if desired size rounds to same power of 2 as actual size. */
X  nbytes = (n + sizeof *p + EXTRA + 7) & ~7L;
X
X  /* If ok, use the same block, just marking its size as changed.  */
X  if (nbytes > BUCKETSIZE(nunits-1) && nbytes <= BUCKETSIZE(nunits))
X    {
X#ifdef rcheck
X      register char *m = mem + tocopy;
X      *m++ = 0;  *m++ = 0;  *m++ = 0;  *m++ = 0;
X      p-> mh_nbytes = n;
X      m = mem + n;
X      *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;
X#else /* not rcheck */
X      p -> mh_size = n;
X#endif /* not rcheck */
X      return mem;
X    }
X
X  if (n < tocopy)
X    tocopy = n;
X  {
X    register char *new;
X
X    if ((new = gnu_malloc (n)) == 0L) {
X#ifdef MSTATS
X      printf("realloc failing with %ld bytes of free memory\n",Malloc(-1L));
X      memory_statistic();
X#endif /* MSTATS */
X      return 0L;
X    }
X    gnu_bcopy (mem, new, tocopy);
X    gnu_free (mem);
X    return new;
X  }
X}
X
Xchar *
Xmemalign (alignment, size)
X     unsigned alignment;
X     unsigned long size;
X{
X  register char *ptr = gnu_malloc (size + alignment);
X  register char *aligned;
X  register struct mhead *p;
X
X  if (ptr == 0L)
X    return 0L;
X  /* If entire block has the desired alignment, just accept it.  */
X  if (((long) ptr & (alignment - 1)) == 0L)
X    return ptr;
X  /* Otherwise, get address of byte in the block that has that alignment.  */
X  aligned = (char *) (((long) ptr + alignment - 1) & -alignment);
X
X  /* Store a suitable indication of how to free the block,
X     so that free can find the true beginning of it.  */
X  p = (struct mhead *) aligned;
X  p--;
X  p -> mh_size = aligned - ptr;
X  p -> mh_alloc = ISMEMALIGN;
X  return aligned;
X}
X
X#ifndef HPUX
X/* This runs into trouble with getpagesize on HPUX.
X   Patching out seems cleaner than the ugly fix needed.  */
Xchar *
Xvalloc (size)
Xunsigned long	size;
X{
X  return memalign (getpagesize (), size);
X}
X#endif /* not HPUX */
X
X#ifdef MSTATS
X/* Return statistics describing allocation of blocks of size 2**n. */
X
Xstruct mstats_value
X  {
X    long	blocksize;
X    int		nfree;
X    int		nused;
X    int		nmaxused;
X  };
X
Xstruct mstats_value
Xmalloc_stats (size)
X     int size;
X{
X  struct mstats_value v;
X  /* register int i; */
X  register struct mhead *p;
X
X  v.nfree = 0;
X
X  if (size < 0 || size >= BUCKETS)
X    {
X      v.blocksize = 0L;
X      if(size = -1) {
X	v.nused = v.nmaxused = nmal;
X	v.nfree = nfre;
X      } else v.nused = 0;
X      return v;
X    }
X
X  v.blocksize = BUCKETSIZE(size);
X  v.nused = nmalloc[size];
X  v.nmaxused = nmaxalloc[size];
X
X  for (p = nextf[size]; p; p = CHAIN (p))
X    if (p -> mh_alloc != ISFREE || p -> mh_index != size) {
X      v.nfree = -v.nfree;
X      break;
X    } else {
X      v.nfree++;
X    }
X
X  return v;
X}
X
Xmemory_statistic()
X{
X	int	i;
X	struct mstats_value	mv;
X
X	printf("statistics about allocated blocks of memory\n");
X	printf("used %6ld\tunused %ld\tfree %ld bytes",
X		malloc_sbrk_used,malloc_sbrk_unused,Malloc(-1L));
X	printf(" (%d%% of %ld)\n",(int)(malloc_sbrk_unused * 100 / lim_data),
X		lim_data);
X	mv = malloc_stats(-1);
X	printf("%d malloc\t%d free\n",mv.nused,mv.nfree);
X	printf("bucket\tblocksize\t# of used blocks (max)\t# of free blocks\n");
X	for(i = 0; i < BUCKETS; i++) {
X		mv = malloc_stats(i);
X		if(mv.nfree || mv.nused)
X			printf("%3d\t%7ld\t\t\t%4d (%d)\t\t%4d\n",
X				i,mv.blocksize,mv.nused,mv.nmaxused,mv.nfree);
X	}
X}
X#endif /* MSTATS */
X
X/*
X *	This function returns the total number of bytes that the process
X *	will be allowed to allocate via the sbrk(2) system call.  On
X *	BSD systems this is the total space allocatable to stack and
X *	data.  On USG systems this is the data space only.
X */
X
X#ifdef atari
Xget_lim_data ()
X{
X	long	ld;
X
X	if((ld = Malloc(-1L)) > malloc_sbrk_unused)
X		lim_data += ld - malloc_sbrk_unused;
X}
X
XBconws(str)
Xchar	*str;
X{
X	while(*str) {
X		if(*str == '\n') Bconout(2,'\r');
X		Bconout(2,*str++);
X	}
X}
X#else /* not Atari ST */
X#ifdef USG
X
Xget_lim_data ()
X{
X  extern long ulimit ();
X    
X  lim_data = ulimit (3, 0);
X  lim_data -= (long) data_space_start;
X}
X
X#else /* not USG */
X#ifndef BSD42
X
Xget_lim_data ()
X{
X  lim_data = vlimit (LIM_DATA, -1);
X}
X
X#else /* BSD42 */
X
Xget_lim_data ()
X{
X  struct rlimit XXrlimit;
X
X  getrlimit (RLIMIT_DATA, &XXrlimit);
X  lim_data = XXrlimit.rlim_cur;		/* soft limit */
X}
X
X#endif /* BSD42 */
X#endif /* not USG */
X#endif /* Atari ST */
END_OF_FILE
if test 21197 -ne `wc -c <'malloc.c'`; then
    echo shar: \"'malloc.c'\" unpacked with wrong size!
fi
# end of 'malloc.c'
fi
echo shar: End of archive 7 \(of 7\).
cp /dev/null ark7isdone
MISSING=""
for I in 1 2 3 4 5 6 7 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 7 archives.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0