Path: blob/main/crypto/krb5/src/plugins/kdb/db2/libdb2/hash/page.h
34928 views
/*-1* Copyright (c) 1990, 1993, 19942* The Regents of the University of California. All rights reserved.3*4* This code is derived from software contributed to Berkeley by5* Margo Seltzer.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. All advertising materials mentioning features or use of this software16* must display the following acknowledgement:17* This product includes software developed by the University of18* California, Berkeley and its contributors.19* 4. Neither the name of the University nor the names of its contributors20* may be used to endorse or promote products derived from this software21* without specific prior written permission.22*23* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND24* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE25* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE26* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL28* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS29* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)30* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT31* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY32* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF33* SUCH DAMAGE.34*35* @(#)page.h 8.4 (Berkeley) 11/7/9536*/3738#define HI_MASK 0xFFFF000039#define LO_MASK (~HI_MASK)4041#define HI(N) ((u_int16_t)(((N) & HI_MASK) >> 16))42#define LO(N) ((u_int16_t)((N) & LO_MASK))4344/* Constants for big key page overhead information. */45#define NUMSHORTS 046#define KEYLEN 147#define DATALEN 248#define NEXTPAGE 34950/*51* Hash pages store meta-data beginning at the top of the page (offset 0)52* and key/data values beginning at the bottom of the page (offset pagesize).53* Fields are always accessed via macros so that we can change the page54* format without too much pain. The only changes that will require massive55* code changes are if we no longer store key/data offsets next to each56* other (since we use that fact to compute key lengths). In the accessor57* macros below, P means a pointer to the page, I means an index of the58* particular entry being accessed.59*60* Hash base page format61* BYTE ITEM NBYTES TYPE ACCESSOR MACRO62* ---- ------------------ ------ -------- --------------63* 0 previous page number 4 db_pgno_t PREV_PGNO(P)64* 4 next page number 4 db_pgno_t NEXT_PGNO(P)65* 8 # pairs on page 2 indx_t NUM_ENT(P)66* 10 page type 1 u_int8_t TYPE(P)67* 11 padding 1 u_int8_t none68* 12 highest free byte 2 indx_t OFFSET(P)69* 14 key offset 0 2 indx_t KEY_OFF(P, I)70* 16 data offset 0 2 indx_t DATA_OFF(P, I)71* 18 key offset 1 2 indx_t KEY_OFF(P, I)72* 20 data offset 1 2 indx_t DATA_OFF(P, I)73* ...etc...74*/7576/* Indices (in bytes) of the beginning of each of these entries */77#define I_PREV_PGNO 078#define I_NEXT_PGNO 479#define I_ENTRIES 880#define I_TYPE 1081#define I_HF_OFFSET 128283/* Overhead is everything prior to the first key/data pair. */84#define PAGE_OVERHEAD (I_HF_OFFSET + sizeof(indx_t))8586/* To allocate a pair, we need room for one key offset and one data offset. */87#define PAIR_OVERHEAD ((sizeof(indx_t) << 1))8889/* Use this macro to extract a value of type T from page P at offset O. */90#define REFERENCE(P, T, O) (((T *)(void *)((u_int8_t *)(void *)(P) + O))[0])9192/*93* Use these macros to access fields on a page; P is a PAGE16 *.94*/95#define NUM_ENT(P) (REFERENCE((P), indx_t, I_ENTRIES))96#define PREV_PGNO(P) (REFERENCE((P), db_pgno_t, I_PREV_PGNO))97#define NEXT_PGNO(P) (REFERENCE((P), db_pgno_t, I_NEXT_PGNO))98#define TYPE(P) (REFERENCE((P), u_int8_t, I_TYPE))99#define OFFSET(P) (REFERENCE((P), indx_t, I_HF_OFFSET))100/*101* We need to store a page's own address on each page (unlike the Btree102* access method which needs the previous page). We use the PREV_PGNO103* field to store our own page number.104*/105#define ADDR(P) (PREV_PGNO((P)))106107/* Extract key/data offsets and data for a given index. */108#define DATA_OFF(P, N) \109REFERENCE(P, indx_t, PAGE_OVERHEAD + N * PAIR_OVERHEAD + sizeof(indx_t))110#define KEY_OFF(P, N) \111REFERENCE(P, indx_t, PAGE_OVERHEAD + N * PAIR_OVERHEAD)112113#define KEY(P, N) (((PAGE8 *)(P)) + KEY_OFF((P), (N)))114#define DATA(P, N) (((PAGE8 *)(P)) + DATA_OFF((P), (N)))115116/*117* Macros used to compute various sizes on a page.118*/119#define PAIRSIZE(K, D) (PAIR_OVERHEAD + (K)->size + (D)->size)120#define BIGOVERHEAD (4 * sizeof(u_int16_t))121#define KEYSIZE(K) (4 * sizeof(u_int16_t) + (K)->size);122#define OVFLSIZE (2 * sizeof(u_int16_t))123#define BIGPAGEOVERHEAD (4 * sizeof(u_int16_t))124#define BIGPAGEOFFSET 4125#define BIGPAGESIZE(P) ((P)->BSIZE - BIGPAGEOVERHEAD)126127#define PAGE_META(N) (((N) + 3) * sizeof(u_int16_t))128#define MINFILL 0.75129#define ISBIG(N, P) (((N) > ((P)->hdr.bsize * MINFILL)) ? 1 : 0)130131#define ITEMSIZE(I) (sizeof(u_int16_t) + (I)->size)132133/*134* Big key/data pages use a different page format. They have a single135* key/data "pair" containing the length of the key and data instead136* of offsets.137*/138#define BIGKEYLEN(P) (KEY_OFF((P), 0))139#define BIGDATALEN(P) (DATA_OFF((P), 0))140#define BIGKEY(P) (((PAGE8 *)(P)) + PAGE_OVERHEAD + PAIR_OVERHEAD)141#define BIGDATA(P) \142(((PAGE8 *)(P)) + PAGE_OVERHEAD + PAIR_OVERHEAD + KEY_OFF((P), 0))143144145#define OVFLPAGE 0146#define BIGPAIR 0147#define INVALID_PGNO 0xFFFFFFFF148149typedef unsigned short PAGE16;150typedef unsigned char PAGE8;151152#define A_BUCKET 0153#define A_OVFL 1154#define A_BITMAP 2155#define A_RAW 4156#define A_HEADER 5157158#define PAIRFITS(P,K,D) ((PAIRSIZE((K),(D))) <= FREESPACE((P)))159#define BIGPAIRFITS(P) ((FREESPACE((P)) >= PAIR_OVERHEAD))160/*161* Since these are all unsigned, we need to guarantee that we never go162* negative. Offset values are 0-based and overheads are one based (i.e.163* one byte of overhead is 1, not 0), so we need to convert OFFSETs to164* 1-based counting before subtraction.165*/166#define FREESPACE(P) \167((OFFSET((P)) + 1 - PAGE_OVERHEAD - (NUM_ENT((P)) * PAIR_OVERHEAD)))168169/*170* Overhead on header pages is just one word -- the length of the171* header info stored on that page.172*/173#define HEADER_OVERHEAD 4174175#define HASH_PAGE 2176#define HASH_BIGPAGE 3177#define HASH_OVFLPAGE 4178179180