bt_page.c revision 272461
121173Sjkh/*- 250479Speter * Copyright (c) 1990, 1993, 1994 350479Speter * The Regents of the University of California. All rights reserved. 421173Sjkh * 521173Sjkh * Redistribution and use in source and binary forms, with or without 621173Sjkh * modification, are permitted provided that the following conditions 721173Sjkh * are met: 821173Sjkh * 1. Redistributions of source code must retain the above copyright 921173Sjkh * notice, this list of conditions and the following disclaimer. 1021173Sjkh * 2. Redistributions in binary form must reproduce the above copyright 11281727Seadler * notice, this list of conditions and the following disclaimer in the 12281727Seadler * documentation and/or other materials provided with the distribution. 13159363Strhodes * 4. Neither the name of the University nor the names of its contributors 14159363Strhodes * may be used to endorse or promote products derived from this software 15281727Seadler * without specific prior written permission. 1664057Salex * 1764057Salex * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1864057Salex * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1921173Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20281727Seadler * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21281727Seadler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22281727Seadler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23281727Seadler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2464057Salex * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25281727Seadler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2664057Salex * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2721173Sjkh * SUCH DAMAGE. 28281727Seadler */ 29281727Seadler 30281727Seadler#if defined(LIBC_SCCS) && !defined(lint) 31281478Seadlerstatic char sccsid[] = "@(#)bt_page.c 8.3 (Berkeley) 7/14/94"; 3221173Sjkh#endif /* LIBC_SCCS and not lint */ 3321173Sjkh#include <sys/cdefs.h> 3421173Sjkh__FBSDID("$FreeBSD: releng/10.1/lib/libc/db/btree/bt_page.c 190498 2009-03-28 07:31:02Z delphij $"); 3521173Sjkh 36281727Seadler#include <sys/types.h> 3793217Sru 3821173Sjkh#include <stdio.h> 39215293Sjoel 40215293Sjoel#include <db.h> 41215293Sjoel#include "btree.h" 4221173Sjkh 4321173Sjkh/* 4464047Salex * __bt_free -- 4521173Sjkh * Put a page on the freelist. 4693217Sru * 4721173Sjkh * Parameters: 4821173Sjkh * t: tree 4921173Sjkh * h: page to free 5021173Sjkh * 5121173Sjkh * Returns: 5221173Sjkh * RET_ERROR, RET_SUCCESS 5393217Sru * 5437283Sjkh * Side-effect: 5521173Sjkh * mpool_put's the page. 5621173Sjkh */ 5721173Sjkhint 5821173Sjkh__bt_free(BTREE *t, PAGE *h) 5921173Sjkh{ 6021173Sjkh /* Insert the page at the head of the free list. */ 61156171Sceri h->prevpg = P_INVALID; 62156171Sceri h->nextpg = t->bt_free; 6321173Sjkh t->bt_free = h->pgno; 6421173Sjkh F_SET(t, B_METADIRTY); 6565298Smarkm 6621173Sjkh /* Make sure the page gets written back. */ 6721173Sjkh return (mpool_put(t->bt_mp, h, MPOOL_DIRTY)); 6821173Sjkh} 69329145Skevans 70329145Skevans/* 7121173Sjkh * __bt_new -- 7221173Sjkh * Get a new page, preferably from the freelist. 73281478Seadler * 74281478Seadler * Parameters: 75281478Seadler * t: tree 7621173Sjkh * npg: storage for page number. 7721173Sjkh * 7821173Sjkh * Returns: 7921173Sjkh * Pointer to a page, NULL on error. 8021173Sjkh */ 8121173SjkhPAGE * 8221173Sjkh__bt_new(BTREE *t, pgno_t *npg) 8321173Sjkh{ 8421173Sjkh PAGE *h; 8521173Sjkh 86318447Sgjb if (t->bt_free != P_INVALID && 87 (h = mpool_get(t->bt_mp, t->bt_free, 0)) != NULL) { 88 *npg = t->bt_free; 89 t->bt_free = h->nextpg; 90 F_SET(t, B_METADIRTY); 91 return (h); 92 } 93 return (mpool_new(t->bt_mp, npg, MPOOL_PAGE_NEXT)); 94} 95