eet: make eet_dictionary thread safe.
[framework/uifw/eet.git] / src / lib / eet_dictionary.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif /* ifdef HAVE_CONFIG_H */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <math.h>
8
9 #include <Eina.h>
10
11 #include "Eet.h"
12 #include "Eet_private.h"
13
14 Eet_Dictionary *
15 eet_dictionary_add(void)
16 {
17    Eet_Dictionary *new;
18
19    new = eet_dictionary_calloc(1);
20    if (!new)
21      return NULL;
22
23    memset(new->hash, -1, sizeof (int) * 256);
24    eina_lock_new(&new->mutex);
25
26    return new;
27 }
28
29 void
30 eet_dictionary_free(Eet_Dictionary *ed)
31 {
32    int i;
33
34    if (!ed) return;
35
36    eina_lock_free(&ed->mutex);
37
38    for (i = 0; i < ed->count; ++i)
39      if (ed->all[i].allocated)
40        eina_stringshare_del(ed->all[i].str);
41
42    if (ed->all)
43      free(ed->all);
44
45    if (ed->converts) eina_hash_free(ed->converts);
46
47    eet_dictionary_mp_free(ed);
48 }
49
50 static int
51 _eet_dictionary_lookup(Eet_Dictionary *ed,
52                        const char     *string,
53                        int             len,
54                        int             hash)
55 {
56    Eina_Bool found = EINA_FALSE;
57    int prev = -1;
58    int current;
59
60    current = ed->hash[hash];
61
62    while (current != -1)
63      {
64         if (ed->all[current].len == len)
65           {
66              if (ed->all[current].str &&
67                  (ed->all[current].str == string || strcmp(ed->all[current].str, string) == 0))
68                {
69                   found = EINA_TRUE;
70                   break;
71                }
72           }
73
74         prev = current;
75         current = ed->all[current].next;
76      }
77
78    if (current == -1 && found)
79      return prev;
80
81    return current;
82 }
83
84 int
85 eet_dictionary_string_add(Eet_Dictionary *ed,
86                           const char     *string)
87 {
88    Eet_String *current;
89    const char *str;
90    int hash;
91    int idx;
92    int len;
93
94    if (!ed)
95      return -1;
96
97    hash = _eet_hash_gen(string, 8);
98    len = strlen(string) + 1;
99
100    eina_lock_take(&ed->mutex);
101
102    idx = _eet_dictionary_lookup(ed, string, len, hash);
103
104    if (idx != -1)
105      {
106         if (ed->all[idx].str && (ed->all[idx].str == string || strcmp(ed->all[idx].str, string) == 0))
107           {
108             eina_lock_release(&ed->mutex);
109             return idx;
110           }
111      }
112
113    if (ed->total == ed->count)
114      {
115         Eet_String *new;
116         int total;
117
118         total = ed->total + 8;
119
120         new = realloc(ed->all, total * sizeof(Eet_String));
121         if (!new) goto on_error;
122
123         ed->all = new;
124         ed->total = total;
125      }
126
127    str = eina_stringshare_add(string);
128    if (!str) goto on_error;
129
130    current = ed->all + ed->count;
131
132    current->allocated = EINA_TRUE;
133
134    current->hash = hash;
135
136    current->str = str;
137    current->len = len;
138
139    if (idx == -1)
140      {
141         current->next = ed->hash[hash];
142         current->prev = -1;
143         ed->hash[hash] = ed->count;
144      }
145    else
146      {
147         current->next = idx;
148         current->prev = ed->all[idx].prev;
149
150         if (current->next != -1)
151           ed->all[current->next].prev = ed->count;
152
153         if (current->prev != -1)
154           ed->all[current->prev].next = ed->count;
155         else
156           ed->hash[hash] = ed->count;
157      }
158
159    eina_lock_release(&ed->mutex);
160    return ed->count++;
161
162  on_error:
163    eina_lock_release(&ed->mutex);
164    return -1;
165 }
166
167 int
168 eet_dictionary_string_get_size(const Eet_Dictionary *ed,
169                                int                   idx)
170 {
171    int length = 0;
172
173    if (!ed) goto done;
174
175    if (idx < 0) goto done;
176
177    eina_lock_take((Eina_Lock*) &ed->mutex);
178
179    if (idx < ed->count)
180      length = ed->all[idx].len;
181
182    eina_lock_release((Eina_Lock*) &ed->mutex);
183
184  done:
185    return length;
186 }
187
188 EAPI int
189 eet_dictionary_count(const Eet_Dictionary *ed)
190 {
191   return ed->count;
192 }
193
194 int
195 eet_dictionary_string_get_hash(const Eet_Dictionary *ed,
196                                int                   idx)
197 {
198    int hash = -1;
199
200    if (!ed) goto done;
201
202    if (idx < 0) goto done;
203
204    eina_lock_take((Eina_Lock*) &ed->mutex);
205
206    if (idx < ed->count)
207      hash = ed->all[idx].hash;
208
209    eina_lock_release((Eina_Lock*) &ed->mutex);
210
211  done:
212    return hash;
213 }
214
215 const char *
216 eet_dictionary_string_get_char(const Eet_Dictionary *ed,
217                                int                   idx)
218 {
219    const char *s = NULL;
220
221    if (!ed) goto done;
222
223    if (idx < 0) goto done;
224
225    eina_lock_take((Eina_Lock*) &ed->mutex);
226
227    if (idx < ed->count)
228      {
229 #ifdef _WIN32
230         /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */
231         if (!ed->all[idx].allocated)
232           {
233              ed->all[idx].str = eina_stringshare_add(ed->all[idx].str);
234              ed->all[idx].allocated = EINA_TRUE;
235           }
236 #endif /* ifdef _WIN32 */
237         s = ed->all[idx].str;
238      }
239
240    eina_lock_release((Eina_Lock*) &ed->mutex);
241
242  done:
243    return s;
244 }
245
246 static inline Eina_Bool
247 _eet_dictionary_string_get_me_cache(const char *s,
248                                     int         len,
249                                     int        *mantisse,
250                                     int        *exponent)
251 {
252    if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p'))
253      {
254         *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
255         *exponent = (s[5] - '0');
256
257         return EINA_TRUE;
258      }
259
260    return EINA_FALSE;
261 }
262
263 static inline Eina_Bool
264 _eet_dictionary_string_get_float_cache(const char *s,
265                                        int         len,
266                                        float      *result)
267 {
268    int mantisse;
269    int exponent;
270
271    if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
272      {
273         if (s[4] == '+')
274           *result = (float)(mantisse << exponent);
275         else
276           *result = (float)mantisse / (float)(1 << exponent);
277
278         return EINA_TRUE;
279      }
280
281    return EINA_FALSE;
282 }
283
284 static inline Eina_Bool
285 _eet_dictionary_string_get_double_cache(const char *s,
286                                         int         len,
287                                         double     *result)
288 {
289    int mantisse;
290    int exponent;
291
292    if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
293      {
294         if (s[4] == '+')
295           *result = (double)(mantisse << exponent);
296         else
297           *result = (double)mantisse / (float)(1 << exponent);
298
299         return EINA_TRUE;
300      }
301
302    return EINA_FALSE;
303 }
304
305 static inline Eina_Bool
306 _eet_dictionary_test(const Eet_Dictionary *ed,
307                      int                   idx,
308                      void                 *result)
309 {
310    Eina_Bool limit = EINA_FALSE;
311
312    if (!result) goto done;
313
314    if (!ed) goto done;
315
316    if (idx < 0) goto done;
317
318    eina_lock_take((Eina_Lock*) &ed->mutex);
319
320    if (!(idx < ed->count)) goto unlock_done;
321
322    limit = EINA_TRUE;
323
324  unlock_done:
325    eina_lock_release((Eina_Lock*) &ed->mutex);
326
327  done:
328    return limit;
329 }
330
331 static Eet_Convert *
332 eet_dictionary_convert_get(const Eet_Dictionary *ed,
333                            int                   idx,
334                            const char          **str)
335 {
336    Eet_Convert *result;
337
338    eina_lock_take((Eina_Lock*) &ed->mutex);
339
340    *str = ed->all[idx].str;
341
342    if (!ed->converts)
343      {
344         ((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free);
345
346         goto add_convert;
347      }
348
349    result = eina_hash_find(ed->converts, &idx);
350    if (result) goto done;
351
352  add_convert:
353    result = calloc(1, sizeof (Eet_Convert));
354
355    eina_hash_add(ed->converts, &idx, result);
356
357  done:
358    eina_lock_release((Eina_Lock*) &ed->mutex);
359
360    return result;
361 }
362
363 Eina_Bool
364 eet_dictionary_string_get_float(const Eet_Dictionary *ed,
365                                 int                   idx,
366                                 float                *result)
367 {
368    Eet_Convert *convert;
369    const char *str;
370
371    if (!_eet_dictionary_test(ed, idx, result))
372      return EINA_FALSE;
373
374    convert = eet_dictionary_convert_get(ed, idx, &str);
375    if (!convert) return EINA_FALSE;
376
377    if (!(convert->type & EET_D_FLOAT))
378      {
379         eina_lock_take((Eina_Lock*) &ed->mutex);
380         if (!_eet_dictionary_string_get_float_cache(str, ed->all[idx].len,
381                                                     &convert->f))
382           {
383              long long mantisse = 0;
384              long exponent = 0;
385
386              if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
387                                    &exponent) == EINA_FALSE)
388                {
389                   eina_lock_release((Eina_Lock*) &ed->mutex);
390                   return EINA_FALSE;
391                }
392
393              convert->f = ldexpf((float)mantisse, exponent);
394           }
395         eina_lock_release((Eina_Lock*) &ed->mutex);
396
397         convert->type |= EET_D_FLOAT;
398      }
399
400    *result = convert->f;
401    return EINA_TRUE;
402 }
403
404 Eina_Bool
405 eet_dictionary_string_get_double(const Eet_Dictionary *ed,
406                                  int                   idx,
407                                  double               *result)
408 {
409    Eet_Convert *convert;
410    const char *str;
411
412    if (!_eet_dictionary_test(ed, idx, result))
413      return EINA_FALSE;
414
415    convert = eet_dictionary_convert_get(ed, idx, &str);
416    if (!convert) return EINA_FALSE;
417
418    if (!(convert->type & EET_D_DOUBLE))
419      {
420         eina_lock_take((Eina_Lock*) &ed->mutex);
421
422         if (!_eet_dictionary_string_get_double_cache(str, ed->all[idx].len,
423                                                      &convert->d))
424           {
425              long long mantisse = 0;
426              long exponent = 0;
427
428              if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
429                                    &exponent) == EINA_FALSE)
430                {
431                   eina_lock_release((Eina_Lock*) &ed->mutex);
432                   return EINA_FALSE;
433                }
434
435              convert->d = ldexp((double)mantisse, exponent);
436           }
437         eina_lock_release((Eina_Lock*) &ed->mutex);
438
439         convert->type |= EET_D_DOUBLE;
440      }
441
442    *result = convert->d;
443    return EINA_TRUE;
444 }
445
446 Eina_Bool
447 eet_dictionary_string_get_fp(const Eet_Dictionary *ed,
448                              int                   idx,
449                              Eina_F32p32          *result)
450 {
451    Eet_Convert *convert;
452    const char *str;
453
454    if (!_eet_dictionary_test(ed, idx, result))
455      return EINA_FALSE;
456
457    convert = eet_dictionary_convert_get(ed, idx, &str);
458    if (!convert) return EINA_FALSE;
459
460    if (!(convert->type & EET_D_FIXED_POINT))
461      {
462         Eina_F32p32 fp;
463
464         eina_lock_take((Eina_Lock*) &ed->mutex);
465         if (!eina_convert_atofp(str, ed->all[idx].len, &fp))
466           {
467              eina_lock_release((Eina_Lock*) &ed->mutex);
468              return EINA_FALSE;
469           }
470         eina_lock_release((Eina_Lock*) &ed->mutex);
471
472         convert->fp = fp;
473         convert->type |= EET_D_FIXED_POINT;
474      }
475
476    *result = convert->fp;
477    return EINA_TRUE;
478 }
479
480 EAPI int
481 eet_dictionary_string_check(Eet_Dictionary *ed,
482                             const char     *string)
483 {
484    int res = 0;
485    int i;
486
487    if ((!ed) || (!string))
488      return 0;
489
490    eina_lock_take(&ed->mutex);
491
492    if ((ed->start <= string) && (string < ed->end))
493      res = 1;
494
495    if (!res)
496      {
497         for (i = 0; i < ed->count; ++i)
498           if ((ed->all[i].allocated) && ed->all[i].str == string)
499             {
500                res = 1;
501                break;
502             }
503      }
504
505    eina_lock_release(&ed->mutex);
506
507    return res;
508 }
509