Allocate memory when monitoring statistics starts 28/194828/1
authorYu <jiung.yu@samsung.com>
Fri, 7 Dec 2018 08:25:51 +0000 (17:25 +0900)
committerYu <jiung.yu@samsung.com>
Fri, 7 Dec 2018 08:27:17 +0000 (17:27 +0900)
Change-Id: Ibd25889af68f548477e5308ac7da01290f58603a
Signed-off-by: Yu Jiung <jiung.yu@samsung.com>
include/inm-statistics.h
src/inm-statistics.c

index dffd98791ece965aa68cd4828a3fc488e5368467..28c1999e07d51ccb3bea7456860bfab4166a3aab 100644 (file)
@@ -69,9 +69,6 @@ int inm_statistics_start_monitor(inm_statistics_callback cb, void *user_data);
 int inm_statistics_stop_monitor();
 int inm_statistics_is_enabled(gboolean *enabled);
 
-int inm_statistics_init();
-int inm_statistics_deinit();
-
 #ifdef __cplusplus
 }
 #endif
index d4de4db54b75f1438ceeaefab8db9463450fd84b..05ada97aade520a5ccf6dd9a90147e0ee57eb081 100644 (file)
 #define INM_PROC_DEV "/proc/net/dev"
 
 typedef struct {
-       gboolean is_initialized;
        gboolean is_enabled;
        guint timer_source_id;
        inm_statistics_callback cb;
        gpointer cb_user_data;
 } statistics_mon_s;
 
-statistics_mon_s stat_mon = {0,};
-
+static statistics_mon_s *g_p_stat_mon = NULL;
 
 static gboolean __get_statistics(gpointer user_data)
 {
@@ -60,7 +58,7 @@ static gboolean __get_statistics(gpointer user_data)
 
        __INM_FUNC_ENTER__;
 
-       if (!stat_mon.cb) {
+       if (!g_p_stat_mon || !g_p_stat_mon->cb) {
                __INM_FUNC_EXIT__;
                return TRUE;
        }
@@ -108,7 +106,7 @@ static gboolean __get_statistics(gpointer user_data)
                                &stats.tx_carrier,              /* tx carrier errors */
                                &stats.tx_compressed    /* tx compressed */
                                );
-               stat_mon.cb(&stats, user_data);
+               g_p_stat_mon->cb(&stats, user_data);
        } else {
                INM_LOGI("No matched Iface name in proc file");
        }
@@ -122,23 +120,30 @@ static gboolean __get_statistics(gpointer user_data)
 int inm_statistics_start_monitor(inm_statistics_callback cb, void *user_data)
 {
        gint ret = INM_STATISTICS_ERROR_NONE;
-       guint source_id = 0;
-       __INM_FUNC_ENTER__;
 
+       __INM_FUNC_ENTER__;
        if (!cb) {
                __INM_FUNC_ENTER__;
                return INM_STATISTICS_ERROR_INVALID_PARAM;
        }
 
-       source_id = g_timeout_add(STAT_POLLING_TIME,
-                       __get_statistics, NULL);
+       if (g_p_stat_mon) {
+               __INM_FUNC_ENTER__;
+               return INM_STATISTICS_ERROR_OPERATION_FAILED;
+       }
+
+       g_p_stat_mon = g_try_malloc0(sizeof(statistics_mon_s));
+       if (!g_p_stat_mon) {
+               __INM_FUNC_ENTER__;
+               return INM_STATISTICS_ERROR_OPERATION_FAILED;
+       }
 
-       if (stat_mon.timer_source_id >  0)
-               g_source_remove(stat_mon.timer_source_id);
+       g_p_stat_mon->timer_source_id = g_timeout_add(STAT_POLLING_TIME,
+                       __get_statistics, NULL);
 
-       stat_mon.timer_source_id = source_id;
-       stat_mon.cb = cb;
-       stat_mon.cb_user_data = user_data;
+       g_p_stat_mon->cb = cb;
+       g_p_stat_mon->cb_user_data = user_data;
+       g_p_stat_mon->is_enabled = TRUE;
 
        __INM_FUNC_EXIT__;
        return ret;
@@ -149,18 +154,16 @@ int inm_statistics_stop_monitor()
        int ret = INM_STATISTICS_ERROR_NONE;
        __INM_FUNC_ENTER__;
 
-       if (!stat_mon.is_enabled) {
+       if (!g_p_stat_mon) {
                __INM_FUNC_ENTER__;
                return INM_STATISTICS_ERROR_NONE;
        }
 
-       if (stat_mon.timer_source_id >  0) {
-               g_source_remove(stat_mon.timer_source_id);
-               stat_mon.timer_source_id = 0;
-       }
+       if (g_p_stat_mon->timer_source_id >  0)
+               g_source_remove(g_p_stat_mon->timer_source_id);
 
-       stat_mon.cb = NULL;
-       stat_mon.cb_user_data = NULL;
+       g_free(g_p_stat_mon);
+       g_p_stat_mon = NULL;
 
        __INM_FUNC_EXIT__;
        return ret;
@@ -171,30 +174,12 @@ int inm_statistics_is_enabled(gboolean *enabled)
        int ret = INM_STATISTICS_ERROR_NONE;
        __INM_FUNC_ENTER__;
 
-       if (!enabled) {
+       if (!g_p_stat_mon || !enabled) {
                __INM_FUNC_ENTER__;
                return INM_STATISTICS_ERROR_INVALID_PARAM;
        }
 
-       *enabled = stat_mon.is_enabled;
-
-       __INM_FUNC_EXIT__;
-       return ret;
-}
-
-int inm_statistics_init()
-{
-       int ret = INM_STATISTICS_ERROR_NONE;
-       __INM_FUNC_ENTER__;
-
-       __INM_FUNC_EXIT__;
-       return ret;
-}
-
-int inm_statistics_deinit()
-{
-       int ret = INM_STATISTICS_ERROR_NONE;
-       __INM_FUNC_ENTER__;
+       *enabled = g_p_stat_mon->is_enabled;
 
        __INM_FUNC_EXIT__;
        return ret;