113259Sroot #ifndef lint 2*15003Ssam static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 09/16/83"; 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 { 3314953Skarels u_char ovu_magic; /* magic number */ 3414953Skarels u_char ovu_index; /* bucket # */ 3514953Skarels #ifdef RCHECK 3614953Skarels u_short ovu_size; /* actual block size */ 3714953Skarels u_int ovu_rmagic; /* range magic number */ 3814953Skarels #endif 3914953Skarels } ovu; 4014953Skarels #define ov_magic ovu.ovu_magic 4114953Skarels #define ov_index ovu.ovu_index 4214953Skarels #define ov_size ovu.ovu_size 4314953Skarels #define ov_rmagic ovu.ovu_rmagic 4414953Skarels }; 4514953Skarels 4614953Skarels #define MAGIC 0xff /* magic # on accounting info */ 4714953Skarels #define RMAGIC 0x55555555 /* magic # on range info */ 4814953Skarels #ifdef RCHECK 4914953Skarels #define RSLOP sizeof (u_int) 5014953Skarels #else 5114953Skarels #define RSLOP 0 5214953Skarels #endif 5314953Skarels 5414953Skarels /* 5514953Skarels * nextf[i] is the pointer to the next free block of size 2^(i+3). The 5614953Skarels * smallest allocatable block is 8 bytes. The overhead information 5714953Skarels * precedes the data area returned to the user. 5814953Skarels */ 5914953Skarels #define NBUCKETS 30 6014953Skarels static union overhead *nextf[NBUCKETS]; 6114953Skarels extern char *sbrk(); 6214953Skarels 6314953Skarels #ifdef MSTATS 6414953Skarels /* 6514953Skarels * nmalloc[i] is the difference between the number of mallocs and frees 6614953Skarels * for a given block size. 6714953Skarels */ 6814953Skarels static u_int nmalloc[NBUCKETS]; 6914953Skarels #include <stdio.h> 7014953Skarels #endif 7114953Skarels 7213259Sroot #ifdef debug 7314953Skarels #define ASSERT(p) if (!(p)) botch("p"); else 7414953Skarels static 7513259Sroot botch(s) 76*15003Ssam char *s; 7713259Sroot { 78*15003Ssam 79*15003Ssam printf("assertion botched: %s\n", s); 8013259Sroot abort(); 8113259Sroot } 8213259Sroot #else 8314953Skarels #define ASSERT(p) 8413259Sroot #endif 8513259Sroot 8613259Sroot char * 8713259Sroot malloc(nbytes) 8814953Skarels register unsigned nbytes; 8913259Sroot { 9014953Skarels register union overhead *p; 9114953Skarels register int bucket = 0; 9214953Skarels register unsigned shiftr; 9313259Sroot 9414953Skarels /* 9514953Skarels * Convert amount of memory requested into 9614953Skarels * closest block size stored in hash buckets 9714953Skarels * which satisfies request. Account for 9814953Skarels * space used per block for accounting. 9914953Skarels */ 10014953Skarels nbytes += sizeof (union overhead) + RSLOP; 10114953Skarels nbytes = (nbytes + 3) &~ 3; 10214953Skarels shiftr = (nbytes - 1) >> 2; 10314953Skarels /* apart from this loop, this is O(1) */ 10414953Skarels while (shiftr >>= 1) 10514953Skarels bucket++; 10614953Skarels /* 10714953Skarels * If nothing in hash bucket right now, 10814953Skarels * request more memory from the system. 10914953Skarels */ 11014953Skarels if (nextf[bucket] == NULL) 11114953Skarels morecore(bucket); 11214953Skarels if ((p = (union overhead *)nextf[bucket]) == NULL) 11314953Skarels return (NULL); 11414953Skarels /* remove from linked list */ 11514953Skarels nextf[bucket] = nextf[bucket]->ov_next; 11614953Skarels p->ov_magic = MAGIC; 11714953Skarels p->ov_index= bucket; 11814953Skarels #ifdef MSTATS 11914953Skarels nmalloc[bucket]++; 12014953Skarels #endif 12114953Skarels #ifdef RCHECK 12214953Skarels /* 12314953Skarels * Record allocated size of block and 12414953Skarels * bound space with magic numbers. 12514953Skarels */ 12614953Skarels if (nbytes <= 0x10000) 12714953Skarels p->ov_size = nbytes - 1; 12814953Skarels p->ov_rmagic = RMAGIC; 12914953Skarels *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC; 13014953Skarels #endif 13114953Skarels return ((char *)(p + 1)); 13213259Sroot } 13313259Sroot 13414953Skarels /* 13514953Skarels * Allocate more memory to the indicated bucket. 13614953Skarels */ 13714953Skarels static 13814953Skarels morecore(bucket) 13914953Skarels register bucket; 14013259Sroot { 14114953Skarels register union overhead *op; 14214953Skarels register int rnu; /* 2^rnu bytes will be requested */ 14314953Skarels register int nblks; /* become nblks blocks of the desired size */ 14414953Skarels register int siz; 14513259Sroot 14614953Skarels if (nextf[bucket]) 14714953Skarels return; 14814953Skarels /* 14914953Skarels * Insure memory is allocated 15014953Skarels * on a page boundary. Should 15114953Skarels * make getpageize call? 15214953Skarels */ 15314953Skarels op = (union overhead *)sbrk(0); 15414953Skarels if ((int)op & 0x3ff) 15514953Skarels sbrk(1024 - ((int)op & 0x3ff)); 15614953Skarels /* take 2k unless the block is bigger than that */ 15714953Skarels rnu = (bucket <= 8) ? 11 : bucket + 3; 15814953Skarels nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ 15914953Skarels if (rnu < bucket) 16014953Skarels rnu = bucket; 16114953Skarels op = (union overhead *)sbrk(1 << rnu); 16214953Skarels /* no more room! */ 16314953Skarels if ((int)op == -1) 16414953Skarels return; 16514953Skarels /* 16614953Skarels * Round up to minimum allocation size boundary 16714953Skarels * and deduct from block count to reflect. 16814953Skarels */ 16914953Skarels if ((int)op & 7) { 17014953Skarels op = (union overhead *)(((int)op + 8) &~ 7); 17114953Skarels nblks--; 17214953Skarels } 17314953Skarels /* 17414953Skarels * Add new memory allocated to that on 17514953Skarels * free list for this hash bucket. 17614953Skarels */ 17714953Skarels nextf[bucket] = op; 17814953Skarels siz = 1 << (bucket + 3); 17914953Skarels while (--nblks > 0) { 18014953Skarels op->ov_next = (union overhead *)((caddr_t)op + siz); 18114953Skarels op = (union overhead *)((caddr_t)op + siz); 18214953Skarels } 18313259Sroot } 18413259Sroot 18514953Skarels free(cp) 18614953Skarels char *cp; 18714953Skarels { 18814953Skarels register int size; 18914953Skarels register union overhead *op; 19013259Sroot 19114953Skarels if (cp == NULL) 19214953Skarels return; 19314953Skarels op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 19414953Skarels #ifdef debug 19514953Skarels ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 19614953Skarels #else 19714953Skarels if (op->ov_magic != MAGIC) 19814953Skarels return; /* sanity */ 19914953Skarels #endif 20014953Skarels #ifdef RCHECK 20114953Skarels ASSERT(op->ov_rmagic == RMAGIC); 20214953Skarels if (op->ov_index <= 13) 20314953Skarels ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC); 20414953Skarels #endif 20514953Skarels ASSERT(op->ov_index < NBUCKETS); 20614953Skarels size = op->ov_index; 20714953Skarels op->ov_next = nextf[size]; 20814953Skarels nextf[size] = op; 20914953Skarels #ifdef MSTATS 21014953Skarels nmalloc[size]--; 21114953Skarels #endif 21214953Skarels } 21314953Skarels 21414953Skarels /* 21514953Skarels * When a program attempts "storage compaction" as mentioned in the 21614953Skarels * old malloc man page, it realloc's an already freed block. Usually 21714953Skarels * this is the last block it freed; occasionally it might be farther 21814953Skarels * back. We have to search all the free lists for the block in order 21914953Skarels * to determine its bucket: 1st we make one pass thru the lists 22014953Skarels * checking only the first block in each; if that fails we search 22114953Skarels * ``realloc_srchlen'' blocks in each list for a match (the variable 22214953Skarels * is extern so the caller can modify it). If that fails we just copy 22314953Skarels * however many bytes was given to realloc() and hope it's not huge. 22414953Skarels */ 225*15003Ssam int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ 22614953Skarels 22713259Sroot char * 22814953Skarels realloc(cp, nbytes) 22914953Skarels char *cp; 23014953Skarels unsigned nbytes; 23114953Skarels { 23214953Skarels register u_int onb; 23314953Skarels union overhead *op; 23414953Skarels char *res; 23514953Skarels register int i; 23614953Skarels int was_alloced = 0; 23714953Skarels 23814953Skarels if (cp == NULL) 23914953Skarels return (malloc(nbytes)); 24014953Skarels op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 24114953Skarels if (op->ov_magic == MAGIC) { 24214953Skarels was_alloced++; 24314953Skarels i = op->ov_index; 244*15003Ssam } else { 245*15003Ssam /* 246*15003Ssam * Already free, doing "compaction". 247*15003Ssam * 248*15003Ssam * Search for the old block of memory on the 249*15003Ssam * free list. First, check the most common 250*15003Ssam * case (last element free'd), then (this failing) 251*15003Ssam * the last ``realloc_srchlen'' items free'd. 252*15003Ssam * If all lookups fail, then assume the size of 253*15003Ssam * the memory block being realloc'd is the 254*15003Ssam * smallest possible. 255*15003Ssam */ 25614953Skarels if ((i = findbucket(op, 1)) < 0 && 25714953Skarels (i = findbucket(op, realloc_srchlen)) < 0) 258*15003Ssam i = 0; 25914953Skarels } 26014953Skarels onb = (1 << (i + 3)) - sizeof (*op) - RSLOP; 261*15003Ssam /* avoid the copy if same size block */ 262*15003Ssam if (was_alloced && 26314953Skarels nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP) 26414953Skarels return(cp); 26514953Skarels if ((res = malloc(nbytes)) == NULL) 26614953Skarels return (NULL); 26714953Skarels if (cp != res) /* common optimization */ 26814953Skarels bcopy(cp, res, (nbytes < onb) ? nbytes : onb); 26914953Skarels if (was_alloced) 27014953Skarels free(cp); 27114953Skarels return (res); 27214953Skarels } 27314953Skarels 27414953Skarels /* 27514953Skarels * Search ``srchlen'' elements of each free list for a block whose 27614953Skarels * header starts at ``freep''. If srchlen is -1 search the whole list. 27714953Skarels * Return bucket number, or -1 if not found. 27814953Skarels */ 27914953Skarels static 28014953Skarels findbucket(freep, srchlen) 281*15003Ssam union overhead *freep; 282*15003Ssam int srchlen; 28313259Sroot { 28414953Skarels register union overhead *p; 28514953Skarels register int i, j; 28613259Sroot 287*15003Ssam for (i = 0; i < NBUCKETS; i++) { 288*15003Ssam j = 0; 289*15003Ssam for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 29014953Skarels if (p == freep) 29114953Skarels return (i); 292*15003Ssam j++; 293*15003Ssam } 294*15003Ssam } 29514953Skarels return (-1); 29613259Sroot } 29713259Sroot 29814953Skarels #ifdef MSTATS 29914953Skarels /* 30014953Skarels * mstats - print out statistics about malloc 30114953Skarels * 30214953Skarels * Prints two lines of numbers, one showing the length of the free list 30314953Skarels * for each size category, the second showing the number of mallocs - 30414953Skarels * frees for each size category. 30514953Skarels */ 30614953Skarels mstats(s) 30714953Skarels char *s; 30813259Sroot { 30914953Skarels register int i, j; 31014953Skarels register union overhead *p; 31114953Skarels int totfree = 0, 31214953Skarels totused = 0; 31314953Skarels 31414953Skarels fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); 31514953Skarels for (i = 0; i < NBUCKETS; i++) { 31614953Skarels for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 31714953Skarels ; 31814953Skarels fprintf(stderr, " %d", j); 31914953Skarels totfree += j * (1 << (i + 3)); 32014953Skarels } 32114953Skarels fprintf(stderr, "\nused:\t"); 32214953Skarels for (i = 0; i < NBUCKETS; i++) { 32314953Skarels fprintf(stderr, " %d", nmalloc[i]); 32414953Skarels totused += nmalloc[i] * (1 << (i + 3)); 32514953Skarels } 326*15003Ssam fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", 327*15003Ssam totused, totfree); 32813259Sroot } 32913259Sroot #endif 330