All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c
@ 2015-03-26 23:57 Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 2/8] tests/hash: misc compilation fixes Emil Velikov
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Emil Velikov @ 2015-03-26 23:57 UTC (permalink / raw)
  To: dri-devel; +Cc: emil.l.velikov

This way with follow up commits we can fix it and wire it up to
make check

v2:
 - Use xf86drmHash.h for common structs.(Jan)
 - Add test to .gitignore.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 .gitignore        |   1 +
 Makefile.sources  |   1 +
 tests/Makefile.am |   3 +-
 tests/hash.c      | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drmHash.c     | 196 +++--------------------------------------------------
 xf86drmHash.h     |  47 +++++++++++++
 6 files changed, 260 insertions(+), 186 deletions(-)
 create mode 100644 tests/hash.c
 create mode 100644 xf86drmHash.h

diff --git a/.gitignore b/.gitignore
index 06cc928..0faa825 100644
--- a/.gitignore
+++ b/.gitignore
@@ -78,6 +78,7 @@ tests/drmstat
 tests/getclient
 tests/getstats
 tests/getversion
+tests/hash
 tests/lock
 tests/openclose
 tests/setversion
diff --git a/Makefile.sources b/Makefile.sources
index 566f7b5..ff4fe5e 100644
--- a/Makefile.sources
+++ b/Makefile.sources
@@ -1,6 +1,7 @@
 LIBDRM_FILES := \
 	xf86drm.c \
 	xf86drmHash.c \
+	xf86drmHash.h \
 	xf86drmRandom.c \
 	xf86drmSL.c \
 	xf86drmMode.c \
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 10f54e3..ea826b5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -29,7 +29,8 @@ LDADD = $(top_builddir)/libdrm.la
 
 check_PROGRAMS = \
 	dristat \
-	drmstat
+	drmstat \
+	hash
 
 if HAVE_NOUVEAU
 SUBDIRS += nouveau
diff --git a/tests/hash.c b/tests/hash.c
new file mode 100644
index 0000000..517a667
--- /dev/null
+++ b/tests/hash.c
@@ -0,0 +1,198 @@
+/* xf86drmHash.c -- Small hash table support for integer -> integer mapping
+ * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
+ *
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ * DESCRIPTION
+ *
+ * This file contains a straightforward implementation of a fixed-sized
+ * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
+ * collision resolution.  There are two potentially interesting things
+ * about this implementation:
+ *
+ * 1) The table is power-of-two sized.  Prime sized tables are more
+ * traditional, but do not have a significant advantage over power-of-two
+ * sized table, especially when double hashing is not used for collision
+ * resolution.
+ *
+ * 2) The hash computation uses a table of random integers [Hanson97,
+ * pp. 39-41].
+ *
+ * FUTURE ENHANCEMENTS
+ *
+ * With a table size of 512, the current implementation is sufficient for a
+ * few hundred keys.  Since this is well above the expected size of the
+ * tables for which this implementation was designed, the implementation of
+ * dynamic hash tables was postponed until the need arises.  A common (and
+ * naive) approach to dynamic hash table implementation simply creates a
+ * new hash table when necessary, rehashes all the data into the new table,
+ * and destroys the old table.  The approach in [Larson88] is superior in
+ * two ways: 1) only a portion of the table is expanded when needed,
+ * distributing the expansion cost over several insertions, and 2) portions
+ * of the table can be locked, enabling a scalable thread-safe
+ * implementation.
+ *
+ * REFERENCES
+ *
+ * [Hanson97] David R. Hanson.  C Interfaces and Implementations:
+ * Techniques for Creating Reusable Software.  Reading, Massachusetts:
+ * Addison-Wesley, 1997.
+ *
+ * [Knuth73] Donald E. Knuth. The Art of Computer Programming.  Volume 3:
+ * Sorting and Searching.  Reading, Massachusetts: Addison-Wesley, 1973.
+ *
+ * [Larson88] Per-Ake Larson. "Dynamic Hash Tables".  CACM 31(4), April
+ * 1988, pp. 446-457.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "xf86drm.h"
+#include "xf86drmHash.h"
+
+#define DIST_LIMIT 10
+static int dist[DIST_LIMIT];
+
+static void clear_dist(void) {
+    int i;
+
+    for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
+}
+
+static int count_entries(HashBucketPtr bucket)
+{
+    int count = 0;
+
+    for (; bucket; bucket = bucket->next) ++count;
+    return count;
+}
+
+static void update_dist(int count)
+{
+    if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
+    else                     ++dist[count];
+}
+
+static void compute_dist(HashTablePtr table)
+{
+    int           i;
+    HashBucketPtr bucket;
+
+    printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
+	   table->entries, table->hits, table->partials, table->misses);
+    clear_dist();
+    for (i = 0; i < HASH_SIZE; i++) {
+	bucket = table->buckets[i];
+	update_dist(count_entries(bucket));
+    }
+    for (i = 0; i < DIST_LIMIT; i++) {
+	if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
+	else                   printf("other %10d\n", dist[i]);
+    }
+}
+
+static void check_table(HashTablePtr table,
+			unsigned long key, unsigned long value)
+{
+    unsigned long retval  = 0;
+    int           retcode = drmHashLookup(table, key, &retval);
+
+    switch (retcode) {
+    case -1:
+	printf("Bad magic = 0x%08lx:"
+	       " key = %lu, expected = %lu, returned = %lu\n",
+	       table->magic, key, value, retval);
+	break;
+    case 1:
+	printf("Not found: key = %lu, expected = %lu returned = %lu\n",
+	       key, value, retval);
+	break;
+    case 0:
+	if (value != retval)
+	    printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
+		   key, value, retval);
+	break;
+    default:
+	printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
+	       retcode, key, value, retval);
+	break;
+    }
+}
+
+int main(void)
+{
+    HashTablePtr table;
+    int          i;
+
+    printf("\n***** 256 consecutive integers ****\n");
+    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);
+    drmHashDestroy(table);
+
+    printf("\n***** 1024 consecutive integers ****\n");
+    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);
+    drmHashDestroy(table);
+
+    printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
+    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);
+    drmHashDestroy(table);
+
+    printf("\n***** 1024 random integers ****\n");
+    table = drmHashCreate();
+    srandom(0xbeefbeef);
+    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);
+    drmHashDestroy(table);
+
+    printf("\n***** 5000 random integers ****\n");
+    table = drmHashCreate();
+    srandom(0xbeefbeef);
+    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);
+    drmHashDestroy(table);
+
+    return 0;
+}
diff --git a/xf86drmHash.c b/xf86drmHash.c
index 82cbc2a..f7d7f73 100644
--- a/xf86drmHash.c
+++ b/xf86drmHash.c
@@ -71,60 +71,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#define HASH_MAIN 0
-
-#if !HASH_MAIN
-# include "xf86drm.h"
-#endif
+#include "xf86drm.h"
+#include "xf86drmHash.h"
 
 #define HASH_MAGIC 0xdeadbeef
 #define HASH_DEBUG 0
-#define HASH_SIZE  512		/* Good for about 100 entries */
-				/* If you change this value, you probably
-                                   have to change the HashHash hashing
-                                   function! */
-
-#if HASH_MAIN
-#define HASH_ALLOC malloc
-#define HASH_FREE  free
-#define HASH_RANDOM_DECL
-#define HASH_RANDOM_INIT(seed)  srandom(seed)
-#define HASH_RANDOM             random()
-#define HASH_RANDOM_DESTROY
-#else
-#define HASH_ALLOC drmMalloc
-#define HASH_FREE  drmFree
-#define HASH_RANDOM_DECL        void *state
-#define HASH_RANDOM_INIT(seed)  state = drmRandomCreate(seed)
-#define HASH_RANDOM             drmRandom(state)
-#define HASH_RANDOM_DESTROY     drmRandomDestroy(state)
-
-#endif
-
-typedef struct HashBucket {
-    unsigned long     key;
-    void              *value;
-    struct HashBucket *next;
-} HashBucket, *HashBucketPtr;
-
-typedef struct HashTable {
-    unsigned long    magic;
-    unsigned long    entries;
-    unsigned long    hits;	/* At top of linked list */
-    unsigned long    partials;	/* Not at top of linked list */
-    unsigned long    misses;	/* Not in table */
-    HashBucketPtr    buckets[HASH_SIZE];
-    int              p0;
-    HashBucketPtr    p1;
-} HashTable, *HashTablePtr;
-
-#if HASH_MAIN
-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)
 {
@@ -135,10 +86,10 @@ static unsigned long HashHash(unsigned long key)
     int                  i;
 
     if (!init) {
-	HASH_RANDOM_DECL;
-	HASH_RANDOM_INIT(37);
-	for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
-	HASH_RANDOM_DESTROY;
+	void *state;
+	state = drmRandomCreate(37);
+	for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
+	drmRandomDestroy(state);
 	++init;
     }
 
@@ -159,7 +110,7 @@ void *drmHashCreate(void)
     HashTablePtr table;
     int          i;
 
-    table           = HASH_ALLOC(sizeof(*table));
+    table           = drmMalloc(sizeof(*table));
     if (!table) return NULL;
     table->magic    = HASH_MAGIC;
     table->entries  = 0;
@@ -183,11 +134,11 @@ int drmHashDestroy(void *t)
     for (i = 0; i < HASH_SIZE; i++) {
 	for (bucket = table->buckets[i]; bucket;) {
 	    next = bucket->next;
-	    HASH_FREE(bucket);
+	    drmFree(bucket);
 	    bucket = next;
 	}
     }
-    HASH_FREE(table);
+    drmFree(table);
     return 0;
 }
 
@@ -245,7 +196,7 @@ int drmHashInsert(void *t, unsigned long key, void *value)
 
     if (HashFind(table, key, &hash)) return 1; /* Already in table */
 
-    bucket               = HASH_ALLOC(sizeof(*bucket));
+    bucket               = drmMalloc(sizeof(*bucket));
     if (!bucket) return -1;	/* Error */
     bucket->key          = key;
     bucket->value        = value;
@@ -270,7 +221,7 @@ int drmHashDelete(void *t, unsigned long key)
     if (!bucket) return 1;	/* Not found */
 
     table->buckets[hash] = bucket->next;
-    HASH_FREE(bucket);
+    drmFree(bucket);
     return 0;
 }
 
@@ -301,128 +252,3 @@ int drmHashFirst(void *t, unsigned long *key, void **value)
     table->p1 = table->buckets[0];
     return drmHashNext(table, key, value);
 }
-
-#if HASH_MAIN
-#define DIST_LIMIT 10
-static int dist[DIST_LIMIT];
-
-static void clear_dist(void) {
-    int i;
-
-    for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
-}
-
-static int count_entries(HashBucketPtr bucket)
-{
-    int count = 0;
-
-    for (; bucket; bucket = bucket->next) ++count;
-    return count;
-}
-
-static void update_dist(int count)
-{
-    if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
-    else                     ++dist[count];
-}
-
-static void compute_dist(HashTablePtr table)
-{
-    int           i;
-    HashBucketPtr bucket;
-
-    printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
-	   table->entries, table->hits, table->partials, table->misses);
-    clear_dist();
-    for (i = 0; i < HASH_SIZE; i++) {
-	bucket = table->buckets[i];
-	update_dist(count_entries(bucket));
-    }
-    for (i = 0; i < DIST_LIMIT; i++) {
-	if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
-	else                   printf("other %10d\n", dist[i]);
-    }
-}
-
-static void check_table(HashTablePtr table,
-			unsigned long key, unsigned long value)
-{
-    unsigned long retval  = 0;
-    int           retcode = drmHashLookup(table, key, &retval);
-
-    switch (retcode) {
-    case -1:
-	printf("Bad magic = 0x%08lx:"
-	       " key = %lu, expected = %lu, returned = %lu\n",
-	       table->magic, key, value, retval);
-	break;
-    case 1:
-	printf("Not found: key = %lu, expected = %lu returned = %lu\n",
-	       key, value, retval);
-	break;
-    case 0:
-	if (value != retval)
-	    printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
-		   key, value, retval);
-	break;
-    default:
-	printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
-	       retcode, key, value, retval);
-	break;
-    }
-}
-
-int main(void)
-{
-    HashTablePtr table;
-    int          i;
-
-    printf("\n***** 256 consecutive integers ****\n");
-    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);
-    drmHashDestroy(table);
-
-    printf("\n***** 1024 consecutive integers ****\n");
-    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);
-    drmHashDestroy(table);
-
-    printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
-    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);
-    drmHashDestroy(table);
-
-    printf("\n***** 1024 random integers ****\n");
-    table = drmHashCreate();
-    srandom(0xbeefbeef);
-    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);
-    drmHashDestroy(table);
-
-    printf("\n***** 5000 random integers ****\n");
-    table = drmHashCreate();
-    srandom(0xbeefbeef);
-    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);
-    drmHashDestroy(table);
-
-    return 0;
-}
-#endif
diff --git a/xf86drmHash.h b/xf86drmHash.h
new file mode 100644
index 0000000..38fd84b
--- /dev/null
+++ b/xf86drmHash.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ */
+
+#define HASH_SIZE  512		/* Good for about 100 entries */
+				/* If you change this value, you probably
+                                   have to change the HashHash hashing
+                                   function! */
+
+typedef struct HashBucket {
+    unsigned long     key;
+    void              *value;
+    struct HashBucket *next;
+} HashBucket, *HashBucketPtr;
+
+typedef struct HashTable {
+    unsigned long    magic;
+    unsigned long    entries;
+    unsigned long    hits;	/* At top of linked list */
+    unsigned long    partials;	/* Not at top of linked list */
+    unsigned long    misses;	/* Not in table */
+    HashBucketPtr    buckets[HASH_SIZE];
+    int              p0;
+    HashBucketPtr    p1;
+} HashTable, *HashTablePtr;
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH libdrm v2 2/8] tests/hash: misc compilation fixes
  2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
@ 2015-03-26 23:57 ` Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 3/8] tests/hash: style fixes Emil Velikov
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Emil Velikov @ 2015-03-26 23:57 UTC (permalink / raw)
  To: dri-devel; +Cc: emil.l.velikov

Get the test from completely broken to working like a charm.

 - Use the same variable type for both HashInsert and HashLookup.
 - Use correct storage type for the HashLookup return value.
 - Remove useless backward iteration of HashLookup(i).

v2:
 - Use void * instead of unsigned long.
 - Change value to key << 16 | key.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 tests/hash.c | 45 +++++++++++++++++++++------------------------
 1 file changed, 21 insertions(+), 24 deletions(-)

diff --git a/tests/hash.c b/tests/hash.c
index 517a667..ee11e23 100644
--- a/tests/hash.c
+++ b/tests/hash.c
@@ -116,28 +116,28 @@ static void compute_dist(HashTablePtr table)
 }
 
 static void check_table(HashTablePtr table,
-			unsigned long key, unsigned long value)
+			unsigned long key, void * value)
 {
-    unsigned long retval  = 0;
-    int           retcode = drmHashLookup(table, key, &retval);
+    void *retval;
+    int   retcode = drmHashLookup(table, key, &retval);
 
     switch (retcode) {
     case -1:
 	printf("Bad magic = 0x%08lx:"
-	       " key = %lu, expected = %lu, returned = %lu\n",
+	       " key = %lu, expected = %p, returned = %p\n",
 	       table->magic, key, value, retval);
 	break;
     case 1:
-	printf("Not found: key = %lu, expected = %lu returned = %lu\n",
+	printf("Not found: key = %lu, expected = %p, returned = %p\n",
 	       key, value, retval);
 	break;
     case 0:
 	if (value != retval)
-	    printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
+	    printf("Bad value: key = %lu, expected = %p, returned = %p\n",
 		   key, value, retval);
 	break;
     default:
-	printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
+	printf("Bad retcode = %d: key = %lu, expected = %p, returned = %p\n",
 	       retcode, key, value, retval);
 	break;
     }
@@ -145,52 +145,49 @@ static void check_table(HashTablePtr table,
 
 int main(void)
 {
-    HashTablePtr table;
-    int          i;
+    HashTablePtr  table;
+    unsigned long i;
 
     printf("\n***** 256 consecutive integers ****\n");
     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);
+    for (i = 0; i < 256; i++) drmHashInsert(table, i, (void *)(i << 16 | i));
+    for (i = 0; i < 256; i++) check_table(table, i, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
     printf("\n***** 1024 consecutive integers ****\n");
     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);
+    for (i = 0; i < 1024; i++) drmHashInsert(table, i, (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++) check_table(table, i, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
     printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
     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);
+    for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++) check_table(table, i*4096, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
     printf("\n***** 1024 random integers ****\n");
     table = drmHashCreate();
     srandom(0xbeefbeef);
-    for (i = 0; i < 1024; i++) drmHashInsert(table, random(), i);
+    for (i = 0; i < 1024; i++) drmHashInsert(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
-    for (i = 0; i < 1024; i++) check_table(table, random(), i);
+    for (i = 0; i < 1024; i++) check_table(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
-    for (i = 0; i < 1024; i++) check_table(table, random(), i);
+    for (i = 0; i < 1024; i++) check_table(table, random(), (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
     printf("\n***** 5000 random integers ****\n");
     table = drmHashCreate();
     srandom(0xbeefbeef);
-    for (i = 0; i < 5000; i++) drmHashInsert(table, random(), i);
+    for (i = 0; i < 5000; i++) drmHashInsert(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
-    for (i = 0; i < 5000; i++) check_table(table, random(), i);
+    for (i = 0; i < 5000; i++) check_table(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
-    for (i = 0; i < 5000; i++) check_table(table, random(), i);
+    for (i = 0; i < 5000; i++) check_table(table, random(), (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH libdrm v2 3/8] tests/hash: style fixes
  2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 2/8] tests/hash: misc compilation fixes Emil Velikov
@ 2015-03-26 23:57 ` Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 4/8] tests/hash: return non-zero on failure Emil Velikov
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Emil Velikov @ 2015-03-26 23:57 UTC (permalink / raw)
  To: dri-devel; +Cc: emil.l.velikov

v2: Rebase on earlier changes. Keep count initialisation as is.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Jan Vesely <jan.vesely@rutgers.edu>
---
 tests/hash.c | 90 ++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 54 insertions(+), 36 deletions(-)

diff --git a/tests/hash.c b/tests/hash.c
index ee11e23..1543c86 100644
--- a/tests/hash.c
+++ b/tests/hash.c
@@ -80,21 +80,25 @@ static int dist[DIST_LIMIT];
 static void clear_dist(void) {
     int i;
 
-    for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
+    for (i = 0; i < DIST_LIMIT; i++)
+        dist[i] = 0;
 }
 
 static int count_entries(HashBucketPtr bucket)
 {
     int count = 0;
 
-    for (; bucket; bucket = bucket->next) ++count;
+    for (; bucket; bucket = bucket->next)
+        ++count;
     return count;
 }
 
 static void update_dist(int count)
 {
-    if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
-    else                     ++dist[count];
+    if (count >= DIST_LIMIT)
+        ++dist[DIST_LIMIT-1];
+    else
+        ++dist[count];
 }
 
 static void compute_dist(HashTablePtr table)
@@ -103,43 +107,45 @@ static void compute_dist(HashTablePtr table)
     HashBucketPtr bucket;
 
     printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
-	   table->entries, table->hits, table->partials, table->misses);
+          table->entries, table->hits, table->partials, table->misses);
     clear_dist();
     for (i = 0; i < HASH_SIZE; i++) {
-	bucket = table->buckets[i];
-	update_dist(count_entries(bucket));
+        bucket = table->buckets[i];
+        update_dist(count_entries(bucket));
     }
     for (i = 0; i < DIST_LIMIT; i++) {
-	if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
-	else                   printf("other %10d\n", dist[i]);
+        if (i != DIST_LIMIT-1)
+            printf("%5d %10d\n", i, dist[i]);
+        else
+            printf("other %10d\n", dist[i]);
     }
 }
 
 static void check_table(HashTablePtr table,
-			unsigned long key, void * value)
+                        unsigned long key, void * value)
 {
     void *retval;
     int   retcode = drmHashLookup(table, key, &retval);
 
     switch (retcode) {
     case -1:
-	printf("Bad magic = 0x%08lx:"
-	       " key = %lu, expected = %p, returned = %p\n",
-	       table->magic, key, value, retval);
-	break;
+        printf("Bad magic = 0x%08lx:"
+               " key = %lu, expected = %p, returned = %p\n",
+               table->magic, key, value, retval);
+        break;
     case 1:
-	printf("Not found: key = %lu, expected = %p, returned = %p\n",
-	       key, value, retval);
-	break;
+        printf("Not found: key = %lu, expected = %p, returned = %p\n",
+               key, value, retval);
+        break;
     case 0:
-	if (value != retval)
-	    printf("Bad value: key = %lu, expected = %p, returned = %p\n",
-		   key, value, retval);
-	break;
+        if (value != retval)
+            printf("Bad value: key = %lu, expected = %p, returned = %p\n",
+                   key, value, retval);
+        break;
     default:
-	printf("Bad retcode = %d: key = %lu, expected = %p, returned = %p\n",
-	       retcode, key, value, retval);
-	break;
+        printf("Bad retcode = %d: key = %lu, expected = %p, returned = %p\n",
+               retcode, key, value, retval);
+        break;
     }
 }
 
@@ -150,44 +156,56 @@ int main(void)
 
     printf("\n***** 256 consecutive integers ****\n");
     table = drmHashCreate();
-    for (i = 0; i < 256; i++) drmHashInsert(table, i, (void *)(i << 16 | i));
-    for (i = 0; i < 256; i++) check_table(table, i, (void *)(i << 16 | i));
+    for (i = 0; i < 256; i++)
+        drmHashInsert(table, i, (void *)(i << 16 | i));
+    for (i = 0; i < 256; i++)
+        check_table(table, i, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
     printf("\n***** 1024 consecutive integers ****\n");
     table = drmHashCreate();
-    for (i = 0; i < 1024; i++) drmHashInsert(table, i, (void *)(i << 16 | i));
-    for (i = 0; i < 1024; i++) check_table(table, i, (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++)
+        drmHashInsert(table, i, (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++)
+        check_table(table, i, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
     printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
     table = drmHashCreate();
-    for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, (void *)(i << 16 | i));
-    for (i = 0; i < 1024; i++) check_table(table, i*4096, (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++)
+        drmHashInsert(table, i*4096, (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++)
+        check_table(table, i*4096, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
     printf("\n***** 1024 random integers ****\n");
     table = drmHashCreate();
     srandom(0xbeefbeef);
-    for (i = 0; i < 1024; i++) drmHashInsert(table, random(), (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++)
+        drmHashInsert(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
-    for (i = 0; i < 1024; i++) check_table(table, random(), (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++)
+        check_table(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
-    for (i = 0; i < 1024; i++) check_table(table, random(), (void *)(i << 16 | i));
+    for (i = 0; i < 1024; i++)
+        check_table(table, random(), (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
     printf("\n***** 5000 random integers ****\n");
     table = drmHashCreate();
     srandom(0xbeefbeef);
-    for (i = 0; i < 5000; i++) drmHashInsert(table, random(), (void *)(i << 16 | i));
+    for (i = 0; i < 5000; i++)
+        drmHashInsert(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
-    for (i = 0; i < 5000; i++) check_table(table, random(), (void *)(i << 16 | i));
+    for (i = 0; i < 5000; i++)
+        check_table(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
-    for (i = 0; i < 5000; i++) check_table(table, random(), (void *)(i << 16 | i));
+    for (i = 0; i < 5000; i++)
+        check_table(table, random(), (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH libdrm v2 4/8] tests/hash: return non-zero on failure
  2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 2/8] tests/hash: misc compilation fixes Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 3/8] tests/hash: style fixes Emil Velikov
@ 2015-03-26 23:57 ` Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 5/8] tests/random: extract test out of xf86drmRandom.c Emil Velikov
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Emil Velikov @ 2015-03-26 23:57 UTC (permalink / raw)
  To: dri-devel; +Cc: emil.l.velikov

... and wire up to `make check' now that it's useful.

v2: Really return non-zero on failure.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 tests/Makefile.am | 10 ++++++----
 tests/hash.c      | 26 +++++++++++++++-----------
 2 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index ea826b5..79a13c4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -29,13 +29,15 @@ LDADD = $(top_builddir)/libdrm.la
 
 check_PROGRAMS = \
 	dristat \
-	drmstat \
-	hash
+	drmstat
 
 if HAVE_NOUVEAU
 SUBDIRS += nouveau
 endif
 
+TESTS = \
+	hash
+
 if HAVE_LIBUDEV
 
 check_LTLIBRARIES = libdrmtest.la
@@ -62,6 +64,6 @@ TESTS =						\
 	updatedraw				\
 	name_from_fd
 
-check_PROGRAMS += $(TESTS)
-
 endif
+
+check_PROGRAMS += $(TESTS)
diff --git a/tests/hash.c b/tests/hash.c
index 1543c86..fc093c1 100644
--- a/tests/hash.c
+++ b/tests/hash.c
@@ -121,8 +121,8 @@ static void compute_dist(HashTablePtr table)
     }
 }
 
-static void check_table(HashTablePtr table,
-                        unsigned long key, void * value)
+static int check_table(HashTablePtr table,
+                       unsigned long key, void * value)
 {
     void *retval;
     int   retcode = drmHashLookup(table, key, &retval);
@@ -138,28 +138,32 @@ static void check_table(HashTablePtr table,
                key, value, retval);
         break;
     case 0:
-        if (value != retval)
+        if (value != retval) {
             printf("Bad value: key = %lu, expected = %p, returned = %p\n",
                    key, value, retval);
+            retcode = -1;
+        }
         break;
     default:
         printf("Bad retcode = %d: key = %lu, expected = %p, returned = %p\n",
                retcode, key, value, retval);
         break;
     }
+    return retcode;
 }
 
 int main(void)
 {
     HashTablePtr  table;
     unsigned long i;
+    int           ret;
 
     printf("\n***** 256 consecutive integers ****\n");
     table = drmHashCreate();
     for (i = 0; i < 256; i++)
         drmHashInsert(table, i, (void *)(i << 16 | i));
     for (i = 0; i < 256; i++)
-        check_table(table, i, (void *)(i << 16 | i));
+        ret |= check_table(table, i, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
@@ -168,7 +172,7 @@ int main(void)
     for (i = 0; i < 1024; i++)
         drmHashInsert(table, i, (void *)(i << 16 | i));
     for (i = 0; i < 1024; i++)
-        check_table(table, i, (void *)(i << 16 | i));
+        ret |= check_table(table, i, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
@@ -177,7 +181,7 @@ int main(void)
     for (i = 0; i < 1024; i++)
         drmHashInsert(table, i*4096, (void *)(i << 16 | i));
     for (i = 0; i < 1024; i++)
-        check_table(table, i*4096, (void *)(i << 16 | i));
+        ret |= check_table(table, i*4096, (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
@@ -188,10 +192,10 @@ int main(void)
         drmHashInsert(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
     for (i = 0; i < 1024; i++)
-        check_table(table, random(), (void *)(i << 16 | i));
+        ret |= check_table(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
     for (i = 0; i < 1024; i++)
-        check_table(table, random(), (void *)(i << 16 | i));
+        ret |= check_table(table, random(), (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
@@ -202,12 +206,12 @@ int main(void)
         drmHashInsert(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
     for (i = 0; i < 5000; i++)
-        check_table(table, random(), (void *)(i << 16 | i));
+        ret |= check_table(table, random(), (void *)(i << 16 | i));
     srandom(0xbeefbeef);
     for (i = 0; i < 5000; i++)
-        check_table(table, random(), (void *)(i << 16 | i));
+        ret |= check_table(table, random(), (void *)(i << 16 | i));
     compute_dist(table);
     drmHashDestroy(table);
 
-    return 0;
+    return ret;
 }
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH libdrm v2 5/8] tests/random: extract test out of xf86drmRandom.c
  2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
                   ` (2 preceding siblings ...)
  2015-03-26 23:57 ` [PATCH libdrm v2 4/8] tests/hash: return non-zero on failure Emil Velikov
@ 2015-03-26 23:57 ` Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 6/8] tests/random: return non-zero on test failure Emil Velikov
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Emil Velikov @ 2015-03-26 23:57 UTC (permalink / raw)
  To: dri-devel; +Cc: emil.l.velikov

With follow up commits we can clear it up and wire to
make check

v2:
 - Use xf86drmRandom.h for common struct.(Jan)
 - Add test to .gitignore.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 .gitignore        |   1 +
 Makefile.sources  |   1 +
 tests/Makefile.am |   3 +-
 tests/random.c    | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drmRandom.c   |  78 ++----------------------------------
 xf86drmRandom.h   |  35 ++++++++++++++++
 6 files changed, 161 insertions(+), 75 deletions(-)
 create mode 100644 tests/random.c
 create mode 100644 xf86drmRandom.h

diff --git a/.gitignore b/.gitignore
index 0faa825..cc31f8f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -81,6 +81,7 @@ tests/getversion
 tests/hash
 tests/lock
 tests/openclose
+tests/random
 tests/setversion
 tests/updatedraw
 tests/modeprint/modeprint
diff --git a/Makefile.sources b/Makefile.sources
index ff4fe5e..f215747 100644
--- a/Makefile.sources
+++ b/Makefile.sources
@@ -3,6 +3,7 @@ LIBDRM_FILES := \
 	xf86drmHash.c \
 	xf86drmHash.h \
 	xf86drmRandom.c \
+	xf86drmRandom.h \
 	xf86drmSL.c \
 	xf86drmMode.c \
 	xf86atomic.h \
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 79a13c4..e3443df 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -29,7 +29,8 @@ LDADD = $(top_builddir)/libdrm.la
 
 check_PROGRAMS = \
 	dristat \
-	drmstat
+	drmstat \
+	random
 
 if HAVE_NOUVEAU
 SUBDIRS += nouveau
diff --git a/tests/random.c b/tests/random.c
new file mode 100644
index 0000000..db341f9
--- /dev/null
+++ b/tests/random.c
@@ -0,0 +1,118 @@
+/* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
+ * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com
+ *
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ * 
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ * DESCRIPTION
+ *
+ * This file contains a simple, straightforward implementation of the Park
+ * & Miller "Minimal Standard" PRNG [PM88, PMS93], which is a Lehmer
+ * multiplicative linear congruential generator (MLCG) with a period of
+ * 2^31-1.
+ *
+ * This implementation is intended to provide a reliable, portable PRNG
+ * that is suitable for testing a hash table implementation and for
+ * implementing skip lists.
+ *
+ * FUTURE ENHANCEMENTS
+ *
+ * If initial seeds are not selected randomly, two instances of the PRNG
+ * can be correlated.  [Knuth81, pp. 32-33] describes a shuffling technique
+ * that can eliminate this problem.
+ *
+ * If PRNGs are used for simulation, the period of the current
+ * implementation may be too short.  [LE88] discusses methods of combining
+ * MLCGs to produce much longer periods, and suggests some alternative
+ * values for A and M.  [LE90 and Sch92] also provide information on
+ * long-period PRNGs.
+ *
+ * REFERENCES
+ *
+ * [Knuth81] Donald E. Knuth. The Art of Computer Programming.  Volume 2:
+ * Seminumerical Algorithms.  Reading, Massachusetts: Addison-Wesley, 1981.
+ *
+ * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Number
+ * Generators".  CACM 31(6), June 1988, pp. 742-774.
+ *
+ * [LE90] Pierre L'Ecuyer. "Random Numbers for Simulation". CACM 33(10,
+ * October 1990, pp. 85-97.
+ *
+ * [PM88] Stephen K. Park and Keith W. Miller. "Random Number Generators:
+ * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201.
+ *
+ * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit
+ * CPUs".  Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40.
+ *
+ * [PMS93] Stephen K. Park, Keith W. Miller, and Paul K. Stockmeyer.  In
+ * "Technical Correspondence: Remarks on Choosing and Implementing Random
+ * Number Generators". CACM 36(7), July 1993, pp. 105-110.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "xf86drm.h"
+#include "xf86drmRandom.h"
+
+static void check_period(unsigned long seed)
+{
+    unsigned long count = 0;
+    unsigned long initial;
+    void          *state;
+    
+    state = drmRandomCreate(seed);
+    initial = drmRandom(state);
+    ++count;
+    while (initial != drmRandom(state)) {
+	if (!++count) break;
+    }
+    printf("With seed of %10lu, period = %10lu (0x%08lx)\n",
+	   seed, count, count);
+    drmRandomDestroy(state);
+}
+
+int main(void)
+{
+    RandomState   *state;
+    int           i;
+    unsigned long rand;
+
+    state = drmRandomCreate(1);
+    for (i = 0; i < 10000; i++) {
+	rand = drmRandom(state);
+    }
+    printf("After 10000 iterations: %lu (%lu expected): %s\n",
+	   rand, state->check,
+	   rand - state->check ? "*INCORRECT*" : "CORRECT");
+    drmRandomDestroy(state);
+
+    printf("Checking periods...\n");
+    check_period(1);
+    check_period(2);
+    check_period(31415926);
+    
+    return 0;
+}
diff --git a/xf86drmRandom.c b/xf86drmRandom.c
index 94922ad..b3a4be9 100644
--- a/xf86drmRandom.c
+++ b/xf86drmRandom.c
@@ -74,45 +74,17 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#define RANDOM_MAIN 0
-
-#if !RANDOM_MAIN
-# include "xf86drm.h"
-#endif
+#include "xf86drm.h"
+#include "xf86drmRandom.h"
 
 #define RANDOM_MAGIC 0xfeedbeef
 #define RANDOM_DEBUG 0
 
-#if RANDOM_MAIN
-#define RANDOM_ALLOC malloc
-#define RANDOM_FREE  free
-#else
-#define RANDOM_ALLOC drmMalloc
-#define RANDOM_FREE  drmFree
-#endif
-
-typedef struct RandomState {
-    unsigned long magic;
-    unsigned long a;
-    unsigned long m;
-    unsigned long q;		/* m div a */
-    unsigned long r;		/* m mod a */
-    unsigned long check;
-    unsigned long seed;
-} RandomState;
-
-#if RANDOM_MAIN
-extern void          *drmRandomCreate(unsigned long seed);
-extern int           drmRandomDestroy(void *state);
-extern unsigned long drmRandom(void *state);
-extern double        drmRandomDouble(void *state);
-#endif
-
 void *drmRandomCreate(unsigned long seed)
 {
     RandomState  *state;
 
-    state           = RANDOM_ALLOC(sizeof(*state));
+    state           = drmMalloc(sizeof(*state));
     if (!state) return NULL;
     state->magic    = RANDOM_MAGIC;
 #if 0
@@ -140,7 +112,7 @@ void *drmRandomCreate(unsigned long seed)
 
 int drmRandomDestroy(void *state)
 {
-    RANDOM_FREE(state);
+    drmFree(state);
     return 0;
 }
 
@@ -164,45 +136,3 @@ double drmRandomDouble(void *state)
     
     return (double)drmRandom(state)/(double)s->m;
 }
-
-#if RANDOM_MAIN
-static void check_period(unsigned long seed)
-{
-    unsigned long count = 0;
-    unsigned long initial;
-    void          *state;
-    
-    state = drmRandomCreate(seed);
-    initial = drmRandom(state);
-    ++count;
-    while (initial != drmRandom(state)) {
-	if (!++count) break;
-    }
-    printf("With seed of %10lu, period = %10lu (0x%08lx)\n",
-	   seed, count, count);
-    drmRandomDestroy(state);
-}
-
-int main(void)
-{
-    RandomState   *state;
-    int           i;
-    unsigned long rand;
-
-    state = drmRandomCreate(1);
-    for (i = 0; i < 10000; i++) {
-	rand = drmRandom(state);
-    }
-    printf("After 10000 iterations: %lu (%lu expected): %s\n",
-	   rand, state->check,
-	   rand - state->check ? "*INCORRECT*" : "CORRECT");
-    drmRandomDestroy(state);
-
-    printf("Checking periods...\n");
-    check_period(1);
-    check_period(2);
-    check_period(31415926);
-    
-    return 0;
-}
-#endif
diff --git a/xf86drmRandom.h b/xf86drmRandom.h
new file mode 100644
index 0000000..43b730c
--- /dev/null
+++ b/xf86drmRandom.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ */
+
+typedef struct RandomState {
+    unsigned long magic;
+    unsigned long a;
+    unsigned long m;
+    unsigned long q;		/* m div a */
+    unsigned long r;		/* m mod a */
+    unsigned long check;
+    unsigned long seed;
+} RandomState;
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH libdrm v2 6/8] tests/random: return non-zero on test failure
  2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
                   ` (3 preceding siblings ...)
  2015-03-26 23:57 ` [PATCH libdrm v2 5/8] tests/random: extract test out of xf86drmRandom.c Emil Velikov
@ 2015-03-26 23:57 ` Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 7/8] drm: replace HASH_DEBUG with DEBUG Emil Velikov
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Emil Velikov @ 2015-03-26 23:57 UTC (permalink / raw)
  To: dri-devel; +Cc: emil.l.velikov

... and wire it up to make check

v2: s/rand - state->check/rand != state->check/. (Jan)

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 tests/Makefile.am | 6 +++---
 tests/random.c    | 6 ++++--
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index e3443df..d129317 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -29,15 +29,15 @@ LDADD = $(top_builddir)/libdrm.la
 
 check_PROGRAMS = \
 	dristat \
-	drmstat \
-	random
+	drmstat
 
 if HAVE_NOUVEAU
 SUBDIRS += nouveau
 endif
 
 TESTS = \
-	hash
+	hash \
+	random
 
 if HAVE_LIBUDEV
 
diff --git a/tests/random.c b/tests/random.c
index db341f9..13d4c80 100644
--- a/tests/random.c
+++ b/tests/random.c
@@ -98,15 +98,17 @@ int main(void)
 {
     RandomState   *state;
     int           i;
+    int           ret;
     unsigned long rand;
 
     state = drmRandomCreate(1);
     for (i = 0; i < 10000; i++) {
 	rand = drmRandom(state);
     }
+    ret = rand != state->check;
     printf("After 10000 iterations: %lu (%lu expected): %s\n",
 	   rand, state->check,
-	   rand - state->check ? "*INCORRECT*" : "CORRECT");
+	   ret ? "*INCORRECT*" : "CORRECT");
     drmRandomDestroy(state);
 
     printf("Checking periods...\n");
@@ -114,5 +116,5 @@ int main(void)
     check_period(2);
     check_period(31415926);
     
-    return 0;
+    return ret;
 }
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH libdrm v2 7/8] drm: replace HASH_DEBUG with DEBUG
  2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
                   ` (4 preceding siblings ...)
  2015-03-26 23:57 ` [PATCH libdrm v2 6/8] tests/random: return non-zero on test failure Emil Velikov
@ 2015-03-26 23:57 ` Emil Velikov
  2015-03-26 23:57 ` [PATCH libdrm v2 8/8] drm: use correct printf modifiers Emil Velikov
  2015-04-01 15:16 ` [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Jan Vesely
  7 siblings, 0 replies; 9+ messages in thread
From: Emil Velikov @ 2015-03-26 23:57 UTC (permalink / raw)
  To: dri-devel; +Cc: emil.l.velikov

... and remove the useless SL_DEBUG and RANDOM_DEBUG

v2: Rebase on earlier changes.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 xf86drmHash.c   | 5 ++---
 xf86drmRandom.c | 1 -
 xf86drmSL.c     | 1 -
 3 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/xf86drmHash.c b/xf86drmHash.c
index f7d7f73..199a2a3 100644
--- a/xf86drmHash.c
+++ b/xf86drmHash.c
@@ -75,7 +75,6 @@
 #include "xf86drmHash.h"
 
 #define HASH_MAGIC 0xdeadbeef
-#define HASH_DEBUG 0
 
 static unsigned long HashHash(unsigned long key)
 {
@@ -99,7 +98,7 @@ static unsigned long HashHash(unsigned long key)
     }
 
     hash %= HASH_SIZE;
-#if HASH_DEBUG
+#if DEBUG
     printf( "Hash(%d) = %d\n", key, hash);
 #endif
     return hash;
@@ -202,7 +201,7 @@ int drmHashInsert(void *t, unsigned long key, void *value)
     bucket->value        = value;
     bucket->next         = table->buckets[hash];
     table->buckets[hash] = bucket;
-#if HASH_DEBUG
+#if DEBUG
     printf("Inserted %d at %d/%p\n", key, hash, bucket);
 #endif
     return 0;			/* Added to table */
diff --git a/xf86drmRandom.c b/xf86drmRandom.c
index b3a4be9..81f0301 100644
--- a/xf86drmRandom.c
+++ b/xf86drmRandom.c
@@ -78,7 +78,6 @@
 #include "xf86drmRandom.h"
 
 #define RANDOM_MAGIC 0xfeedbeef
-#define RANDOM_DEBUG 0
 
 void *drmRandomCreate(unsigned long seed)
 {
diff --git a/xf86drmSL.c b/xf86drmSL.c
index acddb54..9bbf8fb 100644
--- a/xf86drmSL.c
+++ b/xf86drmSL.c
@@ -53,7 +53,6 @@
 #define SL_ENTRY_MAGIC 0x00fab1edLU
 #define SL_FREED_MAGIC 0xdecea5edLU
 #define SL_MAX_LEVEL   16
-#define SL_DEBUG       0
 #define SL_RANDOM_SEED 0xc01055a1LU
 
 #if SL_MAIN
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH libdrm v2 8/8] drm: use correct printf modifiers
  2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
                   ` (5 preceding siblings ...)
  2015-03-26 23:57 ` [PATCH libdrm v2 7/8] drm: replace HASH_DEBUG with DEBUG Emil Velikov
@ 2015-03-26 23:57 ` Emil Velikov
  2015-04-01 15:16 ` [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Jan Vesely
  7 siblings, 0 replies; 9+ messages in thread
From: Emil Velikov @ 2015-03-26 23:57 UTC (permalink / raw)
  To: dri-devel; +Cc: emil.l.velikov

The valies are unsigned long, thus we should use %lu.

v2: Drop old printf statement. (Jan)

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 xf86drmHash.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/xf86drmHash.c b/xf86drmHash.c
index 199a2a3..f287e61 100644
--- a/xf86drmHash.c
+++ b/xf86drmHash.c
@@ -99,7 +99,7 @@ static unsigned long HashHash(unsigned long key)
 
     hash %= HASH_SIZE;
 #if DEBUG
-    printf( "Hash(%d) = %d\n", key, hash);
+    printf( "Hash(%lu) = %lu\n", key, hash);
 #endif
     return hash;
 }
@@ -202,7 +202,7 @@ int drmHashInsert(void *t, unsigned long key, void *value)
     bucket->next         = table->buckets[hash];
     table->buckets[hash] = bucket;
 #if DEBUG
-    printf("Inserted %d at %d/%p\n", key, hash, bucket);
+    printf("Inserted %lu at %lu/%p\n", key, hash, bucket);
 #endif
     return 0;			/* Added to table */
 }
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c
  2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
                   ` (6 preceding siblings ...)
  2015-03-26 23:57 ` [PATCH libdrm v2 8/8] drm: use correct printf modifiers Emil Velikov
@ 2015-04-01 15:16 ` Jan Vesely
  7 siblings, 0 replies; 9+ messages in thread
From: Jan Vesely @ 2015-04-01 15:16 UTC (permalink / raw)
  To: Emil Velikov; +Cc: dri-devel


[-- Attachment #1.1: Type: text/plain, Size: 20054 bytes --]

On Thu, 2015-03-26 at 23:57 +0000, Emil Velikov wrote:
> This way with follow up commits we can fix it and wire it up to
> make check
> 
> v2:
>  - Use xf86drmHash.h for common structs.(Jan)
>  - Add test to .gitignore.
> 
> Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
> ---
>  .gitignore        |   1 +
>  Makefile.sources  |   1 +
>  tests/Makefile.am |   3 +-
>  tests/hash.c      | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  xf86drmHash.c     | 196 +++--------------------------------------------------
>  xf86drmHash.h     |  47 +++++++++++++
>  6 files changed, 260 insertions(+), 186 deletions(-)
>  create mode 100644 tests/hash.c
>  create mode 100644 xf86drmHash.h
> 
> diff --git a/.gitignore b/.gitignore
> index 06cc928..0faa825 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -78,6 +78,7 @@ tests/drmstat
>  tests/getclient
>  tests/getstats
>  tests/getversion
> +tests/hash
>  tests/lock
>  tests/openclose
>  tests/setversion
> diff --git a/Makefile.sources b/Makefile.sources
> index 566f7b5..ff4fe5e 100644
> --- a/Makefile.sources
> +++ b/Makefile.sources
> @@ -1,6 +1,7 @@
>  LIBDRM_FILES := \
>  	xf86drm.c \
>  	xf86drmHash.c \
> +	xf86drmHash.h \
>  	xf86drmRandom.c \
>  	xf86drmSL.c \
>  	xf86drmMode.c \
> diff --git a/tests/Makefile.am b/tests/Makefile.am
> index 10f54e3..ea826b5 100644
> --- a/tests/Makefile.am
> +++ b/tests/Makefile.am
> @@ -29,7 +29,8 @@ LDADD = $(top_builddir)/libdrm.la
>  
>  check_PROGRAMS = \
>  	dristat \
> -	drmstat
> +	drmstat \
> +	hash
>  
>  if HAVE_NOUVEAU
>  SUBDIRS += nouveau
> diff --git a/tests/hash.c b/tests/hash.c
> new file mode 100644
> index 0000000..517a667
> --- /dev/null
> +++ b/tests/hash.c
> @@ -0,0 +1,198 @@
> +/* xf86drmHash.c -- Small hash table support for integer -> integer mapping
> + * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
> + *
> + * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
> + * All Rights Reserved.
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
> + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
> + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
> + * DEALINGS IN THE SOFTWARE.
> + *
> + * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
> + *
> + * DESCRIPTION
> + *
> + * This file contains a straightforward implementation of a fixed-sized
> + * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
> + * collision resolution.  There are two potentially interesting things
> + * about this implementation:
> + *
> + * 1) The table is power-of-two sized.  Prime sized tables are more
> + * traditional, but do not have a significant advantage over power-of-two
> + * sized table, especially when double hashing is not used for collision
> + * resolution.
> + *
> + * 2) The hash computation uses a table of random integers [Hanson97,
> + * pp. 39-41].
> + *
> + * FUTURE ENHANCEMENTS
> + *
> + * With a table size of 512, the current implementation is sufficient for a
> + * few hundred keys.  Since this is well above the expected size of the
> + * tables for which this implementation was designed, the implementation of
> + * dynamic hash tables was postponed until the need arises.  A common (and
> + * naive) approach to dynamic hash table implementation simply creates a
> + * new hash table when necessary, rehashes all the data into the new table,
> + * and destroys the old table.  The approach in [Larson88] is superior in
> + * two ways: 1) only a portion of the table is expanded when needed,
> + * distributing the expansion cost over several insertions, and 2) portions
> + * of the table can be locked, enabling a scalable thread-safe
> + * implementation.
> + *
> + * REFERENCES
> + *
> + * [Hanson97] David R. Hanson.  C Interfaces and Implementations:
> + * Techniques for Creating Reusable Software.  Reading, Massachusetts:
> + * Addison-Wesley, 1997.
> + *
> + * [Knuth73] Donald E. Knuth. The Art of Computer Programming.  Volume 3:
> + * Sorting and Searching.  Reading, Massachusetts: Addison-Wesley, 1973.
> + *
> + * [Larson88] Per-Ake Larson. "Dynamic Hash Tables".  CACM 31(4), April
> + * 1988, pp. 446-457.
> + *
> + */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +
> +#include "xf86drm.h"
> +#include "xf86drmHash.h"
> +
> +#define DIST_LIMIT 10
> +static int dist[DIST_LIMIT];
> +
> +static void clear_dist(void) {
> +    int i;
> +
> +    for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
> +}
> +
> +static int count_entries(HashBucketPtr bucket)
> +{
> +    int count = 0;
> +
> +    for (; bucket; bucket = bucket->next) ++count;
> +    return count;
> +}
> +
> +static void update_dist(int count)
> +{
> +    if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
> +    else                     ++dist[count];
> +}
> +
> +static void compute_dist(HashTablePtr table)
> +{
> +    int           i;
> +    HashBucketPtr bucket;
> +
> +    printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
> +	   table->entries, table->hits, table->partials, table->misses);
> +    clear_dist();
> +    for (i = 0; i < HASH_SIZE; i++) {
> +	bucket = table->buckets[i];
> +	update_dist(count_entries(bucket));
> +    }
> +    for (i = 0; i < DIST_LIMIT; i++) {
> +	if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
> +	else                   printf("other %10d\n", dist[i]);
> +    }
> +}
> +
> +static void check_table(HashTablePtr table,
> +			unsigned long key, unsigned long value)
> +{
> +    unsigned long retval  = 0;
> +    int           retcode = drmHashLookup(table, key, &retval);
> +
> +    switch (retcode) {
> +    case -1:
> +	printf("Bad magic = 0x%08lx:"
> +	       " key = %lu, expected = %lu, returned = %lu\n",
> +	       table->magic, key, value, retval);
> +	break;
> +    case 1:
> +	printf("Not found: key = %lu, expected = %lu returned = %lu\n",
> +	       key, value, retval);
> +	break;
> +    case 0:
> +	if (value != retval)
> +	    printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
> +		   key, value, retval);
> +	break;
> +    default:
> +	printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
> +	       retcode, key, value, retval);
> +	break;
> +    }
> +}
> +
> +int main(void)
> +{
> +    HashTablePtr table;
> +    int          i;
> +
> +    printf("\n***** 256 consecutive integers ****\n");
> +    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);
> +    drmHashDestroy(table);
> +
> +    printf("\n***** 1024 consecutive integers ****\n");
> +    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);
> +    drmHashDestroy(table);
> +
> +    printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
> +    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);
> +    drmHashDestroy(table);
> +
> +    printf("\n***** 1024 random integers ****\n");
> +    table = drmHashCreate();
> +    srandom(0xbeefbeef);
> +    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);
> +    drmHashDestroy(table);
> +
> +    printf("\n***** 5000 random integers ****\n");
> +    table = drmHashCreate();
> +    srandom(0xbeefbeef);
> +    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);
> +    drmHashDestroy(table);
> +
> +    return 0;
> +}
> diff --git a/xf86drmHash.c b/xf86drmHash.c
> index 82cbc2a..f7d7f73 100644
> --- a/xf86drmHash.c
> +++ b/xf86drmHash.c
> @@ -71,60 +71,11 @@
>  #include <stdio.h>
>  #include <stdlib.h>
>  
> -#define HASH_MAIN 0
> -
> -#if !HASH_MAIN
> -# include "xf86drm.h"
> -#endif
> +#include "xf86drm.h"
> +#include "xf86drmHash.h"
>  
>  #define HASH_MAGIC 0xdeadbeef
>  #define HASH_DEBUG 0
> -#define HASH_SIZE  512		/* Good for about 100 entries */
> -				/* If you change this value, you probably
> -                                   have to change the HashHash hashing
> -                                   function! */
> -
> -#if HASH_MAIN
> -#define HASH_ALLOC malloc
> -#define HASH_FREE  free
> -#define HASH_RANDOM_DECL
> -#define HASH_RANDOM_INIT(seed)  srandom(seed)
> -#define HASH_RANDOM             random()
> -#define HASH_RANDOM_DESTROY
> -#else
> -#define HASH_ALLOC drmMalloc
> -#define HASH_FREE  drmFree
> -#define HASH_RANDOM_DECL        void *state
> -#define HASH_RANDOM_INIT(seed)  state = drmRandomCreate(seed)
> -#define HASH_RANDOM             drmRandom(state)
> -#define HASH_RANDOM_DESTROY     drmRandomDestroy(state)
> -
> -#endif
> -
> -typedef struct HashBucket {
> -    unsigned long     key;
> -    void              *value;
> -    struct HashBucket *next;
> -} HashBucket, *HashBucketPtr;
> -
> -typedef struct HashTable {
> -    unsigned long    magic;
> -    unsigned long    entries;
> -    unsigned long    hits;	/* At top of linked list */
> -    unsigned long    partials;	/* Not at top of linked list */
> -    unsigned long    misses;	/* Not in table */
> -    HashBucketPtr    buckets[HASH_SIZE];
> -    int              p0;
> -    HashBucketPtr    p1;
> -} HashTable, *HashTablePtr;
> -
> -#if HASH_MAIN
> -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)
>  {
> @@ -135,10 +86,10 @@ static unsigned long HashHash(unsigned long key)
>      int                  i;
>  
>      if (!init) {
> -	HASH_RANDOM_DECL;
> -	HASH_RANDOM_INIT(37);
> -	for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
> -	HASH_RANDOM_DESTROY;
> +	void *state;
> +	state = drmRandomCreate(37);
> +	for (i = 0; i < 256; i++) scatter[i] = drmRandom(state);
> +	drmRandomDestroy(state);
>  	++init;
>      }
>  
> @@ -159,7 +110,7 @@ void *drmHashCreate(void)
>      HashTablePtr table;
>      int          i;
>  
> -    table           = HASH_ALLOC(sizeof(*table));
> +    table           = drmMalloc(sizeof(*table));
>      if (!table) return NULL;
>      table->magic    = HASH_MAGIC;
>      table->entries  = 0;
> @@ -183,11 +134,11 @@ int drmHashDestroy(void *t)
>      for (i = 0; i < HASH_SIZE; i++) {
>  	for (bucket = table->buckets[i]; bucket;) {
>  	    next = bucket->next;
> -	    HASH_FREE(bucket);
> +	    drmFree(bucket);
>  	    bucket = next;
>  	}
>      }
> -    HASH_FREE(table);
> +    drmFree(table);
>      return 0;
>  }
>  
> @@ -245,7 +196,7 @@ int drmHashInsert(void *t, unsigned long key, void *value)
>  
>      if (HashFind(table, key, &hash)) return 1; /* Already in table */
>  
> -    bucket               = HASH_ALLOC(sizeof(*bucket));
> +    bucket               = drmMalloc(sizeof(*bucket));
>      if (!bucket) return -1;	/* Error */
>      bucket->key          = key;
>      bucket->value        = value;
> @@ -270,7 +221,7 @@ int drmHashDelete(void *t, unsigned long key)
>      if (!bucket) return 1;	/* Not found */
>  
>      table->buckets[hash] = bucket->next;
> -    HASH_FREE(bucket);
> +    drmFree(bucket);
>      return 0;
>  }
>  
> @@ -301,128 +252,3 @@ int drmHashFirst(void *t, unsigned long *key, void **value)
>      table->p1 = table->buckets[0];
>      return drmHashNext(table, key, value);
>  }
> -
> -#if HASH_MAIN
> -#define DIST_LIMIT 10
> -static int dist[DIST_LIMIT];
> -
> -static void clear_dist(void) {
> -    int i;
> -
> -    for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
> -}
> -
> -static int count_entries(HashBucketPtr bucket)
> -{
> -    int count = 0;
> -
> -    for (; bucket; bucket = bucket->next) ++count;
> -    return count;
> -}
> -
> -static void update_dist(int count)
> -{
> -    if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
> -    else                     ++dist[count];
> -}
> -
> -static void compute_dist(HashTablePtr table)
> -{
> -    int           i;
> -    HashBucketPtr bucket;
> -
> -    printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
> -	   table->entries, table->hits, table->partials, table->misses);
> -    clear_dist();
> -    for (i = 0; i < HASH_SIZE; i++) {
> -	bucket = table->buckets[i];
> -	update_dist(count_entries(bucket));
> -    }
> -    for (i = 0; i < DIST_LIMIT; i++) {
> -	if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
> -	else                   printf("other %10d\n", dist[i]);
> -    }
> -}
> -
> -static void check_table(HashTablePtr table,
> -			unsigned long key, unsigned long value)
> -{
> -    unsigned long retval  = 0;
> -    int           retcode = drmHashLookup(table, key, &retval);
> -
> -    switch (retcode) {
> -    case -1:
> -	printf("Bad magic = 0x%08lx:"
> -	       " key = %lu, expected = %lu, returned = %lu\n",
> -	       table->magic, key, value, retval);
> -	break;
> -    case 1:
> -	printf("Not found: key = %lu, expected = %lu returned = %lu\n",
> -	       key, value, retval);
> -	break;
> -    case 0:
> -	if (value != retval)
> -	    printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
> -		   key, value, retval);
> -	break;
> -    default:
> -	printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
> -	       retcode, key, value, retval);
> -	break;
> -    }
> -}
> -
> -int main(void)
> -{
> -    HashTablePtr table;
> -    int          i;
> -
> -    printf("\n***** 256 consecutive integers ****\n");
> -    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);
> -    drmHashDestroy(table);
> -
> -    printf("\n***** 1024 consecutive integers ****\n");
> -    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);
> -    drmHashDestroy(table);
> -
> -    printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
> -    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);
> -    drmHashDestroy(table);
> -
> -    printf("\n***** 1024 random integers ****\n");
> -    table = drmHashCreate();
> -    srandom(0xbeefbeef);
> -    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);
> -    drmHashDestroy(table);
> -
> -    printf("\n***** 5000 random integers ****\n");
> -    table = drmHashCreate();
> -    srandom(0xbeefbeef);
> -    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);
> -    drmHashDestroy(table);
> -
> -    return 0;
> -}
> -#endif
> diff --git a/xf86drmHash.h b/xf86drmHash.h
> new file mode 100644
> index 0000000..38fd84b
> --- /dev/null
> +++ b/xf86drmHash.h
> @@ -0,0 +1,47 @@
> +/*
> + * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
> + * All Rights Reserved.
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
> + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
> + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
> + * DEALINGS IN THE SOFTWARE.
> + *
> + * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
> + */
> +
> +#define HASH_SIZE  512		/* Good for about 100 entries */
> +				/* If you change this value, you probably
> +                                   have to change the HashHash hashing
> +                                   function! */
> +
> +typedef struct HashBucket {
> +    unsigned long     key;
> +    void              *value;
> +    struct HashBucket *next;
> +} HashBucket, *HashBucketPtr;
> +
> +typedef struct HashTable {
> +    unsigned long    magic;
> +    unsigned long    entries;
> +    unsigned long    hits;	/* At top of linked list */
> +    unsigned long    partials;	/* Not at top of linked list */
> +    unsigned long    misses;	/* Not in table */
> +    HashBucketPtr    buckets[HASH_SIZE];
> +    int              p0;
> +    HashBucketPtr    p1;
> +} HashTable, *HashTablePtr;

For the series:
Reviewed-by: Jan Vesely <jan.vesely@rutgers.edu>

-- 
Jan Vesely <jan.vesely@rutgers.edu>

[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

[-- Attachment #2: Type: text/plain, Size: 159 bytes --]

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-04-01 15:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-26 23:57 [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Emil Velikov
2015-03-26 23:57 ` [PATCH libdrm v2 2/8] tests/hash: misc compilation fixes Emil Velikov
2015-03-26 23:57 ` [PATCH libdrm v2 3/8] tests/hash: style fixes Emil Velikov
2015-03-26 23:57 ` [PATCH libdrm v2 4/8] tests/hash: return non-zero on failure Emil Velikov
2015-03-26 23:57 ` [PATCH libdrm v2 5/8] tests/random: extract test out of xf86drmRandom.c Emil Velikov
2015-03-26 23:57 ` [PATCH libdrm v2 6/8] tests/random: return non-zero on test failure Emil Velikov
2015-03-26 23:57 ` [PATCH libdrm v2 7/8] drm: replace HASH_DEBUG with DEBUG Emil Velikov
2015-03-26 23:57 ` [PATCH libdrm v2 8/8] drm: use correct printf modifiers Emil Velikov
2015-04-01 15:16 ` [PATCH libdrm v2 1/8] tests/hash: extract test out of xf86drmHash.c Jan Vesely

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.