xref: /csrg-svn/lib/libc/db/btree/bt_page.c (revision 66192)
156500Sbostic /*-
261196Sbostic  * Copyright (c) 1990, 1993
361196Sbostic  *	The Regents of the University of California.  All rights reserved.
456500Sbostic  *
556500Sbostic  * %sccs.include.redist.c%
656500Sbostic  */
756500Sbostic 
856500Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*66192Sbostic static char sccsid[] = "@(#)bt_page.c	8.2 (Berkeley) 02/21/94";
1056500Sbostic #endif /* LIBC_SCCS and not lint */
1156500Sbostic 
1256500Sbostic #include <sys/types.h>
1356739Sbostic 
1456500Sbostic #include <stdio.h>
1556500Sbostic 
1657932Sbostic #include <db.h>
1756500Sbostic #include "btree.h"
1856500Sbostic 
1956500Sbostic /*
2056500Sbostic  * __BT_FREE -- Put a page on the freelist.
2156500Sbostic  *
2256500Sbostic  * Parameters:
2356500Sbostic  *	t:	tree
2456500Sbostic  *	h:	page to free
2556500Sbostic  *
2656500Sbostic  * Returns:
2756500Sbostic  *	RET_ERROR, RET_SUCCESS
2856500Sbostic  */
2956500Sbostic int
__bt_free(t,h)3056500Sbostic __bt_free(t, h)
3156500Sbostic 	BTREE *t;
3256500Sbostic 	PAGE *h;
3356500Sbostic {
3456500Sbostic 	/* Insert the page at the start of the free list. */
3556500Sbostic 	h->prevpg = P_INVALID;
3656500Sbostic 	h->nextpg = t->bt_free;
3756500Sbostic 	t->bt_free = h->pgno;
3856500Sbostic 
3956500Sbostic 	/* Make sure the page gets written back. */
4056500Sbostic 	return (mpool_put(t->bt_mp, h, MPOOL_DIRTY));
4156500Sbostic }
4256500Sbostic 
4356500Sbostic /*
4456500Sbostic  * __BT_NEW -- Get a new page, preferably from the freelist.
4556500Sbostic  *
4656500Sbostic  * Parameters:
4756500Sbostic  *	t:	tree
4856500Sbostic  *	npg:	storage for page number.
4956500Sbostic  *
5056500Sbostic  * Returns:
5156500Sbostic  *	Pointer to a page, NULL on error.
5256500Sbostic  */
5356500Sbostic PAGE *
__bt_new(t,npg)5456500Sbostic __bt_new(t, npg)
5556500Sbostic 	BTREE *t;
5656500Sbostic 	pgno_t *npg;
5756500Sbostic {
5856500Sbostic 	PAGE *h;
5956500Sbostic 
6056500Sbostic 	if (t->bt_free != P_INVALID &&
6156500Sbostic 	    (h = mpool_get(t->bt_mp, t->bt_free, 0)) != NULL) {
6256500Sbostic 			*npg = t->bt_free;
6356500Sbostic 			t->bt_free = h->nextpg;
6456500Sbostic 			return (h);
6556500Sbostic 	}
6656500Sbostic 	return (mpool_new(t->bt_mp, npg));
6756500Sbostic }
68