1 /* $RCSfile: malloc.c,v $$Revision: 4.0.1.4 $$Date: 92/06/08 14:28:38 $
4 * Revision 4.0.1.4 92/06/08 14:28:38 lwall
5 * patch20: removed implicit int declarations on functions
6 * patch20: hash tables now split only if the memory is available to do so
7 * patch20: realloc(0, size) now does malloc in case library routines call it
9 * Revision 4.0.1.3 91/11/05 17:57:40 lwall
10 * patch11: safe malloc code now integrated into Perl's malloc when possible
12 * Revision 4.0.1.2 91/06/07 11:20:45 lwall
13 * patch4: many, many itty-bitty portability fixes
15 * Revision 4.0.1.1 91/04/11 17:48:31 lwall
16 * patch1: Configure now figures out malloc ptr type
18 * Revision 4.0 91/03/20 01:28:52 lwall
25 static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
31 * malloc.c (Caltech) 2/21/82
32 * Chris Kingsley, kingsley@cit-20.
34 * This is a very fast storage allocator. It allocates blocks of a small
35 * number of different sizes, and keeps free lists of each size. Blocks that
36 * don't exactly fit are passed up to the next larger size. In this
37 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
38 * This is designed for use in a program that uses vast quantities of memory,
39 * but bombs when it runs out.
45 static findbucket(), morecore();
47 /* I don't much care whether these are defined in sys/types.h--LAW */
49 #define u_char unsigned char
50 #define u_int unsigned int
51 #define u_short unsigned short
54 * The overhead on a block is at least 4 bytes. When free, this space
55 * contains a pointer to the next free block, and the bottom two bits must
56 * be zero. When in use, the first byte is set to MAGIC, and the second
57 * byte is the size index. The remaining bytes are for alignment.
58 * If range checking is enabled and the size of the block fits
59 * in two bytes, then the top two bytes hold the size of the requested block
60 * plus the range checking words, and the header word MINUS ONE.
63 union overhead *ov_next; /* when free */
65 double strut; /* alignment problems */
68 u_char ovu_magic; /* magic number */
69 u_char ovu_index; /* bucket # */
71 u_short ovu_size; /* actual block size */
72 u_int ovu_rmagic; /* range magic number */
75 #define ov_magic ovu.ovu_magic
76 #define ov_index ovu.ovu_index
77 #define ov_size ovu.ovu_size
78 #define ov_rmagic ovu.ovu_rmagic
81 #define MAGIC 0xff /* magic # on accounting info */
82 #define OLDMAGIC 0x7f /* same after a free() */
83 #define RMAGIC 0x55555555 /* magic # on range info */
85 #define RSLOP sizeof (u_int)
91 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
92 * smallest allocatable block is 8 bytes. The overhead information
93 * precedes the data area returned to the user.
96 static union overhead *nextf[NBUCKETS];
101 * nmalloc[i] is the difference between the number of mallocs and frees
102 * for a given block size.
104 static u_int nmalloc[NBUCKETS];
109 #define ASSERT(p) if (!(p)) botch("p"); else
115 printf("assertion botched: %s\n", s);
128 register MEM_SIZE nbytes;
130 register union overhead *p;
131 register int bucket = 0;
132 register MEM_SIZE shiftr;
136 MEM_SIZE size = nbytes;
140 if (nbytes > 0xffff) {
141 fprintf(stderr, "Allocation too large: %lx\n", (long)nbytes);
146 if ((long)nbytes < 0)
147 fatal("panic: malloc");
149 #endif /* safemalloc */
152 * Convert amount of memory requested into
153 * closest block size stored in hash buckets
154 * which satisfies request. Account for
155 * space used per block for accounting.
157 nbytes += sizeof (union overhead) + RSLOP;
158 nbytes = (nbytes + 3) &~ 3;
159 shiftr = (nbytes - 1) >> 2;
160 /* apart from this loop, this is O(1) */
164 * If nothing in hash bucket right now,
165 * request more memory from the system.
167 if (nextf[bucket] == NULL)
169 if ((p = (union overhead *)nextf[bucket]) == NULL) {
172 fputs("Out of memory!\n", stderr);
182 # if !(defined(I286) || defined(atarist))
184 fprintf(stderr,"0x%x: (%05d) malloc %ld bytes\n",p+1,an++,(long)size);
187 fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",p+1,an++,(long)size);
190 #endif /* safemalloc */
192 /* remove from linked list */
194 if (*((int*)p) & (sizeof(union overhead) - 1))
195 #if !(defined(I286) || defined(atarist))
196 fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
198 fprintf(stderr,"Corrupt malloc ptr 0x%lx at 0x%lx\n",*((int*)p),p);
201 nextf[bucket] = p->ov_next;
209 * Record allocated size of block and
210 * bound space with magic numbers.
212 if (nbytes <= 0x10000)
213 p->ov_size = nbytes - 1;
214 p->ov_rmagic = RMAGIC;
215 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
217 return ((MALLOCPTRTYPE *)(p + 1));
221 * Allocate more memory to the indicated bucket.
227 register union overhead *op;
228 register int rnu; /* 2^rnu bytes will be requested */
229 register int nblks; /* become nblks blocks of the desired size */
230 register MEM_SIZE siz;
235 * Insure memory is allocated
236 * on a page boundary. Should
237 * make getpageize call?
239 #ifndef atarist /* on the atari we dont have to worry about this */
240 op = (union overhead *)sbrk(0);
243 (void)sbrk(1024 - ((int)op & 0x3ff));
245 /* The sbrk(0) call on the I286 always returns the next segment */
249 #if !(defined(I286) || defined(atarist))
250 /* take 2k unless the block is bigger than that */
251 rnu = (bucket <= 8) ? 11 : bucket + 3;
253 /* take 16k unless the block is bigger than that
254 (80286s like large segments!), probably good on the atari too */
255 rnu = (bucket <= 11) ? 14 : bucket + 3;
257 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
260 op = (union overhead *)sbrk(1L << rnu);
265 * Round up to minimum allocation size boundary
266 * and deduct from block count to reflect.
270 op = (union overhead *)(((MEM_SIZE)op + 8) &~ 7);
274 /* Again, this should always be ok on an 80286 */
277 * Add new memory allocated to that on
278 * free list for this hash bucket.
281 siz = 1 << (bucket + 3);
282 while (--nblks > 0) {
283 op->ov_next = (union overhead *)((caddr_t)op + siz);
284 op = (union overhead *)((caddr_t)op + siz);
292 register MEM_SIZE size;
293 register union overhead *op;
294 char *cp = (char*)mp;
298 # if !(defined(I286) || defined(atarist))
300 fprintf(stderr,"0x%x: (%05d) free\n",cp,an++);
303 fprintf(stderr,"0x%lx: (%05d) free\n",cp,an++);
306 #endif /* safemalloc */
310 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
312 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
314 if (op->ov_magic != MAGIC) {
315 warn("%s free() ignored",
316 op->ov_magic == OLDMAGIC ? "Duplicate" : "Bad");
319 op->ov_magic = OLDMAGIC;
322 ASSERT(op->ov_rmagic == RMAGIC);
323 if (op->ov_index <= 13)
324 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
326 ASSERT(op->ov_index < NBUCKETS);
328 op->ov_next = nextf[size];
336 * When a program attempts "storage compaction" as mentioned in the
337 * old malloc man page, it realloc's an already freed block. Usually
338 * this is the last block it freed; occasionally it might be farther
339 * back. We have to search all the free lists for the block in order
340 * to determine its bucket: 1st we make one pass thru the lists
341 * checking only the first block in each; if that fails we search
342 * ``reall_srchlen'' blocks in each list for a match (the variable
343 * is extern so the caller can modify it). If that fails we just copy
344 * however many bytes was given to realloc() and hope it's not huge.
346 int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
353 register MEM_SIZE onb;
358 char *cp = (char*)mp;
362 MEM_SIZE size = nbytes;
366 if (nbytes > 0xffff) {
367 fprintf(stderr, "Reallocation too large: %lx\n", size);
372 return malloc(nbytes);
374 if ((long)nbytes < 0)
375 fatal("panic: realloc");
377 #endif /* safemalloc */
379 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
380 if (op->ov_magic == MAGIC) {
385 * Already free, doing "compaction".
387 * Search for the old block of memory on the
388 * free list. First, check the most common
389 * case (last element free'd), then (this failing)
390 * the last ``reall_srchlen'' items free'd.
391 * If all lookups fail, then assume the size of
392 * the memory block being realloc'd is the
395 if ((i = findbucket(op, 1)) < 0 &&
396 (i = findbucket(op, reall_srchlen)) < 0)
399 onb = (1L << (i + 3)) - sizeof (*op) - RSLOP;
400 /* avoid the copy if same size block */
402 nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP) {
405 * Record new allocated size of block and
406 * bound space with magic numbers.
408 if (op->ov_index <= 13) {
410 * Convert amount of memory requested into
411 * closest block size stored in hash buckets
412 * which satisfies request. Account for
413 * space used per block for accounting.
415 nbytes += sizeof (union overhead) + RSLOP;
416 nbytes = (nbytes + 3) &~ 3;
417 op->ov_size = nbytes - 1;
418 *((u_int *)((caddr_t)op + nbytes - RSLOP)) = RMAGIC;
424 if ((res = (char*)malloc(nbytes)) == NULL)
426 if (cp != res) /* common optimization */
427 Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char);
434 # if !(defined(I286) || defined(atarist))
436 fprintf(stderr,"0x%x: (%05d) rfree\n",res,an++);
437 fprintf(stderr,"0x%x: (%05d) realloc %ld bytes\n",res,an++,(long)size);
441 fprintf(stderr,"0x%lx: (%05d) rfree\n",res,an++);
442 fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",res,an++,(long)size);
446 #endif /* safemalloc */
447 return ((MALLOCPTRTYPE*)res);
451 * Search ``srchlen'' elements of each free list for a block whose
452 * header starts at ``freep''. If srchlen is -1 search the whole list.
453 * Return bucket number, or -1 if not found.
456 findbucket(freep, srchlen)
457 union overhead *freep;
460 register union overhead *p;
463 for (i = 0; i < NBUCKETS; i++) {
465 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
476 * mstats - print out statistics about malloc
478 * Prints two lines of numbers, one showing the length of the free list
479 * for each size category, the second showing the number of mallocs -
480 * frees for each size category.
487 register union overhead *p;
491 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
492 for (i = 0; i < NBUCKETS; i++) {
493 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
495 fprintf(stderr, " %d", j);
496 totfree += j * (1 << (i + 3));
498 fprintf(stderr, "\nused:\t");
499 for (i = 0; i < NBUCKETS; i++) {
500 fprintf(stderr, " %d", nmalloc[i]);
501 totused += nmalloc[i] * (1 << (i + 3));
503 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",