Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

lock_util.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: lock_util.c,v 12.2 2005/06/16 20:23:11 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <string.h>
00016 #endif
00017 
00018 #include "db_int.h"
00019 #include "dbinc/db_page.h"
00020 #include "dbinc/db_shash.h"
00021 #include "dbinc/hash.h"
00022 #include "dbinc/lock.h"
00023 
00024 /*
00025  * __lock_cmp --
00026  *      This function is used to compare a DBT that is about to be entered
00027  *      into a hash table with an object already in the hash table.  Note
00028  *      that it just returns true on equal and 0 on not-equal.  Therefore
00029  *      this function cannot be used as a sort function; its purpose is to
00030  *      be used as a hash comparison function.
00031  *
00032  * PUBLIC: int __lock_cmp __P((const DBT *, DB_LOCKOBJ *));
00033  */
00034 int
00035 __lock_cmp(dbt, lock_obj)
00036         const DBT *dbt;
00037         DB_LOCKOBJ *lock_obj;
00038 {
00039         void *obj_data;
00040 
00041         obj_data = SH_DBT_PTR(&lock_obj->lockobj);
00042         return (dbt->size == lock_obj->lockobj.size &&
00043                 memcmp(dbt->data, obj_data, dbt->size) == 0);
00044 }
00045 
00046 /*
00047  * PUBLIC: int __lock_locker_cmp __P((u_int32_t, DB_LOCKER *));
00048  */
00049 int
00050 __lock_locker_cmp(locker, sh_locker)
00051         u_int32_t locker;
00052         DB_LOCKER *sh_locker;
00053 {
00054         return (locker == sh_locker->id);
00055 }
00056 
00057 /*
00058  * The next two functions are the hash functions used to store objects in the
00059  * lock hash tables.  They are hashing the same items, but one (__lock_ohash)
00060  * takes a DBT (used for hashing a parameter passed from the user) and the
00061  * other (__lock_lhash) takes a DB_LOCKOBJ (used for hashing something that is
00062  * already in the lock manager).  In both cases, we have a special check to
00063  * fast path the case where we think we are doing a hash on a DB page/fileid
00064  * pair.  If the size is right, then we do the fast hash.
00065  *
00066  * We know that DB uses DB_LOCK_ILOCK types for its lock objects.  The first
00067  * four bytes are the 4-byte page number and the next DB_FILE_ID_LEN bytes
00068  * are a unique file id, where the first 4 bytes on UNIX systems are the file
00069  * inode number, and the first 4 bytes on Windows systems are the FileIndexLow
00070  * bytes.  So, we use the XOR of the page number and the first four bytes of
00071  * the file id to produce a 32-bit hash value.
00072  *
00073  * We have no particular reason to believe that this algorithm will produce
00074  * a good hash, but we want a fast hash more than we want a good one, when
00075  * we're coming through this code path.
00076  */
00077 #define FAST_HASH(P) {                  \
00078         u_int32_t __h;                  \
00079         u_int8_t *__cp, *__hp;          \
00080         __hp = (u_int8_t *)&__h;        \
00081         __cp = (u_int8_t *)(P);         \
00082         __hp[0] = __cp[0] ^ __cp[4];    \
00083         __hp[1] = __cp[1] ^ __cp[5];    \
00084         __hp[2] = __cp[2] ^ __cp[6];    \
00085         __hp[3] = __cp[3] ^ __cp[7];    \
00086         return (__h);                   \
00087 }
00088 
00089 /*
00090  * __lock_ohash --
00091  *
00092  * PUBLIC: u_int32_t __lock_ohash __P((const DBT *));
00093  */
00094 u_int32_t
00095 __lock_ohash(dbt)
00096         const DBT *dbt;
00097 {
00098         if (dbt->size == sizeof(DB_LOCK_ILOCK))
00099                 FAST_HASH(dbt->data);
00100 
00101         return (__ham_func5(NULL, dbt->data, dbt->size));
00102 }
00103 
00104 /*
00105  * __lock_lhash --
00106  *
00107  * PUBLIC: u_int32_t __lock_lhash __P((DB_LOCKOBJ *));
00108  */
00109 u_int32_t
00110 __lock_lhash(lock_obj)
00111         DB_LOCKOBJ *lock_obj;
00112 {
00113         void *obj_data;
00114 
00115         obj_data = SH_DBT_PTR(&lock_obj->lockobj);
00116 
00117         if (lock_obj->lockobj.size == sizeof(DB_LOCK_ILOCK))
00118                 FAST_HASH(obj_data);
00119 
00120         return (__ham_func5(NULL, obj_data, lock_obj->lockobj.size));
00121 }
00122 
00123 /*
00124  * __lock_nomem --
00125  *      Report a lack of some resource.
00126  *
00127  * PUBLIC: int __lock_nomem __P((DB_ENV *, const char *));
00128  */
00129 int
00130 __lock_nomem(dbenv, res)
00131         DB_ENV *dbenv;
00132         const char *res;
00133 {
00134         __db_err(dbenv, "Lock table is out of available %s", res);
00135         return (ENOMEM);
00136 }

Generated on Sun Dec 25 12:14:40 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2