summaryrefslogtreecommitdiff
path: root/xserver/Xext/hashtable.c
blob: 3434c994877cd9da0f09462dce6e5d065f782f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <stdlib.h>
#include "misc.h"
#include "hashtable.h"

/* HashResourceID */
#include "resource.h"

#define INITHASHSIZE 6
#define MAXHASHSIZE 11

struct HashTableRec {
    int             keySize;
    int             dataSize;

    int             elements;   /* number of elements inserted */
    int             bucketBits; /* number of buckets is 1 << bucketBits */
    struct xorg_list *buckets;  /* array of bucket list heads */

    HashFunc        hash;
    HashCompareFunc compare;

    void            *cdata;
};

typedef struct {
    struct xorg_list l;
    void *key;
    void *data;
} BucketRec, *BucketPtr;

HashTable
ht_create(int             keySize,
          int             dataSize,
          HashFunc        hash,
          HashCompareFunc compare,
          void            *cdata)
{
    int c;
    int numBuckets;
    HashTable ht = malloc(sizeof(struct HashTableRec));

    if (!ht) {
        return NULL;
    }

    ht->keySize = keySize;
    ht->dataSize = dataSize;
    ht->hash = hash;
    ht->compare = compare;
    ht->elements = 0;
    ht->bucketBits = INITHASHSIZE;
    numBuckets = 1 << ht->bucketBits;
    ht->buckets = xallocarray(numBuckets, sizeof(*ht->buckets));
    ht->cdata = cdata;

    if (ht->buckets) {
        for (c = 0; c < numBuckets; ++c) {
            xorg_list_init(&ht->buckets[c]);
        }
        return ht;
    } else {
        free(ht);
        return NULL;
    }
}

void
ht_destroy(HashTable ht)
{
    int c;
    BucketPtr it, tmp;
    int numBuckets = 1 << ht->bucketBits;
    for (c = 0; c < numBuckets; ++c) {
        xorg_list_for_each_entry_safe(it, tmp, &ht->buckets[c], l) {
            xorg_list_del(&it->l);
            free(it->key);
            free(it->data);
            free(it);
        }
    }
    free(ht->buckets);
    free(ht);
}

static Bool
double_size(HashTable ht)
{
    struct xorg_list *newBuckets;
    int numBuckets = 1 << ht->bucketBits;
    int newBucketBits = ht->bucketBits + 1;
    int newNumBuckets = 1 << newBucketBits;
    int c;

    newBuckets = xallocarray(newNumBuckets, sizeof(*ht->buckets));
    if (newBuckets) {
        for (c = 0; c < newNumBuckets; ++c) {
            xorg_list_init(&newBuckets[c]);
        }

        for (c = 0; c < numBuckets; ++c) {
            BucketPtr it, tmp;
            xorg_list_for_each_entry_safe(it, tmp, &ht->buckets[c], l) {
                struct xorg_list *newBucket =
                    &newBuckets[ht->hash(ht->cdata, it->key, newBucketBits)];
                xorg_list_del(&it->l);
                xorg_list_add(&it->l, newBucket);
            }
        }
        free(ht->buckets);

        ht->buckets = newBuckets;
        ht->bucketBits = newBucketBits;
        return TRUE;
    } else {
        return FALSE;
    }
}

void *
ht_add(HashTable ht, const void *key)
{
    unsigned index = ht->hash(ht->cdata, key, ht->bucketBits);
    struct xorg_list *bucket = &ht->buckets[index];
    BucketRec *elem = calloc(1, sizeof(BucketRec));
    if (!elem) {
        goto outOfMemory;
    }
    elem->key = malloc(ht->keySize);
    if (!elem->key) {
        goto outOfMemory;
    }
    /* we avoid signaling an out-of-memory error if dataSize is 0 */
    elem->data = calloc(1, ht->dataSize);
    if (ht->dataSize && !elem->data) {
        goto outOfMemory;
    }
    xorg_list_add(&elem->l, bucket);
    ++ht->elements;

    memcpy(elem->key, key, ht->keySize);

    if (ht->elements > 4 * (1 << ht->bucketBits) &&
        ht->bucketBits < MAXHASHSIZE) {
        if (!double_size(ht)) {
            --ht->elements;
            xorg_list_del(&elem->l);
            goto outOfMemory;
        }
    }

    /* if memory allocation has failed due to dataSize being 0, return
       a "dummy" pointer pointing at the of the key */
    return elem->data ? elem->data : ((char*) elem->key + ht->keySize);

 outOfMemory:
    if (elem) {
        free(elem->key);
        free(elem->data);
        free(elem);
    }

    return NULL;
}

void
ht_remove(HashTable ht, const void *key)
{
    unsigned index = ht->hash(ht->cdata, key, ht->bucketBits);
    struct xorg_list *bucket = &ht->buckets[index];
    BucketPtr it;

    xorg_list_for_each_entry(it, bucket, l) {
        if (ht->compare(ht->cdata, key, it->key) == 0) {
            xorg_list_del(&it->l);
            --ht->elements;
            free(it->key);
            free(it->data);
            free(it);
            return;
        }
    }
}

void *
ht_find(HashTable ht, const void *key)
{
    unsigned index = ht->hash(ht->cdata, key, ht->bucketBits);
    struct xorg_list *bucket = &ht->buckets[index];
    BucketPtr it;

    xorg_list_for_each_entry(it, bucket, l) {
        if (ht->compare(ht->cdata, key, it->key) == 0) {
            return it->data ? it->data : ((char*) it->key + ht->keySize);
        }
    }

    return NULL;
}

void
ht_dump_distribution(HashTable ht)
{
    int c;
    int numBuckets = 1 << ht->bucketBits;
    for (c = 0; c < numBuckets; ++c) {
        BucketPtr it;
        int n = 0;

        xorg_list_for_each_entry(it, &ht->buckets[c], l) {
            ++n;
        }
        printf("%d: %d\n", c, n);
    }
}

/* Picked the function from http://burtleburtle.net/bob/hash/doobs.html by
   Bob Jenkins, which is released in public domain */
static CARD32
one_at_a_time_hash(const void *data, int len)
{
    CARD32 hash;
    int i;
    const char *key = data;
    for (hash=0, i=0; i<len; ++i) {
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

unsigned
ht_generic_hash(void *cdata, const void *ptr, int numBits)
{
    HtGenericHashSetupPtr setup = cdata;
    return one_at_a_time_hash(ptr, setup->keySize) & ~((~0U) << numBits);
}

int
ht_generic_compare(void *cdata, const void *l, const void *r)
{
    HtGenericHashSetupPtr setup = cdata;
    return memcmp(l, r, setup->keySize);
}

unsigned
ht_resourceid_hash(void * cdata, const void * data, int numBits)
{
    const XID* idPtr = data;
    XID id = *idPtr & RESOURCE_ID_MASK;
    (void) cdata;
    return HashResourceID(id, numBits);
}

int
ht_resourceid_compare(void* cdata, const void* a, const void* b)
{
    const XID* xa = a;
    const XID* xb = b;
    (void) cdata;
    return
        *xa < *xb ? -1 :
        *xa > *xb ? 1 :
        0;
}

void
ht_dump_contents(HashTable ht,
                 void (*print_key)(void *opaque, void *key),
                 void (*print_value)(void *opaque, void *value),
                 void* opaque)
{
    int c;
    int numBuckets = 1 << ht->bucketBits;
    for (c = 0; c < numBuckets; ++c) {
        BucketPtr it;
        int n = 0;

        printf("%d: ", c);
        xorg_list_for_each_entry(it, &ht->buckets[c], l) {
            if (n > 0) {
                printf(", ");
            }
            print_key(opaque, it->key);
            printf("->");
            print_value(opaque, it->data);
            ++n;
        }
        printf("\n");
    }
}