哈希(Hash)函数(均匀Hash)

[linux 2.4.0 -> catalog.c]

/* Number of hash table slots */
#define C_HASHBITS  10
#define C_HASHSIZE  (1UL << C_HASHBITS)
#define C_HASHMASK  (C_HASHSIZE - 1)

/*
 * hashfn()
 * hash an (struct mdb *) and a (struct hfs_cat_key *) to an integer.
 */
static inline unsigned int hashfn(const struct hfs_mdb *mdb,
                  const struct hfs_cat_key *key)
{
    unsigned int hash;

    hash = (unsigned long) mdb | (unsigned long) key->ParID[3] | 
        hfs_strhash(key->CName.Name, key->CName.Len);
    hash = hash ^ (hash >> C_HASHBITS) ^ (hash >> C_HASHBITS*2);
    return hash & C_HASHMASK;
}

[linux 2.4.0 -> include\linux\sched.h]

#define PIDHASH_SZ (4096 >> 2)
extern struct task_struct *pidhash[PIDHASH_SZ];

#define pid_hashfn(x)   ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))

[apache -> vhost.c]

/* This hashing function is designed to get good distribution in the cases
 * where the server is handling entire "networks" of servers.  i.e. a
 * whack of /24s.  This is probably the most common configuration for
 * ISPs with large virtual servers.
 *
 * NOTE: This function is symmetric (i.e. collapses all 4 octets
 * into one), so machine byte order (big/little endianness) does not matter.
 *
 * Hash function provided by David Hankins.
 */
static APR_INLINE unsigned hash_inaddr(unsigned key)
{
    key ^= (key >> 16);
    return ((key >> 8) ^ key) % IPHASH_TABLE_SIZE;
}

#define IPHASH_TABLE_SIZE 256

(性能评估待定)

猜你喜欢

转载自blog.csdn.net/wwchao2012/article/details/80342585