summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandeep Singh <randeep.s@samsung.com>2021-06-09 05:02:04 +0000
committerGerrit Code Review <gerrit@review>2021-06-09 05:02:04 +0000
commit355ade454ee6ca74d0709803a350549a22b2e06f (patch)
tree8f3e5b9678221dc07057c708e097ae6b25e27f74
parenta14b6209e4c59a97dde106bb68666f534e4e705e (diff)
parenta1772216ce7ae95a1fa97b4862609d454f4e234c (diff)
downloadbatterymonitor-355ade454ee6ca74d0709803a350549a22b2e06f.tar.gz
batterymonitor-355ade454ee6ca74d0709803a350549a22b2e06f.tar.bz2
batterymonitor-355ade454ee6ca74d0709803a350549a22b2e06f.zip
Merge "[Non-ACR] hold pid always, change app_id" into tizen
-rw-r--r--src/battery_dump/bm_listeners.c83
1 files changed, 50 insertions, 33 deletions
diff --git a/src/battery_dump/bm_listeners.c b/src/battery_dump/bm_listeners.c
index 63c796e..f504b97 100644
--- a/src/battery_dump/bm_listeners.c
+++ b/src/battery_dump/bm_listeners.c
@@ -108,15 +108,16 @@ static void bd_set_free_data_object(void)
static void bd_get_app_id_from_table(gint pid, char **app_id)
{
- if (pid_map == NULL) {
- _ERR("invalid pid_map");
+ BD_CHECK_VALIDITY_RETURN((pid_map != NULL), {});
+
+ if (g_hash_table_size(pid_map) <= 0) {
+ _WARN("empty app_id-pid map");
return;
}
- _DBG("app_id for pid[%d], table size[%d]", pid, g_hash_table_size(pid_map));
+ _DBG("get app_id for pid[%d], table size[%d]", pid, g_hash_table_size(pid_map));
- void *orig_pid = NULL;
- void *orig_app_id = NULL;
+ void *orig_pid = NULL, *orig_app_id = NULL;
if (g_hash_table_lookup_extended(pid_map, &pid, &orig_pid, &orig_app_id) == true) {
*app_id = strdup((char *)orig_app_id);
@@ -131,23 +132,53 @@ static void bd_get_app_id_from_table(gint pid, char **app_id)
static void bd_insert_app_id_into_table(gint pid, char *appid)
{
- if (appid == NULL) {
- _ERR("invalid appid");
- return;
- }
+ BD_CHECK_VALIDITY_RETURN((appid != NULL), {});
- /* redundant keys are not required */
- if (g_hash_table_contains(pid_map, &pid) == true) {
- _DBG("key[%d] already present, no insertion required", pid);
- return;
- }
+ gint* lpid = NULL;
+ static bool isEmpty = true;
- gint* lpid = g_new0(gint, 1);
- if (lpid == NULL) {
- _ERR("failed to allocate memory");
- return;
+ _DBG("insert app_id for pid[%d], table size[%d]", pid, g_hash_table_size(pid_map));
+
+ if (!isEmpty) {
+ gpointer orig_pid = NULL, orig_app_id = NULL;
+ /* if not empty then check if pid-app_id is redundant */
+ if (g_hash_table_lookup_extended(pid_map, &pid, &orig_pid, &orig_app_id) == true) {
+ /* redundant pid, check if redundant app_id */
+ if (g_strcmp0(appid, (char *)orig_app_id) == 0) {
+ _DBG("redundant pid[%d], app_id[%s]", pid, appid);
+ return;
+ } else {
+ _DBG("replacing pid[%d], app_id[%s]", pid, appid);
+ /* app_id is not redundant,
+ * means this pid is assigned to new app_id
+ * replace old app_id with new app_id, delete old pid-app_id */
+ lpid = g_new0(gint, 1);
+ BD_CHECK_VALIDITY_RETURN((lpid != NULL), {});
+
+ *lpid = pid;
+
+ if (g_hash_table_replace(pid_map, lpid, strdup(appid)) == false)
+ _ERR("failed to replace old app_id");
+ }
+ } else {
+ GHashTableIter iter;
+ gpointer stale_pid = NULL, stale_app_id = NULL;
+
+ g_hash_table_iter_init(&iter, pid_map);
+ /* delete stale pid-app_id */
+ while (g_hash_table_iter_next(&iter, &stale_pid, &stale_app_id)) {
+ if (g_strcmp0(appid, (char *)stale_app_id) == 0) {
+ _DBG("removed stale - pid[%d], app_id[%s]", \
+ *((int *)stale_pid), (char *)stale_app_id);
+ g_hash_table_iter_remove(&iter);
+ }
+ }
+ }
}
+ lpid = g_new0(gint, 1);
+ BD_CHECK_VALIDITY_RETURN((lpid != NULL), {});
+
*lpid = pid;
_DBG("creating map - pid[%d], appid[%s]", *lpid, appid);
@@ -155,19 +186,8 @@ static void bd_insert_app_id_into_table(gint pid, char *appid)
if (g_hash_table_insert(pid_map, lpid, strdup(appid)) == false)
_WARN("key already available, replacing old value");
- return;
-}
+ isEmpty = false;
-static void bd_remove_app_id_from_table(gint pid)
-{
- if (g_hash_table_contains(pid_map, &pid) != true) {
- _DBG("key[%d] not present in list", pid);
- return;
- }
-
- g_hash_table_remove(pid_map, &pid);
-
- _DBG("removed key[%d] from table", pid);
return;
}
@@ -2112,9 +2132,6 @@ static void _bd_listener_app_terminate_signal_cb(GDBusConnection *conn, const gc
return;
}
- /* remove app_id from table as terminate signal is received */
- bd_remove_app_id_from_table(pid);
-
/* get event object */
event_pool *event = bd_listener_get_event_obj(LISTEN_APP_STATUS, val, app_id);
if (event == NULL) {