1 /* $Header: malloc.c,v 2.0 88/06/05 00:09:16 root Exp $
4 * Revision 2.0 88/06/05 00:09:16 root
5 * Baseline version 2.0.
10 static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
15 * malloc.c (Caltech) 2/21/82
16 * Chris Kingsley, kingsley@cit-20.
18 * This is a very fast storage allocator. It allocates blocks of a small
19 * number of different sizes, and keeps free lists of each size. Blocks that
20 * don't exactly fit are passed up to the next larger size. In this
21 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
22 * This is designed for use in a program that uses vast quantities of memory,
23 * but bombs when it runs out.
29 /* I don't much care whether these are defined in sys/types.h--LAW */
31 #define u_char unsigned char
32 #define u_int unsigned int
33 #define u_short unsigned short
36 * The overhead on a block is at least 4 bytes. When free, this space
37 * contains a pointer to the next free block, and the bottom two bits must
38 * be zero. When in use, the first byte is set to MAGIC, and the second
39 * byte is the size index. The remaining bytes are for alignment.
40 * If range checking is enabled and the size of the block fits
41 * in two bytes, then the top two bytes hold the size of the requested block
42 * plus the range checking words, and the header word MINUS ONE.
45 union overhead *ov_next; /* when free */
47 u_char ovu_magic; /* magic number */
48 u_char ovu_index; /* bucket # */
50 u_short ovu_size; /* actual block size */
51 u_int ovu_rmagic; /* range magic number */
54 #define ov_magic ovu.ovu_magic
55 #define ov_index ovu.ovu_index
56 #define ov_size ovu.ovu_size
57 #define ov_rmagic ovu.ovu_rmagic
60 #define MAGIC 0xff /* magic # on accounting info */
61 #define OLDMAGIC 0x7f /* same after a free() */
62 #define RMAGIC 0x55555555 /* magic # on range info */
64 #define RSLOP sizeof (u_int)
70 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
71 * smallest allocatable block is 8 bytes. The overhead information
72 * precedes the data area returned to the user.
75 static union overhead *nextf[NBUCKETS];
80 * nmalloc[i] is the difference between the number of mallocs and frees
81 * for a given block size.
83 static u_int nmalloc[NBUCKETS];
88 #define ASSERT(p) if (!(p)) botch("p"); else
94 printf("assertion botched: %s\n", s);
103 register unsigned nbytes;
105 register union overhead *p;
106 register int bucket = 0;
107 register unsigned shiftr;
110 * Convert amount of memory requested into
111 * closest block size stored in hash buckets
112 * which satisfies request. Account for
113 * space used per block for accounting.
115 nbytes += sizeof (union overhead) + RSLOP;
116 nbytes = (nbytes + 3) &~ 3;
117 shiftr = (nbytes - 1) >> 2;
118 /* apart from this loop, this is O(1) */
122 * If nothing in hash bucket right now,
123 * request more memory from the system.
125 if (nextf[bucket] == NULL)
127 if ((p = (union overhead *)nextf[bucket]) == NULL)
129 /* remove from linked list */
130 if (*((int*)p) > 0x10000000)
131 fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
132 nextf[bucket] = nextf[bucket]->ov_next;
140 * Record allocated size of block and
141 * bound space with magic numbers.
143 if (nbytes <= 0x10000)
144 p->ov_size = nbytes - 1;
145 p->ov_rmagic = RMAGIC;
146 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
148 return ((char *)(p + 1));
152 * Allocate more memory to the indicated bucket.
158 register union overhead *op;
159 register int rnu; /* 2^rnu bytes will be requested */
160 register int nblks; /* become nblks blocks of the desired size */
166 * Insure memory is allocated
167 * on a page boundary. Should
168 * make getpageize call?
170 op = (union overhead *)sbrk(0);
172 sbrk(1024 - ((int)op & 0x3ff));
173 /* take 2k unless the block is bigger than that */
174 rnu = (bucket <= 8) ? 11 : bucket + 3;
175 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
178 op = (union overhead *)sbrk(1 << rnu);
183 * Round up to minimum allocation size boundary
184 * and deduct from block count to reflect.
187 op = (union overhead *)(((int)op + 8) &~ 7);
191 * Add new memory allocated to that on
192 * free list for this hash bucket.
195 siz = 1 << (bucket + 3);
196 while (--nblks > 0) {
197 op->ov_next = (union overhead *)((caddr_t)op + siz);
198 op = (union overhead *)((caddr_t)op + siz);
206 register union overhead *op;
210 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
212 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
214 if (op->ov_magic != MAGIC) {
215 fprintf(stderr,"%s free() ignored\n",
216 op->ov_magic == OLDMAGIC ? "Duplicate" : "Bad");
219 op->ov_magic = OLDMAGIC;
222 ASSERT(op->ov_rmagic == RMAGIC);
223 if (op->ov_index <= 13)
224 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
226 ASSERT(op->ov_index < NBUCKETS);
228 op->ov_next = nextf[size];
236 * When a program attempts "storage compaction" as mentioned in the
237 * old malloc man page, it realloc's an already freed block. Usually
238 * this is the last block it freed; occasionally it might be farther
239 * back. We have to search all the free lists for the block in order
240 * to determine its bucket: 1st we make one pass thru the lists
241 * checking only the first block in each; if that fails we search
242 * ``reall_srchlen'' blocks in each list for a match (the variable
243 * is extern so the caller can modify it). If that fails we just copy
244 * however many bytes was given to realloc() and hope it's not huge.
246 int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
260 return (malloc(nbytes));
261 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
262 if (op->ov_magic == MAGIC) {
267 * Already free, doing "compaction".
269 * Search for the old block of memory on the
270 * free list. First, check the most common
271 * case (last element free'd), then (this failing)
272 * the last ``reall_srchlen'' items free'd.
273 * If all lookups fail, then assume the size of
274 * the memory block being realloc'd is the
277 if ((i = findbucket(op, 1)) < 0 &&
278 (i = findbucket(op, reall_srchlen)) < 0)
281 onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
282 /* avoid the copy if same size block */
284 nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP)
286 if ((res = malloc(nbytes)) == NULL)
288 if (cp != res) /* common optimization */
289 bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
296 * Search ``srchlen'' elements of each free list for a block whose
297 * header starts at ``freep''. If srchlen is -1 search the whole list.
298 * Return bucket number, or -1 if not found.
301 findbucket(freep, srchlen)
302 union overhead *freep;
305 register union overhead *p;
308 for (i = 0; i < NBUCKETS; i++) {
310 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
321 * mstats - print out statistics about malloc
323 * Prints two lines of numbers, one showing the length of the free list
324 * for each size category, the second showing the number of mallocs -
325 * frees for each size category.
331 register union overhead *p;
335 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
336 for (i = 0; i < NBUCKETS; i++) {
337 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
339 fprintf(stderr, " %d", j);
340 totfree += j * (1 << (i + 3));
342 fprintf(stderr, "\nused:\t");
343 for (i = 0; i < NBUCKETS; i++) {
344 fprintf(stderr, " %d", nmalloc[i]);
345 totused += nmalloc[i] * (1 << (i + 3));
347 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",