summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlastair Poole <netstar@gmail.com>2020-05-18 12:16:35 +0100
committerAlastair Poole <netstar@gmail.com>2020-05-18 12:16:35 +0100
commit914f97e086b0b9a92dcd0ef4405589f41a1fa481 (patch)
tree8ba968fcee0857c5a29d926b7dde2cc62fe51f4b
parentb88601ba7db27b93dcf058ebbed819a36a5a4151 (diff)
ui: create a generic cache API.
Reduce code duplication, create: evisum_ui_item_cache_... functions.
-rw-r--r--src/bin/ui/meson.build2
-rw-r--r--src/bin/ui/ui.c78
-rw-r--r--src/bin/ui/ui.h7
-rw-r--r--src/bin/ui/ui_cache.c87
-rw-r--r--src/bin/ui/ui_cache.h31
-rw-r--r--src/bin/ui/ui_process_view.c136
-rw-r--r--src/bin/ui/ui_process_view.h8
7 files changed, 174 insertions, 175 deletions
diff --git a/src/bin/ui/meson.build b/src/bin/ui/meson.build
index 7445263..b3f6d9a 100644
--- a/src/bin/ui/meson.build
+++ b/src/bin/ui/meson.build
@@ -2,6 +2,8 @@ src += files([
2 'gettext.h', 2 'gettext.h',
3 'ui.h', 3 'ui.h',
4 'ui.c', 4 'ui.c',
5 'ui_cache.c',
6 'ui_cache.h',
5 'ui_misc.c', 7 'ui_misc.c',
6 'ui_misc.h', 8 'ui_misc.h',
7 'ui_disk.c', 9 'ui_disk.c',
diff --git a/src/bin/ui/ui.c b/src/bin/ui/ui.c
index 14e6a75..7a3d454 100644
--- a/src/bin/ui/ui.c
+++ b/src/bin/ui/ui.c
@@ -303,35 +303,21 @@ _proc_pid_cpu_usage_get(Ui *ui, Proc_Info *proc)
303 _proc_pid_cpu_time_save(ui, proc); 303 _proc_pid_cpu_time_save(ui, proc);
304} 304}
305 305
306#define ITEM_CACHE_INIT_SIZE 50
307
308typedef struct _Item_Cache {
309 Evas_Object *obj;
310 Eina_Bool used;
311} Item_Cache;
312
313static void 306static void
314_item_unrealized_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) 307_item_unrealized_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
315{ 308{
316 Ui *ui; 309 Ui *ui;
317 Item_Cache *it;
318 Evas_Object *o; 310 Evas_Object *o;
319 Eina_List *l, *contents = NULL; 311 Eina_List *contents = NULL;
320 312
321 ui = data; 313 ui = data;
322 314
323 elm_genlist_item_all_contents_unset(event_info, &contents); 315 elm_genlist_item_all_contents_unset(event_info, &contents);
316
324 EINA_LIST_FREE(contents, o) 317 EINA_LIST_FREE(contents, o)
325 { 318 {
326 EINA_LIST_FOREACH(ui->item_cache, l, it) 319 evisum_ui_item_cache_item_release(ui->cache, o);
327 { 320 }
328 if (it->obj == o)
329 {
330 it->used = EINA_FALSE;
331 break;
332 }
333 }
334 }
335} 321}
336 322
337static void 323static void
@@ -389,45 +375,6 @@ _item_create(Evas_Object *parent)
389 return table; 375 return table;
390} 376}
391 377
392static void
393_item_cache_init(Ui *ui)
394{
395 for (int i = 0; i < ITEM_CACHE_INIT_SIZE; i++)
396 {
397 Item_Cache *it = calloc(1, sizeof(Item_Cache));
398 if (it)
399 {
400 it->obj = _item_create(ui->genlist_procs);
401 ui->item_cache = eina_list_append(ui->item_cache, it);
402 }
403 }
404}
405
406static Item_Cache *
407_item_cache_get(Ui *ui)
408{
409 Eina_List *l;
410 Item_Cache *it;
411
412 EINA_LIST_FOREACH(ui->item_cache, l, it)
413 {
414 if (it->used == 0)
415 {
416 it->used = 1;
417 return it;
418 }
419 }
420
421 it = calloc(1, sizeof(Item_Cache));
422 if (it)
423 {
424 it->obj = _item_create(ui->genlist_procs);
425 it->used = 1;
426 ui->item_cache = eina_list_append(ui->item_cache, it);
427 }
428 return it;
429}
430
431static Evas_Object * 378static Evas_Object *
432_content_get(void *data, Evas_Object *obj, const char *source) 379_content_get(void *data, Evas_Object *obj, const char *source)
433{ 380{
@@ -444,7 +391,7 @@ _content_get(void *data, Evas_Object *obj, const char *source)
444 if (!proc) return NULL; 391 if (!proc) return NULL;
445 if (!ui->ready) return NULL; 392 if (!ui->ready) return NULL;
446 393
447 Item_Cache *it = _item_cache_get(ui); 394 Item_Cache *it = evisum_ui_item_cache_item_get(ui->cache);
448 if (!it) 395 if (!it)
449 { 396 {
450 fprintf(stderr, "Error: Object cache creation failed.\n"); 397 fprintf(stderr, "Error: Object cache creation failed.\n");
@@ -1560,8 +1507,6 @@ evisum_icon_path_get(const char *name)
1560void 1507void
1561evisum_ui_shutdown(Ui *ui) 1508evisum_ui_shutdown(Ui *ui)
1562{ 1509{
1563 Item_Cache *it;
1564
1565 evas_object_del(ui->win); 1510 evas_object_del(ui->win);
1566 1511
1567 if (ui->timer_pid) 1512 if (ui->timer_pid)
@@ -1581,16 +1526,10 @@ evisum_ui_shutdown(Ui *ui)
1581 1526
1582 _proc_pid_cpu_times_free(ui); 1527 _proc_pid_cpu_times_free(ui);
1583 1528
1584 EINA_LIST_FREE(ui->item_cache, it)
1585 {
1586 free(it);
1587 }
1588
1589 if (ui->cpu_list) 1529 if (ui->cpu_list)
1590 eina_list_free(ui->cpu_list); 1530 eina_list_free(ui->cpu_list);
1591 1531
1592 if (ui->item_cache) 1532 evisum_ui_item_cache_free(ui->cache);
1593 eina_list_free(ui->item_cache);
1594 1533
1595 eina_lock_free(&_lock); 1534 eina_lock_free(&_lock);
1596} 1535}
@@ -1713,7 +1652,6 @@ _ui_init(Evas_Object *parent)
1713 ui->mem_visible = ui->misc_visible = EINA_TRUE; 1652 ui->mem_visible = ui->misc_visible = EINA_TRUE;
1714 ui->cpu_times = NULL; 1653 ui->cpu_times = NULL;
1715 ui->cpu_list = NULL; 1654 ui->cpu_list = NULL;
1716 ui->item_cache = NULL;
1717 1655
1718 _ui = NULL; 1656 _ui = NULL;
1719 _evisum_config = NULL; 1657 _evisum_config = NULL;
@@ -1729,7 +1667,7 @@ _ui_init(Evas_Object *parent)
1729 ui_tab_disk_add(ui); 1667 ui_tab_disk_add(ui);
1730 ui_tab_misc_add(ui); 1668 ui_tab_misc_add(ui);
1731 1669
1732 _item_cache_init(ui); 1670 ui->cache = evisum_ui_item_cache_new(ui->genlist_procs, _item_create, 50);
1733 1671
1734 return ui; 1672 return ui;
1735} 1673}
diff --git a/src/bin/ui/ui.h b/src/bin/ui/ui.h
index aa7724c..b0c03ed 100644
--- a/src/bin/ui/ui.h
+++ b/src/bin/ui/ui.h
@@ -5,6 +5,7 @@
5#include "gettext.h" 5#include "gettext.h"
6#include "system/process.h" 6#include "system/process.h"
7#include "../evisum_config.h" 7#include "../evisum_config.h"
8#include "ui/ui_cache.h"
8 9
9#define _(STR) gettext(STR) 10#define _(STR) gettext(STR)
10 11
@@ -106,9 +107,9 @@ typedef struct Ui
106 Evas_Object *btn_state; 107 Evas_Object *btn_state;
107 Evas_Object *btn_cpu_usage; 108 Evas_Object *btn_cpu_usage;
108 109
109 Eina_List *item_cache; 110 Evisum_Ui_Cache *cache;
110 Evas_Object *genlist_procs; 111 Evas_Object *genlist_procs;
111 Evas_Object *entry_search; 112 Evas_Object *entry_search;
112 113
113 Ecore_Thread *thread_system; 114 Ecore_Thread *thread_system;
114 Ecore_Thread *thread_process; 115 Ecore_Thread *thread_process;
diff --git a/src/bin/ui/ui_cache.c b/src/bin/ui/ui_cache.c
new file mode 100644
index 0000000..d4f59a1
--- /dev/null
+++ b/src/bin/ui/ui_cache.c
@@ -0,0 +1,87 @@
1#include "ui_cache.h"
2
3Evisum_Ui_Cache *
4evisum_ui_item_cache_new(Evas_Object *parent, Evas_Object *(create_cb)(Evas_Object *), int size)
5{
6 Evisum_Ui_Cache *cache = malloc(sizeof(Evisum_Ui_Cache));
7 if (!cache) return NULL;
8
9 cache->parent = parent;
10 cache->item_create_cb = create_cb;
11 cache->items = NULL;
12
13 for (int i = 0; i < size; i++)
14 {
15 Item_Cache *it = calloc(1, sizeof(Item_Cache));
16 if (it)
17 {
18 it->obj = cache->item_create_cb(parent);
19 cache->items = eina_list_append(cache->items, it);
20 }
21 }
22
23 return cache;
24}
25
26Item_Cache *
27evisum_ui_item_cache_item_get(Evisum_Ui_Cache *cache)
28{
29 Eina_List *l;
30 Item_Cache *it;
31
32 EINA_LIST_FOREACH(cache->items, l, it)
33 {
34 if (it->used == 0)
35 {
36 it->used = 1;
37 return it;
38 }
39 }
40
41 it = calloc(1, sizeof(Item_Cache));
42 if (it)
43 {
44 it->obj = cache->item_create_cb(cache->parent);
45 it->used = 1;
46 cache->items = eina_list_append(cache->items, it);
47 }
48
49 return it;
50}
51
52Eina_Bool
53evisum_ui_item_cache_item_release(Evisum_Ui_Cache *cache, Evas_Object *obj)
54{
55 Item_Cache *it;
56 Eina_List *l;
57 Eina_Bool released = EINA_FALSE;
58
59 EINA_LIST_FOREACH(cache->items, l, it)
60 {
61 if (it->obj == obj)
62 {
63 it->used = 0;
64 released = EINA_TRUE;
65 break;
66 }
67 }
68
69 return released;
70}
71
72void
73evisum_ui_item_cache_free(Evisum_Ui_Cache *cache)
74{
75 Item_Cache *it;
76
77 evas_object_del(cache->parent);
78
79 EINA_LIST_FREE(cache->items, it)
80 {
81 free(it);
82 }
83
84 eina_list_free(cache->items);
85
86 free(cache);
87}
diff --git a/src/bin/ui/ui_cache.h b/src/bin/ui/ui_cache.h
new file mode 100644
index 0000000..48ebd95
--- /dev/null
+++ b/src/bin/ui/ui_cache.h
@@ -0,0 +1,31 @@
1#ifndef __UI_CACHE_H__
2#define __UI_CACHE_H__
3
4#include <Eina.h>
5#include <Evas.h>
6
7typedef struct _Evisum_Ui_Cache {
8 Eina_List *items;
9 Evas_Object *parent;
10 Evas_Object *(*item_create_cb)(Evas_Object *);
11} Evisum_Ui_Cache;
12
13typedef struct _Item_Cache {
14 Evas_Object *obj;
15 Eina_Bool used;
16} Item_Cache;
17
18Evisum_Ui_Cache *
19evisum_ui_item_cache_new(Evas_Object *parent, Evas_Object *(create_cb)(Evas_Object *), int size);
20
21Item_Cache *
22evisum_ui_item_cache_item_get(Evisum_Ui_Cache *cache);
23
24Eina_Bool
25evisum_ui_item_cache_item_release(Evisum_Ui_Cache *cache, Evas_Object *obj);
26
27void
28evisum_ui_item_cache_free(Evisum_Ui_Cache *cache);
29
30
31#endif
diff --git a/src/bin/ui/ui_process_view.c b/src/bin/ui/ui_process_view.c
index d793aee..73fe2dc 100644
--- a/src/bin/ui/ui_process_view.c
+++ b/src/bin/ui/ui_process_view.c
@@ -1,6 +1,29 @@
1#include "ui_process_view.h" 1#include "ui_process_view.h"
2#include "../system/process.h" 2#include "../system/process.h"
3 3
4typedef struct {
5 int tid;
6 char *name;
7 char *state;
8 int cpu_id;
9 double cpu_usage;
10} Thread_Info;
11
12static Thread_Info *
13_thread_info_new(Proc_Info *thr, double cpu_usage)
14{
15 Thread_Info *t = calloc(1, sizeof(Thread_Info));
16 if (!t) return NULL;
17
18 t->tid = thr->tid;
19 t->name = strdup(thr->thread_name);
20 t->state = strdup(thr->state);
21 t->cpu_id = thr->cpu_id;
22 t->cpu_usage = cpu_usage;
23
24 return t;
25}
26
4Eina_List * 27Eina_List *
5_exe_response(const char *command) 28_exe_response(const char *command)
6{ 29{
@@ -24,65 +47,32 @@ _exe_response(const char *command)
24 return lines; 47 return lines;
25} 48}
26 49
27#define ITEM_CACHE_INIT_SIZE 25
28
29typedef struct _Item_Cache {
30 Evas_Object *obj;
31 Eina_Bool used;
32} Item_Cache;
33
34static void 50static void
35_item_unrealized_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) 51_item_unrealized_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
36{ 52{
37 Ui_Process *ui; 53 Ui_Process *ui;
38 Item_Cache *it;
39 Evas_Object *o; 54 Evas_Object *o;
40 Eina_List *l, *contents = NULL; 55 Eina_List *contents = NULL;
41 56
42 ui = data; 57 ui = data;
43 58
44 elm_genlist_item_all_contents_unset(event_info, &contents); 59 elm_genlist_item_all_contents_unset(event_info, &contents);
60
45 EINA_LIST_FREE(contents, o) 61 EINA_LIST_FREE(contents, o)
46 { 62 {
47 EINA_LIST_FOREACH(ui->item_cache, l, it) 63 evisum_ui_item_cache_item_release(ui->cache, o);
48 {
49 if (it->obj == o)
50 {
51 it->used = 0;
52 break;
53 }
54 }
55 } 64 }
56} 65}
57 66
58typedef struct {
59 int tid;
60 char *name;
61 char *state;
62 int cpu_id;
63 double cpu_usage;
64} Thread_Info;
65
66static Thread_Info *
67_thread_info_new(Proc_Info *thr, double cpu_usage)
68{
69 Thread_Info *t = calloc(1, sizeof(Thread_Info));
70 t->tid = thr->tid;
71 t->name = strdup(thr->thread_name);
72 t->state = strdup(thr->state);
73 t->cpu_id = thr->cpu_id;
74 t->cpu_usage = cpu_usage;
75
76 return t;
77}
78
79static void 67static void
80_item_del(void *data, Evas_Object *obj EINA_UNUSED) 68_item_del(void *data, Evas_Object *obj EINA_UNUSED)
81{ 69{
82 Thread_Info *t = data; 70 Thread_Info *t = data;
83 71
84 if (t->name) free(t->name); 72 if (t->name)
85 if (t->state) free(t->state); 73 free(t->name);
74 if (t->state)
75 free(t->state);
86 free(t); 76 free(t);
87} 77}
88 78
@@ -129,57 +119,6 @@ _item_create(Evas_Object *parent)
129 return table; 119 return table;
130} 120}
131 121
132static void
133_cache_free(Eina_List *cache)
134{
135 Item_Cache *it;
136 EINA_LIST_FREE(cache, it)
137 {
138 free(it);
139 }
140
141 eina_list_free(cache);
142}
143
144static void
145_cache_init(Ui_Process *ui)
146{
147 for (int i = 0; i < ITEM_CACHE_INIT_SIZE; i++)
148 {
149 Item_Cache *it = calloc(1, sizeof(Item_Cache));
150 if (it)
151 {
152 it->obj = _item_create(ui->genlist_threads);
153 ui->item_cache = eina_list_append(ui->item_cache, it);
154 }
155 }
156}
157
158static Item_Cache *
159_cache_get(Ui_Process *ui)
160{
161 Eina_List *l;
162 Item_Cache *it;
163
164 EINA_LIST_FOREACH(ui->item_cache, l, it)
165 {
166 if (it->used == 0)
167 {
168 it->used = 1;
169 return it;
170 }
171 }
172
173 it = calloc(1, sizeof(Item_Cache));
174 if (it)
175 {
176 it->obj = _item_create(ui->genlist_threads);
177 it->used = 1;
178 ui->item_cache = eina_list_append(ui->item_cache, it);
179 }
180 return it;
181}
182
183static Evas_Object * 122static Evas_Object *
184_content_get(void *data, Evas_Object *obj, const char *source) 123_content_get(void *data, Evas_Object *obj, const char *source)
185{ 124{
@@ -196,7 +135,7 @@ _content_get(void *data, Evas_Object *obj, const char *source)
196 if (!ui) return NULL; 135 if (!ui) return NULL;
197 if (!ui->threads_ready) return NULL; 136 if (!ui->threads_ready) return NULL;
198 137
199 Item_Cache *it = _cache_get(ui); 138 Item_Cache *it = evisum_ui_item_cache_item_get(ui->cache);
200 if (!it) 139 if (!it)
201 { 140 {
202 fprintf(stderr, "Error: Object cache creation failed.\n"); 141 fprintf(stderr, "Error: Object cache creation failed.\n");
@@ -382,9 +321,9 @@ _thread_info_set(Ui_Process *ui, Proc_Info *proc)
382 EINA_LIST_FOREACH(threads, l, t) 321 EINA_LIST_FOREACH(threads, l, t)
383 { 322 {
384 Thread_Info *prev = elm_object_item_data_get(it); 323 Thread_Info *prev = elm_object_item_data_get(it);
385 if (prev) _item_del(prev, NULL); 324 if (prev) _item_del(prev, NULL);
386 elm_object_item_data_set(it, t); 325 elm_object_item_data_set(it, t);
387 elm_genlist_item_update(it); 326 elm_genlist_item_update(it);
388 it = elm_genlist_item_next_get(it); 327 it = elm_genlist_item_next_get(it);
389 } 328 }
390 eina_list_free(threads); 329 eina_list_free(threads);
@@ -988,8 +927,8 @@ _win_del_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUS
988 ecore_timer_del(ui->timer_pid); 927 ecore_timer_del(ui->timer_pid);
989 if (ui->selected_cmd) 928 if (ui->selected_cmd)
990 free(ui->selected_cmd); 929 free(ui->selected_cmd);
991 if (ui->item_cache) 930 if (ui->cache)
992 _cache_free(ui->item_cache); 931 evisum_ui_item_cache_free(ui->cache);
993 932
994 evas_object_del(win); 933 evas_object_del(win);
995 934
@@ -1012,7 +951,7 @@ ui_process_win_add(int pid, const char *cmd)
1012 ui->selected_pid = pid; 951 ui->selected_pid = pid;
1013 ui->selected_cmd = strdup(cmd); 952 ui->selected_cmd = strdup(cmd);
1014 ui->poll_delay = 3; 953 ui->poll_delay = 3;
1015 ui->item_cache = NULL; 954 ui->cache = NULL;
1016 ui->sort_reverse = EINA_TRUE; 955 ui->sort_reverse = EINA_TRUE;
1017 ui->sort_cb = _sort_by_cpu_usage; 956 ui->sort_cb = _sort_by_cpu_usage;
1018 957
@@ -1050,7 +989,8 @@ ui_process_win_add(int pid, const char *cmd)
1050 evas_object_resize(win, 540 * elm_config_scale_get(), 480 * elm_config_scale_get()); 989 evas_object_resize(win, 540 * elm_config_scale_get(), 480 * elm_config_scale_get());
1051 evas_object_show(win); 990 evas_object_show(win);
1052 991
1053 _cache_init(ui); 992 ui->cache = evisum_ui_item_cache_new(ui->genlist_threads, _item_create, 10);
993
1054 _proc_info_update(ui); 994 _proc_info_update(ui);
1055} 995}
1056 996
diff --git a/src/bin/ui/ui_process_view.h b/src/bin/ui/ui_process_view.h
index 7f031f6..8aed51c 100644
--- a/src/bin/ui/ui_process_view.h
+++ b/src/bin/ui/ui_process_view.h
@@ -2,6 +2,7 @@
2#define __UI_PROCESS_H__ 2#define __UI_PROCESS_H__
3 3
4#include "ui.h" 4#include "ui.h"
5#include "ui_cache.h"
5 6
6typedef struct _Ui_Process { 7typedef struct _Ui_Process {
7 Evas_Object *win; 8 Evas_Object *win;
@@ -15,8 +16,9 @@ typedef struct _Ui_Process {
15 Evas_Object *btn_info; 16 Evas_Object *btn_info;
16 Evas_Object *btn_thread; 17 Evas_Object *btn_thread;
17 18
18 Evas_Object *entry_info; 19 Evas_Object *entry_info;
19 Evas_Object *genlist_threads; 20 Evas_Object *genlist_threads;
21 Evisum_Ui_Cache *cache;
20 22
21 Evas_Object *entry_pid_cmd; 23 Evas_Object *entry_pid_cmd;
22 Evas_Object *entry_pid_cmd_args; 24 Evas_Object *entry_pid_cmd_args;
@@ -42,8 +44,6 @@ typedef struct _Ui_Process {
42 44
43 Eina_Hash *hash_cpu_times; 45 Eina_Hash *hash_cpu_times;
44 46
45 Eina_List *item_cache;
46
47 int poll_delay; 47 int poll_delay;
48 char *selected_cmd; 48 char *selected_cmd;
49 int selected_pid; 49 int selected_pid;