1 /* $Header: malloc.c,v 1.0.1.1 88/01/24 03:53:23 root Exp $
4 * Revision 1.0.1.1 88/01/24 03:53:23 root
5 * patch 2: made depend on perl.h.
7 * Revision 1.0 87/12/18 13:05:35 root
13 static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
19 * malloc.c (Caltech) 2/21/82
20 * Chris Kingsley, kingsley@cit-20.
22 * This is a very fast storage allocator. It allocates blocks of a small
23 * number of different sizes, and keeps free lists of each size. Blocks that
24 * don't exactly fit are passed up to the next larger size. In this
25 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
26 * This is designed for use in a program that uses vast quantities of memory,
27 * but bombs when it runs out.
35 /* I don't much care whether these are defined in sys/types.h--LAW */
37 #define u_char unsigned char
38 #define u_int unsigned int
39 #define u_short unsigned short
44 * The overhead on a block is at least 4 bytes. When free, this space
45 * contains a pointer to the next free block, and the bottom two bits must
46 * be zero. When in use, the first byte is set to MAGIC, and the second
47 * byte is the size index. The remaining bytes are for alignment.
48 * If range checking is enabled and the size of the block fits
49 * in two bytes, then the top two bytes hold the size of the requested block
50 * plus the range checking words, and the header word MINUS ONE.
53 union overhead *ov_next; /* when free */
55 u_char ovu_magic; /* magic number */
56 u_char ovu_index; /* bucket # */
58 u_short ovu_size; /* actual block size */
59 u_int ovu_rmagic; /* range magic number */
62 #define ov_magic ovu.ovu_magic
63 #define ov_index ovu.ovu_index
64 #define ov_size ovu.ovu_size
65 #define ov_rmagic ovu.ovu_rmagic
68 #define MAGIC 0xff /* magic # on accounting info */
69 #define RMAGIC 0x55555555 /* magic # on range info */
71 #define RSLOP sizeof (u_int)
77 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
78 * smallest allocatable block is 8 bytes. The overhead information
79 * precedes the data area returned to the user.
82 static union overhead *nextf[NBUCKETS];
87 * nmalloc[i] is the difference between the number of mallocs and frees
88 * for a given block size.
90 static u_int nmalloc[NBUCKETS];
95 #define ASSERT(p) if (!(p)) botch("p"); else
101 printf("assertion botched: %s\n", s);
110 register unsigned nbytes;
112 register union overhead *p;
113 register int bucket = 0;
114 register unsigned shiftr;
117 * Convert amount of memory requested into
118 * closest block size stored in hash buckets
119 * which satisfies request. Account for
120 * space used per block for accounting.
122 nbytes += sizeof (union overhead) + RSLOP;
123 nbytes = (nbytes + 3) &~ 3;
124 shiftr = (nbytes - 1) >> 2;
125 /* apart from this loop, this is O(1) */
129 * If nothing in hash bucket right now,
130 * request more memory from the system.
132 if (nextf[bucket] == NULL)
134 if ((p = (union overhead *)nextf[bucket]) == NULL)
136 /* remove from linked list */
137 if (*((int*)p) > 0x10000000)
138 fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
139 nextf[bucket] = nextf[bucket]->ov_next;
147 * Record allocated size of block and
148 * bound space with magic numbers.
150 if (nbytes <= 0x10000)
151 p->ov_size = nbytes - 1;
152 p->ov_rmagic = RMAGIC;
153 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
155 return ((char *)(p + 1));
159 * Allocate more memory to the indicated bucket.
165 register union overhead *op;
166 register int rnu; /* 2^rnu bytes will be requested */
167 register int nblks; /* become nblks blocks of the desired size */
173 * Insure memory is allocated
174 * on a page boundary. Should
175 * make getpageize call?
177 op = (union overhead *)sbrk(0);
179 sbrk(1024 - ((int)op & 0x3ff));
180 /* take 2k unless the block is bigger than that */
181 rnu = (bucket <= 8) ? 11 : bucket + 3;
182 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
185 op = (union overhead *)sbrk(1 << rnu);
190 * Round up to minimum allocation size boundary
191 * and deduct from block count to reflect.
194 op = (union overhead *)(((int)op + 8) &~ 7);
198 * Add new memory allocated to that on
199 * free list for this hash bucket.
202 siz = 1 << (bucket + 3);
203 while (--nblks > 0) {
204 op->ov_next = (union overhead *)((caddr_t)op + siz);
205 op = (union overhead *)((caddr_t)op + siz);
213 register union overhead *op;
217 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
219 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
221 if (op->ov_magic != MAGIC)
225 ASSERT(op->ov_rmagic == RMAGIC);
226 if (op->ov_index <= 13)
227 ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
229 ASSERT(op->ov_index < NBUCKETS);
231 op->ov_next = nextf[size];
239 * When a program attempts "storage compaction" as mentioned in the
240 * old malloc man page, it realloc's an already freed block. Usually
241 * this is the last block it freed; occasionally it might be farther
242 * back. We have to search all the free lists for the block in order
243 * to determine its bucket: 1st we make one pass thru the lists
244 * checking only the first block in each; if that fails we search
245 * ``realloc_srchlen'' blocks in each list for a match (the variable
246 * is extern so the caller can modify it). If that fails we just copy
247 * however many bytes was given to realloc() and hope it's not huge.
249 int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
263 return (malloc(nbytes));
264 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
265 if (op->ov_magic == MAGIC) {
270 * Already free, doing "compaction".
272 * Search for the old block of memory on the
273 * free list. First, check the most common
274 * case (last element free'd), then (this failing)
275 * the last ``realloc_srchlen'' items free'd.
276 * If all lookups fail, then assume the size of
277 * the memory block being realloc'd is the
280 if ((i = findbucket(op, 1)) < 0 &&
281 (i = findbucket(op, realloc_srchlen)) < 0)
284 onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
285 /* avoid the copy if same size block */
287 nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP)
289 if ((res = malloc(nbytes)) == NULL)
291 if (cp != res) /* common optimization */
292 bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
299 * Search ``srchlen'' elements of each free list for a block whose
300 * header starts at ``freep''. If srchlen is -1 search the whole list.
301 * Return bucket number, or -1 if not found.
304 findbucket(freep, srchlen)
305 union overhead *freep;
308 register union overhead *p;
311 for (i = 0; i < NBUCKETS; i++) {
313 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
324 * mstats - print out statistics about malloc
326 * Prints two lines of numbers, one showing the length of the free list
327 * for each size category, the second showing the number of mallocs -
328 * frees for each size category.
334 register union overhead *p;
338 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
339 for (i = 0; i < NBUCKETS; i++) {
340 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
342 fprintf(stderr, " %d", j);
343 totfree += j * (1 << (i + 3));
345 fprintf(stderr, "\nused:\t");
346 for (i = 0; i < NBUCKETS; i++) {
347 fprintf(stderr, " %d", nmalloc[i]);
348 totused += nmalloc[i] * (1 << (i + 3));
350 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",