xref: /csrg-svn/lib/libc/stdlib/malloc.c (revision 61180)
121347Sdist /*
2*61180Sbostic  * Copyright (c) 1983, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
435329Sbostic  *
542634Sbostic  * %sccs.include.redist.c%
621347Sdist  */
721347Sdist 
826569Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61180Sbostic static char sccsid[] = "@(#)malloc.c	8.1 (Berkeley) 06/04/93";
1035329Sbostic #endif /* LIBC_SCCS and not lint */
1114953Skarels 
1214953Skarels /*
1314953Skarels  * malloc.c (Caltech) 2/21/82
1414953Skarels  * Chris Kingsley, kingsley@cit-20.
1514953Skarels  *
1614953Skarels  * This is a very fast storage allocator.  It allocates blocks of a small
1714953Skarels  * number of different sizes, and keeps free lists of each size.  Blocks that
1814953Skarels  * don't exactly fit are passed up to the next larger size.  In this
1926410Slepreau  * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
2026410Slepreau  * This is designed for use in a virtual memory environment.
2114953Skarels  */
2214953Skarels 
2314953Skarels #include <sys/types.h>
2446599Sdonn #include <stdlib.h>
2546599Sdonn #include <string.h>
2646599Sdonn #include <unistd.h>
2714953Skarels 
2814953Skarels #define	NULL 0
2914953Skarels 
3046599Sdonn static void morecore();
3146599Sdonn static int findbucket();
3246599Sdonn 
3314953Skarels /*
3414953Skarels  * The overhead on a block is at least 4 bytes.  When free, this space
3514953Skarels  * contains a pointer to the next free block, and the bottom two bits must
3614953Skarels  * be zero.  When in use, the first byte is set to MAGIC, and the second
3714953Skarels  * byte is the size index.  The remaining bytes are for alignment.
3826410Slepreau  * If range checking is enabled then a second word holds the size of the
3926410Slepreau  * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
4026410Slepreau  * The order of elements is critical: ov_magic must overlay the low order
4126410Slepreau  * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
4214953Skarels  */
4314953Skarels union	overhead {
4414953Skarels 	union	overhead *ov_next;	/* when free */
4514953Skarels 	struct {
4614953Skarels 		u_char	ovu_magic;	/* magic number */
4714953Skarels 		u_char	ovu_index;	/* bucket # */
4826410Slepreau #ifdef RCHECK
4926410Slepreau 		u_short	ovu_rmagic;	/* range magic number */
5017333Sralph 		u_int	ovu_size;	/* actual block size */
5114953Skarels #endif
5214953Skarels 	} ovu;
5314953Skarels #define	ov_magic	ovu.ovu_magic
5414953Skarels #define	ov_index	ovu.ovu_index
5517333Sralph #define	ov_rmagic	ovu.ovu_rmagic
5614953Skarels #define	ov_size		ovu.ovu_size
5714953Skarels };
5814953Skarels 
5917333Sralph #define	MAGIC		0xef		/* magic # on accounting info */
6017333Sralph #define RMAGIC		0x5555		/* magic # on range info */
6117333Sralph 
6214953Skarels #ifdef RCHECK
6317333Sralph #define	RSLOP		sizeof (u_short)
6414953Skarels #else
6514953Skarels #define	RSLOP		0
6614953Skarels #endif
6714953Skarels 
6814953Skarels /*
6914953Skarels  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
7014953Skarels  * smallest allocatable block is 8 bytes.  The overhead information
7114953Skarels  * precedes the data area returned to the user.
7214953Skarels  */
7314953Skarels #define	NBUCKETS 30
7414953Skarels static	union overhead *nextf[NBUCKETS];
7514953Skarels extern	char *sbrk();
7614953Skarels 
7717333Sralph static	int pagesz;			/* page size */
7817333Sralph static	int pagebucket;			/* page size bucket */
7917333Sralph 
8014953Skarels #ifdef MSTATS
8114953Skarels /*
8214953Skarels  * nmalloc[i] is the difference between the number of mallocs and frees
8314953Skarels  * for a given block size.
8414953Skarels  */
8514953Skarels static	u_int nmalloc[NBUCKETS];
8614953Skarels #include <stdio.h>
8714953Skarels #endif
8814953Skarels 
8925799Slepreau #if defined(DEBUG) || defined(RCHECK)
9017333Sralph #define	ASSERT(p)   if (!(p)) botch("p")
9125799Slepreau #include <stdio.h>
9214953Skarels static
botch(s)9313259Sroot botch(s)
9415003Ssam 	char *s;
9513259Sroot {
9625799Slepreau 	fprintf(stderr, "\r\nassertion botched: %s\r\n", s);
9725799Slepreau  	(void) fflush(stderr);		/* just in case user buffered it */
9813259Sroot 	abort();
9913259Sroot }
10013259Sroot #else
10114953Skarels #define	ASSERT(p)
10213259Sroot #endif
10313259Sroot 
10446599Sdonn void *
malloc(nbytes)10513259Sroot malloc(nbytes)
10646599Sdonn 	size_t nbytes;
10713259Sroot {
10817333Sralph   	register union overhead *op;
10939839Smckusick   	register int bucket, n;
11039839Smckusick 	register unsigned amt;
11113259Sroot 
11214953Skarels 	/*
11317333Sralph 	 * First time malloc is called, setup page size and
11417333Sralph 	 * align break pointer so all data will be page aligned.
11514953Skarels 	 */
11617333Sralph 	if (pagesz == 0) {
11717333Sralph 		pagesz = n = getpagesize();
11817333Sralph 		op = (union overhead *)sbrk(0);
11917333Sralph   		n = n - sizeof (*op) - ((int)op & (n - 1));
12017333Sralph 		if (n < 0)
12117333Sralph 			n += pagesz;
12217333Sralph   		if (n) {
12317333Sralph   			if (sbrk(n) == (char *)-1)
12417333Sralph 				return (NULL);
12517333Sralph 		}
12617333Sralph 		bucket = 0;
12717333Sralph 		amt = 8;
12817333Sralph 		while (pagesz > amt) {
12917333Sralph 			amt <<= 1;
13017333Sralph 			bucket++;
13117333Sralph 		}
13217333Sralph 		pagebucket = bucket;
13317333Sralph 	}
13414953Skarels 	/*
13517333Sralph 	 * Convert amount of memory requested into closest block size
13617333Sralph 	 * stored in hash buckets which satisfies request.
13717333Sralph 	 * Account for space used per block for accounting.
13817333Sralph 	 */
13917333Sralph 	if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
14017333Sralph #ifndef RCHECK
14117333Sralph 		amt = 8;	/* size of first bucket */
14217333Sralph 		bucket = 0;
14317333Sralph #else
14417333Sralph 		amt = 16;	/* size of first bucket */
14517333Sralph 		bucket = 1;
14617333Sralph #endif
14717333Sralph 		n = -(sizeof (*op) + RSLOP);
14817333Sralph 	} else {
14917333Sralph 		amt = pagesz;
15017333Sralph 		bucket = pagebucket;
15117333Sralph 	}
15217333Sralph 	while (nbytes > amt + n) {
15317333Sralph 		amt <<= 1;
15425793Smckusick 		if (amt == 0)
15525793Smckusick 			return (NULL);
15617333Sralph 		bucket++;
15717333Sralph 	}
15817333Sralph 	/*
15914953Skarels 	 * If nothing in hash bucket right now,
16014953Skarels 	 * request more memory from the system.
16114953Skarels 	 */
16217333Sralph   	if ((op = nextf[bucket]) == NULL) {
16314953Skarels   		morecore(bucket);
16417333Sralph   		if ((op = nextf[bucket]) == NULL)
16517333Sralph   			return (NULL);
16617333Sralph 	}
16714953Skarels 	/* remove from linked list */
16817333Sralph   	nextf[bucket] = op->ov_next;
16917333Sralph 	op->ov_magic = MAGIC;
17017333Sralph 	op->ov_index = bucket;
17114953Skarels #ifdef MSTATS
17214953Skarels   	nmalloc[bucket]++;
17314953Skarels #endif
17414953Skarels #ifdef RCHECK
17514953Skarels 	/*
17614953Skarels 	 * Record allocated size of block and
17714953Skarels 	 * bound space with magic numbers.
17814953Skarels 	 */
17917438Sralph 	op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
18017333Sralph 	op->ov_rmagic = RMAGIC;
18117438Sralph   	*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
18214953Skarels #endif
18317333Sralph   	return ((char *)(op + 1));
18413259Sroot }
18513259Sroot 
18614953Skarels /*
18714953Skarels  * Allocate more memory to the indicated bucket.
18814953Skarels  */
18946599Sdonn static void
morecore(bucket)19014953Skarels morecore(bucket)
19117333Sralph 	int bucket;
19213259Sroot {
19314953Skarels   	register union overhead *op;
19417333Sralph 	register int sz;		/* size of desired block */
19525793Smckusick   	int amt;			/* amount to allocate */
19625793Smckusick   	int nblks;			/* how many blocks we get */
19713259Sroot 
19825793Smckusick 	/*
19925793Smckusick 	 * sbrk_size <= 0 only for big, FLUFFY, requests (about
20025793Smckusick 	 * 2^30 bytes on a VAX, I think) or for a negative arg.
20125793Smckusick 	 */
20217333Sralph 	sz = 1 << (bucket + 3);
20326410Slepreau #ifdef DEBUG
20426410Slepreau 	ASSERT(sz > 0);
20526410Slepreau #else
20625793Smckusick 	if (sz <= 0)
20725793Smckusick 		return;
20826410Slepreau #endif
20917333Sralph 	if (sz < pagesz) {
21017333Sralph 		amt = pagesz;
21117333Sralph   		nblks = amt / sz;
21217333Sralph 	} else {
21317333Sralph 		amt = sz + pagesz;
21417333Sralph 		nblks = 1;
21517333Sralph 	}
21617333Sralph 	op = (union overhead *)sbrk(amt);
21714953Skarels 	/* no more room! */
21814953Skarels   	if ((int)op == -1)
21914953Skarels   		return;
22014953Skarels 	/*
22114953Skarels 	 * Add new memory allocated to that on
22214953Skarels 	 * free list for this hash bucket.
22314953Skarels 	 */
22414953Skarels   	nextf[bucket] = op;
22514953Skarels   	while (--nblks > 0) {
22617333Sralph 		op->ov_next = (union overhead *)((caddr_t)op + sz);
22717333Sralph 		op = (union overhead *)((caddr_t)op + sz);
22814953Skarels   	}
22913259Sroot }
23013259Sroot 
23146599Sdonn void
free(cp)23214953Skarels free(cp)
23346599Sdonn 	void *cp;
23414953Skarels {
23514953Skarels   	register int size;
23614953Skarels 	register union overhead *op;
23713259Sroot 
23814953Skarels   	if (cp == NULL)
23914953Skarels   		return;
24014953Skarels 	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
24117333Sralph #ifdef DEBUG
24214953Skarels   	ASSERT(op->ov_magic == MAGIC);		/* make sure it was in use */
24314953Skarels #else
24414953Skarels 	if (op->ov_magic != MAGIC)
24514953Skarels 		return;				/* sanity */
24614953Skarels #endif
24714953Skarels #ifdef RCHECK
24814953Skarels   	ASSERT(op->ov_rmagic == RMAGIC);
24917333Sralph 	ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
25014953Skarels #endif
25114953Skarels   	size = op->ov_index;
25217333Sralph   	ASSERT(size < NBUCKETS);
25326410Slepreau 	op->ov_next = nextf[size];	/* also clobbers ov_magic */
25414953Skarels   	nextf[size] = op;
25514953Skarels #ifdef MSTATS
25614953Skarels   	nmalloc[size]--;
25714953Skarels #endif
25814953Skarels }
25914953Skarels 
26014953Skarels /*
26114953Skarels  * When a program attempts "storage compaction" as mentioned in the
26214953Skarels  * old malloc man page, it realloc's an already freed block.  Usually
26314953Skarels  * this is the last block it freed; occasionally it might be farther
26414953Skarels  * back.  We have to search all the free lists for the block in order
26514953Skarels  * to determine its bucket: 1st we make one pass thru the lists
26614953Skarels  * checking only the first block in each; if that fails we search
26757483Sbostic  * ``__realloc_srchlen'' blocks in each list for a match (the variable
26814953Skarels  * is extern so the caller can modify it).  If that fails we just copy
26914953Skarels  * however many bytes was given to realloc() and hope it's not huge.
27014953Skarels  */
27157483Sbostic int __realloc_srchlen = 4;	/* 4 should be plenty, -1 =>'s whole list */
27214953Skarels 
27346599Sdonn void *
realloc(cp,nbytes)27414953Skarels realloc(cp, nbytes)
27546599Sdonn 	void *cp;
27646599Sdonn 	size_t nbytes;
27714953Skarels {
27839839Smckusick   	register u_int onb;
27939839Smckusick 	register int i;
28014953Skarels 	union overhead *op;
28114953Skarels   	char *res;
28214953Skarels 	int was_alloced = 0;
28314953Skarels 
28414953Skarels   	if (cp == NULL)
28514953Skarels   		return (malloc(nbytes));
28614953Skarels 	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
28714953Skarels 	if (op->ov_magic == MAGIC) {
28814953Skarels 		was_alloced++;
28914953Skarels 		i = op->ov_index;
29015003Ssam 	} else {
29115003Ssam 		/*
29215003Ssam 		 * Already free, doing "compaction".
29315003Ssam 		 *
29415003Ssam 		 * Search for the old block of memory on the
29515003Ssam 		 * free list.  First, check the most common
29615003Ssam 		 * case (last element free'd), then (this failing)
29757483Sbostic 		 * the last ``__realloc_srchlen'' items free'd.
29815003Ssam 		 * If all lookups fail, then assume the size of
29915003Ssam 		 * the memory block being realloc'd is the
30025799Slepreau 		 * largest possible (so that all "nbytes" of new
30125799Slepreau 		 * memory are copied into).  Note that this could cause
30225799Slepreau 		 * a memory fault if the old area was tiny, and the moon
30325799Slepreau 		 * is gibbous.  However, that is very unlikely.
30415003Ssam 		 */
30514953Skarels 		if ((i = findbucket(op, 1)) < 0 &&
30657483Sbostic 		    (i = findbucket(op, __realloc_srchlen)) < 0)
30725799Slepreau 			i = NBUCKETS;
30814953Skarels 	}
30917333Sralph 	onb = 1 << (i + 3);
31017333Sralph 	if (onb < pagesz)
31117333Sralph 		onb -= sizeof (*op) + RSLOP;
31217333Sralph 	else
31317333Sralph 		onb += pagesz - sizeof (*op) - RSLOP;
31415003Ssam 	/* avoid the copy if same size block */
31517333Sralph 	if (was_alloced) {
31617333Sralph 		if (i) {
31717333Sralph 			i = 1 << (i + 2);
31817333Sralph 			if (i < pagesz)
31917333Sralph 				i -= sizeof (*op) + RSLOP;
32017333Sralph 			else
32117333Sralph 				i += pagesz - sizeof (*op) - RSLOP;
32217333Sralph 		}
32317333Sralph 		if (nbytes <= onb && nbytes > i) {
32417332Sralph #ifdef RCHECK
32517438Sralph 			op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
32617333Sralph 			*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
32717332Sralph #endif
32817333Sralph 			return(cp);
32917333Sralph 		} else
33017333Sralph 			free(cp);
33117332Sralph 	}
33214953Skarels   	if ((res = malloc(nbytes)) == NULL)
33314953Skarels   		return (NULL);
33425799Slepreau   	if (cp != res)		/* common optimization if "compacting" */
33514953Skarels 		bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
33614953Skarels   	return (res);
33714953Skarels }
33814953Skarels 
33914953Skarels /*
34014953Skarels  * Search ``srchlen'' elements of each free list for a block whose
34114953Skarels  * header starts at ``freep''.  If srchlen is -1 search the whole list.
34214953Skarels  * Return bucket number, or -1 if not found.
34314953Skarels  */
34414953Skarels static
34514953Skarels findbucket(freep, srchlen)
34615003Ssam 	union overhead *freep;
34715003Ssam 	int srchlen;
34813259Sroot {
34914953Skarels 	register union overhead *p;
35014953Skarels 	register int i, j;
35113259Sroot 
35215003Ssam 	for (i = 0; i < NBUCKETS; i++) {
35315003Ssam 		j = 0;
35415003Ssam 		for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
35514953Skarels 			if (p == freep)
35614953Skarels 				return (i);
35715003Ssam 			j++;
35815003Ssam 		}
35915003Ssam 	}
36014953Skarels 	return (-1);
36113259Sroot }
36213259Sroot 
36314953Skarels #ifdef MSTATS
36414953Skarels /*
36514953Skarels  * mstats - print out statistics about malloc
36614953Skarels  *
36714953Skarels  * Prints two lines of numbers, one showing the length of the free list
36814953Skarels  * for each size category, the second showing the number of mallocs -
36914953Skarels  * frees for each size category.
37014953Skarels  */
mstats(s)37114953Skarels mstats(s)
37214953Skarels 	char *s;
37313259Sroot {
37414953Skarels   	register int i, j;
37514953Skarels   	register union overhead *p;
37614953Skarels   	int totfree = 0,
37714953Skarels   	totused = 0;
37814953Skarels 
37914953Skarels   	fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
38014953Skarels   	for (i = 0; i < NBUCKETS; i++) {
38114953Skarels   		for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
38214953Skarels   			;
38314953Skarels   		fprintf(stderr, " %d", j);
38414953Skarels   		totfree += j * (1 << (i + 3));
38514953Skarels   	}
38614953Skarels   	fprintf(stderr, "\nused:\t");
38714953Skarels   	for (i = 0; i < NBUCKETS; i++) {
38814953Skarels   		fprintf(stderr, " %d", nmalloc[i]);
38914953Skarels   		totused += nmalloc[i] * (1 << (i + 3));
39014953Skarels   	}
39115003Ssam   	fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
39215003Ssam 	    totused, totfree);
39313259Sroot }
39413259Sroot #endif
395