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