summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsubhransu mohanty <sub.mohanty@samsung.com>2017-11-09 10:34:01 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2017-11-10 11:20:38 +0900
commitd4a2b2025bd1e7396de74c0becb09d4ecbf3b6b9 (patch)
tree31443b3f73fe8b0b53bc036c9ff01223f4ebe0ee /src
parent76e75dc105ce98b29d5fd9026a0bc2fd18e41acd (diff)
evas/common: added a generic cache in evas common.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile_Evas.am4
-rw-r--r--src/lib/evas/common/evas_common_generic_cache.c104
2 files changed, 107 insertions, 1 deletions
diff --git a/src/Makefile_Evas.am b/src/Makefile_Evas.am
index f1d0148667..408c0c7583 100644
--- a/src/Makefile_Evas.am
+++ b/src/Makefile_Evas.am
@@ -494,7 +494,9 @@ lib/evas/common/language/evas_language_utils.h \
494lib/evas/common/language/evas_script_table.h \ 494lib/evas/common/language/evas_script_table.h \
495lib/evas/common/evas_text_utils.h \ 495lib/evas/common/evas_text_utils.h \
496lib/evas/common/evas_font_ot.h \ 496lib/evas/common/evas_font_ot.h \
497lib/evas/common/evas_font_draw.h 497lib/evas/common/evas_font_draw.h \
498lib/evas/common/evas_common_generic_cache.c
499
498 500
499lib_evas_libevas_la_CPPFLAGS = -I$(top_builddir)/src/lib/efl \ 501lib_evas_libevas_la_CPPFLAGS = -I$(top_builddir)/src/lib/efl \
500-I$(top_srcdir)/src/lib/evas/canvas \ 502-I$(top_srcdir)/src/lib/evas/canvas \
diff --git a/src/lib/evas/common/evas_common_generic_cache.c b/src/lib/evas/common/evas_common_generic_cache.c
new file mode 100644
index 0000000000..55fd6e06b2
--- /dev/null
+++ b/src/lib/evas/common/evas_common_generic_cache.c
@@ -0,0 +1,104 @@
1#include "evas_common_private.h"
2
3EAPI Generic_Cache*
4generic_cache_new(void *user_data, Generic_Cache_Free func)
5{
6 Generic_Cache *cache;
7 cache = calloc(1, sizeof(Generic_Cache));
8 cache->hash = eina_hash_int32_new(NULL);
9 cache->user_data = user_data;
10 cache->free_func = func;
11 return cache;
12}
13
14EAPI void
15generic_cache_destroy(Generic_Cache *cache)
16{
17 generic_cache_dump(cache);
18 eina_hash_free(cache->hash);
19}
20
21EAPI void
22generic_cache_dump(Generic_Cache *cache)
23{
24 Generic_Cache_Entry *entry;
25 if (cache)
26 {
27 eina_hash_free_buckets(cache->hash);
28 EINA_LIST_FREE(cache->lru_list, entry)
29 {
30 cache->free_func(cache->user_data, entry->data);
31 free(entry);
32 }
33 }
34}
35
36EAPI void
37generic_cache_set(Generic_Cache *cache, void *key, void *surface)
38{
39 Generic_Cache_Entry *entry = NULL;
40 int count;
41
42 entry = calloc(1, sizeof(Generic_Cache_Entry));
43 entry->key = key;
44 entry->data = surface;
45 entry->ref = 1;
46 eina_hash_add(cache->hash, &key, entry);
47 cache->lru_list = eina_list_prepend(cache->lru_list, entry);
48 count = eina_list_count(cache->lru_list);
49 if (count > 50)
50 {
51 entry = eina_list_data_get(eina_list_last(cache->lru_list));
52 // if its still being ref.
53 if (entry->ref) return;
54 eina_hash_del(cache->hash, &entry->key, entry);
55 cache->lru_list = eina_list_remove_list(cache->lru_list, eina_list_last(cache->lru_list));
56 cache->free_func(cache->user_data, entry->data);
57 free(entry);
58 }
59}
60
61EAPI void *
62generic_cache_get(Generic_Cache *cache, void *key)
63{
64 Generic_Cache_Entry *entry = NULL, *lru_data;
65 Eina_List *l;
66
67 entry = eina_hash_find(cache->hash, &key);
68 if (entry)
69 {
70 // update the ref
71 entry->ref += 1;
72 // promote in lru
73 EINA_LIST_FOREACH(cache->lru_list, l, lru_data)
74 {
75 if (lru_data == entry)
76 {
77 cache->lru_list = eina_list_promote_list(cache->lru_list, l);
78 break;
79 }
80 }
81 return entry->data;
82 }
83 return NULL;
84}
85
86EAPI void
87generic_cache_drop(Generic_Cache *cache, void *key)
88{
89 Generic_Cache_Entry *entry = NULL;
90
91 entry = eina_hash_find(cache->hash, &key);
92 if (entry)
93 {
94 entry->ref -= 1;
95 // if its still being ref.
96 if (entry->ref) return;
97 eina_hash_del(cache->hash, &entry->key, entry);
98 // find and remove from lru list
99 cache->lru_list = eina_list_remove(cache->lru_list, entry);
100 cache->free_func(cache->user_data, entry->data);
101 free(entry);
102 }
103}
104