113259Sroot #ifndef lint 2*17333Sralph static char sccsid[] = "@(#)malloc.c 4.5 (Berkeley) 11/02/84"; 313259Sroot #endif 414953Skarels 514953Skarels /* 614953Skarels * malloc.c (Caltech) 2/21/82 714953Skarels * Chris Kingsley, kingsley@cit-20. 814953Skarels * 914953Skarels * This is a very fast storage allocator. It allocates blocks of a small 1014953Skarels * number of different sizes, and keeps free lists of each size. Blocks that 1114953Skarels * don't exactly fit are passed up to the next larger size. In this 1214953Skarels * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long. 1314953Skarels * This is designed for use in a program that uses vast quantities of memory, 1414953Skarels * but bombs when it runs out. 1514953Skarels */ 1614953Skarels 1714953Skarels #include <sys/types.h> 1814953Skarels 1914953Skarels #define NULL 0 2014953Skarels 2114953Skarels /* 2214953Skarels * The overhead on a block is at least 4 bytes. When free, this space 2314953Skarels * contains a pointer to the next free block, and the bottom two bits must 2414953Skarels * be zero. When in use, the first byte is set to MAGIC, and the second 2514953Skarels * byte is the size index. The remaining bytes are for alignment. 2614953Skarels * If range checking is enabled and the size of the block fits 2714953Skarels * in two bytes, then the top two bytes hold the size of the requested block 2814953Skarels * plus the range checking words, and the header word MINUS ONE. 2914953Skarels */ 3014953Skarels union overhead { 3114953Skarels union overhead *ov_next; /* when free */ 3214953Skarels struct { 33*17333Sralph #ifndef RCHECK 3414953Skarels u_char ovu_magic; /* magic number */ 3514953Skarels u_char ovu_index; /* bucket # */ 36*17333Sralph #else 37*17333Sralph u_int ovu_size; /* actual block size */ 38*17333Sralph u_char ovu_magic; /* magic number */ 39*17333Sralph u_char ovu_index; /* bucket # */ 40*17333Sralph u_short ovu_rmagic; /* range magic number */ 4114953Skarels #endif 4214953Skarels } ovu; 4314953Skarels #define ov_magic ovu.ovu_magic 4414953Skarels #define ov_index ovu.ovu_index 45*17333Sralph #define ov_rmagic ovu.ovu_rmagic 4614953Skarels #define ov_size ovu.ovu_size 4714953Skarels }; 4814953Skarels 49*17333Sralph #define MAGIC 0xef /* magic # on accounting info */ 50*17333Sralph #define RMAGIC 0x5555 /* magic # on range info */ 51*17333Sralph 5214953Skarels #ifdef RCHECK 53*17333Sralph #define RSLOP sizeof (u_short) 5414953Skarels #else 5514953Skarels #define RSLOP 0 5614953Skarels #endif 5714953Skarels 5814953Skarels /* 5914953Skarels * nextf[i] is the pointer to the next free block of size 2^(i+3). The 6014953Skarels * smallest allocatable block is 8 bytes. The overhead information 6114953Skarels * precedes the data area returned to the user. 6214953Skarels */ 6314953Skarels #define NBUCKETS 30 6414953Skarels static union overhead *nextf[NBUCKETS]; 6514953Skarels extern char *sbrk(); 6614953Skarels 67*17333Sralph static int pagesz; /* page size */ 68*17333Sralph static int pagebucket; /* page size bucket */ 69*17333Sralph 7014953Skarels #ifdef MSTATS 7114953Skarels /* 7214953Skarels * nmalloc[i] is the difference between the number of mallocs and frees 7314953Skarels * for a given block size. 7414953Skarels */ 7514953Skarels static u_int nmalloc[NBUCKETS]; 7614953Skarels #include <stdio.h> 7714953Skarels #endif 7814953Skarels 79*17333Sralph #ifdef DEBUG 80*17333Sralph #define ASSERT(p) if (!(p)) botch("p") 8114953Skarels static 8213259Sroot botch(s) 8315003Ssam char *s; 8413259Sroot { 8515003Ssam 8615003Ssam printf("assertion botched: %s\n", s); 8713259Sroot abort(); 8813259Sroot } 8913259Sroot #else 9014953Skarels #define ASSERT(p) 9113259Sroot #endif 9213259Sroot 9313259Sroot char * 9413259Sroot malloc(nbytes) 95*17333Sralph unsigned nbytes; 9613259Sroot { 97*17333Sralph register union overhead *op; 98*17333Sralph register int bucket; 99*17333Sralph register unsigned amt, n; 10013259Sroot 10114953Skarels /* 102*17333Sralph * First time malloc is called, setup page size and 103*17333Sralph * align break pointer so all data will be page aligned. 10414953Skarels */ 105*17333Sralph if (pagesz == 0) { 106*17333Sralph pagesz = n = getpagesize(); 107*17333Sralph op = (union overhead *)sbrk(0); 108*17333Sralph n = n - sizeof (*op) - ((int)op & (n - 1)); 109*17333Sralph if (n < 0) 110*17333Sralph n += pagesz; 111*17333Sralph if (n) { 112*17333Sralph if (sbrk(n) == (char *)-1) 113*17333Sralph return (NULL); 114*17333Sralph } 115*17333Sralph bucket = 0; 116*17333Sralph amt = 8; 117*17333Sralph while (pagesz > amt) { 118*17333Sralph amt <<= 1; 119*17333Sralph bucket++; 120*17333Sralph } 121*17333Sralph pagebucket = bucket; 122*17333Sralph } 12314953Skarels /* 124*17333Sralph * Convert amount of memory requested into closest block size 125*17333Sralph * stored in hash buckets which satisfies request. 126*17333Sralph * Account for space used per block for accounting. 127*17333Sralph */ 128*17333Sralph if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) { 129*17333Sralph #ifndef RCHECK 130*17333Sralph amt = 8; /* size of first bucket */ 131*17333Sralph bucket = 0; 132*17333Sralph #else 133*17333Sralph amt = 16; /* size of first bucket */ 134*17333Sralph bucket = 1; 135*17333Sralph #endif 136*17333Sralph n = -(sizeof (*op) + RSLOP); 137*17333Sralph } else { 138*17333Sralph amt = pagesz; 139*17333Sralph bucket = pagebucket; 140*17333Sralph } 141*17333Sralph while (nbytes > amt + n) { 142*17333Sralph amt <<= 1; 143*17333Sralph bucket++; 144*17333Sralph } 145*17333Sralph /* 14614953Skarels * If nothing in hash bucket right now, 14714953Skarels * request more memory from the system. 14814953Skarels */ 149*17333Sralph if ((op = nextf[bucket]) == NULL) { 15014953Skarels morecore(bucket); 151*17333Sralph if ((op = nextf[bucket]) == NULL) 152*17333Sralph return (NULL); 153*17333Sralph } 15414953Skarels /* remove from linked list */ 155*17333Sralph nextf[bucket] = op->ov_next; 156*17333Sralph op->ov_magic = MAGIC; 157*17333Sralph op->ov_index = bucket; 15814953Skarels #ifdef MSTATS 15914953Skarels nmalloc[bucket]++; 16014953Skarels #endif 16114953Skarels #ifdef RCHECK 16214953Skarels /* 16314953Skarels * Record allocated size of block and 16414953Skarels * bound space with magic numbers. 16514953Skarels */ 166*17333Sralph op->ov_size = nbytes; 167*17333Sralph op->ov_rmagic = RMAGIC; 168*17333Sralph *(u_short *)((caddr_t)(op + 1) + nbytes) = RMAGIC; 16914953Skarels #endif 170*17333Sralph return ((char *)(op + 1)); 17113259Sroot } 17213259Sroot 17314953Skarels /* 17414953Skarels * Allocate more memory to the indicated bucket. 17514953Skarels */ 17614953Skarels static 17714953Skarels morecore(bucket) 178*17333Sralph int bucket; 17913259Sroot { 18014953Skarels register union overhead *op; 181*17333Sralph register int sz; /* size of desired block */ 182*17333Sralph register int amt; /* amount to allocate */ 183*17333Sralph register int nblks; /* how many blocks we get */ 18413259Sroot 185*17333Sralph sz = 1 << (bucket + 3); 186*17333Sralph if (sz < pagesz) { 187*17333Sralph amt = pagesz; 188*17333Sralph nblks = amt / sz; 189*17333Sralph } else { 190*17333Sralph amt = sz + pagesz; 191*17333Sralph nblks = 1; 192*17333Sralph } 193*17333Sralph op = (union overhead *)sbrk(amt); 19414953Skarels /* no more room! */ 19514953Skarels if ((int)op == -1) 19614953Skarels return; 19714953Skarels /* 19814953Skarels * Add new memory allocated to that on 19914953Skarels * free list for this hash bucket. 20014953Skarels */ 20114953Skarels nextf[bucket] = op; 20214953Skarels while (--nblks > 0) { 203*17333Sralph op->ov_next = (union overhead *)((caddr_t)op + sz); 204*17333Sralph op = (union overhead *)((caddr_t)op + sz); 20514953Skarels } 20613259Sroot } 20713259Sroot 20814953Skarels free(cp) 20914953Skarels char *cp; 21014953Skarels { 21114953Skarels register int size; 21214953Skarels register union overhead *op; 21313259Sroot 21414953Skarels if (cp == NULL) 21514953Skarels return; 21614953Skarels op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 217*17333Sralph #ifdef DEBUG 21814953Skarels ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 21914953Skarels #else 22014953Skarels if (op->ov_magic != MAGIC) 22114953Skarels return; /* sanity */ 22214953Skarels #endif 22314953Skarels #ifdef RCHECK 22414953Skarels ASSERT(op->ov_rmagic == RMAGIC); 225*17333Sralph ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); 22614953Skarels #endif 22714953Skarels size = op->ov_index; 228*17333Sralph ASSERT(size < NBUCKETS); 22914953Skarels op->ov_next = nextf[size]; 23014953Skarels nextf[size] = op; 23114953Skarels #ifdef MSTATS 23214953Skarels nmalloc[size]--; 23314953Skarels #endif 23414953Skarels } 23514953Skarels 23614953Skarels /* 23714953Skarels * When a program attempts "storage compaction" as mentioned in the 23814953Skarels * old malloc man page, it realloc's an already freed block. Usually 23914953Skarels * this is the last block it freed; occasionally it might be farther 24014953Skarels * back. We have to search all the free lists for the block in order 24114953Skarels * to determine its bucket: 1st we make one pass thru the lists 24214953Skarels * checking only the first block in each; if that fails we search 24314953Skarels * ``realloc_srchlen'' blocks in each list for a match (the variable 24414953Skarels * is extern so the caller can modify it). If that fails we just copy 24514953Skarels * however many bytes was given to realloc() and hope it's not huge. 24614953Skarels */ 24715003Ssam int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ 24814953Skarels 24913259Sroot char * 25014953Skarels realloc(cp, nbytes) 25114953Skarels char *cp; 25214953Skarels unsigned nbytes; 25314953Skarels { 254*17333Sralph register u_int onb, i; 25514953Skarels union overhead *op; 25614953Skarels char *res; 25714953Skarels int was_alloced = 0; 25814953Skarels 25914953Skarels if (cp == NULL) 26014953Skarels return (malloc(nbytes)); 26114953Skarels op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 26214953Skarels if (op->ov_magic == MAGIC) { 26314953Skarels was_alloced++; 26414953Skarels i = op->ov_index; 26515003Ssam } else { 26615003Ssam /* 26715003Ssam * Already free, doing "compaction". 26815003Ssam * 26915003Ssam * Search for the old block of memory on the 27015003Ssam * free list. First, check the most common 27115003Ssam * case (last element free'd), then (this failing) 27215003Ssam * the last ``realloc_srchlen'' items free'd. 27315003Ssam * If all lookups fail, then assume the size of 27415003Ssam * the memory block being realloc'd is the 27515003Ssam * smallest possible. 27615003Ssam */ 27714953Skarels if ((i = findbucket(op, 1)) < 0 && 27814953Skarels (i = findbucket(op, realloc_srchlen)) < 0) 279*17333Sralph #ifndef RCHECK 28015003Ssam i = 0; 281*17333Sralph #else 282*17333Sralph i = 1; /* smallest possible w/ RCHECK */ 283*17333Sralph #endif 28414953Skarels } 285*17333Sralph onb = 1 << (i + 3); 286*17333Sralph if (onb < pagesz) 287*17333Sralph onb -= sizeof (*op) + RSLOP; 288*17333Sralph else 289*17333Sralph onb += pagesz - sizeof (*op) - RSLOP; 29015003Ssam /* avoid the copy if same size block */ 291*17333Sralph if (was_alloced) { 292*17333Sralph if (i) { 293*17333Sralph i = 1 << (i + 2); 294*17333Sralph if (i < pagesz) 295*17333Sralph i -= sizeof (*op) + RSLOP; 296*17333Sralph else 297*17333Sralph i += pagesz - sizeof (*op) - RSLOP; 298*17333Sralph } 299*17333Sralph if (nbytes <= onb && nbytes > i) { 30017332Sralph #ifdef RCHECK 301*17333Sralph op->ov_size = nbytes; 302*17333Sralph *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 30317332Sralph #endif 304*17333Sralph return(cp); 305*17333Sralph } else 306*17333Sralph free(cp); 30717332Sralph } 30814953Skarels if ((res = malloc(nbytes)) == NULL) 30914953Skarels return (NULL); 31014953Skarels if (cp != res) /* common optimization */ 31114953Skarels bcopy(cp, res, (nbytes < onb) ? nbytes : onb); 31214953Skarels return (res); 31314953Skarels } 31414953Skarels 31514953Skarels /* 31614953Skarels * Search ``srchlen'' elements of each free list for a block whose 31714953Skarels * header starts at ``freep''. If srchlen is -1 search the whole list. 31814953Skarels * Return bucket number, or -1 if not found. 31914953Skarels */ 32014953Skarels static 32114953Skarels findbucket(freep, srchlen) 32215003Ssam union overhead *freep; 32315003Ssam int srchlen; 32413259Sroot { 32514953Skarels register union overhead *p; 32614953Skarels register int i, j; 32713259Sroot 32815003Ssam for (i = 0; i < NBUCKETS; i++) { 32915003Ssam j = 0; 33015003Ssam for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 33114953Skarels if (p == freep) 33214953Skarels return (i); 33315003Ssam j++; 33415003Ssam } 33515003Ssam } 33614953Skarels return (-1); 33713259Sroot } 33813259Sroot 33914953Skarels #ifdef MSTATS 34014953Skarels /* 34114953Skarels * mstats - print out statistics about malloc 34214953Skarels * 34314953Skarels * Prints two lines of numbers, one showing the length of the free list 34414953Skarels * for each size category, the second showing the number of mallocs - 34514953Skarels * frees for each size category. 34614953Skarels */ 34714953Skarels mstats(s) 34814953Skarels char *s; 34913259Sroot { 35014953Skarels register int i, j; 35114953Skarels register union overhead *p; 35214953Skarels int totfree = 0, 35314953Skarels totused = 0; 35414953Skarels 35514953Skarels fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); 35614953Skarels for (i = 0; i < NBUCKETS; i++) { 35714953Skarels for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 35814953Skarels ; 35914953Skarels fprintf(stderr, " %d", j); 36014953Skarels totfree += j * (1 << (i + 3)); 36114953Skarels } 36214953Skarels fprintf(stderr, "\nused:\t"); 36314953Skarels for (i = 0; i < NBUCKETS; i++) { 36414953Skarels fprintf(stderr, " %d", nmalloc[i]); 36514953Skarels totused += nmalloc[i] * (1 << (i + 3)); 36614953Skarels } 36715003Ssam fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", 36815003Ssam totused, totfree); 36913259Sroot } 37013259Sroot #endif 371