Blame xf86drmHash.c

Packit Service 103f6b
/* xf86drmHash.c -- Small hash table support for integer -> integer mapping
Packit Service 103f6b
 * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
Packit Service 103f6b
 *
Packit Service 103f6b
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
Packit Service 103f6b
 * All Rights Reserved.
Packit Service 103f6b
 *
Packit Service 103f6b
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit Service 103f6b
 * copy of this software and associated documentation files (the "Software"),
Packit Service 103f6b
 * to deal in the Software without restriction, including without limitation
Packit Service 103f6b
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit Service 103f6b
 * and/or sell copies of the Software, and to permit persons to whom the
Packit Service 103f6b
 * Software is furnished to do so, subject to the following conditions:
Packit Service 103f6b
 *
Packit Service 103f6b
 * The above copyright notice and this permission notice (including the next
Packit Service 103f6b
 * paragraph) shall be included in all copies or substantial portions of the
Packit Service 103f6b
 * Software.
Packit Service 103f6b
 *
Packit Service 103f6b
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 103f6b
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 103f6b
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
Packit Service 103f6b
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
Packit Service 103f6b
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Packit Service 103f6b
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit Service 103f6b
 * DEALINGS IN THE SOFTWARE.
Packit Service 103f6b
 *
Packit Service 103f6b
 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
Packit Service 103f6b
 *
Packit Service 103f6b
 * DESCRIPTION
Packit Service 103f6b
 *
Packit Service 103f6b
 * This file contains a straightforward implementation of a fixed-sized
Packit Service 103f6b
 * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
Packit Service 103f6b
 * collision resolution.  There are two potentially interesting things
Packit Service 103f6b
 * about this implementation:
Packit Service 103f6b
 *
Packit Service 103f6b
 * 1) The table is power-of-two sized.  Prime sized tables are more
Packit Service 103f6b
 * traditional, but do not have a significant advantage over power-of-two
Packit Service 103f6b
 * sized table, especially when double hashing is not used for collision
Packit Service 103f6b
 * resolution.
Packit Service 103f6b
 *
Packit Service 103f6b
 * 2) The hash computation uses a table of random integers [Hanson97,
Packit Service 103f6b
 * pp. 39-41].
Packit Service 103f6b
 *
Packit Service 103f6b
 * FUTURE ENHANCEMENTS
Packit Service 103f6b
 *
Packit Service 103f6b
 * With a table size of 512, the current implementation is sufficient for a
Packit Service 103f6b
 * few hundred keys.  Since this is well above the expected size of the
Packit Service 103f6b
 * tables for which this implementation was designed, the implementation of
Packit Service 103f6b
 * dynamic hash tables was postponed until the need arises.  A common (and
Packit Service 103f6b
 * naive) approach to dynamic hash table implementation simply creates a
Packit Service 103f6b
 * new hash table when necessary, rehashes all the data into the new table,
Packit Service 103f6b
 * and destroys the old table.  The approach in [Larson88] is superior in
Packit Service 103f6b
 * two ways: 1) only a portion of the table is expanded when needed,
Packit Service 103f6b
 * distributing the expansion cost over several insertions, and 2) portions
Packit Service 103f6b
 * of the table can be locked, enabling a scalable thread-safe
Packit Service 103f6b
 * implementation.
Packit Service 103f6b
 *
Packit Service 103f6b
 * REFERENCES
Packit Service 103f6b
 *
Packit Service 103f6b
 * [Hanson97] David R. Hanson.  C Interfaces and Implementations:
Packit Service 103f6b
 * Techniques for Creating Reusable Software.  Reading, Massachusetts:
Packit Service 103f6b
 * Addison-Wesley, 1997.
Packit Service 103f6b
 *
Packit Service 103f6b
 * [Knuth73] Donald E. Knuth. The Art of Computer Programming.  Volume 3:
Packit Service 103f6b
 * Sorting and Searching.  Reading, Massachusetts: Addison-Wesley, 1973.
Packit Service 103f6b
 *
Packit Service 103f6b
 * [Larson88] Per-Ake Larson. "Dynamic Hash Tables".  CACM 31(4), April
Packit Service 103f6b
 * 1988, pp. 446-457.
Packit Service 103f6b
 *
Packit Service 103f6b
 */
Packit Service 103f6b
Packit Service 103f6b
#include <stdio.h>
Packit Service 103f6b
#include <stdlib.h>
Packit Service 103f6b
Packit Service 103f6b
#include "libdrm_macros.h"
Packit Service 103f6b
#include "xf86drm.h"
Packit Service 103f6b
#include "xf86drmHash.h"
Packit Service 103f6b
Packit Service 103f6b
#define HASH_MAGIC 0xdeadbeef
Packit Service 103f6b
Packit Service 103f6b
static unsigned long HashHash(unsigned long key)
Packit Service 103f6b
{
Packit Service 103f6b
    unsigned long        hash = 0;
Packit Service 103f6b
    unsigned long        tmp  = key;
Packit Service 103f6b
    static int           init = 0;
Packit Service 103f6b
    static unsigned long scatter[256];
Packit Service 103f6b
    int                  i;
Packit Service 103f6b
Packit Service 103f6b
    if (!init) {
Packit Service 103f6b
	void *state;
Packit Service 103f6b
	state = drmRandomCreate(37);
Packit Service 103f6b
	for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
Packit Service 103f6b
	drmRandomDestroy(state);
Packit Service 103f6b
	++init;
Packit Service 103f6b
    }
Packit Service 103f6b
Packit Service 103f6b
    while (tmp) {
Packit Service 103f6b
	hash = (hash << 1) + scatter[tmp & 0xff];
Packit Service 103f6b
	tmp >>= 8;
Packit Service 103f6b
    }
Packit Service 103f6b
Packit Service 103f6b
    hash %= HASH_SIZE;
Packit Service 103f6b
    return hash;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public void *drmHashCreate(void)
Packit Service 103f6b
{
Packit Service 103f6b
    HashTablePtr table;
Packit Service 103f6b
Packit Service 103f6b
    table           = drmMalloc(sizeof(*table));
Packit Service 103f6b
    if (!table) return NULL;
Packit Service 103f6b
    table->magic    = HASH_MAGIC;
Packit Service 103f6b
Packit Service 103f6b
    return table;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public int drmHashDestroy(void *t)
Packit Service 103f6b
{
Packit Service 103f6b
    HashTablePtr  table = (HashTablePtr)t;
Packit Service 103f6b
    HashBucketPtr bucket;
Packit Service 103f6b
    HashBucketPtr next;
Packit Service 103f6b
    int           i;
Packit Service 103f6b
Packit Service 103f6b
    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Packit Service 103f6b
Packit Service 103f6b
    for (i = 0; i < HASH_SIZE; i++) {
Packit Service 103f6b
	for (bucket = table->buckets[i]; bucket;) {
Packit Service 103f6b
	    next = bucket->next;
Packit Service 103f6b
	    drmFree(bucket);
Packit Service 103f6b
	    bucket = next;
Packit Service 103f6b
	}
Packit Service 103f6b
    }
Packit Service 103f6b
    drmFree(table);
Packit Service 103f6b
    return 0;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
/* Find the bucket and organize the list so that this bucket is at the
Packit Service 103f6b
   top. */
Packit Service 103f6b
Packit Service 103f6b
static HashBucketPtr HashFind(HashTablePtr table,
Packit Service 103f6b
			      unsigned long key, unsigned long *h)
Packit Service 103f6b
{
Packit Service 103f6b
    unsigned long hash = HashHash(key);
Packit Service 103f6b
    HashBucketPtr prev = NULL;
Packit Service 103f6b
    HashBucketPtr bucket;
Packit Service 103f6b
Packit Service 103f6b
    if (h) *h = hash;
Packit Service 103f6b
Packit Service 103f6b
    for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
Packit Service 103f6b
	if (bucket->key == key) {
Packit Service 103f6b
	    if (prev) {
Packit Service 103f6b
				/* Organize */
Packit Service 103f6b
		prev->next           = bucket->next;
Packit Service 103f6b
		bucket->next         = table->buckets[hash];
Packit Service 103f6b
		table->buckets[hash] = bucket;
Packit Service 103f6b
		++table->partials;
Packit Service 103f6b
	    } else {
Packit Service 103f6b
		++table->hits;
Packit Service 103f6b
	    }
Packit Service 103f6b
	    return bucket;
Packit Service 103f6b
	}
Packit Service 103f6b
	prev = bucket;
Packit Service 103f6b
    }
Packit Service 103f6b
    ++table->misses;
Packit Service 103f6b
    return NULL;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public int drmHashLookup(void *t, unsigned long key, void **value)
Packit Service 103f6b
{
Packit Service 103f6b
    HashTablePtr  table = (HashTablePtr)t;
Packit Service 103f6b
    HashBucketPtr bucket;
Packit Service 103f6b
Packit Service 103f6b
    if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
Packit Service 103f6b
Packit Service 103f6b
    bucket = HashFind(table, key, NULL);
Packit Service 103f6b
    if (!bucket) return 1;	/* Not found */
Packit Service 103f6b
    *value = bucket->value;
Packit Service 103f6b
    return 0;			/* Found */
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public int drmHashInsert(void *t, unsigned long key, void *value)
Packit Service 103f6b
{
Packit Service 103f6b
    HashTablePtr  table = (HashTablePtr)t;
Packit Service 103f6b
    HashBucketPtr bucket;
Packit Service 103f6b
    unsigned long hash;
Packit Service 103f6b
Packit Service 103f6b
    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Packit Service 103f6b
Packit Service 103f6b
    if (HashFind(table, key, &hash)) return 1; /* Already in table */
Packit Service 103f6b
Packit Service 103f6b
    bucket               = drmMalloc(sizeof(*bucket));
Packit Service 103f6b
    if (!bucket) return -1;	/* Error */
Packit Service 103f6b
    bucket->key          = key;
Packit Service 103f6b
    bucket->value        = value;
Packit Service 103f6b
    bucket->next         = table->buckets[hash];
Packit Service 103f6b
    table->buckets[hash] = bucket;
Packit Service 103f6b
    return 0;			/* Added to table */
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public int drmHashDelete(void *t, unsigned long key)
Packit Service 103f6b
{
Packit Service 103f6b
    HashTablePtr  table = (HashTablePtr)t;
Packit Service 103f6b
    unsigned long hash;
Packit Service 103f6b
    HashBucketPtr bucket;
Packit Service 103f6b
Packit Service 103f6b
    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Packit Service 103f6b
Packit Service 103f6b
    bucket = HashFind(table, key, &hash);
Packit Service 103f6b
Packit Service 103f6b
    if (!bucket) return 1;	/* Not found */
Packit Service 103f6b
Packit Service 103f6b
    table->buckets[hash] = bucket->next;
Packit Service 103f6b
    drmFree(bucket);
Packit Service 103f6b
    return 0;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public int drmHashNext(void *t, unsigned long *key, void **value)
Packit Service 103f6b
{
Packit Service 103f6b
    HashTablePtr  table = (HashTablePtr)t;
Packit Service 103f6b
Packit Service 103f6b
    while (table->p0 < HASH_SIZE) {
Packit Service 103f6b
	if (table->p1) {
Packit Service 103f6b
	    *key       = table->p1->key;
Packit Service 103f6b
	    *value     = table->p1->value;
Packit Service 103f6b
	    table->p1  = table->p1->next;
Packit Service 103f6b
	    return 1;
Packit Service 103f6b
	}
Packit Service 103f6b
	table->p1 = table->buckets[table->p0];
Packit Service 103f6b
	++table->p0;
Packit Service 103f6b
    }
Packit Service 103f6b
    return 0;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public int drmHashFirst(void *t, unsigned long *key, void **value)
Packit Service 103f6b
{
Packit Service 103f6b
    HashTablePtr  table = (HashTablePtr)t;
Packit Service 103f6b
Packit Service 103f6b
    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
Packit Service 103f6b
Packit Service 103f6b
    table->p0 = 0;
Packit Service 103f6b
    table->p1 = table->buckets[0];
Packit Service 103f6b
    return drmHashNext(table, key, value);
Packit Service 103f6b
}