Merge branch 'master' of git+ssh://git.freedesktop.org/git/mesa/drm into modesetting-101
[profile/ivi/libdrm.git] / libdrm / xf86drmSL.c
1 /* xf86drmSL.c -- Skip list support
2  * Created: Mon May 10 09:28:13 1999 by faith@precisioninsight.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  * 
26  * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
27  *
28  * DESCRIPTION
29  *
30  * This file contains a straightforward skip list implementation.n
31  *
32  * FUTURE ENHANCEMENTS
33  *
34  * REFERENCES
35  *
36  * [Pugh90] William Pugh.  Skip Lists: A Probabilistic Alternative to
37  * Balanced Trees. CACM 33(6), June 1990, pp. 668-676.
38  *
39  */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #define SL_MAIN 0
45
46 #if !SL_MAIN
47 # include "drm.h"
48 # include "xf86drm.h"
49 #else
50 # include <sys/time.h>
51 #endif
52
53 #define SL_LIST_MAGIC  0xfacade00LU
54 #define SL_ENTRY_MAGIC 0x00fab1edLU
55 #define SL_FREED_MAGIC 0xdecea5edLU
56 #define SL_MAX_LEVEL   16
57 #define SL_DEBUG       0
58 #define SL_RANDOM_SEED 0xc01055a1LU
59
60 #if SL_MAIN
61 #define SL_ALLOC malloc
62 #define SL_FREE  free
63 #define SL_RANDOM_DECL        static int state = 0;
64 #define SL_RANDOM_INIT(seed)  if (!state) { srandom(seed); ++state; }
65 #define SL_RANDOM             random()
66 #else
67 #define SL_ALLOC drmMalloc
68 #define SL_FREE  drmFree
69 #define SL_RANDOM_DECL        static void *state = NULL
70 #define SL_RANDOM_INIT(seed)  if (!state) state = drmRandomCreate(seed)
71 #define SL_RANDOM             drmRandom(state)
72
73 #endif
74
75 typedef struct SLEntry {
76     unsigned long     magic;       /* SL_ENTRY_MAGIC */
77     unsigned long     key;
78     void              *value;
79     int               levels;
80     struct SLEntry    *forward[1]; /* variable sized array */
81 } SLEntry, *SLEntryPtr;
82
83 typedef struct SkipList {
84     unsigned long    magic;     /* SL_LIST_MAGIC */
85     int              level;
86     int              count;
87     SLEntryPtr       head;
88     SLEntryPtr       p0;        /* Position for iteration */
89 } SkipList, *SkipListPtr;
90
91 #if SL_MAIN
92 extern void *drmSLCreate(void);
93 extern int  drmSLDestroy(void *l);
94 extern int  drmSLLookup(void *l, unsigned long key, void **value);
95 extern int  drmSLInsert(void *l, unsigned long key, void *value);
96 extern int  drmSLDelete(void *l, unsigned long key);
97 extern int  drmSLNext(void *l, unsigned long *key, void **value);
98 extern int  drmSLFirst(void *l, unsigned long *key, void **value);
99 extern void drmSLDump(void *l);
100 extern int  drmSLLookupNeighbors(void *l, unsigned long key,
101                                  unsigned long *prev_key, void **prev_value,
102                                  unsigned long *next_key, void **next_value);
103 #endif
104
105 static SLEntryPtr SLCreateEntry(int max_level, unsigned long key, void *value)
106 {
107     SLEntryPtr entry;
108     
109     if (max_level < 0 || max_level > SL_MAX_LEVEL) max_level = SL_MAX_LEVEL;
110
111     entry         = SL_ALLOC(sizeof(*entry)
112                              + (max_level + 1) * sizeof(entry->forward[0]));
113     if (!entry) return NULL;
114     entry->magic  = SL_ENTRY_MAGIC;
115     entry->key    = key;
116     entry->value  = value;
117     entry->levels = max_level + 1;
118
119     return entry;
120 }
121
122 static int SLRandomLevel(void)
123 {
124     int level = 1;
125     SL_RANDOM_DECL;
126
127     SL_RANDOM_INIT(SL_RANDOM_SEED);
128     
129     while ((SL_RANDOM & 0x01) && level < SL_MAX_LEVEL) ++level;
130     return level;
131 }
132
133 void *drmSLCreate(void)
134 {
135     SkipListPtr  list;
136     int          i;
137
138     list           = SL_ALLOC(sizeof(*list));
139     if (!list) return NULL;
140     list->magic    = SL_LIST_MAGIC;
141     list->level    = 0;
142     list->head     = SLCreateEntry(SL_MAX_LEVEL, 0, NULL);
143     list->count    = 0;
144
145     for (i = 0; i <= SL_MAX_LEVEL; i++) list->head->forward[i] = NULL;
146     
147     return list;
148 }
149
150 int drmSLDestroy(void *l)
151 {
152     SkipListPtr   list  = (SkipListPtr)l;
153     SLEntryPtr    entry;
154     SLEntryPtr    next;
155
156     if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
157
158     for (entry = list->head; entry; entry = next) {
159         if (entry->magic != SL_ENTRY_MAGIC) return -1; /* Bad magic */
160         next         = entry->forward[0];
161         entry->magic = SL_FREED_MAGIC;
162         SL_FREE(entry);
163     }
164
165     list->magic = SL_FREED_MAGIC;
166     SL_FREE(list);
167     return 0;
168 }
169
170 static SLEntryPtr SLLocate(void *l, unsigned long key, SLEntryPtr *update)
171 {
172     SkipListPtr   list  = (SkipListPtr)l;
173     SLEntryPtr    entry;
174     int           i;
175
176     if (list->magic != SL_LIST_MAGIC) return NULL;
177
178     for (i = list->level, entry = list->head; i >= 0; i--) {
179         while (entry->forward[i] && entry->forward[i]->key < key)
180             entry = entry->forward[i];
181         update[i] = entry;
182     }
183
184     return entry->forward[0];
185 }
186
187 int drmSLInsert(void *l, unsigned long key, void *value)
188 {
189     SkipListPtr   list  = (SkipListPtr)l;
190     SLEntryPtr    entry;
191     SLEntryPtr    update[SL_MAX_LEVEL + 1];
192     int           level;
193     int           i;
194
195     if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
196
197     entry = SLLocate(list, key, update);
198
199     if (entry && entry->key == key) return 1; /* Already in list */
200
201
202     level = SLRandomLevel();
203     if (level > list->level) {
204         level = ++list->level;
205         update[level] = list->head;
206     }
207
208     entry = SLCreateEntry(level, key, value);
209
210                                 /* Fix up forward pointers */
211     for (i = 0; i <= level; i++) {
212         entry->forward[i]     = update[i]->forward[i];
213         update[i]->forward[i] = entry;
214     }
215
216     ++list->count;
217     return 0;                   /* Added to table */
218 }
219
220 int drmSLDelete(void *l, unsigned long key)
221 {
222     SkipListPtr   list = (SkipListPtr)l;
223     SLEntryPtr    update[SL_MAX_LEVEL + 1];
224     SLEntryPtr    entry;
225     int           i;
226
227     if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
228
229     entry = SLLocate(list, key, update);
230
231     if (!entry || entry->key != key) return 1; /* Not found */
232
233                                 /* Fix up forward pointers */
234     for (i = 0; i <= list->level; i++) {
235         if (update[i]->forward[i] == entry)
236             update[i]->forward[i] = entry->forward[i];
237     }
238
239     entry->magic = SL_FREED_MAGIC;
240     SL_FREE(entry);
241
242     while (list->level && !list->head->forward[list->level]) --list->level;
243     --list->count;
244     return 0;
245 }
246
247 int drmSLLookup(void *l, unsigned long key, void **value)
248 {
249     SkipListPtr   list = (SkipListPtr)l;
250     SLEntryPtr    update[SL_MAX_LEVEL + 1];
251     SLEntryPtr    entry;
252
253     entry = SLLocate(list, key, update);
254
255     if (entry && entry->key == key) {
256         *value = entry;
257         return 0;
258     }
259     *value = NULL;
260     return -1;
261 }
262
263 int drmSLLookupNeighbors(void *l, unsigned long key,
264                          unsigned long *prev_key, void **prev_value,
265                          unsigned long *next_key, void **next_value)
266 {
267     SkipListPtr   list = (SkipListPtr)l;
268     SLEntryPtr    update[SL_MAX_LEVEL + 1];
269     SLEntryPtr    entry;
270     int           retcode = 0;
271
272     entry = SLLocate(list, key, update);
273
274     *prev_key   = *next_key   = key;
275     *prev_value = *next_value = NULL;
276         
277     if (update[0]) {
278         *prev_key   = update[0]->key;
279         *prev_value = update[0]->value;
280         ++retcode;
281         if (update[0]->forward[0]) {
282             *next_key   = update[0]->forward[0]->key;
283             *next_value = update[0]->forward[0]->value;
284             ++retcode;
285         }
286     }
287     return retcode;
288 }
289
290 int drmSLNext(void *l, unsigned long *key, void **value)
291 {
292     SkipListPtr   list = (SkipListPtr)l;
293     SLEntryPtr    entry;
294     
295     if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
296
297     entry    = list->p0;
298
299     if (entry) {
300         list->p0 = entry->forward[0];
301         *key     = entry->key;
302         *value   = entry->value;
303         return 1;
304     }
305     list->p0 = NULL;
306     return 0;
307 }
308
309 int drmSLFirst(void *l, unsigned long *key, void **value)
310 {
311     SkipListPtr   list = (SkipListPtr)l;
312     
313     if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
314     
315     list->p0 = list->head->forward[0];
316     return drmSLNext(list, key, value);
317 }
318
319 /* Dump internal data structures for debugging. */
320 void drmSLDump(void *l)
321 {
322     SkipListPtr   list = (SkipListPtr)l;
323     SLEntryPtr    entry;
324     int           i;
325     
326     if (list->magic != SL_LIST_MAGIC) {
327         printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
328                list->magic, SL_LIST_MAGIC);
329         return;
330     }
331
332     printf("Level = %d, count = %d\n", list->level, list->count);
333     for (entry = list->head; entry; entry = entry->forward[0]) {
334         if (entry->magic != SL_ENTRY_MAGIC) {
335             printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
336                    list->magic, SL_ENTRY_MAGIC);
337         }
338         printf("\nEntry %p <0x%08lx, %p> has %2d levels\n",
339                entry, entry->key, entry->value, entry->levels);
340         for (i = 0; i < entry->levels; i++) {
341             if (entry->forward[i]) {
342                 printf("   %2d: %p <0x%08lx, %p>\n",
343                        i,
344                        entry->forward[i],
345                        entry->forward[i]->key,
346                        entry->forward[i]->value);
347             } else {
348                 printf("   %2d: %p\n", i, entry->forward[i]);
349             }
350         }
351     }
352 }
353
354 #if SL_MAIN
355 static void print(SkipListPtr list)
356 {
357     unsigned long key;
358     void          *value;
359     
360     if (drmSLFirst(list, &key, &value)) {
361         do {
362             printf("key = %5lu, value = %p\n", key, value);
363         } while (drmSLNext(list, &key, &value));
364     }
365 }
366
367 static double do_time(int size, int iter)
368 {
369     SkipListPtr    list;
370     int            i, j;
371     unsigned long  keys[1000000];
372     unsigned long  previous;
373     unsigned long  key;
374     void           *value;
375     struct timeval start, stop;
376     double         usec;
377     SL_RANDOM_DECL;
378
379     SL_RANDOM_INIT(12345);
380     
381     list = drmSLCreate();
382
383     for (i = 0; i < size; i++) {
384         keys[i] = SL_RANDOM;
385         drmSLInsert(list, keys[i], NULL);
386     }
387
388     previous = 0;
389     if (drmSLFirst(list, &key, &value)) {
390         do {
391             if (key <= previous) {
392                 printf( "%lu !< %lu\n", previous, key);
393             }
394             previous = key;
395         } while (drmSLNext(list, &key, &value));
396     }
397     
398     gettimeofday(&start, NULL);
399     for (j = 0; j < iter; j++) {
400         for (i = 0; i < size; i++) {
401             if (drmSLLookup(list, keys[i], &value))
402                 printf("Error %lu %d\n", keys[i], i);
403         }
404     }
405     gettimeofday(&stop, NULL);
406     
407     usec = (double)(stop.tv_sec * 1000000 + stop.tv_usec
408                     - start.tv_sec * 1000000 - start.tv_usec) / (size * iter);
409     
410     printf("%0.2f microseconds for list length %d\n", usec, size);
411
412     drmSLDestroy(list);
413     
414     return usec;
415 }
416
417 static void print_neighbors(void *list, unsigned long key)
418 {
419     unsigned long prev_key = 0;
420     unsigned long next_key = 0;
421     void          *prev_value;
422     void          *next_value;
423     int           retval;
424
425     retval = drmSLLookupNeighbors(list, key,
426                                   &prev_key, &prev_value,
427                                   &next_key, &next_value);
428     printf("Neighbors of %5lu: %d %5lu %5lu\n",
429            key, retval, prev_key, next_key);
430 }
431
432 int main(void)
433 {
434     SkipListPtr    list;
435     double         usec, usec2, usec3, usec4;
436
437     list = drmSLCreate();
438     printf( "list at %p\n", list);
439
440     print(list);
441     printf("\n==============================\n\n");
442
443     drmSLInsert(list, 123, NULL);
444     drmSLInsert(list, 213, NULL);
445     drmSLInsert(list, 50, NULL);
446     print(list);
447     printf("\n==============================\n\n");
448     
449     print_neighbors(list, 0);
450     print_neighbors(list, 50);
451     print_neighbors(list, 51);
452     print_neighbors(list, 123);
453     print_neighbors(list, 200);
454     print_neighbors(list, 213);
455     print_neighbors(list, 256);
456     printf("\n==============================\n\n");    
457     
458     drmSLDelete(list, 50);
459     print(list);
460     printf("\n==============================\n\n");
461
462     drmSLDump(list);
463     drmSLDestroy(list);
464     printf("\n==============================\n\n");
465
466     usec  = do_time(100, 10000);
467     usec2 = do_time(1000, 500);
468     printf("Table size increased by %0.2f, search time increased by %0.2f\n",
469            1000.0/100.0, usec2 / usec);
470     
471     usec3 = do_time(10000, 50);
472     printf("Table size increased by %0.2f, search time increased by %0.2f\n",
473            10000.0/100.0, usec3 / usec);
474     
475     usec4 = do_time(100000, 4);
476     printf("Table size increased by %0.2f, search time increased by %0.2f\n",
477            100000.0/100.0, usec4 / usec);
478
479     return 0;
480 }
481 #endif