1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley Software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 static char *sccsid = "@(#)alloc.c 5.2 (Berkeley) 06/06/85"; 9 #endif 10 11 /* 12 * malloc.c (Caltech) 2/21/82 13 * Chris Kingsley, kingsley@cit-20. 14 * 15 * This is a very fast storage allocator. It allocates blocks of a small 16 * number of different sizes, and keeps free lists of each size. Blocks that 17 * don't exactly fit are passed up to the next larger size. In this 18 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long. 19 * This is designed for use in a program that uses vast quantities of memory, 20 * but bombs when it runs out. 21 */ 22 23 #include <sys/types.h> 24 25 #define NULL 0 26 27 /* 28 * The overhead on a block is at least 4 bytes. When free, this space 29 * contains a pointer to the next free block, and the bottom two bits must 30 * be zero. When in use, the first byte is set to MAGIC, and the second 31 * byte is the size index. The remaining bytes are for alignment. 32 * If range checking is enabled and the size of the block fits 33 * in two bytes, then the top two bytes hold the size of the requested block 34 * plus the range checking words, and the header word MINUS ONE. 35 */ 36 union overhead { 37 union overhead *ov_next; /* when free */ 38 struct { 39 u_char ovu_magic; /* magic number */ 40 u_char ovu_index; /* bucket # */ 41 #ifdef RCHECK 42 u_short ovu_size; /* actual block size */ 43 u_int ovu_rmagic; /* range magic number */ 44 #endif 45 } ovu; 46 #define ov_magic ovu.ovu_magic 47 #define ov_index ovu.ovu_index 48 #define ov_size ovu.ovu_size 49 #define ov_rmagic ovu.ovu_rmagic 50 }; 51 52 #define MAGIC 0xff /* magic # on accounting info */ 53 #define RMAGIC 0x55555555 /* magic # on range info */ 54 #ifdef RCHECK 55 #define RSLOP sizeof (u_int) 56 #else 57 #define RSLOP 0 58 #endif 59 60 /* 61 * nextf[i] is the pointer to the next free block of size 2^(i+3). The 62 * smallest allocatable block is 8 bytes. The overhead information 63 * precedes the data area returned to the user. 64 */ 65 #define NBUCKETS 30 66 static union overhead *nextf[NBUCKETS]; 67 extern char *sbrk(); 68 69 /* 70 * nmalloc[i] is the difference between the number of mallocs and frees 71 * for a given block size. 72 */ 73 static u_int nmalloc[NBUCKETS]; 74 75 #ifdef debug 76 #define ASSERT(p) if (!(p)) botch("p"); else 77 static 78 botch(s) 79 char *s; 80 { 81 printf("assertion botched: %s\n",s); 82 abort(); 83 } 84 #else 85 #define ASSERT(p) 86 #endif 87 88 char * 89 malloc(nbytes) 90 register unsigned nbytes; 91 { 92 register union overhead *p; 93 register int bucket = 0; 94 register unsigned shiftr; 95 96 /* 97 * Convert amount of memory requested into 98 * closest block size stored in hash buckets 99 * which satisfies request. Account for 100 * space used per block for accounting. 101 */ 102 nbytes += sizeof (union overhead) + RSLOP; 103 nbytes = (nbytes + 3) &~ 3; 104 shiftr = (nbytes - 1) >> 2; 105 /* apart from this loop, this is O(1) */ 106 while (shiftr >>= 1) 107 bucket++; 108 /* 109 * If nothing in hash bucket right now, 110 * request more memory from the system. 111 */ 112 if (nextf[bucket] == NULL) 113 morecore(bucket); 114 if ((p = (union overhead *)nextf[bucket]) == NULL) 115 return (NULL); 116 /* remove from linked list */ 117 nextf[bucket] = nextf[bucket]->ov_next; 118 p->ov_magic = MAGIC; 119 p->ov_index= bucket; 120 nmalloc[bucket]++; 121 #ifdef RCHECK 122 /* 123 * Record allocated size of block and 124 * bound space with magic numbers. 125 */ 126 if (nbytes <= 0x10000) 127 p->ov_size = nbytes - 1; 128 p->ov_rmagic = RMAGIC; 129 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC; 130 #endif 131 return ((char *)(p + 1)); 132 } 133 134 /* 135 * Allocate more memory to the indicated bucket. 136 */ 137 static 138 morecore(bucket) 139 register bucket; 140 { 141 register union overhead *op; 142 register int rnu; /* 2^rnu bytes will be requested */ 143 register int nblks; /* become nblks blocks of the desired size */ 144 register int siz; 145 146 if (nextf[bucket]) 147 return; 148 /* 149 * Insure memory is allocated 150 * on a page boundary. Should 151 * make getpageize call? 152 */ 153 op = (union overhead *)sbrk(0); 154 if ((int)op & 0x3ff) 155 (void) sbrk(1024 - ((int)op & 0x3ff)); 156 /* take 2k unless the block is bigger than that */ 157 rnu = (bucket <= 8) ? 11 : bucket + 3; 158 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */ 159 if (rnu < bucket) 160 rnu = bucket; 161 op = (union overhead *)sbrk(1 << rnu); 162 /* no more room! */ 163 if ((int)op == -1) 164 return; 165 /* 166 * Round up to minimum allocation size boundary 167 * and deduct from block count to reflect. 168 */ 169 if ((int)op & 7) { 170 op = (union overhead *)(((int)op + 8) &~ 7); 171 nblks--; 172 } 173 /* 174 * Add new memory allocated to that on 175 * free list for this hash bucket. 176 */ 177 nextf[bucket] = op; 178 siz = 1 << (bucket + 3); 179 while (--nblks > 0) { 180 op->ov_next = (union overhead *)((caddr_t)op + siz); 181 op = (union overhead *)((caddr_t)op + siz); 182 } 183 } 184 185 free(cp) 186 char *cp; 187 { 188 register int size; 189 register union overhead *op; 190 191 if (cp == NULL) 192 return; 193 op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 194 195 /* 196 ** The following botch is because csh tries to free a free block 197 ** when processing the =~ or !~ operators. -- layer@ucbmonet 198 */ 199 #ifdef CSHbotch /* was debug */ 200 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 201 #else 202 if (op->ov_magic != MAGIC) 203 return; /* sanity */ 204 #endif 205 206 #ifdef RCHECK 207 ASSERT(op->ov_rmagic == RMAGIC); 208 if (op->ov_index <= 13) 209 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC); 210 #endif 211 ASSERT(op->ov_index < NBUCKETS); 212 size = op->ov_index; 213 op->ov_next = nextf[size]; 214 nextf[size] = op; 215 nmalloc[size]--; 216 } 217 218 /* 219 * When a program attempts "storage compaction" as mentioned in the 220 * old malloc man page, it realloc's an already freed block. Usually 221 * this is the last block it freed; occasionally it might be farther 222 * back. We have to search all the free lists for the block in order 223 * to determine its bucket: 1st we make one pass thru the lists 224 * checking only the first block in each; if that fails we search 225 * ``realloc_srchlen'' blocks in each list for a match (the variable 226 * is extern so the caller can modify it). If that fails we just copy 227 * however many bytes was given to realloc() and hope it's not huge. 228 */ 229 int realloc_srchlen = 4; /* 4 should be plenty. -1 means whole list */ 230 231 char * 232 realloc(cp, nbytes) 233 char *cp; 234 unsigned nbytes; 235 { 236 register u_int onb; 237 union overhead *op; 238 char *res; 239 register int i; 240 int was_alloced = 0; 241 242 if (cp == NULL) 243 return (malloc(nbytes)); 244 op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 245 if (op->ov_magic == MAGIC) { 246 was_alloced++; 247 i = op->ov_index; 248 } 249 else { /* already free: he is doing "compaction" (tee hee) */ 250 if ((i = findbucket(op, 1)) < 0 && 251 (i = findbucket(op, realloc_srchlen)) < 0) 252 i = 0; /* assume smallest possible */ 253 } 254 onb = (1 << (i + 3)) - sizeof (*op) - RSLOP; 255 if (was_alloced && /* avoid the copy if same size block */ 256 nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP) 257 return(cp); 258 if ((res = malloc(nbytes)) == NULL) 259 return (NULL); 260 if (cp != res) /* common optimization */ 261 bcopy(cp, res, (nbytes < onb) ? nbytes : onb); 262 if (was_alloced) 263 free(cp); 264 return (res); 265 } 266 267 /* 268 * Search ``srchlen'' elements of each free list for a block whose 269 * header starts at ``freep''. If srchlen is -1 search the whole list. 270 * Return bucket number, or -1 if not found. 271 */ 272 static 273 findbucket(freep, srchlen) 274 union overhead *freep; 275 int srchlen; 276 { 277 register union overhead *p; 278 register int i, j; 279 280 for (i = 0; i < NBUCKETS; i++) 281 for (j = 0, p = nextf[i]; p && j != srchlen; j++, p = p->ov_next) 282 if (p == freep) 283 return (i); 284 return (-1); 285 } 286 287 /* 288 * mstats - print out statistics about malloc 289 * 290 * Prints two lines of numbers, one showing the length of the free list 291 * for each size category, the second showing the number of mallocs - 292 * frees for each size category. 293 */ 294 showall(s) 295 char **s; 296 { 297 register int i, j; 298 register union overhead *p; 299 int totfree = 0, 300 totused = 0; 301 302 if (s[1]) 303 printf("Memory allocation statistics %s\nfree:", s[1]); 304 for (i = 0; i < NBUCKETS; i++) { 305 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 306 ; 307 308 if (s[1]) 309 printf(" %d", j); 310 totfree += j * (1 << (i + 3)); 311 } 312 if (s[1]) 313 printf("\nused:"); 314 for (i = 0; i < NBUCKETS; i++) { 315 if (s[1]) 316 printf(" %d", nmalloc[i]); 317 totused += nmalloc[i] * (1 << (i + 3)); 318 } 319 if (s[1]) 320 printf("\n"); 321 printf("Total in use: %d, total free: %d\n", totused, totfree); 322 } 323