1 /* $RCSfile: malloc.c,v $$Revision: 4.0.1.2 $$Date: 91/06/07 11:20:45 $
4 * Revision 4.0.1.2 91/06/07 11:20:45 lwall
5 * patch4: many, many itty-bitty portability fixes
7 * Revision 4.0.1.1 91/04/11 17:48:31 lwall
8 * patch1: Configure now figures out malloc ptr type
10 * Revision 4.0 91/03/20 01:28:52 lwall
16 static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
22 * malloc.c (Caltech) 2/21/82
23 * Chris Kingsley, kingsley@cit-20.
25 * This is a very fast storage allocator. It allocates blocks of a small
26 * number of different sizes, and keeps free lists of each size. Blocks that
27 * don't exactly fit are passed up to the next larger size. In this
28 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
29 * This is designed for use in a program that uses vast quantities of memory,
30 * but bombs when it runs out.
36 static findbucket(), morecore();
38 /* I don't much care whether these are defined in sys/types.h--LAW */
40 #define u_char unsigned char
41 #define u_int unsigned int
42 #define u_short unsigned short
45 * The overhead on a block is at least 4 bytes. When free, this space
46 * contains a pointer to the next free block, and the bottom two bits must
47 * be zero. When in use, the first byte is set to MAGIC, and the second
48 * byte is the size index. The remaining bytes are for alignment.
49 * If range checking is enabled and the size of the block fits
50 * in two bytes, then the top two bytes hold the size of the requested block
51 * plus the range checking words, and the header word MINUS ONE.
54 union overhead *ov_next; /* when free */
56 double strut; /* alignment problems */
59 u_char ovu_magic; /* magic number */
60 u_char ovu_index; /* bucket # */
62 u_short ovu_size; /* actual block size */
63 u_int ovu_rmagic; /* range magic number */
66 #define ov_magic ovu.ovu_magic
67 #define ov_index ovu.ovu_index
68 #define ov_size ovu.ovu_size
69 #define ov_rmagic ovu.ovu_rmagic
72 #define MAGIC 0xff /* magic # on accounting info */
73 #define OLDMAGIC 0x7f /* same after a free() */
74 #define RMAGIC 0x55555555 /* magic # on range info */
76 #define RSLOP sizeof (u_int)
82 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
83 * smallest allocatable block is 8 bytes. The overhead information
84 * precedes the data area returned to the user.
87 static union overhead *nextf[NBUCKETS];
92 * nmalloc[i] is the difference between the number of mallocs and frees
93 * for a given block size.
95 static u_int nmalloc[NBUCKETS];
100 #define ASSERT(p) if (!(p)) botch("p"); else
106 printf("assertion botched: %s\n", s);
115 register unsigned nbytes;
117 register union overhead *p;
118 register int bucket = 0;
119 register unsigned shiftr;
122 * Convert amount of memory requested into
123 * closest block size stored in hash buckets
124 * which satisfies request. Account for
125 * space used per block for accounting.
127 nbytes += sizeof (union overhead) + RSLOP;
128 nbytes = (nbytes + 3) &~ 3;
129 shiftr = (nbytes - 1) >> 2;
130 /* apart from this loop, this is O(1) */
134 * If nothing in hash bucket right now,
135 * request more memory from the system.
137 if (nextf[bucket] == NULL)
139 if ((p = (union overhead *)nextf[bucket]) == NULL)
141 /* remove from linked list */
143 if (*((int*)p) & (sizeof(union overhead) - 1))
145 fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
147 fprintf(stderr,"Corrupt malloc ptr 0x%lx at 0x%lx\n",*((int*)p),p);
150 nextf[bucket] = p->ov_next;
158 * Record allocated size of block and
159 * bound space with magic numbers.
161 if (nbytes <= 0x10000)
162 p->ov_size = nbytes - 1;
163 p->ov_rmagic = RMAGIC;
164 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
166 return ((MALLOCPTRTYPE *)(p + 1));
170 * Allocate more memory to the indicated bucket.
176 register union overhead *op;
177 register int rnu; /* 2^rnu bytes will be requested */
178 register int nblks; /* become nblks blocks of the desired size */
184 * Insure memory is allocated
185 * on a page boundary. Should
186 * make getpageize call?
188 op = (union overhead *)sbrk(0);
191 (void)sbrk(1024 - ((int)op & 0x3ff));
193 /* The sbrk(0) call on the I286 always returns the next segment */
197 /* take 2k unless the block is bigger than that */
198 rnu = (bucket <= 8) ? 11 : bucket + 3;
200 /* take 16k unless the block is bigger than that
201 (80286s like large segments!) */
202 rnu = (bucket <= 11) ? 14 : bucket + 3;
204 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
207 op = (union overhead *)sbrk(1 << rnu);
212 * Round up to minimum allocation size boundary
213 * and deduct from block count to reflect.
217 op = (union overhead *)(((int)op + 8) &~ 7);
221 /* Again, this should always be ok on an 80286 */
224 * Add new memory allocated to that on
225 * free list for this hash bucket.
228 siz = 1 << (bucket + 3);
229 while (--nblks > 0) {
230 op->ov_next = (union overhead *)((caddr_t)op + siz);
231 op = (union overhead *)((caddr_t)op + siz);
240 register union overhead *op;
241 char *cp = (char*)mp;
245 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
247 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
249 if (op->ov_magic != MAGIC) {
250 warn("%s free() ignored",
251 op->ov_magic == OLDMAGIC ? "Duplicate" : "Bad");
254 op->ov_magic = OLDMAGIC;
257 ASSERT(op->ov_rmagic == RMAGIC);
258 if (op->ov_index <= 13)
259 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
261 ASSERT(op->ov_index < NBUCKETS);
263 op->ov_next = nextf[size];
271 * When a program attempts "storage compaction" as mentioned in the
272 * old malloc man page, it realloc's an already freed block. Usually
273 * this is the last block it freed; occasionally it might be farther
274 * back. We have to search all the free lists for the block in order
275 * to determine its bucket: 1st we make one pass thru the lists
276 * checking only the first block in each; if that fails we search
277 * ``reall_srchlen'' blocks in each list for a match (the variable
278 * is extern so the caller can modify it). If that fails we just copy
279 * however many bytes was given to realloc() and hope it's not huge.
281 int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
293 char *cp = (char*)mp;
296 return (malloc(nbytes));
297 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
298 if (op->ov_magic == MAGIC) {
303 * Already free, doing "compaction".
305 * Search for the old block of memory on the
306 * free list. First, check the most common
307 * case (last element free'd), then (this failing)
308 * the last ``reall_srchlen'' items free'd.
309 * If all lookups fail, then assume the size of
310 * the memory block being realloc'd is the
313 if ((i = findbucket(op, 1)) < 0 &&
314 (i = findbucket(op, reall_srchlen)) < 0)
317 onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
318 /* avoid the copy if same size block */
320 nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP) {
323 * Record new allocated size of block and
324 * bound space with magic numbers.
326 if (op->ov_index <= 13) {
328 * Convert amount of memory requested into
329 * closest block size stored in hash buckets
330 * which satisfies request. Account for
331 * space used per block for accounting.
333 nbytes += sizeof (union overhead) + RSLOP;
334 nbytes = (nbytes + 3) &~ 3;
335 op->ov_size = nbytes - 1;
336 *((u_int *)((caddr_t)op + nbytes - RSLOP)) = RMAGIC;
339 return((MALLOCPTRTYPE*)cp);
341 if ((res = (char*)malloc(nbytes)) == NULL)
343 if (cp != res) /* common optimization */
344 (void)bcopy(cp, res, (int)((nbytes < onb) ? nbytes : onb));
347 return ((MALLOCPTRTYPE*)res);
351 * Search ``srchlen'' elements of each free list for a block whose
352 * header starts at ``freep''. If srchlen is -1 search the whole list.
353 * Return bucket number, or -1 if not found.
356 findbucket(freep, srchlen)
357 union overhead *freep;
360 register union overhead *p;
363 for (i = 0; i < NBUCKETS; i++) {
365 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
376 * mstats - print out statistics about malloc
378 * Prints two lines of numbers, one showing the length of the free list
379 * for each size category, the second showing the number of mallocs -
380 * frees for each size category.
386 register union overhead *p;
390 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
391 for (i = 0; i < NBUCKETS; i++) {
392 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
394 fprintf(stderr, " %d", j);
395 totfree += j * (1 << (i + 3));
397 fprintf(stderr, "\nused:\t");
398 for (i = 0; i < NBUCKETS; i++) {
399 fprintf(stderr, " %d", nmalloc[i]);
400 totused += nmalloc[i] * (1 << (i + 3));
402 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",