xref: /onnv-gate/usr/src/cmd/sendmail/db/btree/bt_search.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*-
2*0Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Copyright (c) 1996, 1997, 1998
5*0Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate /*
8*0Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994, 1995, 1996
9*0Sstevel@tonic-gate  *	Keith Bostic.  All rights reserved.
10*0Sstevel@tonic-gate  */
11*0Sstevel@tonic-gate /*
12*0Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994, 1995
13*0Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
16*0Sstevel@tonic-gate  * Mike Olson.
17*0Sstevel@tonic-gate  *
18*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
19*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
20*0Sstevel@tonic-gate  * are met:
21*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
22*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
23*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
24*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
25*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
26*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
27*0Sstevel@tonic-gate  *    must display the following acknowledgement:
28*0Sstevel@tonic-gate  *	This product includes software developed by the University of
29*0Sstevel@tonic-gate  *	California, Berkeley and its contributors.
30*0Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
31*0Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
32*0Sstevel@tonic-gate  *    without specific prior written permission.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44*0Sstevel@tonic-gate  * SUCH DAMAGE.
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include "config.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #ifndef lint
50*0Sstevel@tonic-gate static const char sccsid[] = "@(#)bt_search.c	10.25 (Sleepycat) 12/16/98";
51*0Sstevel@tonic-gate #endif /* not lint */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
54*0Sstevel@tonic-gate #include <sys/types.h>
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #include <errno.h>
57*0Sstevel@tonic-gate #include <string.h>
58*0Sstevel@tonic-gate #endif
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #include "db_int.h"
61*0Sstevel@tonic-gate #include "db_page.h"
62*0Sstevel@tonic-gate #include "btree.h"
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate /*
65*0Sstevel@tonic-gate  * __bam_search --
66*0Sstevel@tonic-gate  *	Search a btree for a key.
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  * PUBLIC: int __bam_search __P((DBC *,
69*0Sstevel@tonic-gate  * PUBLIC:     const DBT *, u_int32_t, int, db_recno_t *, int *));
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate int
__bam_search(dbc,key,flags,stop,recnop,exactp)72*0Sstevel@tonic-gate __bam_search(dbc, key, flags, stop, recnop, exactp)
73*0Sstevel@tonic-gate 	DBC *dbc;
74*0Sstevel@tonic-gate 	const DBT *key;
75*0Sstevel@tonic-gate 	u_int32_t flags;
76*0Sstevel@tonic-gate 	int stop, *exactp;
77*0Sstevel@tonic-gate 	db_recno_t *recnop;
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	BTREE *t;
80*0Sstevel@tonic-gate 	CURSOR *cp;
81*0Sstevel@tonic-gate 	DB *dbp;
82*0Sstevel@tonic-gate 	DB_LOCK lock;
83*0Sstevel@tonic-gate 	PAGE *h;
84*0Sstevel@tonic-gate 	db_indx_t base, i, indx, lim;
85*0Sstevel@tonic-gate 	db_pgno_t pg;
86*0Sstevel@tonic-gate 	db_recno_t recno;
87*0Sstevel@tonic-gate 	int cmp, jump, ret, stack;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	dbp = dbc->dbp;
90*0Sstevel@tonic-gate 	cp = dbc->internal;
91*0Sstevel@tonic-gate 	t = dbp->internal;
92*0Sstevel@tonic-gate 	recno = 0;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	BT_STK_CLR(cp);
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/*
97*0Sstevel@tonic-gate 	 * There are several ways we search a btree tree.  The flags argument
98*0Sstevel@tonic-gate 	 * specifies if we're acquiring read or write locks, if we position
99*0Sstevel@tonic-gate 	 * to the first or last item in a set of duplicates, if we return
100*0Sstevel@tonic-gate 	 * deleted items, and if we are locking pairs of pages.  In addition,
101*0Sstevel@tonic-gate 	 * if we're modifying record numbers, we have to lock the entire tree
102*0Sstevel@tonic-gate 	 * regardless.  See btree.h for more details.
103*0Sstevel@tonic-gate 	 *
104*0Sstevel@tonic-gate 	 * If write-locking pages, we need to know whether or not to acquire a
105*0Sstevel@tonic-gate 	 * write lock on a page before getting it.  This depends on how deep it
106*0Sstevel@tonic-gate 	 * is in tree, which we don't know until we acquire the root page.  So,
107*0Sstevel@tonic-gate 	 * if we need to lock the root page we may have to upgrade it later,
108*0Sstevel@tonic-gate 	 * because we won't get the correct lock initially.
109*0Sstevel@tonic-gate 	 *
110*0Sstevel@tonic-gate 	 * Retrieve the root page.
111*0Sstevel@tonic-gate 	 */
112*0Sstevel@tonic-gate 	pg = PGNO_ROOT;
113*0Sstevel@tonic-gate 	stack = F_ISSET(dbp, DB_BT_RECNUM) && LF_ISSET(S_STACK);
114*0Sstevel@tonic-gate 	if ((ret = __bam_lget(dbc,
115*0Sstevel@tonic-gate 	    0, pg, stack ? DB_LOCK_WRITE : DB_LOCK_READ, &lock)) != 0)
116*0Sstevel@tonic-gate 		return (ret);
117*0Sstevel@tonic-gate 	if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0) {
118*0Sstevel@tonic-gate 		(void)__BT_LPUT(dbc, lock);
119*0Sstevel@tonic-gate 		return (ret);
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	/*
123*0Sstevel@tonic-gate 	 * Decide if we need to save this page; if we do, write lock it.
124*0Sstevel@tonic-gate 	 * We deliberately don't lock-couple on this call.  If the tree
125*0Sstevel@tonic-gate 	 * is tiny, i.e., one page, and two threads are busily updating
126*0Sstevel@tonic-gate 	 * the root page, we're almost guaranteed deadlocks galore, as
127*0Sstevel@tonic-gate 	 * each one gets a read lock and then blocks the other's attempt
128*0Sstevel@tonic-gate 	 * for a write lock.
129*0Sstevel@tonic-gate 	 */
130*0Sstevel@tonic-gate 	if (!stack &&
131*0Sstevel@tonic-gate 	    ((LF_ISSET(S_PARENT) && (u_int8_t)(stop + 1) >= h->level) ||
132*0Sstevel@tonic-gate 	    (LF_ISSET(S_WRITE) && h->level == LEAFLEVEL))) {
133*0Sstevel@tonic-gate 		(void)memp_fput(dbp->mpf, h, 0);
134*0Sstevel@tonic-gate 		(void)__BT_LPUT(dbc, lock);
135*0Sstevel@tonic-gate 		if ((ret = __bam_lget(dbc, 0, pg, DB_LOCK_WRITE, &lock)) != 0)
136*0Sstevel@tonic-gate 			return (ret);
137*0Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0) {
138*0Sstevel@tonic-gate 			(void)__BT_LPUT(dbc, lock);
139*0Sstevel@tonic-gate 			return (ret);
140*0Sstevel@tonic-gate 		}
141*0Sstevel@tonic-gate 		stack = 1;
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	for (;;) {
145*0Sstevel@tonic-gate 		/*
146*0Sstevel@tonic-gate 		 * Do a binary search on the current page.  If we're searching
147*0Sstevel@tonic-gate 		 * a leaf page, we have to manipulate the indices in groups of
148*0Sstevel@tonic-gate 		 * two.  If we're searching an internal page, they're an index
149*0Sstevel@tonic-gate 		 * per page item.  If we find an exact match on a leaf page,
150*0Sstevel@tonic-gate 		 * we're done.
151*0Sstevel@tonic-gate 		 */
152*0Sstevel@tonic-gate 		jump = TYPE(h) == P_LBTREE ? P_INDX : O_INDX;
153*0Sstevel@tonic-gate 		for (base = 0,
154*0Sstevel@tonic-gate 		    lim = NUM_ENT(h) / (db_indx_t)jump; lim != 0; lim >>= 1) {
155*0Sstevel@tonic-gate 			indx = base + ((lim >> 1) * jump);
156*0Sstevel@tonic-gate 			if ((cmp =
157*0Sstevel@tonic-gate 			    __bam_cmp(dbp, key, h, indx, t->bt_compare)) == 0) {
158*0Sstevel@tonic-gate 				if (TYPE(h) == P_LBTREE)
159*0Sstevel@tonic-gate 					goto match;
160*0Sstevel@tonic-gate 				goto next;
161*0Sstevel@tonic-gate 			}
162*0Sstevel@tonic-gate 			if (cmp > 0) {
163*0Sstevel@tonic-gate 				base = indx + jump;
164*0Sstevel@tonic-gate 				--lim;
165*0Sstevel@tonic-gate 			}
166*0Sstevel@tonic-gate 		}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 		/*
169*0Sstevel@tonic-gate 		 * No match found.  Base is the smallest index greater than
170*0Sstevel@tonic-gate 		 * key and may be zero or a last + O_INDX index.
171*0Sstevel@tonic-gate 		 *
172*0Sstevel@tonic-gate 		 * If it's a leaf page, return base as the "found" value.
173*0Sstevel@tonic-gate 		 * Delete only deletes exact matches.
174*0Sstevel@tonic-gate 		 */
175*0Sstevel@tonic-gate 		if (TYPE(h) == P_LBTREE) {
176*0Sstevel@tonic-gate 			*exactp = 0;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 			if (LF_ISSET(S_EXACT))
179*0Sstevel@tonic-gate 				goto notfound;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 			/*
182*0Sstevel@tonic-gate 			 * !!!
183*0Sstevel@tonic-gate 			 * Possibly returning a deleted record -- DB_SET_RANGE,
184*0Sstevel@tonic-gate 			 * DB_KEYFIRST and DB_KEYLAST don't require an exact
185*0Sstevel@tonic-gate 			 * match, and we don't want to walk multiple pages here
186*0Sstevel@tonic-gate 			 * to find an undeleted record.  This is handled in the
187*0Sstevel@tonic-gate 			 * __bam_c_search() routine.
188*0Sstevel@tonic-gate 			 */
189*0Sstevel@tonic-gate 			BT_STK_ENTER(cp, h, base, lock, ret);
190*0Sstevel@tonic-gate 			return (ret);
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 		/*
194*0Sstevel@tonic-gate 		 * If it's not a leaf page, record the internal page (which is
195*0Sstevel@tonic-gate 		 * a parent page for the key).  Decrement the base by 1 if it's
196*0Sstevel@tonic-gate 		 * non-zero so that if a split later occurs, the inserted page
197*0Sstevel@tonic-gate 		 * will be to the right of the saved page.
198*0Sstevel@tonic-gate 		 */
199*0Sstevel@tonic-gate 		indx = base > 0 ? base - O_INDX : base;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		/*
202*0Sstevel@tonic-gate 		 * If we're trying to calculate the record number, sum up
203*0Sstevel@tonic-gate 		 * all the record numbers on this page up to the indx point.
204*0Sstevel@tonic-gate 		 */
205*0Sstevel@tonic-gate 		if (recnop != NULL)
206*0Sstevel@tonic-gate 			for (i = 0; i < indx; ++i)
207*0Sstevel@tonic-gate 				recno += GET_BINTERNAL(h, i)->nrecs;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate next:		pg = GET_BINTERNAL(h, indx)->pgno;
210*0Sstevel@tonic-gate 		if (stack) {
211*0Sstevel@tonic-gate 			/* Return if this is the lowest page wanted. */
212*0Sstevel@tonic-gate 			if (LF_ISSET(S_PARENT) && stop == h->level) {
213*0Sstevel@tonic-gate 				BT_STK_ENTER(cp, h, indx, lock, ret);
214*0Sstevel@tonic-gate 				return (ret);
215*0Sstevel@tonic-gate 			}
216*0Sstevel@tonic-gate 			BT_STK_PUSH(cp, h, indx, lock, ret);
217*0Sstevel@tonic-gate 			if (ret != 0)
218*0Sstevel@tonic-gate 				goto err;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 			if ((ret =
221*0Sstevel@tonic-gate 			    __bam_lget(dbc, 0, pg, DB_LOCK_WRITE, &lock)) != 0)
222*0Sstevel@tonic-gate 				goto err;
223*0Sstevel@tonic-gate 		} else {
224*0Sstevel@tonic-gate 			/*
225*0Sstevel@tonic-gate 			 * Decide if we want to return a reference to the next
226*0Sstevel@tonic-gate 			 * page in the return stack.  If so, lock it and never
227*0Sstevel@tonic-gate 			 * unlock it.
228*0Sstevel@tonic-gate 			 */
229*0Sstevel@tonic-gate 			if ((LF_ISSET(S_PARENT) &&
230*0Sstevel@tonic-gate 			    (u_int8_t)(stop + 1) >= (u_int8_t)(h->level - 1)) ||
231*0Sstevel@tonic-gate 			    (h->level - 1) == LEAFLEVEL)
232*0Sstevel@tonic-gate 				stack = 1;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 			(void)memp_fput(dbp->mpf, h, 0);
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 			if ((ret =
237*0Sstevel@tonic-gate 			    __bam_lget(dbc, 1, pg, stack && LF_ISSET(S_WRITE) ?
238*0Sstevel@tonic-gate 			    DB_LOCK_WRITE : DB_LOCK_READ, &lock)) != 0)
239*0Sstevel@tonic-gate 				goto err;
240*0Sstevel@tonic-gate 		}
241*0Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0)
242*0Sstevel@tonic-gate 			goto err;
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 	/* NOTREACHED */
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate match:	*exactp = 1;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	/*
249*0Sstevel@tonic-gate 	 * If we're trying to calculate the record number, add in the
250*0Sstevel@tonic-gate 	 * offset on this page and correct for the fact that records
251*0Sstevel@tonic-gate 	 * in the tree are 0-based.
252*0Sstevel@tonic-gate 	 */
253*0Sstevel@tonic-gate 	if (recnop != NULL)
254*0Sstevel@tonic-gate 		*recnop = recno + (indx / P_INDX) + 1;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	/*
257*0Sstevel@tonic-gate 	 * If we got here, we know that we have a btree leaf page.
258*0Sstevel@tonic-gate 	 *
259*0Sstevel@tonic-gate 	 * If there are duplicates, go to the first/last one.  This is
260*0Sstevel@tonic-gate 	 * safe because we know that we're not going to leave the page,
261*0Sstevel@tonic-gate 	 * all duplicate sets that are not on overflow pages exist on a
262*0Sstevel@tonic-gate 	 * single leaf page.
263*0Sstevel@tonic-gate 	 */
264*0Sstevel@tonic-gate 	if (LF_ISSET(S_DUPLAST))
265*0Sstevel@tonic-gate 		while (indx < (db_indx_t)(NUM_ENT(h) - P_INDX) &&
266*0Sstevel@tonic-gate 		    h->inp[indx] == h->inp[indx + P_INDX])
267*0Sstevel@tonic-gate 			indx += P_INDX;
268*0Sstevel@tonic-gate 	else
269*0Sstevel@tonic-gate 		while (indx > 0 &&
270*0Sstevel@tonic-gate 		    h->inp[indx] == h->inp[indx - P_INDX])
271*0Sstevel@tonic-gate 			indx -= P_INDX;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	/*
274*0Sstevel@tonic-gate 	 * Now check if we are allowed to return deleted items; if not
275*0Sstevel@tonic-gate 	 * find the next (or previous) non-deleted item.
276*0Sstevel@tonic-gate 	 */
277*0Sstevel@tonic-gate 	if (LF_ISSET(S_DELNO)) {
278*0Sstevel@tonic-gate 		if (LF_ISSET(S_DUPLAST))
279*0Sstevel@tonic-gate 			while (B_DISSET(GET_BKEYDATA(h, indx + O_INDX)->type) &&
280*0Sstevel@tonic-gate 			    indx > 0 &&
281*0Sstevel@tonic-gate 			    h->inp[indx] == h->inp[indx - P_INDX])
282*0Sstevel@tonic-gate 				indx -= P_INDX;
283*0Sstevel@tonic-gate 		else
284*0Sstevel@tonic-gate 			while (B_DISSET(GET_BKEYDATA(h, indx + O_INDX)->type) &&
285*0Sstevel@tonic-gate 			    indx < (db_indx_t)(NUM_ENT(h) - P_INDX) &&
286*0Sstevel@tonic-gate 			    h->inp[indx] == h->inp[indx + P_INDX])
287*0Sstevel@tonic-gate 				indx += P_INDX;
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 		if (B_DISSET(GET_BKEYDATA(h, indx + O_INDX)->type))
290*0Sstevel@tonic-gate 			goto notfound;
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	BT_STK_ENTER(cp, h, indx, lock, ret);
294*0Sstevel@tonic-gate 	return (ret);
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate notfound:
297*0Sstevel@tonic-gate 	(void)memp_fput(dbp->mpf, h, 0);
298*0Sstevel@tonic-gate 	(void)__BT_LPUT(dbc, lock);
299*0Sstevel@tonic-gate 	ret = DB_NOTFOUND;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate err:	if (cp->csp > cp->sp) {
302*0Sstevel@tonic-gate 		BT_STK_POP(cp);
303*0Sstevel@tonic-gate 		__bam_stkrel(dbc, 0);
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 	return (ret);
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate /*
309*0Sstevel@tonic-gate  * __bam_stkrel --
310*0Sstevel@tonic-gate  *	Release all pages currently held in the stack.
311*0Sstevel@tonic-gate  *
312*0Sstevel@tonic-gate  * PUBLIC: int __bam_stkrel __P((DBC *, int));
313*0Sstevel@tonic-gate  */
314*0Sstevel@tonic-gate int
__bam_stkrel(dbc,nolocks)315*0Sstevel@tonic-gate __bam_stkrel(dbc, nolocks)
316*0Sstevel@tonic-gate 	DBC *dbc;
317*0Sstevel@tonic-gate 	int nolocks;
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	CURSOR *cp;
320*0Sstevel@tonic-gate 	DB *dbp;
321*0Sstevel@tonic-gate 	EPG *epg;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	dbp = dbc->dbp;
324*0Sstevel@tonic-gate 	cp = dbc->internal;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	/* Release inner pages first. */
327*0Sstevel@tonic-gate 	for (epg = cp->sp; epg <= cp->csp; ++epg) {
328*0Sstevel@tonic-gate 		if (epg->page != NULL)
329*0Sstevel@tonic-gate 			(void)memp_fput(dbp->mpf, epg->page, 0);
330*0Sstevel@tonic-gate 		if (epg->lock != LOCK_INVALID)
331*0Sstevel@tonic-gate 			if (nolocks)
332*0Sstevel@tonic-gate 				(void)__BT_LPUT(dbc, epg->lock);
333*0Sstevel@tonic-gate 			else
334*0Sstevel@tonic-gate 				(void)__BT_TLPUT(dbc, epg->lock);
335*0Sstevel@tonic-gate 	}
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	/* Clear the stack, all pages have been released. */
338*0Sstevel@tonic-gate 	BT_STK_CLR(cp);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	return (0);
341*0Sstevel@tonic-gate }
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate /*
344*0Sstevel@tonic-gate  * __bam_stkgrow --
345*0Sstevel@tonic-gate  *	Grow the stack.
346*0Sstevel@tonic-gate  *
347*0Sstevel@tonic-gate  * PUBLIC: int __bam_stkgrow __P((CURSOR *));
348*0Sstevel@tonic-gate  */
349*0Sstevel@tonic-gate int
__bam_stkgrow(cp)350*0Sstevel@tonic-gate __bam_stkgrow(cp)
351*0Sstevel@tonic-gate 	CURSOR *cp;
352*0Sstevel@tonic-gate {
353*0Sstevel@tonic-gate 	EPG *p;
354*0Sstevel@tonic-gate 	size_t entries;
355*0Sstevel@tonic-gate 	int ret;
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	entries = cp->esp - cp->sp;
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	if ((ret = __os_calloc(entries * 2, sizeof(EPG), &p)) != 0)
360*0Sstevel@tonic-gate 		return (ret);
361*0Sstevel@tonic-gate 	memcpy(p, cp->sp, entries * sizeof(EPG));
362*0Sstevel@tonic-gate 	if (cp->sp != cp->stack)
363*0Sstevel@tonic-gate 		__os_free(cp->sp, entries * sizeof(EPG));
364*0Sstevel@tonic-gate 	cp->sp = p;
365*0Sstevel@tonic-gate 	cp->csp = p + entries;
366*0Sstevel@tonic-gate 	cp->esp = p + entries * 2;
367*0Sstevel@tonic-gate 	return (0);
368*0Sstevel@tonic-gate }
369