1 /* $Header: /src/pub/tcsh/tc.alloc.c,v 3.39 2005/01/05 16:06:14 christos Exp $ */
3 * tc.alloc.c (Caltech) 2/21/82
4 * Chris Kingsley, kingsley@cit-20.
6 * This is a very fast storage allocator. It allocates blocks of a small
7 * number of different sizes, and keeps free lists of each size. Blocks that
8 * don't exactly fit are passed up to the next larger size. In this
9 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
10 * This is designed for use in a program that uses vast quantities of memory,
11 * but bombs when it runs out.
14 * Copyright (c) 1980, 1991 The Regents of the University of California.
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 RCSID("$Id: tc.alloc.c,v 3.39 2005/01/05 16:06:14 christos Exp $")
45 static char *memtop = NULL; /* PWP: top of current memory */
46 static char *membot = NULL; /* PWP: bottom of allocatable memory */
51 # define malloc fmalloc
53 # define calloc fcalloc
54 # define realloc frealloc
55 #endif /* WINNT_NATIVE */
66 * Lots of os routines are busted and try to free invalid pointers.
67 * Although our free routine is smart enough and it will pick bad
68 * pointers most of the time, in cases where we know we are going to get
69 * a bad pointer, we'd rather leak.
76 typedef unsigned char U_char; /* we don't really have signed chars */
77 typedef unsigned int U_int;
78 typedef unsigned short U_short;
79 typedef unsigned long U_long;
83 * The overhead on a block is at least 4 bytes. When free, this space
84 * contains a pointer to the next free block, and the bottom two bits must
85 * be zero. When in use, the first byte is set to MAGIC, and the second
86 * byte is the size index. The remaining bytes are for alignment.
87 * If range checking is enabled and the size of the block fits
88 * in two bytes, then the top two bytes hold the size of the requested block
89 * plus the range checking words, and the header word MINUS ONE.
93 #define MEMALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP)
96 union overhead *ov_next; /* when free */
98 U_char ovu_magic; /* magic number */
99 U_char ovu_index; /* bucket # */
101 U_short ovu_size; /* actual block size */
102 U_int ovu_rmagic; /* range magic number */
105 #define ov_magic ovu.ovu_magic
106 #define ov_index ovu.ovu_index
107 #define ov_size ovu.ovu_size
108 #define ov_rmagic ovu.ovu_rmagic
111 #define MAGIC 0xfd /* magic # on accounting info */
112 #define RMAGIC 0x55555555 /* magic # on range info */
114 #define RSLOP sizeof (U_int)
123 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
124 * smallest allocatable block is 8 bytes. The overhead information
125 * precedes the data area returned to the user.
127 #define NBUCKETS ((sizeof(long) << 3) - 3)
128 static union overhead *nextf[NBUCKETS] IZERO_STRUCT;
131 * nmalloc[i] is the difference between the number of mallocs and frees
132 * for a given block size.
134 static U_int nmalloc[NBUCKETS] IZERO_STRUCT;
137 static int findbucket __P((union overhead *, int));
138 static void morecore __P((int));
143 # define CHECK(a, str, p) \
146 xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \
150 # define CHECK(a, str, p) \
153 xprintf(" (memtop = %lx membot = %lx)\n", memtop, membot); \
168 * Convert amount of memory requested into closest block size stored in
169 * hash buckets which satisfies request. Account for space used per block
174 * SunOS localtime() overwrites the 9th byte on an 8 byte malloc()....
175 * so we get one more...
176 * From Michael Schroeder: This is not true. It depends on the
177 * timezone string. In Europe it can overwrite the 13th byte on a
179 * So we punt and we always allocate an extra byte.
184 nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead)) + nbytes + RSLOP);
185 shiftr = (nbytes - 1) >> 2;
187 /* apart from this loop, this is O(1) */
188 while ((shiftr >>= 1) != 0)
191 * If nothing in hash bucket right now, request more memory from the
194 if (nextf[bucket] == NULL)
196 if ((p = (union overhead *) nextf[bucket]) == NULL) {
202 xprintf(CGETS(19, 1, "nbytes=%d: Out of memory\n"), nbytes);
206 return ((memalign_t) 0);
208 /* remove from linked list */
209 nextf[bucket] = nextf[bucket]->ov_next;
211 p->ov_index = bucket;
215 * Record allocated size of block and bound space with magic numbers.
217 p->ov_size = (p->ov_index <= 13) ? nbytes - 1 : 0;
218 p->ov_rmagic = RMAGIC;
219 *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC;
221 return ((memalign_t) (((caddr_t) p) + MEMALIGN(sizeof(union overhead))));
224 return ((memalign_t) 0);
226 return ((memalign_t) 0);
232 * Allocate more memory to the indicated bucket.
239 int rnu; /* 2^rnu bytes will be requested */
240 int nblks; /* become nblks blocks of the desired size */
246 * Insure memory is allocated on a page boundary. Should make getpageize
249 op = (union overhead *) sbrk(0);
250 memtop = (char *) op;
253 if ((long) op & 0x3ff) {
254 memtop = (char *) sbrk((int) (1024 - ((long) op & 0x3ff)));
255 memtop += (long) (1024 - ((long) op & 0x3ff));
258 /* take 2k unless the block is bigger than that */
259 rnu = (bucket <= 8) ? 11 : bucket + 3;
260 nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
261 memtop = (char *) sbrk(1 << rnu); /* PWP */
262 op = (union overhead *) memtop;
266 memtop += (long) (1 << rnu);
268 * Round up to minimum allocation size boundary and deduct from block count
271 if (((U_long) op) & ROUNDUP) {
272 op = (union overhead *) (((U_long) op + (ROUNDUP + 1)) & ~ROUNDUP);
276 * Add new memory allocated to that on free list for this hash bucket.
279 siz = 1 << (bucket + 3);
280 while (--nblks > 0) {
281 op->ov_next = (union overhead *) (((caddr_t) op) + siz);
282 op = (union overhead *) (((caddr_t) op) + siz);
298 * the don't free flag is there so that we avoid os bugs in routines
299 * that free invalid pointers!
301 if (cp == NULL || dont_free)
303 CHECK(!memtop || !membot,
304 CGETS(19, 2, "free(%lx) called before any allocations."), cp);
305 CHECK(cp > (ptr_t) memtop,
306 CGETS(19, 3, "free(%lx) above top of memory."), cp);
307 CHECK(cp < (ptr_t) membot,
308 CGETS(19, 4, "free(%lx) below bottom of memory."), cp);
309 op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
310 CHECK(op->ov_magic != MAGIC,
311 CGETS(19, 5, "free(%lx) bad block."), cp);
314 if (op->ov_index <= 13)
315 CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC,
316 CGETS(19, 6, "free(%lx) bad range check."), cp);
318 CHECK(op->ov_index >= NBUCKETS,
319 CGETS(19, 7, "free(%lx) bad block index."), cp);
321 op->ov_next = nextf[size];
340 scp = cp = (char *) xmalloc((size_t) i);
346 return ((memalign_t) scp);
349 return ((memalign_t) 0);
351 return ((memalign_t) 0);
356 * When a program attempts "storage compaction" as mentioned in the
357 * old malloc man page, it realloc's an already freed block. Usually
358 * this is the last block it freed; occasionally it might be farther
359 * back. We have to search all the free lists for the block in order
360 * to determine its bucket: 1st we make one pass thru the lists
361 * checking only the first block in each; if that fails we search
362 * ``realloc_srchlen'' blocks in each list for a match (the variable
363 * is extern so the caller can modify it). If that fails we just copy
364 * however many bytes was given to realloc() and hope it's not huge.
367 /* 4 should be plenty, -1 =>'s whole list */
368 static int realloc_srchlen = 4;
384 return (malloc(nbytes));
385 op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
386 if (op->ov_magic == MAGIC) {
392 * Already free, doing "compaction".
394 * Search for the old block of memory on the free list. First, check the
395 * most common case (last element free'd), then (this failing) the last
396 * ``realloc_srchlen'' items free'd. If all lookups fail, then assume
397 * the size of the memory block being realloc'd is the smallest
400 if ((i = findbucket(op, 1)) < 0 &&
401 (i = findbucket(op, realloc_srchlen)) < 0)
404 onb = MEMALIGN(nbytes + MEMALIGN(sizeof(union overhead)) + RSLOP);
406 /* avoid the copy if same size block */
407 if (was_alloced && (onb <= (U_int) (1 << (i + 3))) &&
408 (onb > (U_int) (1 << (i + 2)))) {
410 /* JMR: formerly this wasn't updated ! */
411 nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead))+nbytes+RSLOP);
412 *((U_int *) (((caddr_t) op) + nbytes - RSLOP)) = RMAGIC;
413 op->ov_rmagic = RMAGIC;
414 op->ov_size = (op->ov_index <= 13) ? nbytes - 1 : 0;
416 return ((memalign_t) cp);
418 if ((res = malloc(nbytes)) == NULL)
419 return ((memalign_t) NULL);
420 if (cp != res) { /* common optimization */
422 * christos: this used to copy nbytes! It should copy the
423 * smaller of the old and new size
425 onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP;
426 (void) memmove((ptr_t) res, (ptr_t) cp,
427 (size_t) (onb < nbytes ? onb : nbytes));
431 return ((memalign_t) res);
434 return ((memalign_t) 0);
436 return ((memalign_t) 0);
444 * Search ``srchlen'' elements of each free list for a block whose
445 * header starts at ``freep''. If srchlen is -1 search the whole list.
446 * Return bucket number, or -1 if not found.
449 findbucket(freep, srchlen)
450 union overhead *freep;
457 for (i = 0; i < NBUCKETS; i++) {
459 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
471 #else /* SYSMALLOC */
474 ** ``Protected versions'' of malloc, realloc, calloc, and free
478 ** 1. malloc(0) is bad
480 ** 3. realloc(0, n) is bad
481 ** 4. realloc(n, 0) is bad
483 ** Also we call our error routine if we run out of memory.
495 membot = (char*) sbrk(0);
496 #endif /* HAVE_SBRK */
498 if ((ptr = malloc(n)) == (ptr_t) 0) {
503 if (memtop < ((char *) ptr) + n)
504 memtop = ((char *) ptr) + n;
506 membot = (char*) ptr;
507 #endif /* !HAVE_SBRK */
508 return ((memalign_t) ptr);
522 membot = (char*) sbrk(0);
523 #endif /* HAVE_SBRK */
525 if ((ptr = (p ? realloc(p, n) : malloc(n))) == (ptr_t) 0) {
530 if (memtop < ((char *) ptr) + n)
531 memtop = ((char *) ptr) + n;
533 membot = (char*) ptr;
534 #endif /* !HAVE_SBRK */
535 return ((memalign_t) ptr);
550 membot = (char*) sbrk(0);
551 #endif /* HAVE_SBRK */
553 if ((ptr = malloc(n)) == (ptr_t) 0) {
565 if (memtop < ((char *) ptr) + n)
566 memtop = ((char *) ptr) + n;
568 membot = (char*) ptr;
569 #endif /* !HAVE_SBRK */
571 return ((memalign_t) ptr);
582 #endif /* SYSMALLOC */
585 * mstats - print out statistics about malloc
587 * Prints two lines of numbers, one showing the length of the free list
588 * for each size category, the second showing the number of mallocs -
589 * frees for each size category.
600 int totfree = 0, totused = 0;
602 xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname);
603 for (i = 0; i < NBUCKETS; i++) {
604 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
607 totfree += j * (1 << (i + 3));
609 xprintf(CGETS(19, 9, "\nused:\t"));
610 for (i = 0; i < NBUCKETS; i++) {
611 xprintf(" %4u", nmalloc[i]);
612 totused += nmalloc[i] * (1 << (i + 3));
614 xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"),
616 xprintf(CGETS(19, 11,
617 "\tAllocated memory from 0x%lx to 0x%lx. Real top at 0x%lx\n"),
618 (unsigned long) membot, (unsigned long) memtop,
619 (unsigned long) sbrk(0));
622 memtop = (char *) sbrk(0);
623 #endif /* HAVE_SBRK */
624 xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"),
625 (unsigned long) membot, (unsigned long) memtop,
626 (unsigned long) (memtop - membot));
627 #endif /* SYSMALLOC */