- char *cp;
- unsigned nbytes;
-{
- register u_int onb;
- union overhead *op;
- char *res;
- register int i;
- int was_alloced = 0;
-
- if (cp == NULL)
- return (malloc(nbytes));
- op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
- if (op->ov_magic == MAGIC) {
- was_alloced++;
- i = op->ov_index;
- }
- else { /* already free: he is doing "compaction" (tee hee) */
- if ((i = findbucket(op, 1)) < 0 &&
- (i = findbucket(op, realloc_srchlen)) < 0)
- i = 0; /* assume smallest possible */
- }
- onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
- if (was_alloced && /* avoid the copy if same size block */
- nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP)
- return(cp);
- if ((res = malloc(nbytes)) == NULL)
- return (NULL);
- if (cp != res) /* common optimization */
- bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
- if (was_alloced)
- free(cp);
- return (res);
+ ptr_t cp;
+ size_t nbytes;
+{
+#ifndef lint
+ register U_int onb;
+ union overhead *op;
+ char *res;
+ register int i;
+ int was_alloced = 0;
+
+ if (cp == NULL)
+ return (malloc(nbytes));
+ op = (union overhead *) (((caddr_t) cp) - ALIGN(sizeof(union overhead)));
+ if (op->ov_magic == MAGIC) {
+ was_alloced++;
+ i = op->ov_index;
+ }
+ 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)
+ i = 0;
+
+ onb = ALIGN(nbytes + ALIGN(sizeof(union overhead)) + RSLOP);
+
+ /* avoid the copy if same size block */
+ if (was_alloced && (onb < (1 << (i + 3))) && (onb >= (1 << (i + 2))))
+ return ((ptr_t) cp);
+ if ((res = malloc(nbytes)) == NULL)
+ return ((ptr_t) 0);
+ if (cp != res) /* common optimization */
+ bcopy(cp, res, nbytes);
+ if (was_alloced)
+ free(cp);
+ return (res);
+#else
+ if (cp && nbytes)
+ return ((ptr_t) 0);
+ else
+ return ((ptr_t) 0);
+#endif /* !lint */