| 1 |
/* |
| 2 |
* Copyright © 2000 Keith Packard |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 5 |
* documentation for any purpose is hereby granted without fee, provided that |
| 6 |
* the above copyright notice appear in all copies and that both that |
| 7 |
* copyright notice and this permission notice appear in supporting |
| 8 |
* documentation, and that the name of the author(s) not be used in |
| 9 |
* advertising or publicity pertaining to distribution of the software without |
| 10 |
* specific, written prior permission. The authors make no |
| 11 |
* representations about the suitability of this software for any purpose. It |
| 12 |
* is provided "as is" without express or implied warranty. |
| 13 |
* |
| 14 |
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 |
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 20 |
* PERFORMANCE OF THIS SOFTWARE. |
| 21 |
*/ |
| 22 |
#include "fcint.h" |
| 23 |
|
| 24 |
#define FC_HASH_SIZE 227 |
| 25 |
|
| 26 |
typedef struct _FcHashBucket { |
| 27 |
struct _FcHashBucket *next; |
| 28 |
void *key; |
| 29 |
void *value; |
| 30 |
} FcHashBucket; |
| 31 |
|
| 32 |
struct _FcHashTable { |
| 33 |
FcHashBucket *buckets[FC_HASH_SIZE]; |
| 34 |
FcHashFunc hash_func; |
| 35 |
FcCompareFunc compare_func; |
| 36 |
FcCopyFunc key_copy_func; |
| 37 |
FcCopyFunc value_copy_func; |
| 38 |
FcDestroyFunc key_destroy_func; |
| 39 |
FcDestroyFunc value_destroy_func; |
| 40 |
}; |
| 41 |
|
| 42 |
FcBool |
| 43 |
FcHashStrCopy (const void *src, |
| 44 |
void **dest) |
| 45 |
{ |
| 46 |
*dest = FcStrdup (src); |
| 47 |
|
| 48 |
return *dest != NULL; |
| 49 |
} |
| 50 |
|
| 51 |
FcHashTable * |
| 52 |
FcHashTableCreate (FcHashFunc hash_func, |
| 53 |
FcCompareFunc compare_func, |
| 54 |
FcCopyFunc key_copy_func, |
| 55 |
FcCopyFunc value_copy_func, |
| 56 |
FcDestroyFunc key_destroy_func, |
| 57 |
FcDestroyFunc value_destroy_func) |
| 58 |
{ |
| 59 |
FcHashTable *ret = malloc (sizeof (FcHashTable)); |
| 60 |
|
| 61 |
if (ret) { |
| 62 |
memset (ret->buckets, 0, sizeof (FcHashBucket *) * FC_HASH_SIZE); |
| 63 |
ret->hash_func = hash_func; |
| 64 |
ret->compare_func = compare_func; |
| 65 |
ret->key_copy_func = key_copy_func; |
| 66 |
ret->value_copy_func = value_copy_func; |
| 67 |
ret->key_destroy_func = key_destroy_func; |
| 68 |
ret->value_destroy_func = value_destroy_func; |
| 69 |
} |
| 70 |
return ret; |
| 71 |
} |
| 72 |
|
| 73 |
void |
| 74 |
FcHashTableDestroy (FcHashTable *table) |
| 75 |
{ |
| 76 |
int i; |
| 77 |
|
| 78 |
for (i = 0; i < FC_HASH_SIZE; i++) { |
| 79 |
FcHashBucket *bucket = table->buckets[i], *prev; |
| 80 |
|
| 81 |
while (bucket) { |
| 82 |
if (table->key_destroy_func) |
| 83 |
table->key_destroy_func (bucket->key); |
| 84 |
if (table->value_destroy_func) |
| 85 |
table->value_destroy_func (bucket->value); |
| 86 |
prev = bucket; |
| 87 |
bucket = bucket->next; |
| 88 |
free (prev); |
| 89 |
} |
| 90 |
table->buckets[i] = NULL; |
| 91 |
} |
| 92 |
free (table); |
| 93 |
} |
| 94 |
|
| 95 |
FcBool |
| 96 |
FcHashTableFind (FcHashTable *table, |
| 97 |
const void *key, |
| 98 |
void **value) |
| 99 |
{ |
| 100 |
FcHashBucket *bucket; |
| 101 |
FcChar32 hash = table->hash_func (key); |
| 102 |
|
| 103 |
for (bucket = table->buckets[hash % FC_HASH_SIZE]; bucket; bucket = bucket->next) { |
| 104 |
if (!table->compare_func (bucket->key, key)) { |
| 105 |
if (table->value_copy_func) { |
| 106 |
if (!table->value_copy_func (bucket->value, value)) |
| 107 |
return FcFalse; |
| 108 |
} else |
| 109 |
*value = bucket->value; |
| 110 |
return FcTrue; |
| 111 |
} |
| 112 |
} |
| 113 |
return FcFalse; |
| 114 |
} |
| 115 |
|
| 116 |
static FcBool |
| 117 |
FcHashTableAddInternal (FcHashTable *table, |
| 118 |
void *key, |
| 119 |
void *value, |
| 120 |
FcBool replace) |
| 121 |
{ |
| 122 |
FcHashBucket **prev, *bucket, *b; |
| 123 |
FcChar32 hash = table->hash_func (key); |
| 124 |
FcBool ret = FcFalse; |
| 125 |
|
| 126 |
bucket = (FcHashBucket *)malloc (sizeof (FcHashBucket)); |
| 127 |
if (!bucket) |
| 128 |
return FcFalse; |
| 129 |
memset (bucket, 0, sizeof (FcHashBucket)); |
| 130 |
if (table->key_copy_func) |
| 131 |
ret |= !table->key_copy_func (key, &bucket->key); |
| 132 |
else |
| 133 |
bucket->key = key; |
| 134 |
if (table->value_copy_func) |
| 135 |
ret |= !table->value_copy_func (value, &bucket->value); |
| 136 |
else |
| 137 |
bucket->value = value; |
| 138 |
if (ret) { |
| 139 |
destroy: |
| 140 |
if (bucket->key && table->key_destroy_func) |
| 141 |
table->key_destroy_func (bucket->key); |
| 142 |
if (bucket->value && table->value_destroy_func) |
| 143 |
table->value_destroy_func (bucket->value); |
| 144 |
free (bucket); |
| 145 |
|
| 146 |
return !ret; |
| 147 |
} |
| 148 |
retry: |
| 149 |
for (prev = &table->buckets[hash % FC_HASH_SIZE]; |
| 150 |
(b = fc_atomic_ptr_get (prev)); prev = &(b->next)) { |
| 151 |
if (!table->compare_func (b->key, key)) { |
| 152 |
if (replace) { |
| 153 |
bucket->next = b->next; |
| 154 |
if (!fc_atomic_ptr_cmpexch (prev, b, bucket)) |
| 155 |
goto retry; |
| 156 |
bucket = b; |
| 157 |
} else |
| 158 |
ret = FcTrue; |
| 159 |
goto destroy; |
| 160 |
} |
| 161 |
} |
| 162 |
bucket->next = NULL; |
| 163 |
if (!fc_atomic_ptr_cmpexch (prev, b, bucket)) |
| 164 |
goto retry; |
| 165 |
|
| 166 |
return FcTrue; |
| 167 |
} |
| 168 |
|
| 169 |
FcBool |
| 170 |
FcHashTableAdd (FcHashTable *table, |
| 171 |
void *key, |
| 172 |
void *value) |
| 173 |
{ |
| 174 |
return FcHashTableAddInternal (table, key, value, FcFalse); |
| 175 |
} |
| 176 |
|
| 177 |
FcBool |
| 178 |
FcHashTableReplace (FcHashTable *table, |
| 179 |
void *key, |
| 180 |
void *value) |
| 181 |
{ |
| 182 |
return FcHashTableAddInternal (table, key, value, FcTrue); |
| 183 |
} |
| 184 |
|
| 185 |
FcBool |
| 186 |
FcHashTableRemove (FcHashTable *table, |
| 187 |
void *key) |
| 188 |
{ |
| 189 |
FcHashBucket **prev, *bucket; |
| 190 |
FcChar32 hash = table->hash_func (key); |
| 191 |
FcBool ret = FcFalse; |
| 192 |
|
| 193 |
retry: |
| 194 |
for (prev = &table->buckets[hash % FC_HASH_SIZE]; |
| 195 |
(bucket = fc_atomic_ptr_get (prev)); prev = &(bucket->next)) { |
| 196 |
if (!table->compare_func (bucket->key, key)) { |
| 197 |
if (!fc_atomic_ptr_cmpexch (prev, bucket, bucket->next)) |
| 198 |
goto retry; |
| 199 |
if (table->key_destroy_func) |
| 200 |
table->key_destroy_func (bucket->key); |
| 201 |
if (table->value_destroy_func) |
| 202 |
table->value_destroy_func (bucket->value); |
| 203 |
free (bucket); |
| 204 |
ret = FcTrue; |
| 205 |
break; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return ret; |
| 210 |
} |