tests/hash: extract test out of xf86drmHash.c
PATCH v2 1/8 tests/hash: extract test out of xf86drmHash.c
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.
--- .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
.gitignore +1 -0
@@ -78,6 +78,7 @@ tests/drmstat tests/getclient tests/getstats tests/getversion +tests/hash tests/lock tests/openclose tests/setversion
Makefile.sources +1 -0
@@ -1,6 +1,7 @@ LIBDRM_FILES := \ xf86drm.c \ xf86drmHash.c \ + xf86drmHash.h \ xf86drmRandom.c \ xf86drmSL.c \ xf86drmMode.c \
tests/Makefile.am +2 -1
@@ -29,7 +29,8 @@ LDADD = $(top_builddir)/libdrm.la check_PROGRAMS = \ dristat \ - drmstat + drmstat \ + hash if HAVE_NOUVEAU SUBDIRS += nouveau
tests/hash.c new file +198 -0
198 lines not on this page · raw message
xf86drmHash.c +11 -185
241 lines not on this page · raw message
xf86drmHash.h new file +47 -0
47 lines not on this page · raw message
Signature · 6 lines
2.3.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v2 2/8 tests/hash: misc compilation fixes
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.
--- tests/hash.c | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-)
tests/hash.c +21 -24
@@ -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);
Signature · 6 lines
2.3.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v2 3/8 tests/hash: style fixes
In reply to Emil Velikovv2: Rebase on earlier changes. Keep count initialisation as is.
--- tests/hash.c | 90 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 36 deletions(-)
tests/hash.c +54 -36
@@ -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);
Signature · 6 lines
2.3.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v2 4/8 tests/hash: return non-zero on failure
In reply to Emil Velikov... and wire up to `make check' now that it's useful.
v2: Really return non-zero on failure.
--- tests/Makefile.am | 10 ++++++---- tests/hash.c | 26 +++++++++++++++----------- 2 files changed, 21 insertions(+), 15 deletions(-)
tests/Makefile.am +6 -4
@@ -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)
tests/hash.c +15 -11
@@ -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; }
Signature · 6 lines
2.3.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v2 5/8 tests/random: extract test out of xf86drmRandom.c
In reply to Emil VelikovWith 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.
--- .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
.gitignore +1 -0
@@ -81,6 +81,7 @@ tests/getversion tests/hash tests/lock tests/openclose +tests/random tests/setversion tests/updatedraw tests/modeprint/modeprint
Makefile.sources +1 -0
@@ -3,6 +3,7 @@ LIBDRM_FILES := \ xf86drmHash.c \ xf86drmHash.h \ xf86drmRandom.c \ + xf86drmRandom.h \ xf86drmSL.c \ xf86drmMode.c \ xf86atomic.h \
tests/Makefile.am +2 -1
@@ -29,7 +29,8 @@ LDADD = $(top_builddir)/libdrm.la check_PROGRAMS = \ dristat \ - drmstat + drmstat \ + random if HAVE_NOUVEAU SUBDIRS += nouveau
tests/random.c new file +118 -0
118 lines not on this page · raw message
xf86drmRandom.c +4 -74
101 lines not on this page · raw message
xf86drmRandom.h new file +35 -0
@@ -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 <[email protected]> +*/ + +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 ;
Signature · 6 lines
2.3.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v2 6/8 tests/random: return non-zero on test failure
In reply to Emil Velikov... and wire it up to make check
v2: s/rand - state->check/rand != state->check/. (Jan)
--- tests/Makefile.am | 6 +++--- tests/random.c | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-)
tests/Makefile.am +3 -3
@@ -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
tests/random.c +4 -2
@@ -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; }
Signature · 6 lines
2.3.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v2 7/8 drm: replace HASH_DEBUG with DEBUG
In reply to Emil Velikov... and remove the useless SL_DEBUG and RANDOM_DEBUG
v2: Rebase on earlier changes.
--- xf86drmHash.c | 5 ++--- xf86drmRandom.c | 1 - xf86drmSL.c | 1 - 3 files changed, 2 insertions(+), 5 deletions(-)
xf86drmHash.c +2 -3
@@ -75,7 +75,6 @@#include "xf86drmHash.h" #define HASH_MAGIC 0xdeadbeef -#define HASH_DEBUG 0static 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 */
xf86drmRandom.c +0 -1
@@ -78,7 +78,6 @@#include "xf86drmRandom.h" #define RANDOM_MAGIC 0xfeedbeef -#define RANDOM_DEBUG 0void *drmRandomCreate (unsigned long seed) {
xf86drmSL.c +0 -1
@@ -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
Signature · 6 lines
2.3.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v2 8/8 drm: use correct printf modifiers
In reply to Emil VelikovThe valies are unsigned long, thus we should use %lu.
v2: Drop old printf statement. (Jan)
--- xf86drmHash.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xf86drmHash.c +2 -2
@@ -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 */ }
Signature · 6 lines
2.3.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 556 lines · Emil Velikov
This way with follow up commits we can fix it and wire it up to make check
v2:
- Usexf86drmHash.hfor common structs.(Jan)
- Add test to .gitignore.Signed-off-by Emil Velikov <[email protected]>--- .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.gitignore +1
-0@@ -78,6 +78,7 @@ tests/drmstat tests/getclient tests/getstats tests/getversion +tests/hash tests/lock tests/openclose tests/setversionMakefile.sources +1
-0@@ -1,6 +1,7 @@ LIBDRM_FILES := \ xf86drm.c \ xf86drmHash.c \ + xf86drmHash.h \ xf86drmRandom.c \ xf86drmSL.c \ xf86drmMode.c \tests/Makefile.am +2
-1@@ -29,7 +29,8 @@ LDADD = $(top_builddir)/libdrm.la check_PROGRAMS = \ dristat \ - drmstat + drmstat \ + hash if HAVE_NOUVEAU SUBDIRS += nouveautests/hash.c new file +198
-0@@ -0,0 +1,198 @@ +/* xf86drmHash.c -- Small hash table support for integer -> integer mapping +* Created: Sun Apr 18 09:35:45 1999 by [email protected] +* +* 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 <[email protected]> +* +* 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 ; +}xf86drmHash.c +11
-185@@ -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 xf86drmHash.h new file +37
-0@@ -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 <[email protected]> +*/ + +#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 ; +
xf86drmHash.h line 47
Quoted 10 lines
xf86drmHash.h new file +10
-0+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:
Signature · 1 line
Jan Vesely <[email protected]>
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel