Blame tests/hash.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 "xf86drm.h"
Packit Service 103f6b
#include "xf86drmHash.h"
Packit Service 103f6b
Packit Service 103f6b
#define DIST_LIMIT 10
Packit Service 103f6b
static int dist[DIST_LIMIT];
Packit Service 103f6b
Packit Service 103f6b
static void clear_dist(void) {
Packit Service 103f6b
    int i;
Packit Service 103f6b
Packit Service 103f6b
    for (i = 0; i < DIST_LIMIT; i++)
Packit Service 103f6b
        dist[i] = 0;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
static int count_entries(HashBucketPtr bucket)
Packit Service 103f6b
{
Packit Service 103f6b
    int count = 0;
Packit Service 103f6b
Packit Service 103f6b
    for (; bucket; bucket = bucket->next)
Packit Service 103f6b
        ++count;
Packit Service 103f6b
    return count;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
static void update_dist(int count)
Packit Service 103f6b
{
Packit Service 103f6b
    if (count >= DIST_LIMIT)
Packit Service 103f6b
        ++dist[DIST_LIMIT-1];
Packit Service 103f6b
    else
Packit Service 103f6b
        ++dist[count];
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
static void compute_dist(HashTablePtr table)
Packit Service 103f6b
{
Packit Service 103f6b
    int           i;
Packit Service 103f6b
    HashBucketPtr bucket;
Packit Service 103f6b
Packit Service 103f6b
    printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
Packit Service 103f6b
          table->entries, table->hits, table->partials, table->misses);
Packit Service 103f6b
    clear_dist();
Packit Service 103f6b
    for (i = 0; i < HASH_SIZE; i++) {
Packit Service 103f6b
        bucket = table->buckets[i];
Packit Service 103f6b
        update_dist(count_entries(bucket));
Packit Service 103f6b
    }
Packit Service 103f6b
    for (i = 0; i < DIST_LIMIT; i++) {
Packit Service 103f6b
        if (i != DIST_LIMIT-1)
Packit Service 103f6b
            printf("%5d %10d\n", i, dist[i]);
Packit Service 103f6b
        else
Packit Service 103f6b
            printf("other %10d\n", dist[i]);
Packit Service 103f6b
    }
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
static int check_table(HashTablePtr table,
Packit Service 103f6b
                       unsigned long key, void * value)
Packit Service 103f6b
{
Packit Service 103f6b
    void *retval;
Packit Service 103f6b
    int   retcode = drmHashLookup(table, key, &retval);
Packit Service 103f6b
Packit Service 103f6b
    switch (retcode) {
Packit Service 103f6b
    case -1:
Packit Service 103f6b
        printf("Bad magic = 0x%08lx:"
Packit Service 103f6b
               " key = %lu, expected = %p, returned = %p\n",
Packit Service 103f6b
               table->magic, key, value, retval);
Packit Service 103f6b
        break;
Packit Service 103f6b
    case 1:
Packit Service 103f6b
        printf("Not found: key = %lu, expected = %p, returned = %p\n",
Packit Service 103f6b
               key, value, retval);
Packit Service 103f6b
        break;
Packit Service 103f6b
    case 0:
Packit Service 103f6b
        if (value != retval) {
Packit Service 103f6b
            printf("Bad value: key = %lu, expected = %p, returned = %p\n",
Packit Service 103f6b
                   key, value, retval);
Packit Service 103f6b
            retcode = -1;
Packit Service 103f6b
        }
Packit Service 103f6b
        break;
Packit Service 103f6b
    default:
Packit Service 103f6b
        printf("Bad retcode = %d: key = %lu, expected = %p, returned = %p\n",
Packit Service 103f6b
               retcode, key, value, retval);
Packit Service 103f6b
        break;
Packit Service 103f6b
    }
Packit Service 103f6b
    return retcode;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
int main(void)
Packit Service 103f6b
{
Packit Service 103f6b
    HashTablePtr  table;
Packit Service 103f6b
    unsigned long i;
Packit Service 103f6b
    int           ret = 0;
Packit Service 103f6b
Packit Service 103f6b
    printf("\n***** 256 consecutive integers ****\n");
Packit Service 103f6b
    table = drmHashCreate();
Packit Service 103f6b
    for (i = 0; i < 256; i++)
Packit Service 103f6b
        drmHashInsert(table, i, (void *)(i << 16 | i));
Packit Service 103f6b
    for (i = 0; i < 256; i++)
Packit Service 103f6b
        ret |= check_table(table, i, (void *)(i << 16 | i));
Packit Service 103f6b
    compute_dist(table);
Packit Service 103f6b
    drmHashDestroy(table);
Packit Service 103f6b
Packit Service 103f6b
    printf("\n***** 1024 consecutive integers ****\n");
Packit Service 103f6b
    table = drmHashCreate();
Packit Service 103f6b
    for (i = 0; i < 1024; i++)
Packit Service 103f6b
        drmHashInsert(table, i, (void *)(i << 16 | i));
Packit Service 103f6b
    for (i = 0; i < 1024; i++)
Packit Service 103f6b
        ret |= check_table(table, i, (void *)(i << 16 | i));
Packit Service 103f6b
    compute_dist(table);
Packit Service 103f6b
    drmHashDestroy(table);
Packit Service 103f6b
Packit Service 103f6b
    printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
Packit Service 103f6b
    table = drmHashCreate();
Packit Service 103f6b
    for (i = 0; i < 1024; i++)
Packit Service 103f6b
        drmHashInsert(table, i*4096, (void *)(i << 16 | i));
Packit Service 103f6b
    for (i = 0; i < 1024; i++)
Packit Service 103f6b
        ret |= check_table(table, i*4096, (void *)(i << 16 | i));
Packit Service 103f6b
    compute_dist(table);
Packit Service 103f6b
    drmHashDestroy(table);
Packit Service 103f6b
Packit Service 103f6b
    printf("\n***** 1024 random integers ****\n");
Packit Service 103f6b
    table = drmHashCreate();
Packit Service 103f6b
    srandom(0xbeefbeef);
Packit Service 103f6b
    for (i = 0; i < 1024; i++)
Packit Service 103f6b
        drmHashInsert(table, random(), (void *)(i << 16 | i));
Packit Service 103f6b
    srandom(0xbeefbeef);
Packit Service 103f6b
    for (i = 0; i < 1024; i++)
Packit Service 103f6b
        ret |= check_table(table, random(), (void *)(i << 16 | i));
Packit Service 103f6b
    srandom(0xbeefbeef);
Packit Service 103f6b
    for (i = 0; i < 1024; i++)
Packit Service 103f6b
        ret |= check_table(table, random(), (void *)(i << 16 | i));
Packit Service 103f6b
    compute_dist(table);
Packit Service 103f6b
    drmHashDestroy(table);
Packit Service 103f6b
Packit Service 103f6b
    printf("\n***** 5000 random integers ****\n");
Packit Service 103f6b
    table = drmHashCreate();
Packit Service 103f6b
    srandom(0xbeefbeef);
Packit Service 103f6b
    for (i = 0; i < 5000; i++)
Packit Service 103f6b
        drmHashInsert(table, random(), (void *)(i << 16 | i));
Packit Service 103f6b
    srandom(0xbeefbeef);
Packit Service 103f6b
    for (i = 0; i < 5000; i++)
Packit Service 103f6b
        ret |= check_table(table, random(), (void *)(i << 16 | i));
Packit Service 103f6b
    srandom(0xbeefbeef);
Packit Service 103f6b
    for (i = 0; i < 5000; i++)
Packit Service 103f6b
        ret |= check_table(table, random(), (void *)(i << 16 | i));
Packit Service 103f6b
    compute_dist(table);
Packit Service 103f6b
    drmHashDestroy(table);
Packit Service 103f6b
Packit Service 103f6b
    return ret;
Packit Service 103f6b
}