121347Sdist /* 225177Smckusick * Copyright (c) 1983 Regents of the University of California. 335329Sbostic * All rights reserved. 435329Sbostic * 5*42634Sbostic * %sccs.include.redist.c% 621347Sdist */ 721347Sdist 826569Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*42634Sbostic static char sccsid[] = "@(#)malloc.c 5.9 (Berkeley) 06/01/90"; 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> 2414953Skarels 2514953Skarels #define NULL 0 2614953Skarels 2714953Skarels /* 2814953Skarels * The overhead on a block is at least 4 bytes. When free, this space 2914953Skarels * contains a pointer to the next free block, and the bottom two bits must 3014953Skarels * be zero. When in use, the first byte is set to MAGIC, and the second 3114953Skarels * byte is the size index. The remaining bytes are for alignment. 3226410Slepreau * If range checking is enabled then a second word holds the size of the 3326410Slepreau * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC). 3426410Slepreau * The order of elements is critical: ov_magic must overlay the low order 3526410Slepreau * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern. 3614953Skarels */ 3714953Skarels union overhead { 3814953Skarels union overhead *ov_next; /* when free */ 3914953Skarels struct { 4014953Skarels u_char ovu_magic; /* magic number */ 4114953Skarels u_char ovu_index; /* bucket # */ 4226410Slepreau #ifdef RCHECK 4326410Slepreau u_short ovu_rmagic; /* range magic number */ 4417333Sralph u_int ovu_size; /* actual block size */ 4514953Skarels #endif 4614953Skarels } ovu; 4714953Skarels #define ov_magic ovu.ovu_magic 4814953Skarels #define ov_index ovu.ovu_index 4917333Sralph #define ov_rmagic ovu.ovu_rmagic 5014953Skarels #define ov_size ovu.ovu_size 5114953Skarels }; 5214953Skarels 5317333Sralph #define MAGIC 0xef /* magic # on accounting info */ 5417333Sralph #define RMAGIC 0x5555 /* magic # on range info */ 5517333Sralph 5614953Skarels #ifdef RCHECK 5717333Sralph #define RSLOP sizeof (u_short) 5814953Skarels #else 5914953Skarels #define RSLOP 0 6014953Skarels #endif 6114953Skarels 6214953Skarels /* 6314953Skarels * nextf[i] is the pointer to the next free block of size 2^(i+3). The 6414953Skarels * smallest allocatable block is 8 bytes. The overhead information 6514953Skarels * precedes the data area returned to the user. 6614953Skarels */ 6714953Skarels #define NBUCKETS 30 6814953Skarels static union overhead *nextf[NBUCKETS]; 6914953Skarels extern char *sbrk(); 7014953Skarels 7117333Sralph static int pagesz; /* page size */ 7217333Sralph static int pagebucket; /* page size bucket */ 7317333Sralph 7414953Skarels #ifdef MSTATS 7514953Skarels /* 7614953Skarels * nmalloc[i] is the difference between the number of mallocs and frees 7714953Skarels * for a given block size. 7814953Skarels */ 7914953Skarels static u_int nmalloc[NBUCKETS]; 8014953Skarels #include <stdio.h> 8114953Skarels #endif 8214953Skarels 8325799Slepreau #if defined(DEBUG) || defined(RCHECK) 8417333Sralph #define ASSERT(p) if (!(p)) botch("p") 8525799Slepreau #include <stdio.h> 8614953Skarels static 8713259Sroot botch(s) 8815003Ssam char *s; 8913259Sroot { 9025799Slepreau fprintf(stderr, "\r\nassertion botched: %s\r\n", s); 9125799Slepreau (void) fflush(stderr); /* just in case user buffered it */ 9213259Sroot abort(); 9313259Sroot } 9413259Sroot #else 9514953Skarels #define ASSERT(p) 9613259Sroot #endif 9713259Sroot 9813259Sroot char * 9913259Sroot malloc(nbytes) 10017333Sralph unsigned nbytes; 10113259Sroot { 10217333Sralph register union overhead *op; 10339839Smckusick register int bucket, n; 10439839Smckusick register unsigned amt; 10513259Sroot 10614953Skarels /* 10717333Sralph * First time malloc is called, setup page size and 10817333Sralph * align break pointer so all data will be page aligned. 10914953Skarels */ 11017333Sralph if (pagesz == 0) { 11117333Sralph pagesz = n = getpagesize(); 11217333Sralph op = (union overhead *)sbrk(0); 11317333Sralph n = n - sizeof (*op) - ((int)op & (n - 1)); 11417333Sralph if (n < 0) 11517333Sralph n += pagesz; 11617333Sralph if (n) { 11717333Sralph if (sbrk(n) == (char *)-1) 11817333Sralph return (NULL); 11917333Sralph } 12017333Sralph bucket = 0; 12117333Sralph amt = 8; 12217333Sralph while (pagesz > amt) { 12317333Sralph amt <<= 1; 12417333Sralph bucket++; 12517333Sralph } 12617333Sralph pagebucket = bucket; 12717333Sralph } 12814953Skarels /* 12917333Sralph * Convert amount of memory requested into closest block size 13017333Sralph * stored in hash buckets which satisfies request. 13117333Sralph * Account for space used per block for accounting. 13217333Sralph */ 13317333Sralph if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) { 13417333Sralph #ifndef RCHECK 13517333Sralph amt = 8; /* size of first bucket */ 13617333Sralph bucket = 0; 13717333Sralph #else 13817333Sralph amt = 16; /* size of first bucket */ 13917333Sralph bucket = 1; 14017333Sralph #endif 14117333Sralph n = -(sizeof (*op) + RSLOP); 14217333Sralph } else { 14317333Sralph amt = pagesz; 14417333Sralph bucket = pagebucket; 14517333Sralph } 14617333Sralph while (nbytes > amt + n) { 14717333Sralph amt <<= 1; 14825793Smckusick if (amt == 0) 14925793Smckusick return (NULL); 15017333Sralph bucket++; 15117333Sralph } 15217333Sralph /* 15314953Skarels * If nothing in hash bucket right now, 15414953Skarels * request more memory from the system. 15514953Skarels */ 15617333Sralph if ((op = nextf[bucket]) == NULL) { 15714953Skarels morecore(bucket); 15817333Sralph if ((op = nextf[bucket]) == NULL) 15917333Sralph return (NULL); 16017333Sralph } 16114953Skarels /* remove from linked list */ 16217333Sralph nextf[bucket] = op->ov_next; 16317333Sralph op->ov_magic = MAGIC; 16417333Sralph op->ov_index = bucket; 16514953Skarels #ifdef MSTATS 16614953Skarels nmalloc[bucket]++; 16714953Skarels #endif 16814953Skarels #ifdef RCHECK 16914953Skarels /* 17014953Skarels * Record allocated size of block and 17114953Skarels * bound space with magic numbers. 17214953Skarels */ 17317438Sralph op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 17417333Sralph op->ov_rmagic = RMAGIC; 17517438Sralph *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 17614953Skarels #endif 17717333Sralph return ((char *)(op + 1)); 17813259Sroot } 17913259Sroot 18014953Skarels /* 18114953Skarels * Allocate more memory to the indicated bucket. 18214953Skarels */ 18314953Skarels morecore(bucket) 18417333Sralph int bucket; 18513259Sroot { 18614953Skarels register union overhead *op; 18717333Sralph register int sz; /* size of desired block */ 18825793Smckusick int amt; /* amount to allocate */ 18925793Smckusick int nblks; /* how many blocks we get */ 19013259Sroot 19125793Smckusick /* 19225793Smckusick * sbrk_size <= 0 only for big, FLUFFY, requests (about 19325793Smckusick * 2^30 bytes on a VAX, I think) or for a negative arg. 19425793Smckusick */ 19517333Sralph sz = 1 << (bucket + 3); 19626410Slepreau #ifdef DEBUG 19726410Slepreau ASSERT(sz > 0); 19826410Slepreau #else 19925793Smckusick if (sz <= 0) 20025793Smckusick return; 20126410Slepreau #endif 20217333Sralph if (sz < pagesz) { 20317333Sralph amt = pagesz; 20417333Sralph nblks = amt / sz; 20517333Sralph } else { 20617333Sralph amt = sz + pagesz; 20717333Sralph nblks = 1; 20817333Sralph } 20917333Sralph op = (union overhead *)sbrk(amt); 21014953Skarels /* no more room! */ 21114953Skarels if ((int)op == -1) 21214953Skarels return; 21314953Skarels /* 21414953Skarels * Add new memory allocated to that on 21514953Skarels * free list for this hash bucket. 21614953Skarels */ 21714953Skarels nextf[bucket] = op; 21814953Skarels while (--nblks > 0) { 21917333Sralph op->ov_next = (union overhead *)((caddr_t)op + sz); 22017333Sralph op = (union overhead *)((caddr_t)op + sz); 22114953Skarels } 22213259Sroot } 22313259Sroot 22414953Skarels free(cp) 22514953Skarels char *cp; 22614953Skarels { 22714953Skarels register int size; 22814953Skarels register union overhead *op; 22913259Sroot 23014953Skarels if (cp == NULL) 23114953Skarels return; 23214953Skarels op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 23317333Sralph #ifdef DEBUG 23414953Skarels ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 23514953Skarels #else 23614953Skarels if (op->ov_magic != MAGIC) 23714953Skarels return; /* sanity */ 23814953Skarels #endif 23914953Skarels #ifdef RCHECK 24014953Skarels ASSERT(op->ov_rmagic == RMAGIC); 24117333Sralph ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); 24214953Skarels #endif 24314953Skarels size = op->ov_index; 24417333Sralph ASSERT(size < NBUCKETS); 24526410Slepreau op->ov_next = nextf[size]; /* also clobbers ov_magic */ 24614953Skarels nextf[size] = op; 24714953Skarels #ifdef MSTATS 24814953Skarels nmalloc[size]--; 24914953Skarels #endif 25014953Skarels } 25114953Skarels 25214953Skarels /* 25314953Skarels * When a program attempts "storage compaction" as mentioned in the 25414953Skarels * old malloc man page, it realloc's an already freed block. Usually 25514953Skarels * this is the last block it freed; occasionally it might be farther 25614953Skarels * back. We have to search all the free lists for the block in order 25714953Skarels * to determine its bucket: 1st we make one pass thru the lists 25814953Skarels * checking only the first block in each; if that fails we search 25914953Skarels * ``realloc_srchlen'' blocks in each list for a match (the variable 26014953Skarels * is extern so the caller can modify it). If that fails we just copy 26114953Skarels * however many bytes was given to realloc() and hope it's not huge. 26214953Skarels */ 26315003Ssam int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ 26414953Skarels 26513259Sroot char * 26614953Skarels realloc(cp, nbytes) 26714953Skarels char *cp; 26814953Skarels unsigned nbytes; 26914953Skarels { 27039839Smckusick register u_int onb; 27139839Smckusick register int i; 27214953Skarels union overhead *op; 27314953Skarels char *res; 27414953Skarels int was_alloced = 0; 27514953Skarels 27614953Skarels if (cp == NULL) 27714953Skarels return (malloc(nbytes)); 27814953Skarels op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 27914953Skarels if (op->ov_magic == MAGIC) { 28014953Skarels was_alloced++; 28114953Skarels i = op->ov_index; 28215003Ssam } else { 28315003Ssam /* 28415003Ssam * Already free, doing "compaction". 28515003Ssam * 28615003Ssam * Search for the old block of memory on the 28715003Ssam * free list. First, check the most common 28815003Ssam * case (last element free'd), then (this failing) 28915003Ssam * the last ``realloc_srchlen'' items free'd. 29015003Ssam * If all lookups fail, then assume the size of 29115003Ssam * the memory block being realloc'd is the 29225799Slepreau * largest possible (so that all "nbytes" of new 29325799Slepreau * memory are copied into). Note that this could cause 29425799Slepreau * a memory fault if the old area was tiny, and the moon 29525799Slepreau * is gibbous. However, that is very unlikely. 29615003Ssam */ 29714953Skarels if ((i = findbucket(op, 1)) < 0 && 29814953Skarels (i = findbucket(op, realloc_srchlen)) < 0) 29925799Slepreau i = NBUCKETS; 30014953Skarels } 30117333Sralph onb = 1 << (i + 3); 30217333Sralph if (onb < pagesz) 30317333Sralph onb -= sizeof (*op) + RSLOP; 30417333Sralph else 30517333Sralph onb += pagesz - sizeof (*op) - RSLOP; 30615003Ssam /* avoid the copy if same size block */ 30717333Sralph if (was_alloced) { 30817333Sralph if (i) { 30917333Sralph i = 1 << (i + 2); 31017333Sralph if (i < pagesz) 31117333Sralph i -= sizeof (*op) + RSLOP; 31217333Sralph else 31317333Sralph i += pagesz - sizeof (*op) - RSLOP; 31417333Sralph } 31517333Sralph if (nbytes <= onb && nbytes > i) { 31617332Sralph #ifdef RCHECK 31717438Sralph op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 31817333Sralph *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 31917332Sralph #endif 32017333Sralph return(cp); 32117333Sralph } else 32217333Sralph free(cp); 32317332Sralph } 32414953Skarels if ((res = malloc(nbytes)) == NULL) 32514953Skarels return (NULL); 32625799Slepreau if (cp != res) /* common optimization if "compacting" */ 32714953Skarels bcopy(cp, res, (nbytes < onb) ? nbytes : onb); 32814953Skarels return (res); 32914953Skarels } 33014953Skarels 33114953Skarels /* 33214953Skarels * Search ``srchlen'' elements of each free list for a block whose 33314953Skarels * header starts at ``freep''. If srchlen is -1 search the whole list. 33414953Skarels * Return bucket number, or -1 if not found. 33514953Skarels */ 33614953Skarels static 33714953Skarels findbucket(freep, srchlen) 33815003Ssam union overhead *freep; 33915003Ssam int srchlen; 34013259Sroot { 34114953Skarels register union overhead *p; 34214953Skarels register int i, j; 34313259Sroot 34415003Ssam for (i = 0; i < NBUCKETS; i++) { 34515003Ssam j = 0; 34615003Ssam for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 34714953Skarels if (p == freep) 34814953Skarels return (i); 34915003Ssam j++; 35015003Ssam } 35115003Ssam } 35214953Skarels return (-1); 35313259Sroot } 35413259Sroot 35514953Skarels #ifdef MSTATS 35614953Skarels /* 35714953Skarels * mstats - print out statistics about malloc 35814953Skarels * 35914953Skarels * Prints two lines of numbers, one showing the length of the free list 36014953Skarels * for each size category, the second showing the number of mallocs - 36114953Skarels * frees for each size category. 36214953Skarels */ 36314953Skarels mstats(s) 36414953Skarels char *s; 36513259Sroot { 36614953Skarels register int i, j; 36714953Skarels register union overhead *p; 36814953Skarels int totfree = 0, 36914953Skarels totused = 0; 37014953Skarels 37114953Skarels fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); 37214953Skarels for (i = 0; i < NBUCKETS; i++) { 37314953Skarels for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 37414953Skarels ; 37514953Skarels fprintf(stderr, " %d", j); 37614953Skarels totfree += j * (1 << (i + 3)); 37714953Skarels } 37814953Skarels fprintf(stderr, "\nused:\t"); 37914953Skarels for (i = 0; i < NBUCKETS; i++) { 38014953Skarels fprintf(stderr, " %d", nmalloc[i]); 38114953Skarels totused += nmalloc[i] * (1 << (i + 3)); 38214953Skarels } 38315003Ssam fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", 38415003Ssam totused, totfree); 38513259Sroot } 38613259Sroot #endif 387