1*0Sstevel@tonic-gate /*- 2*0Sstevel@tonic-gate * See the file LICENSE for redistribution information. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998 5*0Sstevel@tonic-gate * Sleepycat Software. All rights reserved. 6*0Sstevel@tonic-gate */ 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate #include "config.h" 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gate #ifndef lint 11*0Sstevel@tonic-gate static const char sccsid[] = "@(#)lock_util.c 10.10 (Sleepycat) 9/20/98"; 12*0Sstevel@tonic-gate #endif /* not lint */ 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES 15*0Sstevel@tonic-gate #include <sys/types.h> 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate #include <string.h> 18*0Sstevel@tonic-gate #endif 19*0Sstevel@tonic-gate 20*0Sstevel@tonic-gate #include "db_int.h" 21*0Sstevel@tonic-gate #include "shqueue.h" 22*0Sstevel@tonic-gate #include "db_page.h" 23*0Sstevel@tonic-gate #include "db_shash.h" 24*0Sstevel@tonic-gate #include "hash.h" 25*0Sstevel@tonic-gate #include "lock.h" 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* 28*0Sstevel@tonic-gate * __lock_cmp -- 29*0Sstevel@tonic-gate * This function is used to compare a DBT that is about to be entered 30*0Sstevel@tonic-gate * into a hash table with an object already in the hash table. Note 31*0Sstevel@tonic-gate * that it just returns true on equal and 0 on not-equal. Therefore 32*0Sstevel@tonic-gate * this function cannot be used as a sort function; its purpose is to 33*0Sstevel@tonic-gate * be used as a hash comparison function. 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * PUBLIC: int __lock_cmp __P((const DBT *, DB_LOCKOBJ *)); 36*0Sstevel@tonic-gate */ 37*0Sstevel@tonic-gate int 38*0Sstevel@tonic-gate __lock_cmp(dbt, lock_obj) 39*0Sstevel@tonic-gate const DBT *dbt; 40*0Sstevel@tonic-gate DB_LOCKOBJ *lock_obj; 41*0Sstevel@tonic-gate { 42*0Sstevel@tonic-gate void *obj_data; 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate if (lock_obj->type != DB_LOCK_OBJTYPE) 45*0Sstevel@tonic-gate return (0); 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate obj_data = SH_DBT_PTR(&lock_obj->lockobj); 48*0Sstevel@tonic-gate return (dbt->size == lock_obj->lockobj.size && 49*0Sstevel@tonic-gate memcmp(dbt->data, obj_data, dbt->size) == 0); 50*0Sstevel@tonic-gate } 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate /* 53*0Sstevel@tonic-gate * PUBLIC: int __lock_locker_cmp __P((u_int32_t, DB_LOCKOBJ *)); 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate int 56*0Sstevel@tonic-gate __lock_locker_cmp(locker, lock_obj) 57*0Sstevel@tonic-gate u_int32_t locker; 58*0Sstevel@tonic-gate DB_LOCKOBJ *lock_obj; 59*0Sstevel@tonic-gate { 60*0Sstevel@tonic-gate void *obj_data; 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate if (lock_obj->type != DB_LOCK_LOCKER) 63*0Sstevel@tonic-gate return (0); 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate obj_data = SH_DBT_PTR(&lock_obj->lockobj); 66*0Sstevel@tonic-gate return (memcmp(&locker, obj_data, sizeof(u_int32_t)) == 0); 67*0Sstevel@tonic-gate } 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate /* 70*0Sstevel@tonic-gate * The next two functions are the hash functions used to store objects in the 71*0Sstevel@tonic-gate * lock hash table. They are hashing the same items, but one (__lock_ohash) 72*0Sstevel@tonic-gate * takes a DBT (used for hashing a parameter passed from the user) and the 73*0Sstevel@tonic-gate * other (__lock_lhash) takes a DB_LOCKOBJ (used for hashing something that is 74*0Sstevel@tonic-gate * already in the lock manager). In both cases, we have a special check to 75*0Sstevel@tonic-gate * fast path the case where we think we are doing a hash on a DB page/fileid 76*0Sstevel@tonic-gate * pair. If the size is right, then we do the fast hash. 77*0Sstevel@tonic-gate * 78*0Sstevel@tonic-gate * We know that DB uses DB_LOCK_ILOCK types for its lock objects. The first 79*0Sstevel@tonic-gate * four bytes are the 4-byte page number and the next DB_FILE_ID_LEN bytes 80*0Sstevel@tonic-gate * are a unique file id, where the first 4 bytes on UNIX systems are the file 81*0Sstevel@tonic-gate * inode number, and the first 4 bytes on Windows systems are the FileIndexLow 82*0Sstevel@tonic-gate * bytes. So, we use the XOR of the page number and the first four bytes of 83*0Sstevel@tonic-gate * the file id to produce a 32-bit hash value. 84*0Sstevel@tonic-gate * 85*0Sstevel@tonic-gate * We have no particular reason to believe that this algorithm will produce 86*0Sstevel@tonic-gate * a good hash, but we want a fast hash more than we want a good one, when 87*0Sstevel@tonic-gate * we're coming through this code path. 88*0Sstevel@tonic-gate */ 89*0Sstevel@tonic-gate #define FAST_HASH(P) { \ 90*0Sstevel@tonic-gate u_int32_t __h; \ 91*0Sstevel@tonic-gate u_int8_t *__cp, *__hp; \ 92*0Sstevel@tonic-gate __hp = (u_int8_t *)&__h; \ 93*0Sstevel@tonic-gate __cp = (u_int8_t *)(P); \ 94*0Sstevel@tonic-gate __hp[0] = __cp[0] ^ __cp[4]; \ 95*0Sstevel@tonic-gate __hp[1] = __cp[1] ^ __cp[5]; \ 96*0Sstevel@tonic-gate __hp[2] = __cp[2] ^ __cp[6]; \ 97*0Sstevel@tonic-gate __hp[3] = __cp[3] ^ __cp[7]; \ 98*0Sstevel@tonic-gate return (__h); \ 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate /* 102*0Sstevel@tonic-gate * __lock_ohash -- 103*0Sstevel@tonic-gate * 104*0Sstevel@tonic-gate * PUBLIC: u_int32_t __lock_ohash __P((const DBT *)); 105*0Sstevel@tonic-gate */ 106*0Sstevel@tonic-gate u_int32_t 107*0Sstevel@tonic-gate __lock_ohash(dbt) 108*0Sstevel@tonic-gate const DBT *dbt; 109*0Sstevel@tonic-gate { 110*0Sstevel@tonic-gate if (dbt->size == sizeof(DB_LOCK_ILOCK)) 111*0Sstevel@tonic-gate FAST_HASH(dbt->data); 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate return (__ham_func5(dbt->data, dbt->size)); 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate /* 117*0Sstevel@tonic-gate * __lock_lhash -- 118*0Sstevel@tonic-gate * 119*0Sstevel@tonic-gate * PUBLIC: u_int32_t __lock_lhash __P((DB_LOCKOBJ *)); 120*0Sstevel@tonic-gate */ 121*0Sstevel@tonic-gate u_int32_t 122*0Sstevel@tonic-gate __lock_lhash(lock_obj) 123*0Sstevel@tonic-gate DB_LOCKOBJ *lock_obj; 124*0Sstevel@tonic-gate { 125*0Sstevel@tonic-gate u_int32_t tmp; 126*0Sstevel@tonic-gate void *obj_data; 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate obj_data = SH_DBT_PTR(&lock_obj->lockobj); 129*0Sstevel@tonic-gate if (lock_obj->type == DB_LOCK_LOCKER) { 130*0Sstevel@tonic-gate memcpy(&tmp, obj_data, sizeof(u_int32_t)); 131*0Sstevel@tonic-gate return (tmp); 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate if (lock_obj->lockobj.size == sizeof(DB_LOCK_ILOCK)) 135*0Sstevel@tonic-gate FAST_HASH(obj_data); 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate return (__ham_func5(obj_data, lock_obj->lockobj.size)); 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate /* 141*0Sstevel@tonic-gate * __lock_locker_hash -- 142*0Sstevel@tonic-gate * Hash function for entering lockers into the hash table. Since these 143*0Sstevel@tonic-gate * are simply 32-bit unsigned integers, just return the locker value. 144*0Sstevel@tonic-gate * 145*0Sstevel@tonic-gate * PUBLIC: u_int32_t __lock_locker_hash __P((u_int32_t)); 146*0Sstevel@tonic-gate */ 147*0Sstevel@tonic-gate u_int32_t 148*0Sstevel@tonic-gate __lock_locker_hash(locker) 149*0Sstevel@tonic-gate u_int32_t locker; 150*0Sstevel@tonic-gate { 151*0Sstevel@tonic-gate return (locker); 152*0Sstevel@tonic-gate } 153