Merge branch 'modesetting-gem' of ssh://git.freedesktop.org/git/mesa/drm into modeset...
[profile/ivi/libdrm.git] / libdrm / xf86drmHash.c
index c3e7f0d..82cbc2a 100644 (file)
@@ -25,8 +25,6 @@
  *
  * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
  *
- * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmHash.c,v 1.4 2001/03/21 18:08:54 dawes Exp $
- *
  * DESCRIPTION
  *
  * This file contains a straightforward implementation of a fixed-sized
  *
  */
 
-#ifdef HAVE_XORG_CONFIG_H
-#include <xorg-config.h>
-#endif
+#include <stdio.h>
+#include <stdlib.h>
 
 #define HASH_MAIN 0
 
-#if HASH_MAIN
-# include <stdio.h>
-# include <stdlib.h>
-#else
-# include "drm.h"
+#if !HASH_MAIN
 # include "xf86drm.h"
-# ifdef XFree86LOADER
-#  include "xf86.h"
-#  include "xf86_ansic.h"
-# else
-#  include <stdio.h>
-#  include <stdlib.h>
-# endif
 #endif
 
-#define N(x)  drm##x
-
 #define HASH_MAGIC 0xdeadbeef
 #define HASH_DEBUG 0
 #define HASH_SIZE  512         /* Good for about 100 entries */
@@ -135,11 +119,11 @@ typedef struct HashTable {
 } HashTable, *HashTablePtr;
 
 #if HASH_MAIN
-extern void *N(HashCreate)(void);
-extern int  N(HashDestroy)(void *t);
-extern int  N(HashLookup)(void *t, unsigned long key, unsigned long *value);
-extern int  N(HashInsert)(void *t, unsigned long key, unsigned long value);
-extern int  N(HashDelete)(void *t, unsigned long key);
+extern void *drmHashCreate(void);
+extern int  drmHashDestroy(void *t);
+extern int  drmHashLookup(void *t, unsigned long key, unsigned long *value);
+extern int  drmHashInsert(void *t, unsigned long key, unsigned long value);
+extern int  drmHashDelete(void *t, unsigned long key);
 #endif
 
 static unsigned long HashHash(unsigned long key)
@@ -170,7 +154,7 @@ static unsigned long HashHash(unsigned long key)
     return hash;
 }
 
-void *N(HashCreate)(void)
+void *drmHashCreate(void)
 {
     HashTablePtr table;
     int          i;
@@ -187,7 +171,7 @@ void *N(HashCreate)(void)
     return table;
 }
 
-int N(HashDestroy)(void *t)
+int drmHashDestroy(void *t)
 {
     HashTablePtr  table = (HashTablePtr)t;
     HashBucketPtr bucket;
@@ -238,7 +222,7 @@ static HashBucketPtr HashFind(HashTablePtr table,
     return NULL;
 }
 
-int N(HashLookup)(void *t, unsigned long key, void **value)
+int drmHashLookup(void *t, unsigned long key, void **value)
 {
     HashTablePtr  table = (HashTablePtr)t;
     HashBucketPtr bucket;
@@ -251,7 +235,7 @@ int N(HashLookup)(void *t, unsigned long key, void **value)
     return 0;                  /* Found */
 }
 
-int N(HashInsert)(void *t, unsigned long key, void *value)
+int drmHashInsert(void *t, unsigned long key, void *value)
 {
     HashTablePtr  table = (HashTablePtr)t;
     HashBucketPtr bucket;
@@ -273,7 +257,7 @@ int N(HashInsert)(void *t, unsigned long key, void *value)
     return 0;                  /* Added to table */
 }
 
-int N(HashDelete)(void *t, unsigned long key)
+int drmHashDelete(void *t, unsigned long key)
 {
     HashTablePtr  table = (HashTablePtr)t;
     unsigned long hash;
@@ -290,23 +274,24 @@ int N(HashDelete)(void *t, unsigned long key)
     return 0;
 }
 
-int N(HashNext)(void *t, unsigned long *key, void **value)
+int drmHashNext(void *t, unsigned long *key, void **value)
 {
     HashTablePtr  table = (HashTablePtr)t;
 
-    for (; table->p0 < HASH_SIZE;
-        ++table->p0, table->p1 = table->buckets[table->p0]) {
+    while (table->p0 < HASH_SIZE) {
        if (table->p1) {
            *key       = table->p1->key;
            *value     = table->p1->value;
            table->p1  = table->p1->next;
            return 1;
        }
+       table->p1 = table->buckets[table->p0];
+       ++table->p0;
     }
     return 0;
 }
 
-int N(HashFirst)(void *t, unsigned long *key, void **value)
+int drmHashFirst(void *t, unsigned long *key, void **value)
 {
     HashTablePtr  table = (HashTablePtr)t;
 
@@ -314,7 +299,7 @@ int N(HashFirst)(void *t, unsigned long *key, void **value)
 
     table->p0 = 0;
     table->p1 = table->buckets[0];
-    return N(HashNext)(table, key, value);
+    return drmHashNext(table, key, value);
 }
 
 #if HASH_MAIN
@@ -363,7 +348,7 @@ static void check_table(HashTablePtr table,
                        unsigned long key, unsigned long value)
 {
     unsigned long retval  = 0;
-    int           retcode = N(HashLookup)(table, key, &retval);
+    int           retcode = drmHashLookup(table, key, &retval);
 
     switch (retcode) {
     case -1:
@@ -393,50 +378,50 @@ int main(void)
     int          i;
 
     printf("\n***** 256 consecutive integers ****\n");
-    table = N(HashCreate)();
-    for (i = 0; i < 256; i++) N(HashInsert)(table, i, i);
+    table = drmHashCreate();
+    for (i = 0; i < 256; i++) drmHashInsert(table, i, i);
     for (i = 0; i < 256; i++) check_table(table, i, i);
     for (i = 256; i >= 0; i--) check_table(table, i, i);
     compute_dist(table);
-    N(HashDestroy)(table);
+    drmHashDestroy(table);
 
     printf("\n***** 1024 consecutive integers ****\n");
-    table = N(HashCreate)();
-    for (i = 0; i < 1024; i++) N(HashInsert)(table, i, i);
+    table = drmHashCreate();
+    for (i = 0; i < 1024; i++) drmHashInsert(table, i, i);
     for (i = 0; i < 1024; i++) check_table(table, i, i);
     for (i = 1024; i >= 0; i--) check_table(table, i, i);
     compute_dist(table);
-    N(HashDestroy)(table);
+    drmHashDestroy(table);
 
     printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
-    table = N(HashCreate)();
-    for (i = 0; i < 1024; i++) N(HashInsert)(table, i*4096, i);
+    table = drmHashCreate();
+    for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, i);
     for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
     for (i = 1024; i >= 0; i--) check_table(table, i*4096, i);
     compute_dist(table);
-    N(HashDestroy)(table);
+    drmHashDestroy(table);
 
     printf("\n***** 1024 random integers ****\n");
-    table = N(HashCreate)();
+    table = drmHashCreate();
     srandom(0xbeefbeef);
-    for (i = 0; i < 1024; i++) N(HashInsert)(table, random(), i);
+    for (i = 0; i < 1024; i++) drmHashInsert(table, random(), i);
     srandom(0xbeefbeef);
     for (i = 0; i < 1024; i++) check_table(table, random(), i);
     srandom(0xbeefbeef);
     for (i = 0; i < 1024; i++) check_table(table, random(), i);
     compute_dist(table);
-    N(HashDestroy)(table);
+    drmHashDestroy(table);
 
     printf("\n***** 5000 random integers ****\n");
-    table = N(HashCreate)();
+    table = drmHashCreate();
     srandom(0xbeefbeef);
-    for (i = 0; i < 5000; i++) N(HashInsert)(table, random(), i);
+    for (i = 0; i < 5000; i++) drmHashInsert(table, random(), i);
     srandom(0xbeefbeef);
     for (i = 0; i < 5000; i++) check_table(table, random(), i);
     srandom(0xbeefbeef);
     for (i = 0; i < 5000; i++) check_table(table, random(), i);
     compute_dist(table);
-    N(HashDestroy)(table);
+    drmHashDestroy(table);
 
     return 0;
 }