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[] = "@(#)malloc.c 5.1 (Berkeley) 05/30/85"; 9 #endif not lint 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 #ifndef RCHECK 40 u_char ovu_magic; /* magic number */ 41 u_char ovu_index; /* bucket # */ 42 #else 43 u_int ovu_size; /* actual block size */ 44 u_char ovu_magic; /* magic number */ 45 u_char ovu_index; /* bucket # */ 46 u_short ovu_rmagic; /* range magic number */ 47 #endif 48 } ovu; 49 #define ov_magic ovu.ovu_magic 50 #define ov_index ovu.ovu_index 51 #define ov_rmagic ovu.ovu_rmagic 52 #define ov_size ovu.ovu_size 53 }; 54 55 #define MAGIC 0xef /* magic # on accounting info */ 56 #define RMAGIC 0x5555 /* magic # on range info */ 57 58 #ifdef RCHECK 59 #define RSLOP sizeof (u_short) 60 #else 61 #define RSLOP 0 62 #endif 63 64 /* 65 * nextf[i] is the pointer to the next free block of size 2^(i+3). The 66 * smallest allocatable block is 8 bytes. The overhead information 67 * precedes the data area returned to the user. 68 */ 69 #define NBUCKETS 30 70 static union overhead *nextf[NBUCKETS]; 71 extern char *sbrk(); 72 73 static int pagesz; /* page size */ 74 static int pagebucket; /* page size bucket */ 75 76 #ifdef MSTATS 77 /* 78 * nmalloc[i] is the difference between the number of mallocs and frees 79 * for a given block size. 80 */ 81 static u_int nmalloc[NBUCKETS]; 82 #include <stdio.h> 83 #endif 84 85 #ifdef DEBUG 86 #define ASSERT(p) if (!(p)) botch("p") 87 static 88 botch(s) 89 char *s; 90 { 91 92 printf("assertion botched: %s\n", s); 93 abort(); 94 } 95 #else 96 #define ASSERT(p) 97 #endif 98 99 char * 100 malloc(nbytes) 101 unsigned nbytes; 102 { 103 register union overhead *op; 104 register int bucket; 105 register unsigned amt, n; 106 107 /* 108 * First time malloc is called, setup page size and 109 * align break pointer so all data will be page aligned. 110 */ 111 if (pagesz == 0) { 112 pagesz = n = getpagesize(); 113 op = (union overhead *)sbrk(0); 114 n = n - sizeof (*op) - ((int)op & (n - 1)); 115 if (n < 0) 116 n += pagesz; 117 if (n) { 118 if (sbrk(n) == (char *)-1) 119 return (NULL); 120 } 121 bucket = 0; 122 amt = 8; 123 while (pagesz > amt) { 124 amt <<= 1; 125 bucket++; 126 } 127 pagebucket = bucket; 128 } 129 /* 130 * Convert amount of memory requested into closest block size 131 * stored in hash buckets which satisfies request. 132 * Account for space used per block for accounting. 133 */ 134 if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) { 135 #ifndef RCHECK 136 amt = 8; /* size of first bucket */ 137 bucket = 0; 138 #else 139 amt = 16; /* size of first bucket */ 140 bucket = 1; 141 #endif 142 n = -(sizeof (*op) + RSLOP); 143 } else { 144 amt = pagesz; 145 bucket = pagebucket; 146 } 147 while (nbytes > amt + n) { 148 amt <<= 1; 149 bucket++; 150 } 151 /* 152 * If nothing in hash bucket right now, 153 * request more memory from the system. 154 */ 155 if ((op = nextf[bucket]) == NULL) { 156 morecore(bucket); 157 if ((op = nextf[bucket]) == NULL) 158 return (NULL); 159 } 160 /* remove from linked list */ 161 nextf[bucket] = op->ov_next; 162 op->ov_magic = MAGIC; 163 op->ov_index = bucket; 164 #ifdef MSTATS 165 nmalloc[bucket]++; 166 #endif 167 #ifdef RCHECK 168 /* 169 * Record allocated size of block and 170 * bound space with magic numbers. 171 */ 172 op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 173 op->ov_rmagic = RMAGIC; 174 *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 175 #endif 176 return ((char *)(op + 1)); 177 } 178 179 /* 180 * Allocate more memory to the indicated bucket. 181 */ 182 static 183 morecore(bucket) 184 int bucket; 185 { 186 register union overhead *op; 187 register int sz; /* size of desired block */ 188 register int amt; /* amount to allocate */ 189 register int nblks; /* how many blocks we get */ 190 191 sz = 1 << (bucket + 3); 192 if (sz < pagesz) { 193 amt = pagesz; 194 nblks = amt / sz; 195 } else { 196 amt = sz + pagesz; 197 nblks = 1; 198 } 199 op = (union overhead *)sbrk(amt); 200 /* no more room! */ 201 if ((int)op == -1) 202 return; 203 /* 204 * Add new memory allocated to that on 205 * free list for this hash bucket. 206 */ 207 nextf[bucket] = op; 208 while (--nblks > 0) { 209 op->ov_next = (union overhead *)((caddr_t)op + sz); 210 op = (union overhead *)((caddr_t)op + sz); 211 } 212 } 213 214 free(cp) 215 char *cp; 216 { 217 register int size; 218 register union overhead *op; 219 220 if (cp == NULL) 221 return; 222 op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 223 #ifdef DEBUG 224 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 225 #else 226 if (op->ov_magic != MAGIC) 227 return; /* sanity */ 228 #endif 229 #ifdef RCHECK 230 ASSERT(op->ov_rmagic == RMAGIC); 231 ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); 232 #endif 233 size = op->ov_index; 234 ASSERT(size < NBUCKETS); 235 op->ov_next = nextf[size]; 236 nextf[size] = op; 237 #ifdef MSTATS 238 nmalloc[size]--; 239 #endif 240 } 241 242 /* 243 * When a program attempts "storage compaction" as mentioned in the 244 * old malloc man page, it realloc's an already freed block. Usually 245 * this is the last block it freed; occasionally it might be farther 246 * back. We have to search all the free lists for the block in order 247 * to determine its bucket: 1st we make one pass thru the lists 248 * checking only the first block in each; if that fails we search 249 * ``realloc_srchlen'' blocks in each list for a match (the variable 250 * is extern so the caller can modify it). If that fails we just copy 251 * however many bytes was given to realloc() and hope it's not huge. 252 */ 253 int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ 254 255 char * 256 realloc(cp, nbytes) 257 char *cp; 258 unsigned nbytes; 259 { 260 register u_int onb, i; 261 union overhead *op; 262 char *res; 263 int was_alloced = 0; 264 265 if (cp == NULL) 266 return (malloc(nbytes)); 267 op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 268 if (op->ov_magic == MAGIC) { 269 was_alloced++; 270 i = op->ov_index; 271 } else { 272 /* 273 * Already free, doing "compaction". 274 * 275 * Search for the old block of memory on the 276 * free list. First, check the most common 277 * case (last element free'd), then (this failing) 278 * the last ``realloc_srchlen'' items free'd. 279 * If all lookups fail, then assume the size of 280 * the memory block being realloc'd is the 281 * smallest possible. 282 */ 283 if ((i = findbucket(op, 1)) < 0 && 284 (i = findbucket(op, realloc_srchlen)) < 0) 285 #ifndef RCHECK 286 i = 0; 287 #else 288 i = 1; /* smallest possible w/ RCHECK */ 289 #endif 290 } 291 onb = 1 << (i + 3); 292 if (onb < pagesz) 293 onb -= sizeof (*op) + RSLOP; 294 else 295 onb += pagesz - sizeof (*op) - RSLOP; 296 /* avoid the copy if same size block */ 297 if (was_alloced) { 298 if (i) { 299 i = 1 << (i + 2); 300 if (i < pagesz) 301 i -= sizeof (*op) + RSLOP; 302 else 303 i += pagesz - sizeof (*op) - RSLOP; 304 } 305 if (nbytes <= onb && nbytes > i) { 306 #ifdef RCHECK 307 op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 308 *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 309 #endif 310 return(cp); 311 } else 312 free(cp); 313 } 314 if ((res = malloc(nbytes)) == NULL) 315 return (NULL); 316 if (cp != res) /* common optimization */ 317 bcopy(cp, res, (nbytes < onb) ? nbytes : onb); 318 return (res); 319 } 320 321 /* 322 * Search ``srchlen'' elements of each free list for a block whose 323 * header starts at ``freep''. If srchlen is -1 search the whole list. 324 * Return bucket number, or -1 if not found. 325 */ 326 static 327 findbucket(freep, srchlen) 328 union overhead *freep; 329 int srchlen; 330 { 331 register union overhead *p; 332 register int i, j; 333 334 for (i = 0; i < NBUCKETS; i++) { 335 j = 0; 336 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 337 if (p == freep) 338 return (i); 339 j++; 340 } 341 } 342 return (-1); 343 } 344 345 #ifdef MSTATS 346 /* 347 * mstats - print out statistics about malloc 348 * 349 * Prints two lines of numbers, one showing the length of the free list 350 * for each size category, the second showing the number of mallocs - 351 * frees for each size category. 352 */ 353 mstats(s) 354 char *s; 355 { 356 register int i, j; 357 register union overhead *p; 358 int totfree = 0, 359 totused = 0; 360 361 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); 362 for (i = 0; i < NBUCKETS; i++) { 363 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 364 ; 365 fprintf(stderr, " %d", j); 366 totfree += j * (1 << (i + 3)); 367 } 368 fprintf(stderr, "\nused:\t"); 369 for (i = 0; i < NBUCKETS; i++) { 370 fprintf(stderr, " %d", nmalloc[i]); 371 totused += nmalloc[i] * (1 << (i + 3)); 372 } 373 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", 374 totused, totfree); 375 } 376 #endif 377