1 /* $Header: malloc.c,v 3.0.1.1 89/10/26 23:15:05 lwall Locked $
4 * Revision 3.0.1.1 89/10/26 23:15:05 lwall
5 * patch1: some declarations were missing from malloc.c
6 * patch1: sparc machines had alignment problems in malloc.c
8 * Revision 3.0 89/10/18 15:20:39 lwall
14 static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
20 * malloc.c (Caltech) 2/21/82
21 * Chris Kingsley, kingsley@cit-20.
23 * This is a very fast storage allocator. It allocates blocks of a small
24 * number of different sizes, and keeps free lists of each size. Blocks that
25 * don't exactly fit are passed up to the next larger size. In this
26 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
27 * This is designed for use in a program that uses vast quantities of memory,
28 * but bombs when it runs out.
34 static findbucket(), morecore();
36 /* I don't much care whether these are defined in sys/types.h--LAW */
38 #define u_char unsigned char
39 #define u_int unsigned int
40 #define u_short unsigned short
43 * The overhead on a block is at least 4 bytes. When free, this space
44 * contains a pointer to the next free block, and the bottom two bits must
45 * be zero. When in use, the first byte is set to MAGIC, and the second
46 * byte is the size index. The remaining bytes are for alignment.
47 * If range checking is enabled and the size of the block fits
48 * in two bytes, then the top two bytes hold the size of the requested block
49 * plus the range checking words, and the header word MINUS ONE.
52 union overhead *ov_next; /* when free */
53 #if defined (mips) || defined (sparc)
54 double strut; /* alignment problems */
57 u_char ovu_magic; /* magic number */
58 u_char ovu_index; /* bucket # */
60 u_short ovu_size; /* actual block size */
61 u_int ovu_rmagic; /* range magic number */
64 #define ov_magic ovu.ovu_magic
65 #define ov_index ovu.ovu_index
66 #define ov_size ovu.ovu_size
67 #define ov_rmagic ovu.ovu_rmagic
70 #define MAGIC 0xff /* magic # on accounting info */
71 #define OLDMAGIC 0x7f /* same after a free() */
72 #define RMAGIC 0x55555555 /* magic # on range info */
74 #define RSLOP sizeof (u_int)
80 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
81 * smallest allocatable block is 8 bytes. The overhead information
82 * precedes the data area returned to the user.
85 static union overhead *nextf[NBUCKETS];
90 * nmalloc[i] is the difference between the number of mallocs and frees
91 * for a given block size.
93 static u_int nmalloc[NBUCKETS];
98 #define ASSERT(p) if (!(p)) botch("p"); else
104 printf("assertion botched: %s\n", s);
113 register unsigned nbytes;
115 register union overhead *p;
116 register int bucket = 0;
117 register unsigned shiftr;
120 * Convert amount of memory requested into
121 * closest block size stored in hash buckets
122 * which satisfies request. Account for
123 * space used per block for accounting.
125 nbytes += sizeof (union overhead) + RSLOP;
126 nbytes = (nbytes + 3) &~ 3;
127 shiftr = (nbytes - 1) >> 2;
128 /* apart from this loop, this is O(1) */
132 * If nothing in hash bucket right now,
133 * request more memory from the system.
135 if (nextf[bucket] == NULL)
137 if ((p = (union overhead *)nextf[bucket]) == NULL)
139 /* remove from linked list */
140 if (*((int*)p) > 0x10000000)
142 fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
144 fprintf(stderr,"Corrupt malloc ptr 0x%lx at 0x%lx\n",*((int*)p),p);
146 nextf[bucket] = nextf[bucket]->ov_next;
154 * Record allocated size of block and
155 * bound space with magic numbers.
157 if (nbytes <= 0x10000)
158 p->ov_size = nbytes - 1;
159 p->ov_rmagic = RMAGIC;
160 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
162 return ((char *)(p + 1));
166 * Allocate more memory to the indicated bucket.
172 register union overhead *op;
173 register int rnu; /* 2^rnu bytes will be requested */
174 register int nblks; /* become nblks blocks of the desired size */
180 * Insure memory is allocated
181 * on a page boundary. Should
182 * make getpageize call?
184 op = (union overhead *)sbrk(0);
187 (void)sbrk(1024 - ((int)op & 0x3ff));
189 /* The sbrk(0) call on the I286 always returns the next segment */
193 /* take 2k unless the block is bigger than that */
194 rnu = (bucket <= 8) ? 11 : bucket + 3;
196 /* take 16k unless the block is bigger than that
197 (80286s like large segments!) */
198 rnu = (bucket <= 11) ? 14 : bucket + 3;
200 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
203 op = (union overhead *)sbrk(1 << rnu);
208 * Round up to minimum allocation size boundary
209 * and deduct from block count to reflect.
213 op = (union overhead *)(((int)op + 8) &~ 7);
217 /* Again, this should always be ok on an 80286 */
220 * Add new memory allocated to that on
221 * free list for this hash bucket.
224 siz = 1 << (bucket + 3);
225 while (--nblks > 0) {
226 op->ov_next = (union overhead *)((caddr_t)op + siz);
227 op = (union overhead *)((caddr_t)op + siz);
235 register union overhead *op;
239 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
241 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
243 if (op->ov_magic != MAGIC) {
244 warn("%s free() ignored",
245 op->ov_magic == OLDMAGIC ? "Duplicate" : "Bad");
248 op->ov_magic = OLDMAGIC;
251 ASSERT(op->ov_rmagic == RMAGIC);
252 if (op->ov_index <= 13)
253 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
255 ASSERT(op->ov_index < NBUCKETS);
257 op->ov_next = nextf[size];
265 * When a program attempts "storage compaction" as mentioned in the
266 * old malloc man page, it realloc's an already freed block. Usually
267 * this is the last block it freed; occasionally it might be farther
268 * back. We have to search all the free lists for the block in order
269 * to determine its bucket: 1st we make one pass thru the lists
270 * checking only the first block in each; if that fails we search
271 * ``reall_srchlen'' blocks in each list for a match (the variable
272 * is extern so the caller can modify it). If that fails we just copy
273 * however many bytes was given to realloc() and hope it's not huge.
275 int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
289 return (malloc(nbytes));
290 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
291 if (op->ov_magic == MAGIC) {
296 * Already free, doing "compaction".
298 * Search for the old block of memory on the
299 * free list. First, check the most common
300 * case (last element free'd), then (this failing)
301 * the last ``reall_srchlen'' items free'd.
302 * If all lookups fail, then assume the size of
303 * the memory block being realloc'd is the
306 if ((i = findbucket(op, 1)) < 0 &&
307 (i = findbucket(op, reall_srchlen)) < 0)
310 onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
311 /* avoid the copy if same size block */
313 nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP) {
316 * Record new allocated size of block and
317 * bound space with magic numbers.
319 if (op->ov_index <= 13) {
321 * Convert amount of memory requested into
322 * closest block size stored in hash buckets
323 * which satisfies request. Account for
324 * space used per block for accounting.
326 nbytes += sizeof (union overhead) + RSLOP;
327 nbytes = (nbytes + 3) &~ 3;
328 op->ov_size = nbytes - 1;
329 *((u_int *)((caddr_t)op + nbytes - RSLOP)) = RMAGIC;
334 if ((res = malloc(nbytes)) == NULL)
336 if (cp != res) /* common optimization */
337 (void)bcopy(cp, res, (int)((nbytes < onb) ? nbytes : onb));
344 * Search ``srchlen'' elements of each free list for a block whose
345 * header starts at ``freep''. If srchlen is -1 search the whole list.
346 * Return bucket number, or -1 if not found.
349 findbucket(freep, srchlen)
350 union overhead *freep;
353 register union overhead *p;
356 for (i = 0; i < NBUCKETS; i++) {
358 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
369 * mstats - print out statistics about malloc
371 * Prints two lines of numbers, one showing the length of the free list
372 * for each size category, the second showing the number of mallocs -
373 * frees for each size category.
379 register union overhead *p;
383 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
384 for (i = 0; i < NBUCKETS; i++) {
385 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
387 fprintf(stderr, " %d", j);
388 totfree += j * (1 << (i + 3));
390 fprintf(stderr, "\nused:\t");
391 for (i = 0; i < NBUCKETS; i++) {
392 fprintf(stderr, " %d", nmalloc[i]);
393 totused += nmalloc[i] * (1 << (i + 3));
395 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",