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