util: resource: Rework double type attributes 58/272158/1
authorDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 7 Mar 2022 08:48:49 +0000 (17:48 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 10 Mar 2022 08:02:12 +0000 (17:02 +0900)
To support double type attributes with exact precison, instead of
casting double to void pointer, using double type allocated memory
as like string or array.

Change-Id: Ie432026f02ad0d22b98a24fdeed7ae1ddf5440ef
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
include/util/resource.h
src/resource/resource-display.c
src/resource/resource-system.c
src/util/resource.c

index 030a75e..527e466 100644 (file)
@@ -168,7 +168,8 @@ int get_resource_attr_int(struct resource *resource, u_int64_t attr_id, int32_t
 int get_resource_attr_int64(struct resource *resource, u_int64_t attr_id, int64_t *data);
 int get_resource_attr_uint(struct resource *resource, u_int64_t attr_id, u_int32_t *data);
 int get_resource_attr_uint64(struct resource *resource, u_int64_t attr_id, u_int64_t *data);
-int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double *data);
+int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double **data);
+int put_resource_attr_double(struct resource *resource, u_int64_t attr_id);
 int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char **data);
 int put_resource_attr_string(struct resource *resource, u_int64_t attr_id);
 int get_resource_attr_array(struct resource *resource, u_int64_t attr_id,
index f3b7af9..2b95337 100644 (file)
@@ -56,6 +56,7 @@ static int display_get_fps(const struct resource *res,
        GError *err = NULL;
        struct display_fps_data fps_data;
        int *disp_id;
+       double *fps;
        int ret = 0;
 
        if (!res || !res->priv || !attr || !data)
@@ -112,7 +113,14 @@ static int display_get_fps(const struct resource *res,
                        &fps_data.window, &fps_data.fps);
 
 out:
-       *data = (void *)(intptr_t)fps_data.fps;
+       fps = malloc(sizeof(double));
+       if (!fps) {
+               ret = -ENOMEM;
+               goto err_reply;
+       }
+
+       *fps = fps_data.fps;
+       *data = (void *)fps;
 
 err_reply:
        g_object_unref(reply);
index e62b6d2..46bd145 100644 (file)
@@ -82,19 +82,23 @@ static int system_get_avg_cpu_util(const struct resource *res,
                                void **data)
 {
        struct system_resouce_data *sysdata;
-       double util;
+       double *util;
 
        if (!res || !res->priv ||!attr || !data)
                return -EINVAL;
 
+       util = malloc(sizeof(double));
+       if (!util)
+               return -ENOMEM;
+
        sysdata = (struct system_resouce_data *)res->priv;
 
-       util = __calculate_cpu_util(attr->id, &sysdata->prev_avg, &sysdata->curr_avg);
+       *util = __calculate_cpu_util(attr->id, &sysdata->prev_avg, &sysdata->curr_avg);
        if (util < 0) {
                _W("failed to calculate average cpu util (%s: %s)\n", res->name, attr->name);
-               util = 0;
+               *util = 0.0;
        }
-       *data = (void *)(intptr_t)util;
+       *data = (void *)util;
 
        return 0;
 }
index 535148a..4128e97 100644 (file)
@@ -368,7 +368,7 @@ int get_resource_attr_uint64(struct resource *resource, u_int64_t attr_id, u_int
        return 0;
 }
 
-int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double *data)
+int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double **data)
 {
        struct resource_attribute_value *attr_value = NULL;
 
@@ -379,7 +379,24 @@ int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, doubl
        if (!attr_value)
                return -EINVAL;
 
-       *data = (double)(intptr_t)attr_value->data;
+       *data = (double *)attr_value->data;
+
+       return 0;
+}
+
+int put_resource_attr_double(struct resource *resource, u_int64_t attr_id)
+{
+       struct resource_attribute_value *attr_value = NULL;
+
+       if (!check_attr_validate(resource, attr_id, DATA_TYPE_DOUBLE))
+               return -EINVAL;
+
+       attr_value = get_resource_attr_value(resource, attr_id);
+       if (!attr_value)
+               return -EINVAL;
+
+       free(attr_value->data);
+       attr_value->data = NULL;
 
        return 0;
 }