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