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