xref: /illumos-gate/usr/src/lib/libbsdmalloc/common/malloc.bsd43.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * wizard:/space/4.3reno/usr/src/lib/libc/stdlib/malloc.c
8*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
9*7c478bd9Sstevel@tonic-gate  * All rights reserved.
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
12*7c478bd9Sstevel@tonic-gate  * provided that: (1) source distributions retain this entire copyright
13*7c478bd9Sstevel@tonic-gate  * notice and comment, and (2) distributions including binaries display
14*7c478bd9Sstevel@tonic-gate  * the following acknowledgement:  ``This product includes software
15*7c478bd9Sstevel@tonic-gate  * developed by the University of California, Berkeley and its contributors''
16*7c478bd9Sstevel@tonic-gate  * in the documentation or other materials provided with the distribution
17*7c478bd9Sstevel@tonic-gate  * and in all advertising materials mentioning features or use of this
18*7c478bd9Sstevel@tonic-gate  * software. Neither the name of the University nor the names of its
19*7c478bd9Sstevel@tonic-gate  * contributors may be used to endorse or promote products derived
20*7c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
21*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
22*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
23*7c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * malloc.c (Caltech) 2/21/82
28*7c478bd9Sstevel@tonic-gate  * Chris Kingsley, kingsley@cit-20.
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  * This is a very fast storage allocator.  It allocates blocks of a small
31*7c478bd9Sstevel@tonic-gate  * number of different sizes, and keeps free lists of each size.  Blocks that
32*7c478bd9Sstevel@tonic-gate  * don't exactly fit are passed up to the next larger size.  In this
33*7c478bd9Sstevel@tonic-gate  * implementation, the available sizes are 2^n-4 bytes long (ILP32)
34*7c478bd9Sstevel@tonic-gate  * or 2^n-8 bytes long (LP64).
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate #include <unistd.h>
41*7c478bd9Sstevel@tonic-gate #include <string.h>
42*7c478bd9Sstevel@tonic-gate #include <limits.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * The overhead on a block is at least 4 bytes.  When free, this space
46*7c478bd9Sstevel@tonic-gate  * contains a pointer to the next free block, and the bottom two bits must
47*7c478bd9Sstevel@tonic-gate  * be zero.  When in use, the first byte is set to MAGIC, and the second
48*7c478bd9Sstevel@tonic-gate  * byte is the size index.  The remaining bytes are for alignment.
49*7c478bd9Sstevel@tonic-gate  * The order of elements is critical: ov_magic must overlay the low order
50*7c478bd9Sstevel@tonic-gate  * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
51*7c478bd9Sstevel@tonic-gate  * Overhead is 4 bytes for ILP32, 8 bytes for LP64
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate union	overhead {
54*7c478bd9Sstevel@tonic-gate 	union	overhead *ov_next;	/* when free */
55*7c478bd9Sstevel@tonic-gate 	struct {
56*7c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
57*7c478bd9Sstevel@tonic-gate 		uchar_t	ovu_magic;	/* magic number */
58*7c478bd9Sstevel@tonic-gate 		uchar_t	ovu_index;	/* bucket # */
59*7c478bd9Sstevel@tonic-gate 		uchar_t	ovu_pad[sizeof (union overhead *) - 2];
60*7c478bd9Sstevel@tonic-gate #elif defined(_BIG_ENDIAN)
61*7c478bd9Sstevel@tonic-gate 		uchar_t	ovu_pad[sizeof (union overhead *) - 2];
62*7c478bd9Sstevel@tonic-gate 		uchar_t	ovu_index;	/* bucket # */
63*7c478bd9Sstevel@tonic-gate 		uchar_t	ovu_magic;	/* magic number */
64*7c478bd9Sstevel@tonic-gate #else
65*7c478bd9Sstevel@tonic-gate #error "Endianness is not defined"
66*7c478bd9Sstevel@tonic-gate #endif
67*7c478bd9Sstevel@tonic-gate 	} ovu;
68*7c478bd9Sstevel@tonic-gate };
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate #define	ov_magic	ovu.ovu_magic
71*7c478bd9Sstevel@tonic-gate #define	ov_index	ovu.ovu_index
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #define	MAGIC		0xef		/* magic # on accounting info */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /*
76*7c478bd9Sstevel@tonic-gate  * nextf[i] is the pointer to the next free block of size 2^(i+EXP).
77*7c478bd9Sstevel@tonic-gate  * The smallest allocatable block is 8 bytes (ILP32) or 16 bytes (LP64).
78*7c478bd9Sstevel@tonic-gate  * The overhead information precedes the data area returned to the user.
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate #ifdef _LP64
81*7c478bd9Sstevel@tonic-gate #define	EXP	4
82*7c478bd9Sstevel@tonic-gate #define	NBUCKETS 60
83*7c478bd9Sstevel@tonic-gate #else
84*7c478bd9Sstevel@tonic-gate #define	EXP	3
85*7c478bd9Sstevel@tonic-gate #define	NBUCKETS 29
86*7c478bd9Sstevel@tonic-gate #endif
87*7c478bd9Sstevel@tonic-gate static	union overhead *nextf[NBUCKETS];
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate static	int	pagesz;			/* page size */
90*7c478bd9Sstevel@tonic-gate static	long	sbrk_adjust;		/* in case sbrk() does alignment */
91*7c478bd9Sstevel@tonic-gate static	int	pagebucket;		/* page size bucket */
92*7c478bd9Sstevel@tonic-gate static	void	morecore(int);
93*7c478bd9Sstevel@tonic-gate static	int	findbucket(union overhead *, int);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate void *
malloc(size_t nbytes)96*7c478bd9Sstevel@tonic-gate malloc(size_t nbytes)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	union overhead *op;
99*7c478bd9Sstevel@tonic-gate 	int bucket;
100*7c478bd9Sstevel@tonic-gate 	ssize_t	n;
101*7c478bd9Sstevel@tonic-gate 	size_t amt;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	/*
104*7c478bd9Sstevel@tonic-gate 	 * First time malloc is called, setup page size and
105*7c478bd9Sstevel@tonic-gate 	 * align break pointer so all data will be page aligned.
106*7c478bd9Sstevel@tonic-gate 	 */
107*7c478bd9Sstevel@tonic-gate 	if (pagesz == 0) {
108*7c478bd9Sstevel@tonic-gate 		pagesz = getpagesize();
109*7c478bd9Sstevel@tonic-gate 		op = sbrk(0);
110*7c478bd9Sstevel@tonic-gate 		n = pagesz - sizeof (*op) - ((uintptr_t)op & (pagesz - 1));
111*7c478bd9Sstevel@tonic-gate 		if (n < 0)
112*7c478bd9Sstevel@tonic-gate 			n += pagesz;
113*7c478bd9Sstevel@tonic-gate 		if (n) {
114*7c478bd9Sstevel@tonic-gate 			if (sbrk(n) == (void *)-1)
115*7c478bd9Sstevel@tonic-gate 				return (NULL);
116*7c478bd9Sstevel@tonic-gate 			/*
117*7c478bd9Sstevel@tonic-gate 			 * We were careful to arrange that
118*7c478bd9Sstevel@tonic-gate 			 * sbrk(0) + sizeof (union overhead)
119*7c478bd9Sstevel@tonic-gate 			 * should end up on a page boundary.
120*7c478bd9Sstevel@tonic-gate 			 * If the underlying sbrk() performs alignment
121*7c478bd9Sstevel@tonic-gate 			 * then this is false.  We compute the adjustment.
122*7c478bd9Sstevel@tonic-gate 			 */
123*7c478bd9Sstevel@tonic-gate 			op = sbrk(0);
124*7c478bd9Sstevel@tonic-gate 			sbrk_adjust = (uintptr_t)(op + 1) & (pagesz - 1);
125*7c478bd9Sstevel@tonic-gate 		} else {
126*7c478bd9Sstevel@tonic-gate 			sbrk_adjust = 0;
127*7c478bd9Sstevel@tonic-gate 		}
128*7c478bd9Sstevel@tonic-gate 		bucket = 0;
129*7c478bd9Sstevel@tonic-gate 		amt = (1UL << EXP);
130*7c478bd9Sstevel@tonic-gate 		while (pagesz > amt) {
131*7c478bd9Sstevel@tonic-gate 			amt <<= 1;
132*7c478bd9Sstevel@tonic-gate 			bucket++;
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 		pagebucket = bucket;
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 	/*
137*7c478bd9Sstevel@tonic-gate 	 * Convert amount of memory requested into closest block size
138*7c478bd9Sstevel@tonic-gate 	 * stored in hash buckets which satisfies request.
139*7c478bd9Sstevel@tonic-gate 	 * Account for space used per block for accounting.
140*7c478bd9Sstevel@tonic-gate 	 */
141*7c478bd9Sstevel@tonic-gate 	if (nbytes <= (n = pagesz - sizeof (*op))) {
142*7c478bd9Sstevel@tonic-gate 		amt = (1UL << EXP);	/* size of first bucket */
143*7c478bd9Sstevel@tonic-gate 		bucket = 0;
144*7c478bd9Sstevel@tonic-gate 		n = -(ssize_t)(sizeof (*op));
145*7c478bd9Sstevel@tonic-gate 	} else {
146*7c478bd9Sstevel@tonic-gate 		amt = pagesz;
147*7c478bd9Sstevel@tonic-gate 		bucket = pagebucket;
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	while (nbytes > amt + n) {
150*7c478bd9Sstevel@tonic-gate 		amt <<= 1;
151*7c478bd9Sstevel@tonic-gate 		if (amt == 0)
152*7c478bd9Sstevel@tonic-gate 			return (NULL);
153*7c478bd9Sstevel@tonic-gate 		bucket++;
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 	/*
156*7c478bd9Sstevel@tonic-gate 	 * If nothing in hash bucket right now,
157*7c478bd9Sstevel@tonic-gate 	 * request more memory from the system.
158*7c478bd9Sstevel@tonic-gate 	 */
159*7c478bd9Sstevel@tonic-gate 	if ((op = nextf[bucket]) == NULL) {
160*7c478bd9Sstevel@tonic-gate 		morecore(bucket);
161*7c478bd9Sstevel@tonic-gate 		if ((op = nextf[bucket]) == NULL)
162*7c478bd9Sstevel@tonic-gate 			return (NULL);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	/* remove from linked list */
165*7c478bd9Sstevel@tonic-gate 	nextf[bucket] = op->ov_next;
166*7c478bd9Sstevel@tonic-gate 	op->ov_magic = MAGIC;
167*7c478bd9Sstevel@tonic-gate 	op->ov_index = (uchar_t)bucket;
168*7c478bd9Sstevel@tonic-gate 	return (op + 1);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate /*
172*7c478bd9Sstevel@tonic-gate  * Allocate more memory to the indicated bucket.
173*7c478bd9Sstevel@tonic-gate  */
174*7c478bd9Sstevel@tonic-gate static void
morecore(int bucket)175*7c478bd9Sstevel@tonic-gate morecore(int bucket)
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	union overhead *op;
178*7c478bd9Sstevel@tonic-gate 	size_t sz;			/* size of desired block */
179*7c478bd9Sstevel@tonic-gate 	ssize_t amt;			/* amount to allocate */
180*7c478bd9Sstevel@tonic-gate 	long nblks;			/* how many blocks we get */
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	sz = 1UL << (bucket + EXP);
183*7c478bd9Sstevel@tonic-gate 	if (sz == 0)
184*7c478bd9Sstevel@tonic-gate 		return;
185*7c478bd9Sstevel@tonic-gate 	if (sz < pagesz) {
186*7c478bd9Sstevel@tonic-gate 		amt = pagesz;
187*7c478bd9Sstevel@tonic-gate 		nblks = amt / sz;
188*7c478bd9Sstevel@tonic-gate 	} else {
189*7c478bd9Sstevel@tonic-gate 		amt = sz + pagesz;
190*7c478bd9Sstevel@tonic-gate 		nblks = 1;
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 	if (amt <= 0)
193*7c478bd9Sstevel@tonic-gate 		return;
194*7c478bd9Sstevel@tonic-gate 	if (amt > LONG_MAX) {
195*7c478bd9Sstevel@tonic-gate 		intptr_t	delta;
196*7c478bd9Sstevel@tonic-gate 		/*
197*7c478bd9Sstevel@tonic-gate 		 * the value required is too big for sbrk() to deal with
198*7c478bd9Sstevel@tonic-gate 		 * in one go, so use sbrk() at most 2 times instead.
199*7c478bd9Sstevel@tonic-gate 		 */
200*7c478bd9Sstevel@tonic-gate 		op = sbrk(0);
201*7c478bd9Sstevel@tonic-gate 		delta = LONG_MAX;
202*7c478bd9Sstevel@tonic-gate 		while (delta > 0) {
203*7c478bd9Sstevel@tonic-gate 			if (sbrk(delta) == (void *)-1) {
204*7c478bd9Sstevel@tonic-gate 				if (op != sbrk(0))
205*7c478bd9Sstevel@tonic-gate 					(void) sbrk(-LONG_MAX);
206*7c478bd9Sstevel@tonic-gate 				return;
207*7c478bd9Sstevel@tonic-gate 			}
208*7c478bd9Sstevel@tonic-gate 			amt -= LONG_MAX;
209*7c478bd9Sstevel@tonic-gate 			delta = amt;
210*7c478bd9Sstevel@tonic-gate 		}
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 	else
213*7c478bd9Sstevel@tonic-gate 		op = sbrk(amt);
214*7c478bd9Sstevel@tonic-gate 	/* no more room! */
215*7c478bd9Sstevel@tonic-gate 	if (op == (union overhead *)-1)
216*7c478bd9Sstevel@tonic-gate 		return;
217*7c478bd9Sstevel@tonic-gate 	/* LINTED improper alignment */
218*7c478bd9Sstevel@tonic-gate 	op = (union overhead *)((caddr_t)op - sbrk_adjust);
219*7c478bd9Sstevel@tonic-gate 	/*
220*7c478bd9Sstevel@tonic-gate 	 * Add new memory allocated to that on
221*7c478bd9Sstevel@tonic-gate 	 * free list for this hash bucket.
222*7c478bd9Sstevel@tonic-gate 	 */
223*7c478bd9Sstevel@tonic-gate 	nextf[bucket] = op;
224*7c478bd9Sstevel@tonic-gate 	while (--nblks > 0) {
225*7c478bd9Sstevel@tonic-gate 		/* LINTED improper alignment */
226*7c478bd9Sstevel@tonic-gate 		op->ov_next = (union overhead *)((caddr_t)op + sz);
227*7c478bd9Sstevel@tonic-gate 		/* LINTED improper alignment */
228*7c478bd9Sstevel@tonic-gate 		op = (union overhead *)((caddr_t)op + sz);
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate void
free(void * cp)233*7c478bd9Sstevel@tonic-gate free(void *cp)
234*7c478bd9Sstevel@tonic-gate {
235*7c478bd9Sstevel@tonic-gate 	int size;
236*7c478bd9Sstevel@tonic-gate 	union overhead *op;
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	if (cp == NULL)
239*7c478bd9Sstevel@tonic-gate 		return;
240*7c478bd9Sstevel@tonic-gate 	/* LINTED improper alignment */
241*7c478bd9Sstevel@tonic-gate 	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
242*7c478bd9Sstevel@tonic-gate 	if (op->ov_magic != MAGIC)
243*7c478bd9Sstevel@tonic-gate 		return;			/* previously freed? */
244*7c478bd9Sstevel@tonic-gate 	size = op->ov_index;
245*7c478bd9Sstevel@tonic-gate 	op->ov_next = nextf[size];	/* also clobbers ov_magic */
246*7c478bd9Sstevel@tonic-gate 	nextf[size] = op;
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate /*
250*7c478bd9Sstevel@tonic-gate  * When a program attempts "storage compaction" as mentioned in the
251*7c478bd9Sstevel@tonic-gate  * old malloc man page, it realloc's an already freed block.  Usually
252*7c478bd9Sstevel@tonic-gate  * this is the last block it freed; occasionally it might be farther
253*7c478bd9Sstevel@tonic-gate  * back.  We have to search all the free lists for the block in order
254*7c478bd9Sstevel@tonic-gate  * to determine its bucket: 1st we make one pass thru the lists
255*7c478bd9Sstevel@tonic-gate  * checking only the first block in each; if that fails we search
256*7c478bd9Sstevel@tonic-gate  * ``realloc_srchlen'' blocks in each list for a match (the variable
257*7c478bd9Sstevel@tonic-gate  * is extern so the caller can modify it).  If that fails we just copy
258*7c478bd9Sstevel@tonic-gate  * however many bytes was given to realloc() and hope it's not huge.
259*7c478bd9Sstevel@tonic-gate  */
260*7c478bd9Sstevel@tonic-gate int realloc_srchlen = 4;	/* 4 should be plenty, -1 =>'s whole list */
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate void *
realloc(void * cp,size_t nbytes)263*7c478bd9Sstevel@tonic-gate realloc(void *cp, size_t nbytes)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	size_t onb;
266*7c478bd9Sstevel@tonic-gate 	int i;
267*7c478bd9Sstevel@tonic-gate 	union overhead *op;
268*7c478bd9Sstevel@tonic-gate 	char *res;
269*7c478bd9Sstevel@tonic-gate 	int was_alloced = 0;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	if (cp == NULL)
272*7c478bd9Sstevel@tonic-gate 		return (malloc(nbytes));
273*7c478bd9Sstevel@tonic-gate 	/* LINTED improper alignment */
274*7c478bd9Sstevel@tonic-gate 	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
275*7c478bd9Sstevel@tonic-gate 	if (op->ov_magic == MAGIC) {
276*7c478bd9Sstevel@tonic-gate 		was_alloced++;
277*7c478bd9Sstevel@tonic-gate 		i = op->ov_index;
278*7c478bd9Sstevel@tonic-gate 	} else {
279*7c478bd9Sstevel@tonic-gate 		/*
280*7c478bd9Sstevel@tonic-gate 		 * Already free, doing "compaction".
281*7c478bd9Sstevel@tonic-gate 		 *
282*7c478bd9Sstevel@tonic-gate 		 * Search for the old block of memory on the
283*7c478bd9Sstevel@tonic-gate 		 * free list.  First, check the most common
284*7c478bd9Sstevel@tonic-gate 		 * case (last element free'd), then (this failing)
285*7c478bd9Sstevel@tonic-gate 		 * the last ``realloc_srchlen'' items free'd.
286*7c478bd9Sstevel@tonic-gate 		 * If all lookups fail, then just malloc() the
287*7c478bd9Sstevel@tonic-gate 		 * space and copy the size of the new space.
288*7c478bd9Sstevel@tonic-gate 		 */
289*7c478bd9Sstevel@tonic-gate 		if ((i = findbucket(op, 1)) < 0 &&
290*7c478bd9Sstevel@tonic-gate 		    (i = findbucket(op, realloc_srchlen)) < 0) {
291*7c478bd9Sstevel@tonic-gate 			if ((res = malloc(nbytes)) != NULL)
292*7c478bd9Sstevel@tonic-gate 				(void) memmove(res, cp, nbytes);
293*7c478bd9Sstevel@tonic-gate 			return (res);
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 	}
296*7c478bd9Sstevel@tonic-gate 	onb = 1UL << (i + EXP);
297*7c478bd9Sstevel@tonic-gate 	if (onb < pagesz)
298*7c478bd9Sstevel@tonic-gate 		onb -= sizeof (*op);
299*7c478bd9Sstevel@tonic-gate 	else
300*7c478bd9Sstevel@tonic-gate 		onb += pagesz - sizeof (*op);
301*7c478bd9Sstevel@tonic-gate 	/* avoid the copy if same size block */
302*7c478bd9Sstevel@tonic-gate 	if (was_alloced) {
303*7c478bd9Sstevel@tonic-gate 		size_t sz = 0;
304*7c478bd9Sstevel@tonic-gate 		if (i) {
305*7c478bd9Sstevel@tonic-gate 			sz = 1UL << (i + EXP - 1);
306*7c478bd9Sstevel@tonic-gate 			if (sz < pagesz)
307*7c478bd9Sstevel@tonic-gate 				sz -= sizeof (*op);
308*7c478bd9Sstevel@tonic-gate 			else
309*7c478bd9Sstevel@tonic-gate 				sz += pagesz - sizeof (*op);
310*7c478bd9Sstevel@tonic-gate 		}
311*7c478bd9Sstevel@tonic-gate 		if (nbytes <= onb && nbytes > sz) {
312*7c478bd9Sstevel@tonic-gate 			return (cp);
313*7c478bd9Sstevel@tonic-gate 		} else
314*7c478bd9Sstevel@tonic-gate 			free(cp);
315*7c478bd9Sstevel@tonic-gate 	}
316*7c478bd9Sstevel@tonic-gate 	if ((res = malloc(nbytes)) == NULL)
317*7c478bd9Sstevel@tonic-gate 		return (NULL);
318*7c478bd9Sstevel@tonic-gate 	if (cp != res)		/* common optimization if "compacting" */
319*7c478bd9Sstevel@tonic-gate 		(void) memmove(res, cp, (nbytes < onb) ? nbytes : onb);
320*7c478bd9Sstevel@tonic-gate 	return (res);
321*7c478bd9Sstevel@tonic-gate }
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate /*
324*7c478bd9Sstevel@tonic-gate  * Search ``srchlen'' elements of each free list for a block whose
325*7c478bd9Sstevel@tonic-gate  * header starts at ``freep''.  If srchlen is -1 search the whole list.
326*7c478bd9Sstevel@tonic-gate  * Return bucket number, or -1 if not found.
327*7c478bd9Sstevel@tonic-gate  */
328*7c478bd9Sstevel@tonic-gate static int
findbucket(union overhead * freep,int srchlen)329*7c478bd9Sstevel@tonic-gate findbucket(union overhead *freep, int srchlen)
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	union overhead *p;
332*7c478bd9Sstevel@tonic-gate 	int i, j;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < NBUCKETS; i++) {
335*7c478bd9Sstevel@tonic-gate 		j = 0;
336*7c478bd9Sstevel@tonic-gate 		for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
337*7c478bd9Sstevel@tonic-gate 			if (p == freep)
338*7c478bd9Sstevel@tonic-gate 				return (i);
339*7c478bd9Sstevel@tonic-gate 			j++;
340*7c478bd9Sstevel@tonic-gate 		}
341*7c478bd9Sstevel@tonic-gate 	}
342*7c478bd9Sstevel@tonic-gate 	return (-1);
343*7c478bd9Sstevel@tonic-gate }
344