Path: blob/main/crypto/krb5/src/plugins/kdb/db2/libdb2/hash/hash.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* @(#)hash.h 8.4 (Berkeley) 11/2/9536*/3738#include "mpool.h"39#include "db-queue.h"4041/* Operations */42typedef enum {43HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT44} ACTION;4546/* cursor structure */47typedef struct cursor_t {48TAILQ_ENTRY(cursor_t) queue;49int (*get) __P((const DB *, struct cursor_t *, DBT *, DBT *, \50u_int32_t));51int (*delete) __P((const DB *, struct cursor_t *, u_int32_t));52db_pgno_t bucket;53db_pgno_t pgno;54indx_t ndx;55indx_t pgndx;56u_int16_t *pagep;57void *internal;58} CURSOR;596061#define IS_BUCKET(X) ((X) & BUF_BUCKET)62#define IS_VALID(X) (!((X) & BUF_INVALID))6364/* Hash Table Information */65typedef struct hashhdr { /* Disk resident portion */66int32_t magic; /* Magic NO for hash tables */67int32_t version; /* Version ID */68int32_t lorder; /* Byte Order */69u_int32_t bsize; /* Bucket/Page Size */70int32_t bshift; /* Bucket shift */71int32_t ovfl_point; /* Where overflow pages are being allocated */72u_int32_t last_freed; /* Last overflow page freed */73u_int32_t max_bucket; /* ID of Maximum bucket in use */74u_int32_t high_mask; /* Mask to modulo into entire table */75u_int32_t low_mask; /* Mask to modulo into lower half of table */76u_int32_t ffactor; /* Fill factor */77int32_t nkeys; /* Number of keys in hash table */78u_int32_t hdrpages; /* Size of table header */79u_int32_t h_charkey; /* value of hash(CHARKEY) */80#define NCACHED 32 /* number of bit maps and spare points */81u_int32_t spares[NCACHED];/* spare pages for overflow */82u_int16_t bitmaps[NCACHED]; /* address of overflow page bitmaps */83} HASHHDR;8485typedef struct htab { /* Memory resident data structure */86TAILQ_HEAD(_cursor_queue, cursor_t) curs_queue;87HASHHDR hdr; /* Header */88u_int32_t (*hash) __P((const void *, size_t)); /* Hash Function */89int32_t flags; /* Flag values */90int32_t fp; /* File pointer */91const char *fname; /* File path */92u_int8_t *bigdata_buf; /* Temporary Buffer for BIG data */93u_int8_t *bigkey_buf; /* Temporary Buffer for BIG keys */94u_int16_t *split_buf; /* Temporary buffer for splits */95CURSOR *seq_cursor; /* Cursor used for hash_seq */96int32_t local_errno; /* Error Number -- for DBM compatibility */97int32_t new_file; /* Indicates if fd is backing store or no */98int32_t save_file; /* Indicates whether we need to flush file at99* exit */100u_int32_t *mapp[NCACHED];/* Pointers to page maps */101int32_t nmaps; /* Initial number of bitmaps */102MPOOL *mp; /* mpool for buffer management */103} HTAB;104105/*106* Constants107*/108#define MAX_BSIZE 65536 /* 2^16 */109#define MIN_BUFFERS 6110#define MINHDRSIZE 512111#define DEF_CACHESIZE 65536112#define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */113#define DEF_BUCKET_SIZE (1<<DEF_BUCKET_SHIFT)114#define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */115#define DEF_SEGSIZE (1<<DEF_SEGSIZE_SHIFT)116#define DEF_DIRSIZE 256117#define DEF_FFACTOR 65536118#define MIN_FFACTOR 4119#define SPLTMAX 8120#define CHARKEY "%$sniglet^&"121#define NUMKEY 1038583122#define BYTE_SHIFT 3123#define INT32_T_TO_BYTE 2124#define INT32_T_BYTE_SHIFT 5125#define ALL_SET ((u_int32_t)0xFFFFFFFF)126#define ALL_CLEAR 0127128#define PTROF(X) ((BUFHEAD *)((ptr_t)(X)&~0x3))129#define ISMOD(X) ((ptr_t)(X)&0x1)130#define DOMOD(X) ((X) = (int8_t *)((ptr_t)(X)|0x1))131#define ISDISK(X) ((ptr_t)(X)&0x2)132#define DODISK(X) ((X) = (int8_t *)((ptr_t)(X)|0x2))133134#define BITS_PER_MAP 32135136/* Given the address of the beginning of a big map, clear/set the nth bit */137#define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))138#define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))139#define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))140141/* Overflow management */142/*143* Overflow page numbers are allocated per split point. At each doubling of144* the table, we can allocate extra pages. So, an overflow page number has145* the top 5 bits indicate which split point and the lower 11 bits indicate146* which page at that split point is indicated (pages within split points are147* numberered starting with 1).148*/149150#define SPLITSHIFT 11151#define SPLITMASK 0x7FF152#define SPLITNUM(N) (((u_int32_t)(N)) >> SPLITSHIFT)153#define OPAGENUM(N) ((N) & SPLITMASK)154#define OADDR_OF(S,O) ((u_int32_t)((u_int32_t)(S) << SPLITSHIFT) + (O))155156#define BUCKET_TO_PAGE(B) \157((B) + hashp->hdr.hdrpages + ((B) \158? hashp->hdr.spares[__log2((B)+1)-1] : 0))159#define OADDR_TO_PAGE(B) \160(BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B)))161162#define POW2(N) (1 << (N))163164#define MAX_PAGES(H) (DB_OFF_T_MAX / (H)->hdr.bsize)165166/* Shorthands for accessing structure */167#define METADATA_PGNO 0168#define SPLIT_PGNO 0xFFFF169170typedef struct item_info {171db_pgno_t pgno;172db_pgno_t bucket;173indx_t ndx;174indx_t pgndx;175u_int8_t status;176u_int32_t seek_size;177db_pgno_t seek_found_page;178indx_t key_off;179indx_t data_off;180u_int8_t caused_expand;181} ITEM_INFO;182183184#define ITEM_ERROR 0185#define ITEM_OK 1186#define ITEM_NO_MORE 2187188#define ITEM_GET_FIRST 0189#define ITEM_GET_NEXT 1190#define ITEM_GET_RESET 2191#define ITEM_GET_DONE 3192#define ITEM_GET_N 4193194#define UNKNOWN 0xffffffff /* for num_items */195#define NO_EXPAND 0xfffffffe196197198