SCCS-vsn: lib/libc/stdlib/malloc.c 4.3
-static char sccsid[] = "@(#)malloc.c 4.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) %G%";
#define ASSERT(p) if (!(p)) botch("p"); else
static
botch(s)
#define ASSERT(p) if (!(p)) botch("p"); else
static
botch(s)
- printf("assertion botched: %s\n",s);
+
+ printf("assertion botched: %s\n", s);
* is extern so the caller can modify it). If that fails we just copy
* however many bytes was given to realloc() and hope it's not huge.
*/
* is extern so the caller can modify it). If that fails we just copy
* however many bytes was given to realloc() and hope it's not huge.
*/
-int realloc_srchlen = 4; /* 4 should be plenty. -1 means whole list */
+int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
char *
realloc(cp, nbytes)
char *
realloc(cp, nbytes)
if (op->ov_magic == MAGIC) {
was_alloced++;
i = op->ov_index;
if (op->ov_magic == MAGIC) {
was_alloced++;
i = op->ov_index;
- }
- else { /* already free: he is doing "compaction" (tee hee) */
+ } else {
+ /*
+ * Already free, doing "compaction".
+ *
+ * Search for the old block of memory on the
+ * free list. First, check the most common
+ * case (last element free'd), then (this failing)
+ * the last ``realloc_srchlen'' items free'd.
+ * If all lookups fail, then assume the size of
+ * the memory block being realloc'd is the
+ * smallest possible.
+ */
if ((i = findbucket(op, 1)) < 0 &&
(i = findbucket(op, realloc_srchlen)) < 0)
if ((i = findbucket(op, 1)) < 0 &&
(i = findbucket(op, realloc_srchlen)) < 0)
- i = 0; /* assume shortest possible */
}
onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
}
onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
- if (was_alloced && /* avoid the copy if same size block */
+ /* avoid the copy if same size block */
+ if (was_alloced &&
nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP)
return(cp);
if ((res = malloc(nbytes)) == NULL)
nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP)
return(cp);
if ((res = malloc(nbytes)) == NULL)
*/
static
findbucket(freep, srchlen)
*/
static
findbucket(freep, srchlen)
-union overhead *freep;
-int srchlen;
+ union overhead *freep;
+ int srchlen;
{
register union overhead *p;
register int i, j;
{
register union overhead *p;
register int i, j;
- for (i = 0; i < NBUCKETS; i++)
- for (j = 0, p = nextf[i]; p && j != srchlen; j++, p = p->ov_next)
+ for (i = 0; i < NBUCKETS; i++) {
+ j = 0;
+ for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
if (p == freep)
return (i);
if (p == freep)
return (i);
fprintf(stderr, " %d", nmalloc[i]);
totused += nmalloc[i] * (1 << (i + 3));
}
fprintf(stderr, " %d", nmalloc[i]);
totused += nmalloc[i] * (1 << (i + 3));
}
- fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", totused, totfree);
+ fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
+ totused, totfree);