xref: /netbsd-src/lib/libc/db/btree/bt_page.c (revision aae80e6be7bf8e6ad43f8b63d296d2d3923c4ee5)
1*aae80e6bSchristos /*	$NetBSD: bt_page.c,v 1.15 2016/09/24 21:31:25 christos Exp $	*/
2a954b078Scgd 
39f0aa214Scgd /*-
424420c01Scgd  * Copyright (c) 1990, 1993, 1994
59f0aa214Scgd  *	The Regents of the University of California.  All rights reserved.
69f0aa214Scgd  *
79f0aa214Scgd  * Redistribution and use in source and binary forms, with or without
89f0aa214Scgd  * modification, are permitted provided that the following conditions
99f0aa214Scgd  * are met:
109f0aa214Scgd  * 1. Redistributions of source code must retain the above copyright
119f0aa214Scgd  *    notice, this list of conditions and the following disclaimer.
129f0aa214Scgd  * 2. Redistributions in binary form must reproduce the above copyright
139f0aa214Scgd  *    notice, this list of conditions and the following disclaimer in the
149f0aa214Scgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
169f0aa214Scgd  *    may be used to endorse or promote products derived from this software
179f0aa214Scgd  *    without specific prior written permission.
189f0aa214Scgd  *
199f0aa214Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209f0aa214Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219f0aa214Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229f0aa214Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239f0aa214Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249f0aa214Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259f0aa214Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269f0aa214Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279f0aa214Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289f0aa214Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299f0aa214Scgd  * SUCH DAMAGE.
309f0aa214Scgd  */
319f0aa214Scgd 
32d3595ddfSjoerg #if HAVE_NBTOOL_CONFIG_H
33d3595ddfSjoerg #include "nbtool_config.h"
34d3595ddfSjoerg #endif
35d3595ddfSjoerg 
3600ae392dSchristos #include <sys/cdefs.h>
37*aae80e6bSchristos __RCSID("$NetBSD: bt_page.c,v 1.15 2016/09/24 21:31:25 christos Exp $");
389f0aa214Scgd 
3943fa6fe3Sjtc #include "namespace.h"
409f0aa214Scgd #include <sys/types.h>
419f0aa214Scgd 
42cb9daf8fSchristos #include <assert.h>
439f0aa214Scgd #include <stdio.h>
449f0aa214Scgd 
459f0aa214Scgd #include <db.h>
469f0aa214Scgd #include "btree.h"
479f0aa214Scgd 
489f0aa214Scgd /*
4924420c01Scgd  * __bt_free --
5024420c01Scgd  *	Put a page on the freelist.
519f0aa214Scgd  *
529f0aa214Scgd  * Parameters:
539f0aa214Scgd  *	t:	tree
549f0aa214Scgd  *	h:	page to free
559f0aa214Scgd  *
569f0aa214Scgd  * Returns:
579f0aa214Scgd  *	RET_ERROR, RET_SUCCESS
5824420c01Scgd  *
5924420c01Scgd  * Side-effect:
6024420c01Scgd  *	mpool_put's the page.
619f0aa214Scgd  */
629f0aa214Scgd int
__bt_free(BTREE * t,PAGE * h)63cb9daf8fSchristos __bt_free(BTREE *t, PAGE *h)
649f0aa214Scgd {
6524420c01Scgd 	/* Insert the page at the head of the free list. */
669f0aa214Scgd 	h->prevpg = P_INVALID;
679f0aa214Scgd 	h->nextpg = t->bt_free;
689f0aa214Scgd 	t->bt_free = h->pgno;
698871c98bSis 	F_SET(t, B_METADIRTY);
709f0aa214Scgd 
719f0aa214Scgd 	/* Make sure the page gets written back. */
729f0aa214Scgd 	return (mpool_put(t->bt_mp, h, MPOOL_DIRTY));
739f0aa214Scgd }
749f0aa214Scgd 
759f0aa214Scgd /*
7624420c01Scgd  * __bt_new --
7724420c01Scgd  *	Get a new page, preferably from the freelist.
789f0aa214Scgd  *
799f0aa214Scgd  * Parameters:
809f0aa214Scgd  *	t:	tree
819f0aa214Scgd  *	npg:	storage for page number.
829f0aa214Scgd  *
839f0aa214Scgd  * Returns:
849f0aa214Scgd  *	Pointer to a page, NULL on error.
859f0aa214Scgd  */
869f0aa214Scgd PAGE *
__bt_new(BTREE * t,pgno_t * npg)87cb9daf8fSchristos __bt_new(BTREE *t, pgno_t *npg)
889f0aa214Scgd {
899f0aa214Scgd 	PAGE *h;
909f0aa214Scgd 
919f0aa214Scgd 	if (t->bt_free != P_INVALID &&
92*aae80e6bSchristos 	    (h = mpool_get(t->bt_mp, t->bt_free, 0)) != NULL) {
939f0aa214Scgd 		*npg = t->bt_free;
949f0aa214Scgd 		t->bt_free = h->nextpg;
958871c98bSis 		F_SET(t, B_METADIRTY);
969f0aa214Scgd 		return (h);
979f0aa214Scgd 	}
989f0aa214Scgd 	return (mpool_new(t->bt_mp, npg));
999f0aa214Scgd }
100