Initial revision

This commit is contained in:
Eric Anholt 2004-06-16 09:16:01 +00:00
parent 580b9a7da1
commit bcc1eab1fd
4 changed files with 3470 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,435 @@
/* 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>
*
* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmHash.c,v 1.4 2001/03/21 18:08:54 dawes Exp $
*
* DESCRIPTION
*
* This file contains a straightforward implementation of a fixed-sized
* 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.
*
*/
#define HASH_MAIN 0
#if HASH_MAIN
# include <stdio.h>
# include <stdlib.h>
#else
# include "xf86drm.h"
# ifdef XFree86LOADER
# include "xf86.h"
# include "xf86_ansic.h"
# else
# include <stdio.h>
# include <stdlib.h>
# endif
#endif
#define N(x) drm##x
#define HASH_MAGIC 0xdeadbeef
#define HASH_DEBUG 0
#define HASH_SIZE 512 /* Good for about 100 entries */
/* 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()
#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)
#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 *N(HashCreate)(void);
extern int N(HashDestroy)(void *t);
extern int N(HashLookup)(void *t, unsigned long key, unsigned long *value);
extern int N(HashInsert)(void *t, unsigned long key, unsigned long value);
extern int N(HashDelete)(void *t, unsigned long key);
#endif
static unsigned long HashHash(unsigned long key)
{
unsigned long hash = 0;
unsigned long tmp = key;
static int init = 0;
static unsigned long scatter[256];
int i;
if (!init) {
HASH_RANDOM_DECL;
HASH_RANDOM_INIT(37);
for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
++init;
}
while (tmp) {
hash = (hash << 1) + scatter[tmp & 0xff];
tmp >>= 8;
}
hash %= HASH_SIZE;
#if HASH_DEBUG
printf( "Hash(%d) = %d\n", key, hash);
#endif
return hash;
}
void *N(HashCreate)(void)
{
HashTablePtr table;
int i;
table = HASH_ALLOC(sizeof(*table));
if (!table) return NULL;
table->magic = HASH_MAGIC;
table->entries = 0;
table->hits = 0;
table->partials = 0;
table->misses = 0;
for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
return table;
}
int N(HashDestroy)(void *t)
{
HashTablePtr table = (HashTablePtr)t;
HashBucketPtr bucket;
HashBucketPtr next;
int i;
if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
for (i = 0; i < HASH_SIZE; i++) {
for (bucket = table->buckets[i]; bucket;) {
next = bucket->next;
HASH_FREE(bucket);
bucket = next;
}
}
HASH_FREE(table);
return 0;
}
/* Find the bucket and organize the list so that this bucket is at the
top. */
static HashBucketPtr HashFind(HashTablePtr table,
unsigned long key, unsigned long *h)
{
unsigned long hash = HashHash(key);
HashBucketPtr prev = NULL;
HashBucketPtr bucket;
if (h) *h = hash;
for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
if (bucket->key == key) {
if (prev) {
/* Organize */
prev->next = bucket->next;
bucket->next = table->buckets[hash];
table->buckets[hash] = bucket;
++table->partials;
} else {
++table->hits;
}
return bucket;
}
prev = bucket;
}
++table->misses;
return NULL;
}
int N(HashLookup)(void *t, unsigned long key, void **value)
{
HashTablePtr table = (HashTablePtr)t;
HashBucketPtr bucket;
if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
bucket = HashFind(table, key, NULL);
if (!bucket) return 1; /* Not found */
*value = bucket->value;
return 0; /* Found */
}
int N(HashInsert)(void *t, unsigned long key, void *value)
{
HashTablePtr table = (HashTablePtr)t;
HashBucketPtr bucket;
unsigned long hash;
if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
if (HashFind(table, key, &hash)) return 1; /* Already in table */
bucket = HASH_ALLOC(sizeof(*bucket));
if (!bucket) return -1; /* Error */
bucket->key = key;
bucket->value = value;
bucket->next = table->buckets[hash];
table->buckets[hash] = bucket;
#if HASH_DEBUG
printf("Inserted %d at %d/%p\n", key, hash, bucket);
#endif
return 0; /* Added to table */
}
int N(HashDelete)(void *t, unsigned long key)
{
HashTablePtr table = (HashTablePtr)t;
unsigned long hash;
HashBucketPtr bucket;
if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
bucket = HashFind(table, key, &hash);
if (!bucket) return 1; /* Not found */
table->buckets[hash] = bucket->next;
HASH_FREE(bucket);
return 0;
}
int N(HashNext)(void *t, unsigned long *key, void **value)
{
HashTablePtr table = (HashTablePtr)t;
for (; table->p0 < HASH_SIZE;
++table->p0, table->p1 = table->buckets[table->p0]) {
if (table->p1) {
*key = table->p1->key;
*value = table->p1->value;
table->p1 = table->p1->next;
return 1;
}
}
return 0;
}
int N(HashFirst)(void *t, unsigned long *key, void **value)
{
HashTablePtr table = (HashTablePtr)t;
if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
table->p0 = 0;
table->p1 = table->buckets[0];
return N(HashNext)(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 = N(HashLookup)(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 = N(HashCreate)();
for (i = 0; i < 256; i++) N(HashInsert)(table, i, i);
for (i = 0; i < 256; i++) check_table(table, i, i);
for (i = 256; i >= 0; i--) check_table(table, i, i);
compute_dist(table);
N(HashDestroy)(table);
printf("\n***** 1024 consecutive integers ****\n");
table = N(HashCreate)();
for (i = 0; i < 1024; i++) N(HashInsert)(table, i, i);
for (i = 0; i < 1024; i++) check_table(table, i, i);
for (i = 1024; i >= 0; i--) check_table(table, i, i);
compute_dist(table);
N(HashDestroy)(table);
printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
table = N(HashCreate)();
for (i = 0; i < 1024; i++) N(HashInsert)(table, i*4096, i);
for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
for (i = 1024; i >= 0; i--) check_table(table, i*4096, i);
compute_dist(table);
N(HashDestroy)(table);
printf("\n***** 1024 random integers ****\n");
table = N(HashCreate)();
srandom(0xbeefbeef);
for (i = 0; i < 1024; i++) N(HashInsert)(table, random(), i);
srandom(0xbeefbeef);
for (i = 0; i < 1024; i++) check_table(table, random(), i);
srandom(0xbeefbeef);
for (i = 0; i < 1024; i++) check_table(table, random(), i);
compute_dist(table);
N(HashDestroy)(table);
printf("\n***** 5000 random integers ****\n");
table = N(HashCreate)();
srandom(0xbeefbeef);
for (i = 0; i < 5000; i++) N(HashInsert)(table, random(), i);
srandom(0xbeefbeef);
for (i = 0; i < 5000; i++) check_table(table, random(), i);
srandom(0xbeefbeef);
for (i = 0; i < 5000; i++) check_table(table, random(), i);
compute_dist(table);
N(HashDestroy)(table);
return 0;
}
#endif

View File

@ -0,0 +1,219 @@
/* 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>
*
* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmRandom.c,v 1.4 2000/06/17 00:03:34 martin Exp $
*
* 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.
*
*/
#define RANDOM_MAIN 0
#if RANDOM_MAIN
# include <stdio.h>
# include <stdlib.h>
#else
# include "xf86drm.h"
# ifdef XFree86LOADER
# include "xf86.h"
# include "xf86_ansic.h"
# else
# include <stdio.h>
# include <stdlib.h>
# endif
#endif
#define N(x) drm##x
#define 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;
long seed;
} RandomState;
#if RANDOM_MAIN
extern void *N(RandomCreate)(unsigned long seed);
extern int N(RandomDestroy)(void *state);
extern unsigned long N(Random)(void *state);
extern double N(RandomDouble)(void *state);
#endif
void *N(RandomCreate)(unsigned long seed)
{
RandomState *state;
state = RANDOM_ALLOC(sizeof(*state));
if (!state) return NULL;
state->magic = RANDOM_MAGIC;
#if 0
/* Park & Miller, October 1988 */
state->a = 16807;
state->m = 2147483647;
state->check = 1043618065; /* After 10000 iterations */
#else
/* Park, Miller, and Stockmeyer, July 1993 */
state->a = 48271;
state->m = 2147483647;
state->check = 399268537; /* After 10000 iterations */
#endif
state->q = state->m / state->a;
state->r = state->m % state->a;
state->seed = seed;
/* Check for illegal boundary conditions,
and choose closest legal value. */
if (state->seed <= 0) state->seed = 1;
if (state->seed >= state->m) state->seed = state->m - 1;
return state;
}
int N(RandomDestroy)(void *state)
{
RANDOM_FREE(state);
return 0;
}
unsigned long N(Random)(void *state)
{
RandomState *s = (RandomState *)state;
long hi;
long lo;
hi = s->seed / s->q;
lo = s->seed % s->q;
s->seed = s->a * lo - s->r * hi;
if (s->seed <= 0) s->seed += s->m;
return s->seed;
}
double N(RandomDouble)(void *state)
{
RandomState *s = (RandomState *)state;
return (double)N(Random)(state)/(double)s->m;
}
#if RANDOM_MAIN
static void check_period(long seed)
{
unsigned long count = 0;
unsigned long initial;
void *state;
state = N(RandomCreate)(seed);
initial = N(Random)(state);
++count;
while (initial != N(Random)(state)) {
if (!++count) break;
}
printf("With seed of %10ld, period = %10lu (0x%08lx)\n",
seed, count, count);
N(RandomDestroy)(state);
}
int main(void)
{
RandomState *state;
int i;
unsigned long rand;
state = N(RandomCreate)(1);
for (i = 0; i < 10000; i++) {
rand = N(Random)(state);
}
printf("After 10000 iterations: %lu (%lu expected): %s\n",
rand, state->check,
rand - state->check ? "*INCORRECT*" : "CORRECT");
N(RandomDestroy)(state);
printf("Checking periods...\n");
check_period(1);
check_period(2);
check_period(31415926);
return 0;
}
#endif

View File

@ -0,0 +1,490 @@
/* xf86drmSL.c -- Skip list support
* Created: Mon May 10 09: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>
*
* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmSL.c,v 1.3 2000/06/17 00:03:34 martin Exp $
*
* DESCRIPTION
*
* This file contains a straightforward skip list implementation.n
*
* FUTURE ENHANCEMENTS
*
* REFERENCES
*
* [Pugh90] William Pugh. Skip Lists: A Probabilistic Alternative to
* Balanced Trees. CACM 33(6), June 1990, pp. 668-676.
*
*/
#define SL_MAIN 0
#if SL_MAIN
# include <stdio.h>
# include <stdlib.h>
# include <sys/time.h>
#else
# include "xf86drm.h"
# ifdef XFree86LOADER
# include "xf86.h"
# include "xf86_ansic.h"
# else
# include <stdio.h>
# include <stdlib.h>
# endif
#endif
#define N(x) drm##x
#define SL_LIST_MAGIC 0xfacade00LU
#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
#define SL_ALLOC malloc
#define SL_FREE free
#define SL_RANDOM_DECL static int state = 0;
#define SL_RANDOM_INIT(seed) if (!state) { srandom(seed); ++state; }
#define SL_RANDOM random()
#else
#define SL_ALLOC drmMalloc
#define SL_FREE drmFree
#define SL_RANDOM_DECL static void *state = NULL
#define SL_RANDOM_INIT(seed) if (!state) state = drmRandomCreate(seed)
#define SL_RANDOM drmRandom(state)
#endif
typedef struct SLEntry {
unsigned long magic; /* SL_ENTRY_MAGIC */
unsigned long key;
void *value;
int levels;
struct SLEntry *forward[1]; /* variable sized array */
} SLEntry, *SLEntryPtr;
typedef struct SkipList {
unsigned long magic; /* SL_LIST_MAGIC */
int level;
int count;
SLEntryPtr head;
SLEntryPtr p0; /* Position for iteration */
} SkipList, *SkipListPtr;
#if SL_MAIN
extern void *N(SLCreate)(void);
extern int N(SLDestroy)(void *l);
extern int N(SLLookup)(void *l, unsigned long key, void **value);
extern int N(SLInsert)(void *l, unsigned long key, void *value);
extern int N(SLDelete)(void *l, unsigned long key);
extern int N(SLNext)(void *l, unsigned long *key, void **value);
extern int N(SLFirst)(void *l, unsigned long *key, void **value);
extern void N(SLDump)(void *l);
extern int N(SLLookupNeighbors)(void *l, unsigned long key,
unsigned long *prev_key, void **prev_value,
unsigned long *next_key, void **next_value);
#endif
static SLEntryPtr SLCreateEntry(int max_level, unsigned long key, void *value)
{
SLEntryPtr entry;
if (max_level < 0 || max_level > SL_MAX_LEVEL) max_level = SL_MAX_LEVEL;
entry = SL_ALLOC(sizeof(*entry)
+ (max_level + 1) * sizeof(entry->forward[0]));
if (!entry) return NULL;
entry->magic = SL_ENTRY_MAGIC;
entry->key = key;
entry->value = value;
entry->levels = max_level + 1;
return entry;
}
static int SLRandomLevel(void)
{
int level = 1;
SL_RANDOM_DECL;
SL_RANDOM_INIT(SL_RANDOM_SEED);
while ((SL_RANDOM & 0x01) && level < SL_MAX_LEVEL) ++level;
return level;
}
void *N(SLCreate)(void)
{
SkipListPtr list;
int i;
list = SL_ALLOC(sizeof(*list));
if (!list) return NULL;
list->magic = SL_LIST_MAGIC;
list->level = 0;
list->head = SLCreateEntry(SL_MAX_LEVEL, 0, NULL);
list->count = 0;
for (i = 0; i <= SL_MAX_LEVEL; i++) list->head->forward[i] = NULL;
return list;
}
int N(SLDestroy)(void *l)
{
SkipListPtr list = (SkipListPtr)l;
SLEntryPtr entry;
SLEntryPtr next;
if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
for (entry = list->head; entry; entry = next) {
if (entry->magic != SL_ENTRY_MAGIC) return -1; /* Bad magic */
next = entry->forward[0];
entry->magic = SL_FREED_MAGIC;
SL_FREE(entry);
}
list->magic = SL_FREED_MAGIC;
SL_FREE(list);
return 0;
}
static SLEntryPtr SLLocate(void *l, unsigned long key, SLEntryPtr *update)
{
SkipListPtr list = (SkipListPtr)l;
SLEntryPtr entry;
int i;
if (list->magic != SL_LIST_MAGIC) return NULL;
for (i = list->level, entry = list->head; i >= 0; i--) {
while (entry->forward[i] && entry->forward[i]->key < key)
entry = entry->forward[i];
update[i] = entry;
}
return entry->forward[0];
}
int N(SLInsert)(void *l, unsigned long key, void *value)
{
SkipListPtr list = (SkipListPtr)l;
SLEntryPtr entry;
SLEntryPtr update[SL_MAX_LEVEL + 1];
int level;
int i;
if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
entry = SLLocate(list, key, update);
if (entry && entry->key == key) return 1; /* Already in list */
level = SLRandomLevel();
if (level > list->level) {
level = ++list->level;
update[level] = list->head;
}
entry = SLCreateEntry(level, key, value);
/* Fix up forward pointers */
for (i = 0; i <= level; i++) {
entry->forward[i] = update[i]->forward[i];
update[i]->forward[i] = entry;
}
++list->count;
return 0; /* Added to table */
}
int N(SLDelete)(void *l, unsigned long key)
{
SkipListPtr list = (SkipListPtr)l;
SLEntryPtr update[SL_MAX_LEVEL + 1];
SLEntryPtr entry;
int i;
if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
entry = SLLocate(list, key, update);
if (!entry || entry->key != key) return 1; /* Not found */
/* Fix up forward pointers */
for (i = 0; i <= list->level; i++) {
if (update[i]->forward[i] == entry)
update[i]->forward[i] = entry->forward[i];
}
entry->magic = SL_FREED_MAGIC;
SL_FREE(entry);
while (list->level && !list->head->forward[list->level]) --list->level;
--list->count;
return 0;
}
int N(SLLookup)(void *l, unsigned long key, void **value)
{
SkipListPtr list = (SkipListPtr)l;
SLEntryPtr update[SL_MAX_LEVEL + 1];
SLEntryPtr entry;
entry = SLLocate(list, key, update);
if (entry && entry->key == key) {
*value = entry;
return 0;
}
*value = NULL;
return -1;
}
int N(SLLookupNeighbors)(void *l, unsigned long key,
unsigned long *prev_key, void **prev_value,
unsigned long *next_key, void **next_value)
{
SkipListPtr list = (SkipListPtr)l;
SLEntryPtr update[SL_MAX_LEVEL + 1];
SLEntryPtr entry;
int retcode = 0;
entry = SLLocate(list, key, update);
*prev_key = *next_key = key;
*prev_value = *next_value = NULL;
if (update[0]) {
*prev_key = update[0]->key;
*prev_value = update[0]->value;
++retcode;
if (update[0]->forward[0]) {
*next_key = update[0]->forward[0]->key;
*next_value = update[0]->forward[0]->value;
++retcode;
}
}
return retcode;
}
int N(SLNext)(void *l, unsigned long *key, void **value)
{
SkipListPtr list = (SkipListPtr)l;
SLEntryPtr entry;
if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
entry = list->p0;
if (entry) {
list->p0 = entry->forward[0];
*key = entry->key;
*value = entry->value;
return 1;
}
list->p0 = NULL;
return 0;
}
int N(SLFirst)(void *l, unsigned long *key, void **value)
{
SkipListPtr list = (SkipListPtr)l;
if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
list->p0 = list->head->forward[0];
return N(SLNext)(list, key, value);
}
/* Dump internal data structures for debugging. */
void N(SLDump)(void *l)
{
SkipListPtr list = (SkipListPtr)l;
SLEntryPtr entry;
int i;
if (list->magic != SL_LIST_MAGIC) {
printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
list->magic, SL_LIST_MAGIC);
return;
}
printf("Level = %d, count = %d\n", list->level, list->count);
for (entry = list->head; entry; entry = entry->forward[0]) {
if (entry->magic != SL_ENTRY_MAGIC) {
printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
list->magic, SL_ENTRY_MAGIC);
}
printf("\nEntry %p <0x%08lx, %p> has %2d levels\n",
entry, entry->key, entry->value, entry->levels);
for (i = 0; i < entry->levels; i++) {
if (entry->forward[i]) {
printf(" %2d: %p <0x%08lx, %p>\n",
i,
entry->forward[i],
entry->forward[i]->key,
entry->forward[i]->value);
} else {
printf(" %2d: %p\n", i, entry->forward[i]);
}
}
}
}
#if SL_MAIN
static void print(SkipListPtr list)
{
unsigned long key;
void *value;
if (N(SLFirst)(list, &key, &value)) {
do {
printf("key = %5lu, value = %p\n", key, value);
} while (N(SLNext)(list, &key, &value));
}
}
static double do_time(int size, int iter)
{
SkipListPtr list;
int i, j;
unsigned long keys[1000000];
unsigned long previous;
unsigned long key;
void *value;
struct timeval start, stop;
double usec;
SL_RANDOM_DECL;
SL_RANDOM_INIT(12345);
list = N(SLCreate)();
for (i = 0; i < size; i++) {
keys[i] = SL_RANDOM;
N(SLInsert)(list, keys[i], NULL);
}
previous = 0;
if (N(SLFirst)(list, &key, &value)) {
do {
if (key <= previous) {
printf( "%lu !< %lu\n", previous, key);
}
previous = key;
} while (N(SLNext)(list, &key, &value));
}
gettimeofday(&start, NULL);
for (j = 0; j < iter; j++) {
for (i = 0; i < size; i++) {
if (N(SLLookup)(list, keys[i], &value))
printf("Error %lu %d\n", keys[i], i);
}
}
gettimeofday(&stop, NULL);
usec = (double)(stop.tv_sec * 1000000 + stop.tv_usec
- start.tv_sec * 1000000 - start.tv_usec) / (size * iter);
printf("%0.2f microseconds for list length %d\n", usec, size);
N(SLDestroy)(list);
return usec;
}
static void print_neighbors(void *list, unsigned long key)
{
unsigned long prev_key = 0;
unsigned long next_key = 0;
void *prev_value;
void *next_value;
int retval;
retval = drmSLLookupNeighbors(list, key,
&prev_key, &prev_value,
&next_key, &next_value);
printf("Neighbors of %5lu: %d %5lu %5lu\n",
key, retval, prev_key, next_key);
}
int main(void)
{
SkipListPtr list;
double usec, usec2, usec3, usec4;
list = N(SLCreate)();
printf( "list at %p\n", list);
print(list);
printf("\n==============================\n\n");
N(SLInsert)(list, 123, NULL);
N(SLInsert)(list, 213, NULL);
N(SLInsert)(list, 50, NULL);
print(list);
printf("\n==============================\n\n");
print_neighbors(list, 0);
print_neighbors(list, 50);
print_neighbors(list, 51);
print_neighbors(list, 123);
print_neighbors(list, 200);
print_neighbors(list, 213);
print_neighbors(list, 256);
printf("\n==============================\n\n");
N(SLDelete)(list, 50);
print(list);
printf("\n==============================\n\n");
N(SLDump)(list);
N(SLDestroy)(list);
printf("\n==============================\n\n");
usec = do_time(100, 10000);
usec2 = do_time(1000, 500);
printf("Table size increased by %0.2f, search time increased by %0.2f\n",
1000.0/100.0, usec2 / usec);
usec3 = do_time(10000, 50);
printf("Table size increased by %0.2f, search time increased by %0.2f\n",
10000.0/100.0, usec3 / usec);
usec4 = do_time(100000, 4);
printf("Table size increased by %0.2f, search time increased by %0.2f\n",
100000.0/100.0, usec4 / usec);
return 0;
}
#endif