10 * malloc.c (Caltech) 2/21/82
11 * Chris Kingsley, kingsley@cit-20.
13 * This is a very fast storage allocator. It allocates blocks of a small
14 * number of different sizes, and keeps free lists of each size. Blocks that
15 * don't exactly fit are passed up to the next larger size. In this
16 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
17 * If PACK_MALLOC is defined, small blocks are 2^n bytes long.
18 * This is designed for use in a program that uses vast quantities of memory,
19 * but bombs when it runs out.
25 /* I don't much care whether these are defined in sys/types.h--LAW */
27 #define u_char unsigned char
28 #define u_int unsigned int
29 #define u_short unsigned short
31 /* 286 and atarist like big chunks, which gives too much overhead. */
32 #if (defined(RCHECK) || defined(I286) || defined(atarist)) && defined(PACK_MALLOC)
38 * The description below is applicable if PACK_MALLOC is not defined.
40 * The overhead on a block is at least 4 bytes. When free, this space
41 * contains a pointer to the next free block, and the bottom two bits must
42 * be zero. When in use, the first byte is set to MAGIC, and the second
43 * byte is the size index. The remaining bytes are for alignment.
44 * If range checking is enabled and the size of the block fits
45 * in two bytes, then the top two bytes hold the size of the requested block
46 * plus the range checking words, and the header word MINUS ONE.
49 union overhead *ov_next; /* when free */
50 #if MEM_ALIGNBYTES > 4
51 double strut; /* alignment problems */
54 u_char ovu_magic; /* magic number */
55 u_char ovu_index; /* bucket # */
57 u_short ovu_size; /* actual block size */
58 u_int ovu_rmagic; /* range magic number */
61 #define ov_magic ovu.ovu_magic
62 #define ov_index ovu.ovu_index
63 #define ov_size ovu.ovu_size
64 #define ov_rmagic ovu.ovu_rmagic
68 static void botch _((char *s));
70 static void morecore _((int bucket));
71 static int findbucket _((union overhead *freep, int srchlen));
73 #define MAGIC 0xff /* magic # on accounting info */
74 #define RMAGIC 0x55555555 /* magic # on range info */
76 #define RSLOP sizeof (u_int)
83 * In this case it is assumed that if we do sbrk() in 2K units, we
84 * will get 2K aligned blocks. The bucket number of the given subblock is
85 * on the boundary of 2K block which contains the subblock.
86 * Several following bytes contain the magic numbers for the subblocks
89 * Sizes of chunks are powers of 2 for chunks in buckets <=
90 * MAX_PACKED, after this they are (2^n - sizeof(union overhead)) (to
91 * get alignment right).
93 * We suppose that starts of all the chunks in a 2K block are in
94 * different 2^n-byte-long chunks. If the top of the last chunk is
95 * aligned on a boundary of 2K block, this means that
96 * sizeof(union overhead)*"number of chunks" < 2^n, or
97 * sizeof(union overhead)*2K < 4^n, or n > 6 + log2(sizeof()/2)/2, if a
98 * chunk of size 2^n - overhead is used. Since this rules out n = 7
99 * for 8 byte alignment, we specialcase allocation of the first of 16
100 * 128-byte-long chunks.
102 * Note that with the above assumption we automatically have enough
103 * place for MAGIC at the start of 2K block. Note also that we
104 * overlay union overhead over the chunk, thus the start of the chunk
105 * is immediately overwritten after freeing.
107 # define MAX_PACKED 6
108 # define MAX_2_POT_ALGO ((1<<(MAX_PACKED + 1)) - M_OVERHEAD)
109 # define TWOK_MASK ((1<<11) - 1)
110 # define TWOK_MASKED(x) ((int)x & ~TWOK_MASK)
111 # define TWOK_SHIFT(x) ((int)x & TWOK_MASK)
112 # define OV_INDEXp(block) ((u_char*)(TWOK_MASKED(block)))
113 # define OV_INDEX(block) (*OV_INDEXp(block))
114 # define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) + \
115 (TWOK_SHIFT(block)>>(bucket + 3)) + \
116 (bucket > MAX_NONSHIFT ? 1 : 0)))
117 # define CHUNK_SHIFT 0
119 static u_char n_blks[11 - 3] = {224, 120, 62, 31, 16, 8, 4, 2};
120 static u_short blk_shift[11 - 3] = {256, 128, 64, 32,
121 16*sizeof(union overhead),
122 8*sizeof(union overhead),
123 4*sizeof(union overhead),
124 2*sizeof(union overhead),
125 # define MAX_NONSHIFT 2 /* Shift 64 greater than chunk 32. */
128 # ifdef DEBUGGING_MSTATS
129 static u_int sbrk_slack;
130 static u_int start_slack;
133 #else /* !PACK_MALLOC */
135 # define OV_MAGIC(block,bucket) (block)->ov_magic
136 # define OV_INDEX(block) (block)->ov_index
137 # define CHUNK_SHIFT 1
138 #endif /* !PACK_MALLOC */
140 # define M_OVERHEAD (sizeof(union overhead) + RSLOP)
143 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
144 * smallest allocatable block is 8 bytes. The overhead information
145 * precedes the data area returned to the user.
148 static union overhead *nextf[NBUCKETS];
151 #define sbrk(a) Perl_sbrk(a)
152 char * Perl_sbrk _((int size));
157 #ifdef DEBUGGING_MSTATS
159 * nmalloc[i] is the difference between the number of mallocs and frees
160 * for a given block size.
162 static u_int nmalloc[NBUCKETS];
167 #define ASSERT(p) if (!(p)) botch("p"); else
173 printf("assertion botched: %s\n", s);
182 register MEM_SIZE nbytes;
184 register union overhead *p;
185 register int bucket = 0;
186 register MEM_SIZE shiftr;
190 MEM_SIZE size = nbytes;
194 if (nbytes > 0xffff) {
195 fprintf(stderr, "Allocation too large: %lx\n", (long)nbytes);
200 if ((long)nbytes < 0)
201 croak("panic: malloc");
203 #endif /* safemalloc */
206 * Convert amount of memory requested into
207 * closest block size stored in hash buckets
208 * which satisfies request. Account for
209 * space used per block for accounting.
212 if (nbytes > MAX_2_POT_ALGO) {
214 nbytes += M_OVERHEAD;
215 nbytes = (nbytes + 3) &~ 3;
217 } else if (nbytes == 0) {
221 shiftr = (nbytes - 1) >> 2;
222 /* apart from this loop, this is O(1) */
226 * If nothing in hash bucket right now,
227 * request more memory from the system.
229 if (nextf[bucket] == NULL)
231 if ((p = (union overhead *)nextf[bucket]) == NULL) {
234 fputs("Out of memory!\n", stderr);
243 DEBUG_m(fprintf(Perl_debug_log,"0x%lx: (%05d) malloc %ld bytes\n",
244 (unsigned long)(p+1),an++,(long)size));
245 #endif /* safemalloc */
247 /* remove from linked list */
249 if (*((int*)p) & (sizeof(union overhead) - 1))
250 fprintf(stderr,"Corrupt malloc ptr 0x%lx at 0x%lx\n",
251 (unsigned long)*((int*)p),(unsigned long)p);
253 nextf[bucket] = p->ov_next;
254 OV_MAGIC(p, bucket) = MAGIC;
256 OV_INDEX(p) = bucket;
258 #ifdef DEBUGGING_MSTATS
263 * Record allocated size of block and
264 * bound space with magic numbers.
266 if (nbytes <= 0x10000)
267 p->ov_size = nbytes - 1;
268 p->ov_rmagic = RMAGIC;
269 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
271 return ((Malloc_t)(p + CHUNK_SHIFT));
275 * Allocate more memory to the indicated bucket.
281 register union overhead *op;
282 register int rnu; /* 2^rnu bytes will be requested */
283 register int nblks; /* become nblks blocks of the desired size */
284 register MEM_SIZE siz;
290 * Insure memory is allocated
291 * on a page boundary. Should
292 * make getpageize call?
294 #ifndef atarist /* on the atari we dont have to worry about this */
295 op = (union overhead *)sbrk(0);
299 (void)sbrk(slack = 2048 - ((int)op & 0x7ff));
302 (void)sbrk(slack = 1024 - ((int)op & 0x3ff));
304 # if defined(DEBUGGING_MSTATS) && defined(PACK_MALLOC)
308 /* The sbrk(0) call on the I286 always returns the next segment */
312 #if !(defined(I286) || defined(atarist))
313 /* take 2k unless the block is bigger than that */
314 rnu = (bucket <= 8) ? 11 : bucket + 3;
316 /* take 16k unless the block is bigger than that
317 (80286s like large segments!), probably good on the atari too */
318 rnu = (bucket <= 11) ? 14 : bucket + 3;
320 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
322 rnu = bucket; Why anyone needs this? */
323 op = (union overhead *)sbrk(1L << rnu);
328 * Round up to minimum allocation size boundary
329 * and deduct from block count to reflect.
334 croak("panic: Off-page sbrk");
337 op = (union overhead *)(((MEM_SIZE)op + 8) &~ 7);
341 /* Again, this should always be ok on an 80286 */
344 * Add new memory allocated to that on
345 * free list for this hash bucket.
347 siz = 1 << (bucket + 3);
349 *(u_char*)op = bucket; /* Fill index. */
350 if (bucket <= MAX_PACKED - 3) {
351 op = (union overhead *) ((char*)op + blk_shift[bucket]);
352 nblks = n_blks[bucket];
353 # ifdef DEBUGGING_MSTATS
354 start_slack += blk_shift[bucket];
356 } else if (bucket <= 11 - 1 - 3) {
357 op = (union overhead *) ((char*)op + blk_shift[bucket]);
358 /* nblks = n_blks[bucket]; */
359 siz -= sizeof(union overhead);
360 } else op++; /* One chunk per block. */
361 #endif /* !PACK_MALLOC */
363 while (--nblks > 0) {
364 op->ov_next = (union overhead *)((caddr_t)op + siz);
365 op = (union overhead *)((caddr_t)op + siz);
367 #if defined(USE_PERL_SBRK) || defined(OS2)
368 /* all real sbrks return zeroe-d memory, perl's sbrk doesn't guarantee this */
369 op->ov_next = (union overhead *)NULL;
372 if (bucket == 7 - 3) { /* Special case, explanation is above. */
373 union overhead *n_op = nextf[7 - 3]->ov_next;
374 nextf[7 - 3] = (union overhead *)((caddr_t)nextf[7 - 3]
375 - sizeof(union overhead));
376 nextf[7 - 3]->ov_next = n_op;
378 #endif /* !PACK_MALLOC */
385 register MEM_SIZE size;
386 register union overhead *op;
387 char *cp = (char*)mp;
393 DEBUG_m(fprintf(Perl_debug_log,"0x%lx: (%05d) free\n",(unsigned long)cp,an++));
394 #endif /* safemalloc */
398 op = (union overhead *)((caddr_t)cp
399 - sizeof (union overhead) * CHUNK_SHIFT);
401 bucket = OV_INDEX(op);
404 ASSERT(OV_MAGIC(op, bucket) == MAGIC); /* make sure it was in use */
406 if (OV_MAGIC(op, bucket) != MAGIC) {
407 static bad_free_warn = -1;
408 if (bad_free_warn == -1) {
409 char *pbf = getenv("PERL_BADFREE");
410 bad_free_warn = (pbf) ? atoi(pbf) : 1;
415 warn("%s free() ignored",
416 op->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad");
418 warn("Bad free() ignored");
424 ASSERT(op->ov_rmagic == RMAGIC);
425 if (OV_INDEX(op) <= 13)
426 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
427 op->ov_rmagic = RMAGIC - 1;
429 ASSERT(OV_INDEX(op) < NBUCKETS);
431 op->ov_next = nextf[size];
433 #ifdef DEBUGGING_MSTATS
439 * When a program attempts "storage compaction" as mentioned in the
440 * old malloc man page, it realloc's an already freed block. Usually
441 * this is the last block it freed; occasionally it might be farther
442 * back. We have to search all the free lists for the block in order
443 * to determine its bucket: 1st we make one pass thru the lists
444 * checking only the first block in each; if that fails we search
445 * ``reall_srchlen'' blocks in each list for a match (the variable
446 * is extern so the caller can modify it). If that fails we just copy
447 * however many bytes was given to realloc() and hope it's not huge.
449 int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
456 register MEM_SIZE onb;
461 char *cp = (char*)mp;
465 MEM_SIZE size = nbytes;
469 if (nbytes > 0xffff) {
470 fprintf(stderr, "Reallocation too large: %lx\n", size);
475 return malloc(nbytes);
477 if ((long)nbytes < 0)
478 croak("panic: realloc");
480 #endif /* safemalloc */
482 op = (union overhead *)((caddr_t)cp
483 - sizeof (union overhead) * CHUNK_SHIFT);
485 if (OV_MAGIC(op, i) == MAGIC) {
489 * Already free, doing "compaction".
491 * Search for the old block of memory on the
492 * free list. First, check the most common
493 * case (last element free'd), then (this failing)
494 * the last ``reall_srchlen'' items free'd.
495 * If all lookups fail, then assume the size of
496 * the memory block being realloc'd is the
499 if ((i = findbucket(op, 1)) < 0 &&
500 (i = findbucket(op, reall_srchlen)) < 0)
503 onb = (1L << (i + 3)) -
505 (i <= (MAX_PACKED - 3) ? 0 : M_OVERHEAD)
510 /* avoid the copy if same size block */
512 nbytes <= onb && nbytes > (onb >> 1) - M_OVERHEAD) {
515 * Record new allocated size of block and
516 * bound space with magic numbers.
518 if (OV_INDEX(op) <= 13) {
520 * Convert amount of memory requested into
521 * closest block size stored in hash buckets
522 * which satisfies request. Account for
523 * space used per block for accounting.
525 nbytes += M_OVERHEAD;
526 nbytes = (nbytes + 3) &~ 3;
527 op->ov_size = nbytes - 1;
528 *((u_int *)((caddr_t)op + nbytes - RSLOP)) = RMAGIC;
534 if ((res = (char*)malloc(nbytes)) == NULL)
536 if (cp != res) /* common optimization */
537 Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char);
545 fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)res,an++);
546 fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",
547 (unsigned long)res,an++,(long)size);
550 #endif /* safemalloc */
551 return ((Malloc_t)res);
555 * Search ``srchlen'' elements of each free list for a block whose
556 * header starts at ``freep''. If srchlen is -1 search the whole list.
557 * Return bucket number, or -1 if not found.
560 findbucket(freep, srchlen)
561 union overhead *freep;
564 register union overhead *p;
567 for (i = 0; i < NBUCKETS; i++) {
569 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
579 calloc(elements, size)
580 register MEM_SIZE elements;
581 register MEM_SIZE size;
583 long sz = elements * size;
584 Malloc_t p = malloc(sz);
587 memset((void*)p, 0, sz);
592 #ifdef DEBUGGING_MSTATS
594 * mstats - print out statistics about malloc
596 * Prints two lines of numbers, one showing the length of the free list
597 * for each size category, the second showing the number of mallocs -
598 * frees for each size category.
605 register union overhead *p;
606 int topbucket=0, totfree=0, totused=0;
607 u_int nfree[NBUCKETS];
609 for (i=0; i < NBUCKETS; i++) {
610 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
613 totfree += nfree[i] * (1 << (i + 3));
614 totused += nmalloc[i] * (1 << (i + 3));
615 if (nfree[i] || nmalloc[i])
619 fprintf(stderr, "Memory allocation statistics %s (buckets 8..%d)\n",
620 s, (1 << (topbucket + 3)) );
621 fprintf(stderr, " %7d free: ", totfree);
622 for (i=0; i <= topbucket; i++) {
623 fprintf(stderr, (i<5)?" %5d":" %3d", nfree[i]);
625 fprintf(stderr, "\n %7d used: ", totused);
626 for (i=0; i <= topbucket; i++) {
627 fprintf(stderr, (i<5)?" %5d":" %3d", nmalloc[i]);
629 fprintf(stderr, "\n");
631 if (sbrk_slack || start_slack) {
632 fprintf(stderr, "Odd ends: %7d bytes from sbrk(), %7d from malloc.\n",
633 sbrk_slack, start_slack);
653 #include "Error: -DUSE_PERL_SBRK on the NeXT requires -DHIDEMYMALLOC"
656 /* it may seem schizophrenic to use perl's malloc and let it call system */
657 /* malloc, the reason for that is only the 3.2 version of the OS that had */
658 /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */
659 /* end to the cores */
661 #define SYSTEM_ALLOC(a) malloc(a)
665 /* OS/2 comes to mind ... */
670 static IV Perl_sbrk_oldchunk;
671 static long Perl_sbrk_oldsize;
673 #define PERLSBRK_32_K (1<<15)
674 #define PERLSBRK_64_K (1<<16)
685 reqsize = size; /* just for the DEBUG_m statement */
687 if (size <= Perl_sbrk_oldsize) {
688 got = Perl_sbrk_oldchunk;
689 Perl_sbrk_oldchunk += size;
690 Perl_sbrk_oldsize -= size;
692 if (size >= PERLSBRK_32_K) {
698 size = PERLSBRK_64_K;
701 got = (IV)SYSTEM_ALLOC(size);
703 /* Chunk is small, register the rest for future allocs. */
704 Perl_sbrk_oldchunk = got + reqsize;
705 Perl_sbrk_oldsize = size - reqsize;
710 DEBUG_m(fprintf(stderr,"sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%lx\n",
711 size, reqsize, Perl_sbrk_oldsize, got));
717 #endif /* ! defined USE_PERL_SBRK */