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