xref: /csrg-svn/lib/libc/db/btree/bt_page.c (revision 56500)
1*56500Sbostic /*-
2*56500Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*56500Sbostic  * All rights reserved.
4*56500Sbostic  *
5*56500Sbostic  * %sccs.include.redist.c%
6*56500Sbostic  */
7*56500Sbostic 
8*56500Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*56500Sbostic static char sccsid[] = "@(#)bt_page.c	5.1 (Berkeley) 10/10/92";
10*56500Sbostic #endif /* LIBC_SCCS and not lint */
11*56500Sbostic 
12*56500Sbostic #include <sys/types.h>
13*56500Sbostic #define	__DBINTERFACE_PRIVATE
14*56500Sbostic #include <db.h>
15*56500Sbostic #include <stdio.h>
16*56500Sbostic 
17*56500Sbostic #include "btree.h"
18*56500Sbostic 
19*56500Sbostic /*
20*56500Sbostic  * __BT_FREE -- Put a page on the freelist.
21*56500Sbostic  *
22*56500Sbostic  * Parameters:
23*56500Sbostic  *	t:	tree
24*56500Sbostic  *	h:	page to free
25*56500Sbostic  *
26*56500Sbostic  * Returns:
27*56500Sbostic  *	RET_ERROR, RET_SUCCESS
28*56500Sbostic  */
29*56500Sbostic int
30*56500Sbostic __bt_free(t, h)
31*56500Sbostic 	BTREE *t;
32*56500Sbostic 	PAGE *h;
33*56500Sbostic {
34*56500Sbostic 	/* Insert the page at the start of the free list. */
35*56500Sbostic 	h->prevpg = P_INVALID;
36*56500Sbostic 	h->nextpg = t->bt_free;
37*56500Sbostic 	t->bt_free = h->pgno;
38*56500Sbostic 
39*56500Sbostic 	/* Make sure the page gets written back. */
40*56500Sbostic 	return (mpool_put(t->bt_mp, h, MPOOL_DIRTY));
41*56500Sbostic }
42*56500Sbostic 
43*56500Sbostic /*
44*56500Sbostic  * __BT_NEW -- Get a new page, preferably from the freelist.
45*56500Sbostic  *
46*56500Sbostic  * Parameters:
47*56500Sbostic  *	t:	tree
48*56500Sbostic  *	npg:	storage for page number.
49*56500Sbostic  *
50*56500Sbostic  * Returns:
51*56500Sbostic  *	Pointer to a page, NULL on error.
52*56500Sbostic  */
53*56500Sbostic PAGE *
54*56500Sbostic __bt_new(t, npg)
55*56500Sbostic 	BTREE *t;
56*56500Sbostic 	pgno_t *npg;
57*56500Sbostic {
58*56500Sbostic 	PAGE *h;
59*56500Sbostic 
60*56500Sbostic 	if (t->bt_free != P_INVALID &&
61*56500Sbostic 	    (h = mpool_get(t->bt_mp, t->bt_free, 0)) != NULL) {
62*56500Sbostic 			*npg = t->bt_free;
63*56500Sbostic 			t->bt_free = h->nextpg;
64*56500Sbostic 			return (h);
65*56500Sbostic 	}
66*56500Sbostic 	return (mpool_new(t->bt_mp, npg));
67*56500Sbostic }
68