1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1996, 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)lock_util.c 10.10 (Sleepycat) 9/20/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <string.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "shqueue.h"
1N/A#include "db_page.h"
1N/A#include "db_shash.h"
1N/A#include "hash.h"
1N/A#include "lock.h"
1N/A
1N/A/*
1N/A * __lock_cmp --
1N/A * This function is used to compare a DBT that is about to be entered
1N/A * into a hash table with an object already in the hash table. Note
1N/A * that it just returns true on equal and 0 on not-equal. Therefore
1N/A * this function cannot be used as a sort function; its purpose is to
1N/A * be used as a hash comparison function.
1N/A *
1N/A * PUBLIC: int __lock_cmp __P((const DBT *, DB_LOCKOBJ *));
1N/A */
1N/Aint
1N/A__lock_cmp(dbt, lock_obj)
1N/A const DBT *dbt;
1N/A DB_LOCKOBJ *lock_obj;
1N/A{
1N/A void *obj_data;
1N/A
1N/A if (lock_obj->type != DB_LOCK_OBJTYPE)
1N/A return (0);
1N/A
1N/A obj_data = SH_DBT_PTR(&lock_obj->lockobj);
1N/A return (dbt->size == lock_obj->lockobj.size &&
1N/A memcmp(dbt->data, obj_data, dbt->size) == 0);
1N/A}
1N/A
1N/A/*
1N/A * PUBLIC: int __lock_locker_cmp __P((u_int32_t, DB_LOCKOBJ *));
1N/A */
1N/Aint
1N/A__lock_locker_cmp(locker, lock_obj)
1N/A u_int32_t locker;
1N/A DB_LOCKOBJ *lock_obj;
1N/A{
1N/A void *obj_data;
1N/A
1N/A if (lock_obj->type != DB_LOCK_LOCKER)
1N/A return (0);
1N/A
1N/A obj_data = SH_DBT_PTR(&lock_obj->lockobj);
1N/A return (memcmp(&locker, obj_data, sizeof(u_int32_t)) == 0);
1N/A}
1N/A
1N/A/*
1N/A * The next two functions are the hash functions used to store objects in the
1N/A * lock hash table. They are hashing the same items, but one (__lock_ohash)
1N/A * takes a DBT (used for hashing a parameter passed from the user) and the
1N/A * other (__lock_lhash) takes a DB_LOCKOBJ (used for hashing something that is
1N/A * already in the lock manager). In both cases, we have a special check to
1N/A * fast path the case where we think we are doing a hash on a DB page/fileid
1N/A * pair. If the size is right, then we do the fast hash.
1N/A *
1N/A * We know that DB uses DB_LOCK_ILOCK types for its lock objects. The first
1N/A * four bytes are the 4-byte page number and the next DB_FILE_ID_LEN bytes
1N/A * are a unique file id, where the first 4 bytes on UNIX systems are the file
1N/A * inode number, and the first 4 bytes on Windows systems are the FileIndexLow
1N/A * bytes. So, we use the XOR of the page number and the first four bytes of
1N/A * the file id to produce a 32-bit hash value.
1N/A *
1N/A * We have no particular reason to believe that this algorithm will produce
1N/A * a good hash, but we want a fast hash more than we want a good one, when
1N/A * we're coming through this code path.
1N/A */
1N/A#define FAST_HASH(P) { \
1N/A u_int32_t __h; \
1N/A u_int8_t *__cp, *__hp; \
1N/A __hp = (u_int8_t *)&__h; \
1N/A __cp = (u_int8_t *)(P); \
1N/A __hp[0] = __cp[0] ^ __cp[4]; \
1N/A __hp[1] = __cp[1] ^ __cp[5]; \
1N/A __hp[2] = __cp[2] ^ __cp[6]; \
1N/A __hp[3] = __cp[3] ^ __cp[7]; \
1N/A return (__h); \
1N/A}
1N/A
1N/A/*
1N/A * __lock_ohash --
1N/A *
1N/A * PUBLIC: u_int32_t __lock_ohash __P((const DBT *));
1N/A */
1N/Au_int32_t
1N/A__lock_ohash(dbt)
1N/A const DBT *dbt;
1N/A{
1N/A if (dbt->size == sizeof(DB_LOCK_ILOCK))
1N/A FAST_HASH(dbt->data);
1N/A
1N/A return (__ham_func5(dbt->data, dbt->size));
1N/A}
1N/A
1N/A/*
1N/A * __lock_lhash --
1N/A *
1N/A * PUBLIC: u_int32_t __lock_lhash __P((DB_LOCKOBJ *));
1N/A */
1N/Au_int32_t
1N/A__lock_lhash(lock_obj)
1N/A DB_LOCKOBJ *lock_obj;
1N/A{
1N/A u_int32_t tmp;
1N/A void *obj_data;
1N/A
1N/A obj_data = SH_DBT_PTR(&lock_obj->lockobj);
1N/A if (lock_obj->type == DB_LOCK_LOCKER) {
1N/A memcpy(&tmp, obj_data, sizeof(u_int32_t));
1N/A return (tmp);
1N/A }
1N/A
1N/A if (lock_obj->lockobj.size == sizeof(DB_LOCK_ILOCK))
1N/A FAST_HASH(obj_data);
1N/A
1N/A return (__ham_func5(obj_data, lock_obj->lockobj.size));
1N/A}
1N/A
1N/A/*
1N/A * __lock_locker_hash --
1N/A * Hash function for entering lockers into the hash table. Since these
1N/A * are simply 32-bit unsigned integers, just return the locker value.
1N/A *
1N/A * PUBLIC: u_int32_t __lock_locker_hash __P((u_int32_t));
1N/A */
1N/Au_int32_t
1N/A__lock_locker_hash(locker)
1N/A u_int32_t locker;
1N/A{
1N/A return (locker);
1N/A}
1N/A