1 /* $Header: malloc.c,v 1.0 87/12/18 13:05:35 root Exp $
4 * Revision 1.0 87/12/18 13:05:35 root
10 static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
16 * malloc.c (Caltech) 2/21/82
17 * Chris Kingsley, kingsley@cit-20.
19 * This is a very fast storage allocator. It allocates blocks of a small
20 * number of different sizes, and keeps free lists of each size. Blocks that
21 * don't exactly fit are passed up to the next larger size. In this
22 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
23 * This is designed for use in a program that uses vast quantities of memory,
24 * but bombs when it runs out.
27 #include <sys/types.h>
32 * The overhead on a block is at least 4 bytes. When free, this space
33 * contains a pointer to the next free block, and the bottom two bits must
34 * be zero. When in use, the first byte is set to MAGIC, and the second
35 * byte is the size index. The remaining bytes are for alignment.
36 * If range checking is enabled and the size of the block fits
37 * in two bytes, then the top two bytes hold the size of the requested block
38 * plus the range checking words, and the header word MINUS ONE.
41 union overhead *ov_next; /* when free */
43 u_char ovu_magic; /* magic number */
44 u_char ovu_index; /* bucket # */
46 u_short ovu_size; /* actual block size */
47 u_int ovu_rmagic; /* range magic number */
50 #define ov_magic ovu.ovu_magic
51 #define ov_index ovu.ovu_index
52 #define ov_size ovu.ovu_size
53 #define ov_rmagic ovu.ovu_rmagic
56 #define MAGIC 0xff /* magic # on accounting info */
57 #define RMAGIC 0x55555555 /* magic # on range info */
59 #define RSLOP sizeof (u_int)
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.
70 static union overhead *nextf[NBUCKETS];
75 * nmalloc[i] is the difference between the number of mallocs and frees
76 * for a given block size.
78 static u_int nmalloc[NBUCKETS];
83 #define ASSERT(p) if (!(p)) botch("p"); else
89 printf("assertion botched: %s\n", s);
98 register unsigned nbytes;
100 register union overhead *p;
101 register int bucket = 0;
102 register unsigned shiftr;
105 * Convert amount of memory requested into
106 * closest block size stored in hash buckets
107 * which satisfies request. Account for
108 * space used per block for accounting.
110 nbytes += sizeof (union overhead) + RSLOP;
111 nbytes = (nbytes + 3) &~ 3;
112 shiftr = (nbytes - 1) >> 2;
113 /* apart from this loop, this is O(1) */
117 * If nothing in hash bucket right now,
118 * request more memory from the system.
120 if (nextf[bucket] == NULL)
122 if ((p = (union overhead *)nextf[bucket]) == NULL)
124 /* remove from linked list */
125 if (*((int*)p) > 0x10000000)
126 fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
127 nextf[bucket] = nextf[bucket]->ov_next;
135 * Record allocated size of block and
136 * bound space with magic numbers.
138 if (nbytes <= 0x10000)
139 p->ov_size = nbytes - 1;
140 p->ov_rmagic = RMAGIC;
141 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
143 return ((char *)(p + 1));
147 * Allocate more memory to the indicated bucket.
153 register union overhead *op;
154 register int rnu; /* 2^rnu bytes will be requested */
155 register int nblks; /* become nblks blocks of the desired size */
161 * Insure memory is allocated
162 * on a page boundary. Should
163 * make getpageize call?
165 op = (union overhead *)sbrk(0);
167 sbrk(1024 - ((int)op & 0x3ff));
168 /* take 2k unless the block is bigger than that */
169 rnu = (bucket <= 8) ? 11 : bucket + 3;
170 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
173 op = (union overhead *)sbrk(1 << rnu);
178 * Round up to minimum allocation size boundary
179 * and deduct from block count to reflect.
182 op = (union overhead *)(((int)op + 8) &~ 7);
186 * Add new memory allocated to that on
187 * free list for this hash bucket.
190 siz = 1 << (bucket + 3);
191 while (--nblks > 0) {
192 op->ov_next = (union overhead *)((caddr_t)op + siz);
193 op = (union overhead *)((caddr_t)op + siz);
201 register union overhead *op;
205 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
207 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
209 if (op->ov_magic != MAGIC)
213 ASSERT(op->ov_rmagic == RMAGIC);
214 if (op->ov_index <= 13)
215 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
217 ASSERT(op->ov_index < NBUCKETS);
219 op->ov_next = nextf[size];
227 * When a program attempts "storage compaction" as mentioned in the
228 * old malloc man page, it realloc's an already freed block. Usually
229 * this is the last block it freed; occasionally it might be farther
230 * back. We have to search all the free lists for the block in order
231 * to determine its bucket: 1st we make one pass thru the lists
232 * checking only the first block in each; if that fails we search
233 * ``realloc_srchlen'' blocks in each list for a match (the variable
234 * is extern so the caller can modify it). If that fails we just copy
235 * however many bytes was given to realloc() and hope it's not huge.
237 int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
251 return (malloc(nbytes));
252 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
253 if (op->ov_magic == MAGIC) {
258 * Already free, doing "compaction".
260 * Search for the old block of memory on the
261 * free list. First, check the most common
262 * case (last element free'd), then (this failing)
263 * the last ``realloc_srchlen'' items free'd.
264 * If all lookups fail, then assume the size of
265 * the memory block being realloc'd is the
268 if ((i = findbucket(op, 1)) < 0 &&
269 (i = findbucket(op, realloc_srchlen)) < 0)
272 onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
273 /* avoid the copy if same size block */
275 nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP)
277 if ((res = malloc(nbytes)) == NULL)
279 if (cp != res) /* common optimization */
280 bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
287 * Search ``srchlen'' elements of each free list for a block whose
288 * header starts at ``freep''. If srchlen is -1 search the whole list.
289 * Return bucket number, or -1 if not found.
292 findbucket(freep, srchlen)
293 union overhead *freep;
296 register union overhead *p;
299 for (i = 0; i < NBUCKETS; i++) {
301 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
312 * mstats - print out statistics about malloc
314 * Prints two lines of numbers, one showing the length of the free list
315 * for each size category, the second showing the number of mallocs -
316 * frees for each size category.
322 register union overhead *p;
326 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
327 for (i = 0; i < NBUCKETS; i++) {
328 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
330 fprintf(stderr, " %d", j);
331 totfree += j * (1 << (i + 3));
333 fprintf(stderr, "\nused:\t");
334 for (i = 0; i < NBUCKETS; i++) {
335 fprintf(stderr, " %d", nmalloc[i]);
336 totused += nmalloc[i] * (1 << (i + 3));
338 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",