tpl_wayland_egl_thread: Added an exception checking to prevent blocking issue.
[platform/core/uifw/libtpl-egl.git] / src / tpl_utils_map.c
1 #include "tpl_internal.h"
2
3 struct tpl_util_map_entry {
4         tpl_util_key_t key;
5         void *data;
6         tpl_free_func_t free_func;
7         tpl_util_map_entry_t *next;
8 };
9
10 static inline int
11 __get_bucket_index(tpl_util_map_t *map, const tpl_util_key_t key)
12 {
13         int key_length = 0;
14         int hash;
15
16         if (map->key_length_func) key_length = map->key_length_func(key);
17
18         hash = map->hash_func(key, key_length);
19         return hash & map->bucket_mask;
20 }
21
22 static inline tpl_util_map_entry_t **
23 __get_bucket(tpl_util_map_t *map, const tpl_util_key_t key)
24 {
25         return &map->buckets[__get_bucket_index(map, key)];
26 }
27
28 static int
29 __int64_hash(const tpl_util_key_t key, int key_length)
30 {
31         uint64_t _key = key.key64;
32
33         /* Hash functions from Thomas Wang https://gist.github.com/badboy/6267743 */
34         _key  = ~_key + (_key << 18);
35         _key ^= _key >> 31;
36         _key *= 21;
37         _key ^= _key >> 11;
38         _key += _key << 6;
39         _key ^= _key >> 22;
40
41         return (int)_key;;
42 }
43
44 static int
45 __int64_key_compare(const tpl_util_key_t key0, int key0_length,
46                                         const tpl_util_key_t key1, int key1_length)
47 {
48         return (int)(key0.key64 - key1.key64);
49 }
50
51 static int
52 __int32_hash(const tpl_util_key_t key, int key_length)
53 {
54         uint32_t _key = (uint32_t)key.key32;
55
56         /* Hash functions from Thomas Wang https://gist.github.com/badboy/6267743 */
57         _key  = ~_key + (_key << 15);
58         _key ^= _key >> 12;
59         _key += _key << 2;
60         _key ^= _key >> 4;
61         _key *= 2057;
62         _key ^= _key >> 16;
63
64         return (int)_key;
65 }
66
67 static int
68 __int32_key_compare(const tpl_util_key_t key0, int key0_length,
69                                         const tpl_util_key_t key1, int key1_length)
70 {
71         return (int)(key0.key32 - key1.key32);
72 }
73
74 static int
75 __pointer_hash(const tpl_util_key_t key, int key_length)
76 {
77 #if INTPTR_MAX == INT32_MAX
78         uint32_t _key = (uint32_t)key.ptr;
79
80         _key  = ~_key + (_key << 15);
81         _key ^= _key >> 12;
82         _key += _key << 2;
83         _key ^= _key >> 4;
84         _key *= 2057;
85         _key ^= _key >> 16;
86
87         return (int)_key;
88 #elif INTPTR_MAX == INT64_MAX
89         uint64_t _key = (uint64_t)key.ptr;
90
91         _key  = ~_key + (_key << 18);
92         _key ^= _key >> 31;
93         _key *= 21;
94         _key ^= _key >> 11;
95         _key += _key << 6;
96         _key ^= _key >> 22;
97
98         return (int)_key;
99 #else
100 #error "Not 32 or 64bit system"
101 #endif
102
103         return 0;
104 }
105
106 static int
107 __pointer_key_compare(const tpl_util_key_t key0, int key0_length,
108                                           const tpl_util_key_t key1, int key1_length)
109 {
110         return (int)(key0.ptr - key1.ptr);
111 }
112
113 void
114 tpl_util_map_init(tpl_util_map_t *map, int bucket_bits,
115                                   tpl_util_hash_func_t hash_func,
116                                   tpl_util_key_length_func_t key_length_func,
117                                   tpl_util_key_compare_func_t key_compare_func,
118                                   void *buckets)
119 {
120         map->hash_func = hash_func;
121         map->key_length_func = key_length_func;
122         map->key_compare_func = key_compare_func;
123
124         map->bucket_bits = bucket_bits;
125         map->bucket_size = 1 << bucket_bits;
126         map->bucket_mask = map->bucket_size - 1;
127
128         map->buckets = buckets;
129 }
130
131 void
132 tpl_util_map_int32_init(tpl_util_map_t *map, int bucket_bits, void *buckets)
133 {
134         tpl_util_map_init(map, bucket_bits, __int32_hash, NULL,
135                                           __int32_key_compare, buckets);
136 }
137
138 void
139 tpl_util_map_int64_init(tpl_util_map_t *map, int bucket_bits, void *buckets)
140 {
141         tpl_util_map_init(map, bucket_bits, __int64_hash, NULL,
142                                           __int64_key_compare, buckets);
143 }
144
145 void
146 tpl_util_map_pointer_init(tpl_util_map_t *map, int bucket_bits, void *buckets)
147 {
148         tpl_util_map_init(map, bucket_bits, __pointer_hash, NULL,
149                                           __pointer_key_compare, buckets);
150 }
151
152 void
153 tpl_util_map_fini(tpl_util_map_t *map)
154 {
155         tpl_util_map_clear(map);
156 }
157
158 tpl_util_map_t *
159 tpl_util_map_create(int bucket_bits, tpl_util_hash_func_t hash_func,
160                                         tpl_util_key_length_func_t  key_length_func,
161                                         tpl_util_key_compare_func_t key_compare_func)
162 {
163         tpl_util_map_t *map;
164         int bucket_size = 1 << bucket_bits;
165
166         map = calloc(1,
167                                  sizeof(tpl_util_map_t) + bucket_size * sizeof(tpl_util_map_entry_t *));
168         TPL_CHECK_ON_FALSE_RETURN_VAL(map, NULL);
169
170         tpl_util_map_init(map, bucket_bits, hash_func, key_length_func,
171                                           key_compare_func, map + 1);
172
173         return map;
174 }
175
176 tpl_util_map_t *
177 tpl_util_map_int32_create(int bucket_bits)
178 {
179         return tpl_util_map_create(bucket_bits, __int32_hash, NULL,
180                                                            __int32_key_compare);
181 }
182
183 tpl_util_map_t *
184 tpl_util_map_int64_create(int bucket_bits)
185 {
186         return tpl_util_map_create(bucket_bits, __int64_hash, NULL,
187                                                            __int64_key_compare);
188 }
189
190 tpl_util_map_t *
191 tpl_util_map_pointer_create(int bucket_bits)
192 {
193         return tpl_util_map_create(bucket_bits, __pointer_hash, NULL,
194                                                            __pointer_key_compare);
195 }
196
197 void
198 tpl_util_map_destroy(tpl_util_map_t *map)
199 {
200         tpl_util_map_fini(map);
201         free(map);
202 }
203
204 void
205 tpl_util_map_clear(tpl_util_map_t *map)
206 {
207         int i;
208
209         if (!map->buckets) return;
210
211         for (i = 0; i < map->bucket_size; i++) {
212                 tpl_util_map_entry_t *curr = map->buckets[i];
213
214                 while (curr) {
215                         tpl_util_map_entry_t *next = curr->next;
216
217                         if (curr->free_func) curr->free_func(curr->data);
218
219                         free(curr);
220                         curr = next;
221                 }
222         }
223
224         memset(map->buckets, 0x00, map->bucket_size * sizeof(tpl_util_map_entry_t *));
225 }
226
227 void *
228 tpl_util_map_get(tpl_util_map_t *map, const tpl_util_key_t key)
229 {
230         tpl_util_map_entry_t *curr = *__get_bucket(map, key);
231
232         while (curr) {
233                 int len0 = 0;
234                 int len1 = 0;
235
236                 if (map->key_length_func) {
237                         len0 = map->key_length_func(curr->key);
238                         len1 = map->key_length_func(key);
239                 }
240
241                 if (map->key_compare_func(curr->key, len0, key, len1) == 0)
242                         return curr->data;
243
244                 curr = curr->next;
245         }
246
247         return NULL;
248 }
249
250 void
251 tpl_util_map_set(tpl_util_map_t *map, const tpl_util_key_t key, void *data,
252                                  tpl_free_func_t free_func)
253 {
254         tpl_util_map_entry_t **bucket = __get_bucket(map, key);
255         tpl_util_map_entry_t *curr = *bucket;
256         tpl_util_map_entry_t *prev = NULL;
257         int key_length = 0;
258
259         /* Find existing entry for the key. */
260         while (curr) {
261                 int len0 = 0;
262                 int len1 = 0;
263
264                 if (map->key_length_func) {
265                         len0 = map->key_length_func(curr->key);
266                         len1 = map->key_length_func(key);
267                 }
268
269                 if (map->key_compare_func(curr->key, len0, key, len1) == 0) {
270                         /* Free previous data. */
271                         if (curr->free_func) curr->free_func(curr->data);
272
273                         if (data) {
274                                 /* Set new data. */
275                                 curr->data = data;
276                                 curr->free_func = free_func;
277                         } else {
278                                 /* Delete entry. */
279                                 if (prev) prev->next = curr->next;
280                                 else *bucket = curr->next;
281
282                                 free(curr);
283                         }
284
285                         return;
286                 }
287
288                 prev = curr;
289                 curr = curr->next;
290         }
291
292         if (!data) {
293                 /* Nothing to delete. */
294                 return;
295         }
296
297         /* Allocate a new entry. */
298         if (map->key_length_func) key_length = map->key_length_func(key);
299
300         curr = malloc(sizeof(tpl_util_map_entry_t) + key_length);
301         TPL_CHECK_ON_FALSE_RETURN(curr);
302
303         if (key_length > 0) {
304                 memcpy(curr + 1, key.ptr, key_length);
305                 curr->key.ptr = (void *)(curr + 1);
306         } else {
307                 curr->key = key;
308         }
309
310         curr->data = data;
311         curr->free_func = free_func;
312
313         /* Insert at the head of the bucket. */
314         curr->next = *bucket;
315         *bucket = curr;
316 }