Home | History | Annotate | Line # | Download | only in csh
alloc.c revision 1.2
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1983, 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)alloc.c	5.8 (Berkeley) 6/8/91";
     36  1.2  cgd static char rcsid[] = "$Id: alloc.c,v 1.2 1993/03/22 08:04:00 cgd Exp $";
     37  1.1  cgd #endif /* not lint */
     38  1.1  cgd 
     39  1.1  cgd /*
     40  1.1  cgd  * tc.alloc.c from malloc.c (Caltech) 2/21/82
     41  1.1  cgd  * Chris Kingsley, kingsley@cit-20.
     42  1.1  cgd  *
     43  1.1  cgd  * This is a very fast storage allocator.  It allocates blocks of a small
     44  1.1  cgd  * number of different sizes, and keeps free lists of each size.  Blocks that
     45  1.1  cgd  * don't exactly fit are passed up to the next larger size.  In this
     46  1.1  cgd  * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
     47  1.1  cgd  * This is designed for use in a program that uses vast quantities of memory,
     48  1.1  cgd  * but bombs when it runs out.
     49  1.1  cgd  */
     50  1.1  cgd 
     51  1.1  cgd #include <sys/types.h>
     52  1.1  cgd #include <unistd.h>
     53  1.1  cgd #include <string.h>
     54  1.1  cgd #if __STDC__
     55  1.1  cgd # include <stdarg.h>
     56  1.1  cgd #else
     57  1.1  cgd # include <varargs.h>
     58  1.1  cgd #endif
     59  1.1  cgd 
     60  1.1  cgd #include "csh.h"
     61  1.1  cgd #include "extern.h"
     62  1.1  cgd 
     63  1.1  cgd char   *memtop = NULL;		/* PWP: top of current memory */
     64  1.1  cgd char   *membot = NULL;		/* PWP: bottom of allocatable memory */
     65  1.1  cgd 
     66  1.1  cgd #ifndef SYSMALLOC
     67  1.1  cgd 
     68  1.1  cgd #undef RCHECK
     69  1.1  cgd #undef DEBUG
     70  1.1  cgd 
     71  1.1  cgd 
     72  1.1  cgd #ifndef NULL
     73  1.1  cgd #define	NULL 0
     74  1.1  cgd #endif
     75  1.1  cgd 
     76  1.1  cgd 
     77  1.1  cgd /*
     78  1.1  cgd  * The overhead on a block is at least 4 bytes.  When free, this space
     79  1.1  cgd  * contains a pointer to the next free block, and the bottom two bits must
     80  1.1  cgd  * be zero.  When in use, the first byte is set to MAGIC, and the second
     81  1.1  cgd  * byte is the size index.  The remaining bytes are for alignment.
     82  1.1  cgd  * If range checking is enabled and the size of the block fits
     83  1.1  cgd  * in two bytes, then the top two bytes hold the size of the requested block
     84  1.1  cgd  * plus the range checking words, and the header word MINUS ONE.
     85  1.1  cgd  */
     86  1.1  cgd 
     87  1.1  cgd #define ROUNDUP	7
     88  1.1  cgd 
     89  1.1  cgd #define ALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP)
     90  1.1  cgd 
     91  1.1  cgd union overhead {
     92  1.1  cgd     union overhead *ov_next;	/* when free */
     93  1.1  cgd     struct {
     94  1.1  cgd 	u_char  ovu_magic;	/* magic number */
     95  1.1  cgd 	u_char  ovu_index;	/* bucket # */
     96  1.1  cgd #ifdef RCHECK
     97  1.1  cgd 	u_short ovu_size;	/* actual block size */
     98  1.1  cgd 	u_int   ovu_rmagic;	/* range magic number */
     99  1.1  cgd #endif
    100  1.1  cgd     }       ovu;
    101  1.1  cgd #define	ov_magic	ovu.ovu_magic
    102  1.1  cgd #define	ov_index	ovu.ovu_index
    103  1.1  cgd #define	ov_size		ovu.ovu_size
    104  1.1  cgd #define	ov_rmagic	ovu.ovu_rmagic
    105  1.1  cgd };
    106  1.1  cgd 
    107  1.1  cgd #define	MAGIC		0xfd	/* magic # on accounting info */
    108  1.1  cgd #define RMAGIC		0x55555555	/* magic # on range info */
    109  1.1  cgd #ifdef RCHECK
    110  1.1  cgd #define	RSLOP		sizeof (u_int)
    111  1.1  cgd #else
    112  1.1  cgd #define	RSLOP		0
    113  1.1  cgd #endif
    114  1.1  cgd 
    115  1.1  cgd /*
    116  1.1  cgd  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
    117  1.1  cgd  * smallest allocatable block is 8 bytes.  The overhead information
    118  1.1  cgd  * precedes the data area returned to the user.
    119  1.1  cgd  */
    120  1.1  cgd #define	NBUCKETS 30
    121  1.1  cgd static union overhead *nextf[NBUCKETS];
    122  1.1  cgd 
    123  1.1  cgd static int	findbucket __P((union overhead *, int));
    124  1.1  cgd static void	morecore __P((int));
    125  1.1  cgd 
    126  1.1  cgd /*
    127  1.1  cgd  * nmalloc[i] is the difference between the number of mallocs and frees
    128  1.1  cgd  * for a given block size.
    129  1.1  cgd  */
    130  1.1  cgd static u_int nmalloc[NBUCKETS];
    131  1.1  cgd 
    132  1.1  cgd 
    133  1.1  cgd #ifdef DEBUG
    134  1.1  cgd #define CHECK(a, str, p) \
    135  1.1  cgd     if (a) { \
    136  1.1  cgd 	xprintf(str, p);	\
    137  1.1  cgd 	xprintf("memtop = %lx membot = %lx.\n", memtop, membot);	\
    138  1.1  cgd 	abort(); \
    139  1.1  cgd     }	\
    140  1.1  cgd     else
    141  1.1  cgd #else
    142  1.1  cgd #define CHECK(a, str, p) \
    143  1.1  cgd     if (a) { \
    144  1.1  cgd 	xprintf(str, p);	\
    145  1.1  cgd 	xprintf("memtop = %lx membot = %lx.\n", memtop, membot);	\
    146  1.1  cgd 	return; \
    147  1.1  cgd     }	\
    148  1.1  cgd     else
    149  1.1  cgd #endif
    150  1.1  cgd 
    151  1.1  cgd ptr_t
    152  1.1  cgd malloc(nbytes)
    153  1.1  cgd     register size_t nbytes;
    154  1.1  cgd {
    155  1.1  cgd #ifndef lint
    156  1.1  cgd     register union overhead *p;
    157  1.1  cgd     register int bucket = 0;
    158  1.1  cgd     register unsigned shiftr;
    159  1.1  cgd 
    160  1.1  cgd     /*
    161  1.1  cgd      * Convert amount of memory requested into closest block size stored in
    162  1.1  cgd      * hash buckets which satisfies request.  Account for space used per block
    163  1.1  cgd      * for accounting.
    164  1.1  cgd      */
    165  1.1  cgd     nbytes = ALIGN(ALIGN(sizeof(union overhead)) + nbytes + RSLOP);
    166  1.1  cgd     shiftr = (nbytes - 1) >> 2;
    167  1.1  cgd 
    168  1.1  cgd     /* apart from this loop, this is O(1) */
    169  1.1  cgd     while (shiftr >>= 1)
    170  1.1  cgd 	bucket++;
    171  1.1  cgd     /*
    172  1.1  cgd      * If nothing in hash bucket right now, request more memory from the
    173  1.1  cgd      * system.
    174  1.1  cgd      */
    175  1.1  cgd     if (nextf[bucket] == NULL)
    176  1.1  cgd 	morecore(bucket);
    177  1.1  cgd     if ((p = (union overhead *) nextf[bucket]) == NULL) {
    178  1.1  cgd 	child++;
    179  1.1  cgd #ifndef DEBUG
    180  1.1  cgd 	stderror(ERR_NOMEM);
    181  1.1  cgd #else
    182  1.1  cgd 	showall();
    183  1.1  cgd 	xprintf("nbytes=%d: Out of memory\n", nbytes);
    184  1.1  cgd 	abort();
    185  1.1  cgd #endif
    186  1.1  cgd 	/* fool lint */
    187  1.1  cgd 	return ((ptr_t) 0);
    188  1.1  cgd     }
    189  1.1  cgd     /* remove from linked list */
    190  1.1  cgd     nextf[bucket] = nextf[bucket]->ov_next;
    191  1.1  cgd     p->ov_magic = MAGIC;
    192  1.1  cgd     p->ov_index = bucket;
    193  1.1  cgd     nmalloc[bucket]++;
    194  1.1  cgd #ifdef RCHECK
    195  1.1  cgd     /*
    196  1.1  cgd      * Record allocated size of block and bound space with magic numbers.
    197  1.1  cgd      */
    198  1.1  cgd     if (nbytes <= 0x10000)
    199  1.1  cgd 	p->ov_size = nbytes - 1;
    200  1.1  cgd     p->ov_rmagic = RMAGIC;
    201  1.1  cgd     *((u_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC;
    202  1.1  cgd #endif
    203  1.1  cgd     return ((ptr_t) (((caddr_t) p) + ALIGN(sizeof(union overhead))));
    204  1.1  cgd #else
    205  1.1  cgd     if (nbytes)
    206  1.1  cgd 	return ((ptr_t) 0);
    207  1.1  cgd     else
    208  1.1  cgd 	return ((ptr_t) 0);
    209  1.1  cgd #endif				/* !lint */
    210  1.1  cgd }
    211  1.1  cgd 
    212  1.1  cgd #ifndef lint
    213  1.1  cgd /*
    214  1.1  cgd  * Allocate more memory to the indicated bucket.
    215  1.1  cgd  */
    216  1.1  cgd static void
    217  1.1  cgd morecore(bucket)
    218  1.1  cgd     register int bucket;
    219  1.1  cgd {
    220  1.1  cgd     register union overhead *op;
    221  1.1  cgd     register int rnu;		/* 2^rnu bytes will be requested */
    222  1.1  cgd     register int nblks;		/* become nblks blocks of the desired size */
    223  1.1  cgd     register int siz;
    224  1.1  cgd 
    225  1.1  cgd     if (nextf[bucket])
    226  1.1  cgd 	return;
    227  1.1  cgd     /*
    228  1.1  cgd      * Insure memory is allocated on a page boundary.  Should make getpageize
    229  1.1  cgd      * call?
    230  1.1  cgd      */
    231  1.1  cgd     op = (union overhead *) sbrk(0);
    232  1.1  cgd     memtop = (char *) op;
    233  1.1  cgd     if (membot == NULL)
    234  1.1  cgd 	membot = memtop;
    235  1.1  cgd     if ((int) op & 0x3ff) {
    236  1.1  cgd 	memtop = (char *) sbrk(1024 - ((int) op & 0x3ff));
    237  1.1  cgd 	memtop += 1024 - ((int) op & 0x3ff);
    238  1.1  cgd     }
    239  1.1  cgd 
    240  1.1  cgd     /* take 2k unless the block is bigger than that */
    241  1.1  cgd     rnu = (bucket <= 8) ? 11 : bucket + 3;
    242  1.1  cgd     nblks = 1 << (rnu - (bucket + 3));	/* how many blocks to get */
    243  1.1  cgd     if (rnu < bucket)
    244  1.1  cgd 	rnu = bucket;
    245  1.1  cgd     memtop = (char *) sbrk(1 << rnu);	/* PWP */
    246  1.1  cgd     op = (union overhead *) memtop;
    247  1.1  cgd     memtop += 1 << rnu;
    248  1.1  cgd     /* no more room! */
    249  1.1  cgd     if ((int) op == -1)
    250  1.1  cgd 	return;
    251  1.1  cgd     /*
    252  1.1  cgd      * Round up to minimum allocation size boundary and deduct from block count
    253  1.1  cgd      * to reflect.
    254  1.1  cgd      */
    255  1.1  cgd     if (((u_int) op) & ROUNDUP) {
    256  1.1  cgd 	op = (union overhead *) (((u_int) op + (ROUNDUP + 1)) & ~ROUNDUP);
    257  1.1  cgd 	nblks--;
    258  1.1  cgd     }
    259  1.1  cgd     /*
    260  1.1  cgd      * Add new memory allocated to that on free list for this hash bucket.
    261  1.1  cgd      */
    262  1.1  cgd     nextf[bucket] = op;
    263  1.1  cgd     siz = 1 << (bucket + 3);
    264  1.1  cgd     while (--nblks > 0) {
    265  1.1  cgd 	op->ov_next = (union overhead *) (((caddr_t) op) + siz);
    266  1.1  cgd 	op = (union overhead *) (((caddr_t) op) + siz);
    267  1.1  cgd     }
    268  1.1  cgd }
    269  1.1  cgd 
    270  1.1  cgd #endif
    271  1.1  cgd 
    272  1.1  cgd #ifdef sun
    273  1.1  cgd int
    274  1.1  cgd #else
    275  1.1  cgd void
    276  1.1  cgd #endif
    277  1.1  cgd free(cp)
    278  1.1  cgd     ptr_t   cp;
    279  1.1  cgd {
    280  1.1  cgd #ifndef lint
    281  1.1  cgd     register int size;
    282  1.1  cgd     register union overhead *op;
    283  1.1  cgd 
    284  1.1  cgd     if (cp == NULL)
    285  1.1  cgd 	return;
    286  1.1  cgd     CHECK(!memtop || !membot, "free(%lx) called before any allocations.", cp);
    287  1.1  cgd     CHECK(cp > (ptr_t) memtop, "free(%lx) above top of memory.", cp);
    288  1.1  cgd     CHECK(cp < (ptr_t) membot, "free(%lx) above top of memory.", cp);
    289  1.1  cgd     op = (union overhead *) (((caddr_t) cp) - ALIGN(sizeof(union overhead)));
    290  1.1  cgd     CHECK(op->ov_magic != MAGIC, "free(%lx) bad block.", cp);
    291  1.1  cgd 
    292  1.1  cgd #ifdef RCHECK
    293  1.1  cgd     if (op->ov_index <= 13)
    294  1.1  cgd 	CHECK(*(u_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC,
    295  1.1  cgd 	      "free(%lx) bad range check.", cp);
    296  1.1  cgd #endif
    297  1.1  cgd     CHECK(op->ov_index >= NBUCKETS, "free(%lx) bad block index.", cp);
    298  1.1  cgd     size = op->ov_index;
    299  1.1  cgd     op->ov_next = nextf[size];
    300  1.1  cgd     nextf[size] = op;
    301  1.1  cgd 
    302  1.1  cgd     nmalloc[size]--;
    303  1.1  cgd 
    304  1.1  cgd #else
    305  1.1  cgd     if (cp == NULL)
    306  1.1  cgd 	return;
    307  1.1  cgd #endif
    308  1.1  cgd }
    309  1.1  cgd 
    310  1.1  cgd ptr_t
    311  1.1  cgd calloc(i, j)
    312  1.1  cgd     size_t  i, j;
    313  1.1  cgd {
    314  1.1  cgd #ifndef lint
    315  1.1  cgd     register char *cp, *scp;
    316  1.1  cgd 
    317  1.1  cgd     i *= j;
    318  1.1  cgd     scp = cp = (char *) xmalloc((size_t) i);
    319  1.1  cgd     if (i != 0)
    320  1.1  cgd 	do
    321  1.1  cgd 	    *cp++ = 0;
    322  1.1  cgd 	while (--i);
    323  1.1  cgd 
    324  1.1  cgd     return (scp);
    325  1.1  cgd #else
    326  1.1  cgd     if (i && j)
    327  1.1  cgd 	return ((ptr_t) 0);
    328  1.1  cgd     else
    329  1.1  cgd 	return ((ptr_t) 0);
    330  1.1  cgd #endif
    331  1.1  cgd }
    332  1.1  cgd 
    333  1.1  cgd /*
    334  1.1  cgd  * When a program attempts "storage compaction" as mentioned in the
    335  1.1  cgd  * old malloc man page, it realloc's an already freed block.  Usually
    336  1.1  cgd  * this is the last block it freed; occasionally it might be farther
    337  1.1  cgd  * back.  We have to search all the free lists for the block in order
    338  1.1  cgd  * to determine its bucket: 1st we make one pass thru the lists
    339  1.1  cgd  * checking only the first block in each; if that fails we search
    340  1.1  cgd  * ``realloc_srchlen'' blocks in each list for a match (the variable
    341  1.1  cgd  * is extern so the caller can modify it).  If that fails we just copy
    342  1.1  cgd  * however many bytes was given to realloc() and hope it's not huge.
    343  1.1  cgd  */
    344  1.1  cgd #ifndef lint
    345  1.1  cgd int     realloc_srchlen = 4;	/* 4 should be plenty, -1 =>'s whole list */
    346  1.1  cgd 
    347  1.1  cgd #endif				/* lint */
    348  1.1  cgd 
    349  1.1  cgd ptr_t
    350  1.1  cgd realloc(cp, nbytes)
    351  1.1  cgd     ptr_t   cp;
    352  1.1  cgd     size_t  nbytes;
    353  1.1  cgd {
    354  1.1  cgd #ifndef lint
    355  1.1  cgd     register u_int onb;
    356  1.1  cgd     union overhead *op;
    357  1.1  cgd     char   *res;
    358  1.1  cgd     register int i;
    359  1.1  cgd     int     was_alloced = 0;
    360  1.1  cgd 
    361  1.1  cgd     if (cp == NULL)
    362  1.1  cgd 	return (malloc(nbytes));
    363  1.1  cgd     op = (union overhead *) (((caddr_t) cp) - ALIGN(sizeof(union overhead)));
    364  1.1  cgd     if (op->ov_magic == MAGIC) {
    365  1.1  cgd 	was_alloced++;
    366  1.1  cgd 	i = op->ov_index;
    367  1.1  cgd     }
    368  1.1  cgd     else
    369  1.1  cgd 	/*
    370  1.1  cgd 	 * Already free, doing "compaction".
    371  1.1  cgd 	 *
    372  1.1  cgd 	 * Search for the old block of memory on the free list.  First, check the
    373  1.1  cgd 	 * most common case (last element free'd), then (this failing) the last
    374  1.1  cgd 	 * ``realloc_srchlen'' items free'd. If all lookups fail, then assume
    375  1.1  cgd 	 * the size of the memory block being realloc'd is the smallest
    376  1.1  cgd 	 * possible.
    377  1.1  cgd 	 */
    378  1.1  cgd 	if ((i = findbucket(op, 1)) < 0 &&
    379  1.1  cgd 	    (i = findbucket(op, realloc_srchlen)) < 0)
    380  1.1  cgd 	i = 0;
    381  1.1  cgd 
    382  1.1  cgd     onb = ALIGN(nbytes + ALIGN(sizeof(union overhead)) + RSLOP);
    383  1.1  cgd 
    384  1.1  cgd     /* avoid the copy if same size block */
    385  1.1  cgd     if (was_alloced && (onb < (1 << (i + 3))) && (onb >= (1 << (i + 2))))
    386  1.1  cgd 	return ((ptr_t) cp);
    387  1.1  cgd     if ((res = malloc(nbytes)) == NULL)
    388  1.1  cgd 	return ((ptr_t) 0);
    389  1.1  cgd     if (cp != res)		/* common optimization */
    390  1.1  cgd 	bcopy(cp, res, nbytes);
    391  1.1  cgd     if (was_alloced)
    392  1.1  cgd 	free(cp);
    393  1.1  cgd     return (res);
    394  1.1  cgd #else
    395  1.1  cgd     if (cp && nbytes)
    396  1.1  cgd 	return ((ptr_t) 0);
    397  1.1  cgd     else
    398  1.1  cgd 	return ((ptr_t) 0);
    399  1.1  cgd #endif				/* !lint */
    400  1.1  cgd }
    401  1.1  cgd 
    402  1.1  cgd 
    403  1.1  cgd 
    404  1.1  cgd #ifndef lint
    405  1.1  cgd /*
    406  1.1  cgd  * Search ``srchlen'' elements of each free list for a block whose
    407  1.1  cgd  * header starts at ``freep''.  If srchlen is -1 search the whole list.
    408  1.1  cgd  * Return bucket number, or -1 if not found.
    409  1.1  cgd  */
    410  1.1  cgd static int
    411  1.1  cgd findbucket(freep, srchlen)
    412  1.1  cgd     union overhead *freep;
    413  1.1  cgd     int     srchlen;
    414  1.1  cgd {
    415  1.1  cgd     register union overhead *p;
    416  1.1  cgd     register int i, j;
    417  1.1  cgd 
    418  1.1  cgd     for (i = 0; i < NBUCKETS; i++) {
    419  1.1  cgd 	j = 0;
    420  1.1  cgd 	for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
    421  1.1  cgd 	    if (p == freep)
    422  1.1  cgd 		return (i);
    423  1.1  cgd 	    j++;
    424  1.1  cgd 	}
    425  1.1  cgd     }
    426  1.1  cgd     return (-1);
    427  1.1  cgd }
    428  1.1  cgd 
    429  1.1  cgd #endif
    430  1.1  cgd 
    431  1.1  cgd 
    432  1.1  cgd #else				/* SYSMALLOC */
    433  1.1  cgd 
    434  1.1  cgd /**
    435  1.1  cgd  ** ``Protected versions'' of malloc, realloc, calloc, and free
    436  1.1  cgd  **
    437  1.1  cgd  ** On many systems:
    438  1.1  cgd  **
    439  1.1  cgd  ** 1. malloc(0) is bad
    440  1.1  cgd  ** 2. free(0) is bad
    441  1.1  cgd  ** 3. realloc(0, n) is bad
    442  1.1  cgd  ** 4. realloc(n, 0) is bad
    443  1.1  cgd  **
    444  1.1  cgd  ** Also we call our error routine if we run out of memory.
    445  1.1  cgd  **/
    446  1.1  cgd char   *
    447  1.1  cgd Malloc(n)
    448  1.1  cgd     size_t  n;
    449  1.1  cgd {
    450  1.1  cgd     ptr_t   ptr;
    451  1.1  cgd 
    452  1.1  cgd     n = n ? n : 1;
    453  1.1  cgd 
    454  1.1  cgd     if ((ptr = malloc(n)) == (ptr_t) 0) {
    455  1.1  cgd 	child++;
    456  1.1  cgd 	stderror(ERR_NOMEM);
    457  1.1  cgd     }
    458  1.1  cgd     return ((char *) ptr);
    459  1.1  cgd }
    460  1.1  cgd 
    461  1.1  cgd char   *
    462  1.1  cgd Realloc(p, n)
    463  1.1  cgd     ptr_t   p;
    464  1.1  cgd     size_t  n;
    465  1.1  cgd {
    466  1.1  cgd     ptr_t   ptr;
    467  1.1  cgd 
    468  1.1  cgd     n = n ? n : 1;
    469  1.1  cgd     if ((ptr = (p ? realloc(p, n) : malloc(n))) == (ptr_t) 0) {
    470  1.1  cgd 	child++;
    471  1.1  cgd 	stderror(ERR_NOMEM);
    472  1.1  cgd     }
    473  1.1  cgd     return ((char *) ptr);
    474  1.1  cgd }
    475  1.1  cgd 
    476  1.1  cgd char   *
    477  1.1  cgd Calloc(s, n)
    478  1.1  cgd     size_t  s, n;
    479  1.1  cgd {
    480  1.1  cgd     char   *sptr;
    481  1.1  cgd     ptr_t   ptr;
    482  1.1  cgd 
    483  1.1  cgd     n *= s;
    484  1.1  cgd     n = n ? n : 1;
    485  1.1  cgd     if ((ptr = malloc(n)) == (ptr_t) 0) {
    486  1.1  cgd 	child++;
    487  1.1  cgd 	stderror(ERR_NOMEM);
    488  1.1  cgd     }
    489  1.1  cgd 
    490  1.1  cgd     sptr = (char *) ptr;
    491  1.1  cgd     if (n != 0)
    492  1.1  cgd 	do
    493  1.1  cgd 	    *sptr++ = 0;
    494  1.1  cgd 	while (--n);
    495  1.1  cgd 
    496  1.1  cgd     return ((char *) ptr);
    497  1.1  cgd }
    498  1.1  cgd 
    499  1.1  cgd void
    500  1.1  cgd Free(p)
    501  1.1  cgd     ptr_t   p;
    502  1.1  cgd {
    503  1.1  cgd     if (p)
    504  1.1  cgd 	free(p);
    505  1.1  cgd }
    506  1.1  cgd 
    507  1.1  cgd #endif				/* SYSMALLOC */
    508  1.1  cgd 
    509  1.1  cgd /*
    510  1.1  cgd  * mstats - print out statistics about malloc
    511  1.1  cgd  *
    512  1.1  cgd  * Prints two lines of numbers, one showing the length of the free list
    513  1.1  cgd  * for each size category, the second showing the number of mallocs -
    514  1.1  cgd  * frees for each size category.
    515  1.1  cgd  */
    516  1.1  cgd void
    517  1.1  cgd showall()
    518  1.1  cgd {
    519  1.1  cgd #ifndef SYSMALLOC
    520  1.1  cgd     register int i, j;
    521  1.1  cgd     register union overhead *p;
    522  1.1  cgd     int     totfree = 0, totused = 0;
    523  1.1  cgd 
    524  1.1  cgd     xprintf("csh current memory allocation:\nfree:\t");
    525  1.1  cgd     for (i = 0; i < NBUCKETS; i++) {
    526  1.1  cgd 	for (j = 0, p = nextf[i]; p; p = p->ov_next, j++);
    527  1.1  cgd 	xprintf(" %4d", j);
    528  1.1  cgd 	totfree += j * (1 << (i + 3));
    529  1.1  cgd     }
    530  1.1  cgd     xprintf("\nused:\t");
    531  1.1  cgd     for (i = 0; i < NBUCKETS; i++) {
    532  1.1  cgd 	xprintf(" %4d", nmalloc[i]);
    533  1.1  cgd 	totused += nmalloc[i] * (1 << (i + 3));
    534  1.1  cgd     }
    535  1.1  cgd     xprintf("\n\tTotal in use: %d, total free: %d\n",
    536  1.1  cgd 	    totused, totfree);
    537  1.1  cgd     xprintf("\tAllocated memory from 0x%lx to 0x%lx.  Real top at 0x%lx\n",
    538  1.1  cgd 	    membot, memtop, (char *) sbrk(0));
    539  1.1  cgd #else
    540  1.1  cgd     xprintf("Allocated memory from 0x%lx to 0x%lx (%ld).\n",
    541  1.1  cgd 	    membot, memtop = (char *) sbrk(0), memtop - membot);
    542  1.1  cgd #endif				/* SYSMALLOC */
    543  1.1  cgd }
    544