1 /* $Header: malloc.c,v 3.0.1.3 90/10/16 15:27:47 lwall Locked $
4 * Revision 3.0.1.3 90/10/16 15:27:47 lwall
5 * patch29: various portability fixes
7 * Revision 3.0.1.2 89/11/11 04:36:37 lwall
8 * patch2: malloc pointer corruption check made more portable
10 * Revision 3.0.1.1 89/10/26 23:15:05 lwall
11 * patch1: some declarations were missing from malloc.c
12 * patch1: sparc machines had alignment problems in malloc.c
14 * Revision 3.0 89/10/18 15:20:39 lwall
20 static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
26 * malloc.c (Caltech) 2/21/82
27 * Chris Kingsley, kingsley@cit-20.
29 * This is a very fast storage allocator. It allocates blocks of a small
30 * number of different sizes, and keeps free lists of each size. Blocks that
31 * don't exactly fit are passed up to the next larger size. In this
32 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
33 * This is designed for use in a program that uses vast quantities of memory,
34 * but bombs when it runs out.
40 static findbucket(), morecore();
42 /* I don't much care whether these are defined in sys/types.h--LAW */
44 #define u_char unsigned char
45 #define u_int unsigned int
46 #define u_short unsigned short
49 * The overhead on a block is at least 4 bytes. When free, this space
50 * contains a pointer to the next free block, and the bottom two bits must
51 * be zero. When in use, the first byte is set to MAGIC, and the second
52 * byte is the size index. The remaining bytes are for alignment.
53 * If range checking is enabled and the size of the block fits
54 * in two bytes, then the top two bytes hold the size of the requested block
55 * plus the range checking words, and the header word MINUS ONE.
58 union overhead *ov_next; /* when free */
59 #if defined(mips) || defined(sparc) || defined(luna88k)
60 double strut; /* alignment problems */
63 u_char ovu_magic; /* magic number */
64 u_char ovu_index; /* bucket # */
66 u_short ovu_size; /* actual block size */
67 u_int ovu_rmagic; /* range magic number */
70 #define ov_magic ovu.ovu_magic
71 #define ov_index ovu.ovu_index
72 #define ov_size ovu.ovu_size
73 #define ov_rmagic ovu.ovu_rmagic
76 #define MAGIC 0xff /* magic # on accounting info */
77 #define OLDMAGIC 0x7f /* same after a free() */
78 #define RMAGIC 0x55555555 /* magic # on range info */
80 #define RSLOP sizeof (u_int)
86 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
87 * smallest allocatable block is 8 bytes. The overhead information
88 * precedes the data area returned to the user.
91 static union overhead *nextf[NBUCKETS];
96 * nmalloc[i] is the difference between the number of mallocs and frees
97 * for a given block size.
99 static u_int nmalloc[NBUCKETS];
104 #define ASSERT(p) if (!(p)) botch("p"); else
110 printf("assertion botched: %s\n", s);
119 register unsigned nbytes;
121 register union overhead *p;
122 register int bucket = 0;
123 register unsigned shiftr;
126 * Convert amount of memory requested into
127 * closest block size stored in hash buckets
128 * which satisfies request. Account for
129 * space used per block for accounting.
131 nbytes += sizeof (union overhead) + RSLOP;
132 nbytes = (nbytes + 3) &~ 3;
133 shiftr = (nbytes - 1) >> 2;
134 /* apart from this loop, this is O(1) */
138 * If nothing in hash bucket right now,
139 * request more memory from the system.
141 if (nextf[bucket] == NULL)
143 if ((p = (union overhead *)nextf[bucket]) == NULL)
145 /* remove from linked list */
147 if (*((int*)p) & (sizeof(union overhead) - 1))
149 fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
151 fprintf(stderr,"Corrupt malloc ptr 0x%lx at 0x%lx\n",*((int*)p),p);
154 nextf[bucket] = p->ov_next;
162 * Record allocated size of block and
163 * bound space with magic numbers.
165 if (nbytes <= 0x10000)
166 p->ov_size = nbytes - 1;
167 p->ov_rmagic = RMAGIC;
168 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
170 return ((char *)(p + 1));
174 * Allocate more memory to the indicated bucket.
180 register union overhead *op;
181 register int rnu; /* 2^rnu bytes will be requested */
182 register int nblks; /* become nblks blocks of the desired size */
188 * Insure memory is allocated
189 * on a page boundary. Should
190 * make getpageize call?
192 op = (union overhead *)sbrk(0);
195 (void)sbrk(1024 - ((int)op & 0x3ff));
197 /* The sbrk(0) call on the I286 always returns the next segment */
201 /* take 2k unless the block is bigger than that */
202 rnu = (bucket <= 8) ? 11 : bucket + 3;
204 /* take 16k unless the block is bigger than that
205 (80286s like large segments!) */
206 rnu = (bucket <= 11) ? 14 : bucket + 3;
208 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
211 op = (union overhead *)sbrk(1 << rnu);
216 * Round up to minimum allocation size boundary
217 * and deduct from block count to reflect.
221 op = (union overhead *)(((int)op + 8) &~ 7);
225 /* Again, this should always be ok on an 80286 */
228 * Add new memory allocated to that on
229 * free list for this hash bucket.
232 siz = 1 << (bucket + 3);
233 while (--nblks > 0) {
234 op->ov_next = (union overhead *)((caddr_t)op + siz);
235 op = (union overhead *)((caddr_t)op + siz);
243 register union overhead *op;
247 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
249 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
251 if (op->ov_magic != MAGIC) {
252 warn("%s free() ignored",
253 op->ov_magic == OLDMAGIC ? "Duplicate" : "Bad");
256 op->ov_magic = OLDMAGIC;
259 ASSERT(op->ov_rmagic == RMAGIC);
260 if (op->ov_index <= 13)
261 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
263 ASSERT(op->ov_index < NBUCKETS);
265 op->ov_next = nextf[size];
273 * When a program attempts "storage compaction" as mentioned in the
274 * old malloc man page, it realloc's an already freed block. Usually
275 * this is the last block it freed; occasionally it might be farther
276 * back. We have to search all the free lists for the block in order
277 * to determine its bucket: 1st we make one pass thru the lists
278 * checking only the first block in each; if that fails we search
279 * ``reall_srchlen'' blocks in each list for a match (the variable
280 * is extern so the caller can modify it). If that fails we just copy
281 * however many bytes was given to realloc() and hope it's not huge.
283 int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
297 return (malloc(nbytes));
298 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
299 if (op->ov_magic == MAGIC) {
304 * Already free, doing "compaction".
306 * Search for the old block of memory on the
307 * free list. First, check the most common
308 * case (last element free'd), then (this failing)
309 * the last ``reall_srchlen'' items free'd.
310 * If all lookups fail, then assume the size of
311 * the memory block being realloc'd is the
314 if ((i = findbucket(op, 1)) < 0 &&
315 (i = findbucket(op, reall_srchlen)) < 0)
318 onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
319 /* avoid the copy if same size block */
321 nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP) {
324 * Record new allocated size of block and
325 * bound space with magic numbers.
327 if (op->ov_index <= 13) {
329 * Convert amount of memory requested into
330 * closest block size stored in hash buckets
331 * which satisfies request. Account for
332 * space used per block for accounting.
334 nbytes += sizeof (union overhead) + RSLOP;
335 nbytes = (nbytes + 3) &~ 3;
336 op->ov_size = nbytes - 1;
337 *((u_int *)((caddr_t)op + nbytes - RSLOP)) = RMAGIC;
342 if ((res = malloc(nbytes)) == NULL)
344 if (cp != res) /* common optimization */
345 (void)bcopy(cp, res, (int)((nbytes < onb) ? nbytes : onb));
352 * Search ``srchlen'' elements of each free list for a block whose
353 * header starts at ``freep''. If srchlen is -1 search the whole list.
354 * Return bucket number, or -1 if not found.
357 findbucket(freep, srchlen)
358 union overhead *freep;
361 register union overhead *p;
364 for (i = 0; i < NBUCKETS; i++) {
366 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
377 * mstats - print out statistics about malloc
379 * Prints two lines of numbers, one showing the length of the free list
380 * for each size category, the second showing the number of mallocs -
381 * frees for each size category.
387 register union overhead *p;
391 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
392 for (i = 0; i < NBUCKETS; i++) {
393 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
395 fprintf(stderr, " %d", j);
396 totfree += j * (1 << (i + 3));
398 fprintf(stderr, "\nused:\t");
399 for (i = 0; i < NBUCKETS; i++) {
400 fprintf(stderr, " %d", nmalloc[i]);
401 totused += nmalloc[i] * (1 << (i + 3));
403 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",