/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Margo Seltzer.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334/* Operations */35typedef enum {36HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT37} ACTION;3839/* Buffer Management structures */40typedef struct _bufhead BUFHEAD;4142struct _bufhead {43BUFHEAD *prev; /* LRU links */44BUFHEAD *next; /* LRU links */45BUFHEAD *ovfl; /* Overflow page buffer header */46u_int32_t addr; /* Address of this page */47char *page; /* Actual page data */48char flags;49#define BUF_MOD 0x000150#define BUF_DISK 0x000251#define BUF_BUCKET 0x000452#define BUF_PIN 0x000853};5455#define IS_BUCKET(X) ((X) & BUF_BUCKET)5657typedef BUFHEAD **SEGMENT;5859/* Hash Table Information */60typedef struct hashhdr { /* Disk resident portion */61int32_t magic; /* Magic NO for hash tables */62int32_t version; /* Version ID */63u_int32_t lorder; /* Byte Order */64int32_t bsize; /* Bucket/Page Size */65int32_t bshift; /* Bucket shift */66int32_t dsize; /* Directory Size */67int32_t ssize; /* Segment Size */68int32_t sshift; /* Segment shift */69int32_t ovfl_point; /* Where overflow pages are being70* allocated */71int32_t last_freed; /* Last overflow page freed */72u_int32_t max_bucket; /* ID of Maximum bucket in use */73u_int32_t high_mask; /* Mask to modulo into entire table */74u_int32_t low_mask; /* Mask to modulo into lower half of75* table */76u_int32_t ffactor; /* Fill factor */77int32_t nkeys; /* Number of keys in hash table */78int32_t hdrpages; /* Size of table header */79int32_t h_charkey; /* value of hash(CHARKEY) */80#define NCACHED 32 /* number of bit maps and spare81* points */82int32_t spares[NCACHED];/* spare pages for overflow */83u_int16_t bitmaps[NCACHED]; /* address of overflow page84* bitmaps */85} HASHHDR;8687typedef struct htab { /* Memory resident data structure */88HASHHDR hdr; /* Header */89int nsegs; /* Number of allocated segments */90int exsegs; /* Number of extra allocated91* segments */92u_int32_t /* Hash function */93(*hash)(const void *, size_t);94int flags; /* Flag values */95int fp; /* File pointer */96char *tmp_buf; /* Temporary Buffer for BIG data */97char *tmp_key; /* Temporary Buffer for BIG keys */98BUFHEAD *cpage; /* Current page */99int cbucket; /* Current bucket */100int cndx; /* Index of next item on cpage */101int error; /* Error Number -- for DBM102* compatibility */103int new_file; /* Indicates if fd is backing store104* or no */105int save_file; /* Indicates whether we need to flush106* file at107* exit */108u_int32_t *mapp[NCACHED]; /* Pointers to page maps */109int nmaps; /* Initial number of bitmaps */110int nbufs; /* Number of buffers left to111* allocate */112BUFHEAD bufhead; /* Header of buffer lru list */113SEGMENT *dir; /* Hash Bucket directory */114} HTAB;115116/*117* Constants118*/119#define MAX_BSIZE 32768 /* 2^15 but should be 65536 */120#define MIN_BUFFERS 6121#define MINHDRSIZE 512122#define DEF_BUFSIZE 65536 /* 64 K */123#define DEF_BUCKET_SIZE 4096124#define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */125#define DEF_SEGSIZE 256126#define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */127#define DEF_DIRSIZE 256128#define DEF_FFACTOR 65536129#define MIN_FFACTOR 4130#define SPLTMAX 8131#define CHARKEY "%$sniglet^&"132#define NUMKEY 1038583133#define BYTE_SHIFT 3134#define INT_TO_BYTE 2135#define INT_BYTE_SHIFT 5136#define ALL_SET ((u_int32_t)0xFFFFFFFF)137#define ALL_CLEAR 0138139#define PTROF(X) ((BUFHEAD *)((intptr_t)(X)&~0x3))140#define ISMOD(X) ((u_int32_t)(intptr_t)(X)&0x1)141#define DOMOD(X) ((X) = (char *)((intptr_t)(X)|0x1))142#define ISDISK(X) ((u_int32_t)(intptr_t)(X)&0x2)143#define DODISK(X) ((X) = (char *)((intptr_t)(X)|0x2))144145#define BITS_PER_MAP 32146147/* Given the address of the beginning of a big map, clear/set the nth bit */148#define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))149#define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))150#define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))151152/* Overflow management */153/*154* Overflow page numbers are allocated per split point. At each doubling of155* the table, we can allocate extra pages. So, an overflow page number has156* the top 5 bits indicate which split point and the lower 11 bits indicate157* which page at that split point is indicated (pages within split points are158* numberered starting with 1).159*/160161#define SPLITSHIFT 11162#define SPLITMASK 0x7FF163#define SPLITNUM(N) (((u_int32_t)(N)) >> SPLITSHIFT)164#define OPAGENUM(N) ((N) & SPLITMASK)165#define OADDR_OF(S,O) ((u_int32_t)((u_int32_t)(S) << SPLITSHIFT) + (O))166167#define BUCKET_TO_PAGE(B) \168(B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((B)+1)-1] : 0)169#define OADDR_TO_PAGE(B) \170BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B));171172/*173* page.h contains a detailed description of the page format.174*175* Normally, keys and data are accessed from offset tables in the top of176* each page which point to the beginning of the key and data. There are177* four flag values which may be stored in these offset tables which indicate178* the following:179*180*181* OVFLPAGE Rather than a key data pair, this pair contains182* the address of an overflow page. The format of183* the pair is:184* OVERFLOW_PAGE_NUMBER OVFLPAGE185*186* PARTIAL_KEY This must be the first key/data pair on a page187* and implies that page contains only a partial key.188* That is, the key is too big to fit on a single page189* so it starts on this page and continues on the next.190* The format of the page is:191* KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE192*193* KEY_OFF -- offset of the beginning of the key194* PARTIAL_KEY -- 1195* OVFL_PAGENO - page number of the next overflow page196* OVFLPAGE -- 0197*198* FULL_KEY This must be the first key/data pair on the page. It199* is used in two cases.200*201* Case 1:202* There is a complete key on the page but no data203* (because it wouldn't fit). The next page contains204* the data.205*206* Page format it:207* KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE208*209* KEY_OFF -- offset of the beginning of the key210* FULL_KEY -- 2211* OVFL_PAGENO - page number of the next overflow page212* OVFLPAGE -- 0213*214* Case 2:215* This page contains no key, but part of a large216* data field, which is continued on the next page.217*218* Page format it:219* DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE220*221* KEY_OFF -- offset of the beginning of the data on222* this page223* FULL_KEY -- 2224* OVFL_PAGENO - page number of the next overflow page225* OVFLPAGE -- 0226*227* FULL_KEY_DATA228* This must be the first key/data pair on the page.229* There are two cases:230*231* Case 1:232* This page contains a key and the beginning of the233* data field, but the data field is continued on the234* next page.235*236* Page format is:237* KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF238*239* KEY_OFF -- offset of the beginning of the key240* FULL_KEY_DATA -- 3241* OVFL_PAGENO - page number of the next overflow page242* DATA_OFF -- offset of the beginning of the data243*244* Case 2:245* This page contains the last page of a big data pair.246* There is no key, only the tail end of the data247* on this page.248*249* Page format is:250* DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>251*252* DATA_OFF -- offset of the beginning of the data on253* this page254* FULL_KEY_DATA -- 3255* OVFL_PAGENO - page number of the next overflow page256* OVFLPAGE -- 0257*258* OVFL_PAGENO and OVFLPAGE are optional (they are259* not present if there is no next page).260*/261262#define OVFLPAGE 0263#define PARTIAL_KEY 1264#define FULL_KEY 2265#define FULL_KEY_DATA 3266#define REAL_KEY 4267268/* Short hands for accessing structure */269#define BSIZE hdr.bsize270#define BSHIFT hdr.bshift271#define DSIZE hdr.dsize272#define SGSIZE hdr.ssize273#define SSHIFT hdr.sshift274#define LORDER hdr.lorder275#define OVFL_POINT hdr.ovfl_point276#define LAST_FREED hdr.last_freed277#define MAX_BUCKET hdr.max_bucket278#define FFACTOR hdr.ffactor279#define HIGH_MASK hdr.high_mask280#define LOW_MASK hdr.low_mask281#define NKEYS hdr.nkeys282#define HDRPAGES hdr.hdrpages283#define SPARES hdr.spares284#define BITMAPS hdr.bitmaps285#define VERSION hdr.version286#define MAGIC hdr.magic287#define NEXT_FREE hdr.next_free288#define H_CHARKEY hdr.h_charkey289290291