summaryrefslogtreecommitdiff
path: root/src/lib/eet
diff options
context:
space:
mode:
authorCarsten Haitzler (Rasterman) <raster@rasterman.com>2018-11-20 17:15:57 +0000
committerCarsten Haitzler (Rasterman) <raster@rasterman.com>2018-11-21 13:37:16 +0000
commite68a952b8c039ca57f31b669efef84303c54922d (patch)
tree230805726c859d84ac1442b23d7ec96a2800f737 /src/lib/eet
parent61109729fada2c26bd713a1f105702c03da3d2b7 (diff)
eet - dictorinary - tidy up code to be easier to read
more compact so less scrolling - easier to read.
Diffstat (limited to 'src/lib/eet')
-rw-r--r--src/lib/eet/eet_dictionary.c208
1 files changed, 74 insertions, 134 deletions
diff --git a/src/lib/eet/eet_dictionary.c b/src/lib/eet/eet_dictionary.c
index acff624f1c..46e8cbf723 100644
--- a/src/lib/eet/eet_dictionary.c
+++ b/src/lib/eet/eet_dictionary.c
@@ -14,16 +14,13 @@
14Eet_Dictionary * 14Eet_Dictionary *
15eet_dictionary_add(void) 15eet_dictionary_add(void)
16{ 16{
17 Eet_Dictionary *new; 17 Eet_Dictionary *ed;
18 18
19 new = eet_dictionary_calloc(1); 19 ed = eet_dictionary_calloc(1);
20 if (!new) 20 if (!ed) return NULL;
21 return NULL; 21 memset(ed->hash, -1, sizeof(int) * 256);
22 22 eina_rwlock_new(&ed->rwlock);
23 memset(new->hash, -1, sizeof (int) * 256); 23 return ed;
24 eina_rwlock_new(&new->rwlock);
25
26 return new;
27} 24}
28 25
29void 26void
@@ -32,19 +29,20 @@ eet_dictionary_free(Eet_Dictionary *ed)
32 int i; 29 int i;
33 30
34 if (!ed) return; 31 if (!ed) return;
35
36 eina_rwlock_free(&ed->rwlock); 32 eina_rwlock_free(&ed->rwlock);
37 33
38 for (i = 0; i < ed->count; ++i) 34 for (i = 0; i < ed->count; i++)
39 if (ed->all_allocated[i >> 3] & (1 << (i & 0x7))) 35 {
40 eina_stringshare_del(ed->all[i].str); 36 if (ed->all_allocated[i >> 3] & (1 << (i & 0x7)))
41 37 {
38 eina_stringshare_del(ed->all[i].str);
39 }
40 }
42 free(ed->all); 41 free(ed->all);
43 free(ed->all_hash); 42 free(ed->all_hash);
44 free(ed->all_allocated); 43 free(ed->all_allocated);
45 44
46 if (ed->converts) eina_hash_free(ed->converts); 45 if (ed->converts) eina_hash_free(ed->converts);
47
48 eet_dictionary_mp_free(ed); 46 eet_dictionary_mp_free(ed);
49} 47}
50 48
@@ -55,27 +53,21 @@ _eet_dictionary_lookup(Eet_Dictionary *ed,
55 int hash, 53 int hash,
56 int *previous) 54 int *previous)
57{ 55{
58 int prev = -1; 56 int prev = -1, current;
59 int current;
60 57
61 current = ed->hash[hash]; 58 current = ed->hash[hash];
62
63 while (current != -1) 59 while (current != -1)
64 { 60 {
65 if (ed->all[current].len == len) 61 if ((ed->all[current].str) &&
62 ((ed->all[current].str == string) ||
63 ((ed->all[current].len == len) &&
64 (!strcmp(ed->all[current].str, string)))))
66 { 65 {
67 if (ed->all[current].str && 66 break;
68 ((ed->all[current].str == string) ||
69 (!strcmp(ed->all[current].str, string))))
70 {
71 break;
72 }
73 } 67 }
74
75 prev = current; 68 prev = current;
76 current = ed->all[current].next; 69 current = ed->all[current].next;
77 } 70 }
78
79 if (previous) *previous = prev; 71 if (previous) *previous = prev;
80 return current; 72 return current;
81} 73}
@@ -86,14 +78,9 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
86{ 78{
87 Eet_String *current; 79 Eet_String *current;
88 const char *str; 80 const char *str;
89 int hash; 81 int hash, idx, pidx, len, cnt;
90 int idx;
91 int pidx;
92 int len;
93 int cnt;
94 82
95 if (!ed) 83 if (!ed) return -1;
96 return -1;
97 84
98 hash = _eet_hash_gen(string, 8); 85 hash = _eet_hash_gen(string, 8);
99 len = strlen(string) + 1; 86 len = strlen(string) + 1;
@@ -114,25 +101,24 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
114 eina_rwlock_take_write(&ed->rwlock); 101 eina_rwlock_take_write(&ed->rwlock);
115 if (ed->total == ed->count) 102 if (ed->total == ed->count)
116 { 103 {
117 Eet_String *new; 104 Eet_String *s;
118 unsigned char *new_hash; 105 unsigned char *new_hash, *new_allocated;
119 unsigned char *new_allocated;
120 int total; 106 int total;
121 107
122 total = ed->total + 64; 108 total = ed->total + 64;
123 109
124 new = realloc(ed->all, total * sizeof(Eet_String)); 110 s = realloc(ed->all, total * sizeof(Eet_String));
125 if (!new) goto on_error; 111 if (!s) goto on_error;
126 ed->all = new; 112 ed->all = s;
127 113
128 new_hash = realloc(ed->all_hash, total * sizeof (unsigned char)); 114 new_hash = realloc(ed->all_hash, total);
129 if (!new_hash) goto on_error; 115 if (!new_hash) goto on_error;
130 ed->all_hash = new_hash; 116 ed->all_hash = new_hash;
117
118 new_allocated = realloc(ed->all_allocated, ((total >> 3) + 1));
119 if (!new_allocated) goto on_error;
120 ed->all_allocated = new_allocated;
131 121
132 new_allocated = realloc(ed->all_allocated, ((total >> 3) + 1) * sizeof (unsigned char));
133 if (!new_allocated) goto on_error;
134 ed->all_allocated = new_allocated;
135
136 ed->total = total; 122 ed->total = total;
137 } 123 }
138 124
@@ -152,18 +138,14 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
152 else 138 else
153 { 139 {
154 current->next = idx; 140 current->next = idx;
155 141 if (pidx != -1) ed->all[pidx].next = ed->count;
156 if (pidx != -1) 142 else ed->hash[hash] = ed->count;
157 ed->all[pidx].next = ed->count;
158 else
159 ed->hash[hash] = ed->count;
160 } 143 }
161
162 cnt = ed->count++; 144 cnt = ed->count++;
163 eina_rwlock_release(&ed->rwlock); 145 eina_rwlock_release(&ed->rwlock);
164 return cnt; 146 return cnt;
165 147
166 on_error: 148on_error:
167 eina_rwlock_release(&ed->rwlock); 149 eina_rwlock_release(&ed->rwlock);
168 return -1; 150 return -1;
169} 151}
@@ -175,24 +157,26 @@ eet_dictionary_string_get_size(const Eet_Dictionary *ed,
175 int length = 0; 157 int length = 0;
176 158
177 if (!ed) goto done; 159 if (!ed) goto done;
178
179 if (idx < 0) goto done; 160 if (idx < 0) goto done;
180 161
181 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 162 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
182 163 if (idx < ed->count) length = ed->all[idx].len;
183 if (idx < ed->count)
184 length = ed->all[idx].len;
185
186 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 164 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
187 165done:
188 done:
189 return length; 166 return length;
190} 167}
191 168
192EAPI int 169EAPI int
193eet_dictionary_count(const Eet_Dictionary *ed) 170eet_dictionary_count(const Eet_Dictionary *ed)
194{ 171{
195 return ed->count; 172 int cnt;
173
174 if (!ed) return 0;
175
176 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
177 cnt = ed->count;
178 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
179 return cnt;
196} 180}
197 181
198int 182int
@@ -202,17 +186,12 @@ eet_dictionary_string_get_hash(const Eet_Dictionary *ed,
202 int hash = -1; 186 int hash = -1;
203 187
204 if (!ed) goto done; 188 if (!ed) goto done;
205
206 if (idx < 0) goto done; 189 if (idx < 0) goto done;
207 190
208 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 191 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
209 192 if (idx < ed->count) hash = ed->all_hash[idx];
210 if (idx < ed->count)
211 hash = ed->all_hash[idx];
212
213 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 193 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
214 194done:
215 done:
216 return hash; 195 return hash;
217} 196}
218 197
@@ -223,16 +202,14 @@ eet_dictionary_string_get_char(const Eet_Dictionary *ed,
223 const char *s = NULL; 202 const char *s = NULL;
224 203
225 if (!ed) goto done; 204 if (!ed) goto done;
226
227 if (idx < 0) goto done; 205 if (idx < 0) goto done;
228 206
229 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 207 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
230
231 if (idx < ed->count) 208 if (idx < ed->count)
232 { 209 {
233#ifdef _WIN32 210#ifdef _WIN32
234 /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */ 211 /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */
235 if (!(ed->all_allocated[idx >> 3] & (1 << (idx & 0x7)))) 212 if (!(ed->all_allocated[idx >> 3] & (1 << (idx & 0x7))))
236 { 213 {
237 ed->all[idx].str = eina_stringshare_add(ed->all[idx].str); 214 ed->all[idx].str = eina_stringshare_add(ed->all[idx].str);
238 ed->all_allocated[idx >> 3] |= (1 << (idx & 0x7)); 215 ed->all_allocated[idx >> 3] |= (1 << (idx & 0x7));
@@ -240,10 +217,8 @@ eet_dictionary_string_get_char(const Eet_Dictionary *ed,
240#endif /* ifdef _WIN32 */ 217#endif /* ifdef _WIN32 */
241 s = ed->all[idx].str; 218 s = ed->all[idx].str;
242 } 219 }
243
244 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 220 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
245 221done:
246 done:
247 return s; 222 return s;
248} 223}
249 224
@@ -257,10 +232,8 @@ _eet_dictionary_string_get_me_cache(const char *s,
257 { 232 {
258 *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0'); 233 *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
259 *exponent = (s[5] - '0'); 234 *exponent = (s[5] - '0');
260
261 return EINA_TRUE; 235 return EINA_TRUE;
262 } 236 }
263
264 return EINA_FALSE; 237 return EINA_FALSE;
265} 238}
266 239
@@ -269,19 +242,14 @@ _eet_dictionary_string_get_float_cache(const char *s,
269 int len, 242 int len,
270 float *result) 243 float *result)
271{ 244{
272 int mantisse; 245 int mantisse, exponent;
273 int exponent;
274 246
275 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent)) 247 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
276 { 248 {
277 if (s[4] == '+') 249 if (s[4] == '+') *result = (float)(mantisse << exponent);
278 *result = (float)(mantisse << exponent); 250 else *result = (float)mantisse / (float)(1 << exponent);
279 else
280 *result = (float)mantisse / (float)(1 << exponent);
281
282 return EINA_TRUE; 251 return EINA_TRUE;
283 } 252 }
284
285 return EINA_FALSE; 253 return EINA_FALSE;
286} 254}
287 255
@@ -290,19 +258,14 @@ _eet_dictionary_string_get_double_cache(const char *s,
290 int len, 258 int len,
291 double *result) 259 double *result)
292{ 260{
293 int mantisse; 261 int mantisse, exponent;
294 int exponent;
295 262
296 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent)) 263 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
297 { 264 {
298 if (s[4] == '+') 265 if (s[4] == '+') *result = (double)(mantisse << exponent);
299 *result = (double)(mantisse << exponent); 266 else *result = (double)mantisse / (float)(1 << exponent);
300 else
301 *result = (double)mantisse / (float)(1 << exponent);
302
303 return EINA_TRUE; 267 return EINA_TRUE;
304 } 268 }
305
306 return EINA_FALSE; 269 return EINA_FALSE;
307} 270}
308 271
@@ -314,21 +277,15 @@ _eet_dictionary_test(const Eet_Dictionary *ed,
314 Eina_Bool limit = EINA_FALSE; 277 Eina_Bool limit = EINA_FALSE;
315 278
316 if (!result) goto done; 279 if (!result) goto done;
317
318 if (!ed) goto done; 280 if (!ed) goto done;
319
320 if (idx < 0) goto done; 281 if (idx < 0) goto done;
321 282
322 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 283 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
323
324 if (!(idx < ed->count)) goto unlock_done; 284 if (!(idx < ed->count)) goto unlock_done;
325
326 limit = EINA_TRUE; 285 limit = EINA_TRUE;
327 286unlock_done:
328 unlock_done:
329 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 287 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
330 288done:
331 done:
332 return limit; 289 return limit;
333} 290}
334 291
@@ -340,27 +297,22 @@ eet_dictionary_convert_get(const Eet_Dictionary *ed,
340 Eet_Convert *result; 297 Eet_Convert *result;
341 298
342 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 299 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
343
344 *str = ed->all[idx].str; 300 *str = ed->all[idx].str;
345 301
346 if (!ed->converts) 302 if (!ed->converts)
347 { 303 {
348 ((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free); 304 ((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free);
349
350 goto add_convert; 305 goto add_convert;
351 } 306 }
352 307
353 result = eina_hash_find(ed->converts, &idx); 308 result = eina_hash_find(ed->converts, &idx);
354 if (result) goto done; 309 if (result) goto done;
355 310
356 add_convert: 311add_convert:
357 result = calloc(1, sizeof (Eet_Convert)); 312 result = calloc(1, sizeof (Eet_Convert));
358
359 eina_hash_add(ed->converts, &idx, result); 313 eina_hash_add(ed->converts, &idx, result);
360 314done:
361 done:
362 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 315 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
363
364 return result; 316 return result;
365} 317}
366 318
@@ -372,8 +324,7 @@ eet_dictionary_string_get_float(const Eet_Dictionary *ed,
372 Eet_Convert *convert; 324 Eet_Convert *convert;
373 const char *str; 325 const char *str;
374 326
375 if (!_eet_dictionary_test(ed, idx, result)) 327 if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE;
376 return EINA_FALSE;
377 328
378 convert = eet_dictionary_convert_get(ed, idx, &str); 329 convert = eet_dictionary_convert_get(ed, idx, &str);
379 if (!convert) return EINA_FALSE; 330 if (!convert) return EINA_FALSE;
@@ -393,14 +344,11 @@ eet_dictionary_string_get_float(const Eet_Dictionary *ed,
393 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 344 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
394 return EINA_FALSE; 345 return EINA_FALSE;
395 } 346 }
396
397 convert->f = ldexpf((float)mantisse, exponent); 347 convert->f = ldexpf((float)mantisse, exponent);
398 } 348 }
399 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 349 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
400
401 convert->type |= EET_D_FLOAT; 350 convert->type |= EET_D_FLOAT;
402 } 351 }
403
404 *result = convert->f; 352 *result = convert->f;
405 return EINA_TRUE; 353 return EINA_TRUE;
406} 354}
@@ -413,8 +361,7 @@ eet_dictionary_string_get_double(const Eet_Dictionary *ed,
413 Eet_Convert *convert; 361 Eet_Convert *convert;
414 const char *str; 362 const char *str;
415 363
416 if (!_eet_dictionary_test(ed, idx, result)) 364 if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE;
417 return EINA_FALSE;
418 365
419 convert = eet_dictionary_convert_get(ed, idx, &str); 366 convert = eet_dictionary_convert_get(ed, idx, &str);
420 if (!convert) return EINA_FALSE; 367 if (!convert) return EINA_FALSE;
@@ -435,11 +382,9 @@ eet_dictionary_string_get_double(const Eet_Dictionary *ed,
435 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 382 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
436 return EINA_FALSE; 383 return EINA_FALSE;
437 } 384 }
438
439 convert->d = ldexp((double)mantisse, exponent); 385 convert->d = ldexp((double)mantisse, exponent);
440 } 386 }
441 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 387 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
442
443 convert->type |= EET_D_DOUBLE; 388 convert->type |= EET_D_DOUBLE;
444 } 389 }
445 390
@@ -455,8 +400,7 @@ eet_dictionary_string_get_fp(const Eet_Dictionary *ed,
455 Eet_Convert *convert; 400 Eet_Convert *convert;
456 const char *str; 401 const char *str;
457 402
458 if (!_eet_dictionary_test(ed, idx, result)) 403 if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE;
459 return EINA_FALSE;
460 404
461 convert = eet_dictionary_convert_get(ed, idx, &str); 405 convert = eet_dictionary_convert_get(ed, idx, &str);
462 if (!convert) return EINA_FALSE; 406 if (!convert) return EINA_FALSE;
@@ -485,29 +429,25 @@ EAPI int
485eet_dictionary_string_check(Eet_Dictionary *ed, 429eet_dictionary_string_check(Eet_Dictionary *ed,
486 const char *string) 430 const char *string)
487{ 431{
488 int res = 0; 432 int res = 0, i;
489 int i;
490 433
491 if ((!ed) || (!string)) 434 if ((!ed) || (!string)) return 0;
492 return 0;
493 435
494 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 436 eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
495 437 if ((ed->start <= string) && (string < ed->end)) res = 1;
496 if ((ed->start <= string) && (string < ed->end))
497 res = 1;
498 438
499 if (!res) 439 if (!res)
500 { 440 {
501 for (i = 0; i < ed->count; ++i) 441 for (i = 0; i < ed->count; i++)
502 if ((ed->all_allocated[i >> 3] & (1 << (i & 0x7))) && ed->all[i].str == string) 442 {
503 { 443 if ((ed->all_allocated[i >> 3] & (1 << (i & 0x7))) && ed->all[i].str == string)
504 res = 1; 444 {
505 break; 445 res = 1;
506 } 446 break;
447 }
448 }
507 } 449 }
508
509 eina_rwlock_release((Eina_RWLock *)&ed->rwlock); 450 eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
510
511 return res; 451 return res;
512} 452}
513 453