From: Chanwoo Choi Date: Mon, 4 Jul 2022 21:15:56 +0000 (+0900) Subject: resource: Use getter/setter of struct resource for encapsulation X-Git-Tag: accepted/tizen/unified/20220715.141323~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a502594b7f7e2911ddfdd42d55e35c58017d006a;p=platform%2Fcore%2Fsystem%2Fpass.git resource: Use getter/setter of struct resource for encapsulation Prevent the direct access of the fields of struct resource by using getter/setter functiosn of struct resource. By this change, keep the encapsulation of struct resource and only communicate via getter/setter functions. Change-Id: I4c7b92b79197ef67688773a5ef6b7db5e044bc09 Signed-off-by: Chanwoo Choi --- diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c index 482132a..1cf1a69 100644 --- a/src/monitor/request-handler.c +++ b/src/monitor/request-handler.c @@ -49,14 +49,17 @@ static void update_resource(gpointer key, gpointer value, gpointer user_data) ret = update_resource_attrs(res); if (ret < 0) _E("failed to update resource attributes (name:%s,id:%d)\n", - res->name, res->id); + get_resource_name(res), + get_resource_id(res)); } static void register_resource_to_client(struct request_client *client, struct resource *res) { - g_hash_table_insert(client->resource_table, GINT_TO_POINTER(res->id), - (gpointer)res); + g_hash_table_insert(client->resource_table, + GINT_TO_POINTER(get_resource_id(res)), + (gpointer)res); +} static void unregister_resource_from_client(struct request_client *client, int resource_id) @@ -94,7 +97,7 @@ static int handle_request_create_resource(struct request_client *client, char *a register_resource_to_client(client, res); - return res->id; + return get_resource_id(res); } static int handle_request_delete_resource(struct request_client *client, char *args) @@ -481,8 +484,7 @@ handle_request_get_resource_ts(struct request_client *client, char *args, if (!res) return -EINVAL; - *start = res->ts_start; - *end = res->ts_end; + get_resource_ts(res, start, end); return 0; } diff --git a/src/resource/resource-bus.c b/src/resource/resource-bus.c index c7e9ab7..a1f2a9a 100644 --- a/src/resource/resource-bus.c +++ b/src/resource/resource-bus.c @@ -42,34 +42,40 @@ static int bus_get_value_from_hal_power(const struct resource *res, void *data) { struct bus_context *ctx; + int type; int *val = (int *)data; int _val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } + type = get_resource_type(res); + switch (attr->id) { case BUS_ATTR_CUR_FREQ: - _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name); break; case BUS_ATTR_MIN_FREQ: - _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_min_freq(type, ctx->device_name); break; case BUS_ATTR_MAX_FREQ: - _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_max_freq(type, ctx->device_name); break; case BUS_ATTR_AVAILABLE_MIN_FREQ: - _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name); break; case BUS_ATTR_AVAILABLE_MAX_FREQ: - _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name); break; default: return -EINVAL; @@ -87,21 +93,25 @@ static int bus_get_curr_governor(const struct resource *res, const struct resource_attribute *attr, void *data) { - struct bus_context *ctx; + struct bus_context *ctx;; char *buf = (char *)data; int val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } - val = hal_power_dvfs_get_curr_governor(res->type, ctx->device_name, buf); + val = hal_power_dvfs_get_curr_governor(get_resource_type(res), + ctx->device_name, buf); if (val < 0) return -EINVAL; @@ -162,18 +172,20 @@ static int bus_setup_device_id(const struct resource *res, const struct resource_device *device; int device_id = (int)(intptr_t)data; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) + return -EINVAL; + + ctx = get_resource_privdata(res); + if (!ctx) return -EINVAL; - device = find_resource_device(res->type, device_id); + device = find_resource_device(get_resource_type(res), device_id); if (!device) { _E("Not available resource: type: %s, index: %d\n", - res->name, device_id); + get_resource_name(res), device_id); return -EINVAL; } - ctx = res->priv; - if (ctx->device_name) free(ctx->device_name); @@ -201,7 +213,7 @@ static int bus_init(struct resource *res) if (!ctx) return -ENOMEM; - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -210,15 +222,19 @@ static void bus_exit(struct resource *res) { struct bus_context *ctx; - if (res && res->priv) { - ctx = res->priv; - if (ctx->device_name) { - free(ctx->device_name); - ctx->device_name = NULL; - } - free(ctx); - res->priv = NULL; + if (!res) + return; + + ctx = get_resource_privdata(res); + if (!ctx) + return; + + if (ctx->device_name) { + free(ctx->device_name); + ctx->device_name = NULL; } + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver bus_resource_driver = { @@ -234,4 +250,3 @@ static const struct resource_driver bus_resource_driver = { }, }; RESOURCE_DRIVER_REGISTER(&bus_resource_driver) - diff --git a/src/resource/resource-cpu.c b/src/resource/resource-cpu.c index c25a6c9..1651e30 100644 --- a/src/resource/resource-cpu.c +++ b/src/resource/resource-cpu.c @@ -43,33 +43,38 @@ static int cpu_get_value_from_hal_power(const struct resource *res, { struct cpu_context *ctx; int *val = (int *)data; - int _val; + int _val, type; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name); + _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } + type = get_resource_type(res); + switch (attr->id) { case CPU_ATTR_CUR_FREQ: - _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name); break; case CPU_ATTR_MIN_FREQ: - _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_min_freq(type, ctx->device_name); break; case CPU_ATTR_MAX_FREQ: - _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_max_freq(type, ctx->device_name); break; case CPU_ATTR_AVAILABLE_MIN_FREQ: - _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name); break; case CPU_ATTR_AVAILABLE_MAX_FREQ: - _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name); break; default: return -EINVAL; @@ -91,17 +96,21 @@ static int cpu_get_curr_governor(const struct resource *res, char *buf = (char *)data; int val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name); + _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } - val = hal_power_dvfs_get_curr_governor(res->type, ctx->device_name, buf); + val = hal_power_dvfs_get_curr_governor(get_resource_type(res), + ctx->device_name, buf); if (val < 0) return -EINVAL; @@ -162,18 +171,20 @@ static int cpu_setup_cluster_id(const struct resource *res, const struct resource_device *device; int resource_index = (int)(intptr_t)data; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) + return -EINVAL; + + ctx = get_resource_privdata(res); + if (!ctx) return -EINVAL; - device = find_resource_device(res->type, resource_index); + device = find_resource_device(get_resource_type(res), resource_index); if (!device) { _E("Not available resource: type: %s, index: %d\n", - res->name, resource_index); + get_resource_name(res), resource_index); return -EINVAL; } - ctx = res->priv; - if (ctx->device_name) free(ctx->device_name); @@ -195,13 +206,13 @@ static const struct resource_control cpu_ctrls[] = { static int cpu_init(struct resource *res) { - struct cpu_context *ctx; + struct cpu_context *ctx = get_resource_privdata(res); ctx = calloc(1, sizeof(struct cpu_context)); if (!ctx) return -ENOMEM; - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -210,15 +221,20 @@ static void cpu_exit(struct resource *res) { struct cpu_context *ctx; - if (res && res->priv) { - ctx = res->priv; - if (ctx->device_name) { - free(ctx->device_name); - ctx->device_name = NULL; - } - free(ctx); - res->priv = NULL; + if (!res) + return; + + ctx = get_resource_privdata(res); + if (!ctx) + return; + + if (ctx->device_name) { + free(ctx->device_name); + ctx->device_name = NULL; } + + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver cpu_resource_driver = { diff --git a/src/resource/resource-display.c b/src/resource/resource-display.c index 33b8aee..ea05b32 100644 --- a/src/resource/resource-display.c +++ b/src/resource/resource-display.c @@ -59,13 +59,16 @@ static int display_get_fps(const struct resource *res, double *fps = (double *)data; int ret = 0; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - disp_id = res->priv; + disp_id = get_resource_privdata(res); + if (!disp_id) + return -EINVAL; if (*disp_id < 0) { - _E("%s: DISPLAY_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: DISPLAY_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -140,15 +143,19 @@ static int display_setup_device_id(const struct resource *res, const struct resource_control *ctrl, const void *data) { - int *disp_id; + int *disp_id = NULL; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) return -EINVAL; - disp_id = res->priv; + disp_id = get_resource_privdata(res); + if (!disp_id) + return -EINVAL; *disp_id = (int)(intptr_t)data; + set_resource_privdata(res, disp_id); + return 0; } @@ -172,16 +179,26 @@ static int display_init(struct resource *res) *disp_id = -1; - res->priv = disp_id; + set_resource_privdata(res, disp_id); return 0; } static void display_exit(struct resource *res) { - if (res && res->priv) - free(res->priv); + int *disp_id; + + if (!res) + return; + + disp_id = get_resource_privdata(res); + if (!disp_id) + return; + + free(disp_id); + set_resource_privdata(res, NULL); } + static const struct resource_driver display_resource_driver = { .name = "DISPLAY", .type = RESOURCE_TYPE_DISPLAY, diff --git a/src/resource/resource-gpu.c b/src/resource/resource-gpu.c index b736650..c9cc0b3 100644 --- a/src/resource/resource-gpu.c +++ b/src/resource/resource-gpu.c @@ -42,34 +42,40 @@ static int gpu_get_value_from_hal_power(const struct resource *res, void *data) { struct gpu_context *ctx; + int type; int *val = (int *)data; int _val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } + type = get_resource_type(res); + switch (attr->id) { case GPU_ATTR_CUR_FREQ: - _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name); break; case GPU_ATTR_MIN_FREQ: - _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_min_freq(type, ctx->device_name); break; case GPU_ATTR_MAX_FREQ: - _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_max_freq(type, ctx->device_name); break; case GPU_ATTR_AVAILABLE_MIN_FREQ: - _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name); break; case GPU_ATTR_AVAILABLE_MAX_FREQ: - _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name); break; default: return -EINVAL; @@ -91,17 +97,21 @@ static int gpu_get_curr_governor(const struct resource *res, char *buf = (char *)data; int val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } - val = hal_power_dvfs_get_curr_governor(res->type, ctx->device_name, buf); + val = hal_power_dvfs_get_curr_governor(get_resource_type(res), + ctx->device_name, buf); if (val < 0) return -EINVAL; @@ -162,18 +172,20 @@ static int gpu_setup_device_id(const struct resource *res, const struct resource_device *device; int resource_index = (int)(intptr_t)data; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) + return -EINVAL; + + ctx = get_resource_privdata(res); + if (!ctx) return -EINVAL; - device = find_resource_device(res->type, resource_index); + device = find_resource_device(get_resource_type(res), resource_index); if (!device) { _E("Not available resource: type: %s, index: %d\n", - res->name, resource_index); + get_resource_name(res), resource_index); return -EINVAL; } - ctx = res->priv; - if (ctx->device_name) free(ctx->device_name); @@ -201,7 +213,7 @@ static int gpu_init(struct resource *res) if (!ctx) return -ENOMEM; - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -210,15 +222,19 @@ static void gpu_exit(struct resource *res) { struct gpu_context *ctx; - if (res && res->priv) { - ctx = res->priv; - if (ctx->device_name) { - free(ctx->device_name); - ctx->device_name = NULL; - } - free(ctx); - res->priv = NULL; + if (!res) + return; + + ctx = get_resource_privdata(res); + if (!ctx) + return; + + if (ctx->device_name) { + free(ctx->device_name); + ctx->device_name = NULL; } + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver gpu_resource_driver = { diff --git a/src/resource/resource-process-group.c b/src/resource/resource-process-group.c index b0b2ade..5d056ed 100644 --- a/src/resource/resource-process-group.c +++ b/src/resource/resource-process-group.c @@ -64,10 +64,12 @@ static int process_group_get_pid_list(const struct resource *res, int *data_array; int i = 0; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map) return -EINVAL; @@ -102,10 +104,12 @@ static int process_group_get_comm_list(const struct resource *res, char **data_array; int i = 0; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map) return -EINVAL; @@ -148,10 +152,12 @@ static int process_group_get_cpu_util(const struct resource *res, struct process_group_context *ctx; double *util = (double *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map || ctx->pid < 0) return -EINVAL; @@ -168,10 +174,12 @@ static int process_group_get_mem(const struct resource *res, struct process_group_context *ctx; u_int64_t *mem = (u_int64_t *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map || ctx->pid < 0) return -EINVAL; @@ -209,10 +217,12 @@ static int process_group_get_disk_bps(const struct resource *res, struct process_group_context *ctx; u_int32_t *bps = (u_int32_t *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map || ctx->pid < 0) return -EINVAL; @@ -337,10 +347,12 @@ static int process_group_setup_root_pid(const struct resource *res, int target_pid; int ret; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; target_pid = (int)(intptr_t)data; @@ -501,10 +513,12 @@ static int process_group_prepare_update(struct resource *res) int ret = 0; pid_t tgid; - if (!res || !res->priv) + if (!res) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; task_dir = opendir(PROC_DIR_PATH); if (!task_dir) @@ -652,7 +666,7 @@ static int process_group_init(struct resource *res) ctx->pid = -1; ctx->pi_map = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, free_node); - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -661,16 +675,20 @@ static void process_group_exit(struct resource *res) { struct process_group_context *ctx; - if (res && res->priv) { - ctx = res->priv; + if (!res) + return; - if (ctx->pi_map) { - g_hash_table_destroy(ctx->pi_map); - ctx->pi_map = NULL; - } + ctx = get_resource_privdata(res); + if (!ctx) + return; - free(res->priv); + if (ctx->pi_map) { + g_hash_table_destroy(ctx->pi_map); + ctx->pi_map = NULL; } + + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver process_group_driver = { diff --git a/src/resource/resource-process.c b/src/resource/resource-process.c index 1d9b39a..5d99b3e 100644 --- a/src/resource/resource-process.c +++ b/src/resource/resource-process.c @@ -50,13 +50,16 @@ static int process_get_cpu_util(const struct resource *res, struct taskstats *prev, *curr; double *util = (double *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + _E("resource %s is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -81,13 +84,16 @@ static int process_get_mem_attrs(const struct resource *res, struct taskstats *curr; u_int64_t *mem = (u_int64_t *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + _E("resource %s is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -137,13 +143,16 @@ static int process_get_disk_bps(const struct resource *res, u_int64_t period; u_int32_t *bps = (u_int32_t *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + _E("resource %s is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -171,13 +180,16 @@ static int process_get_context_data(const struct resource *res, { struct process_context *ctx; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + _E("resource %s is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -350,13 +362,16 @@ static int process_setup_tgid(const struct resource *res, struct process_context *ctx; u_int64_t total_memory; int ret; - bool include_gpu_mem = is_resource_attr_interested(res, PROCESS_GROUP_ATTR_MEM_GPU); + bool include_gpu_mem; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; + include_gpu_mem = is_resource_attr_interested(res, PROCESS_GROUP_ATTR_MEM_GPU); total_memory = ctx->total_memory; memset(ctx, 0, sizeof(*ctx)); @@ -393,10 +408,19 @@ static const struct resource_control process_ctrls[] = { static int process_prepare_update(struct resource *res) { - struct process_context *ctx = res->priv; + struct process_context *ctx; u_int64_t total_time; int ret, online; - bool include_gpu_mem = is_resource_attr_interested(res, PROCESS_GROUP_ATTR_MEM_GPU); + bool include_gpu_mem; + + if (!res) + return -EINVAL; + + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; + + include_gpu_mem = is_resource_attr_interested(res, PROCESS_GROUP_ATTR_MEM_GPU); memcpy(&ctx->prev, &ctx->curr, sizeof(struct taskstats)); @@ -440,7 +464,7 @@ static int process_init(struct resource *res) return -EINVAL; } - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -449,11 +473,15 @@ static void process_exit(struct resource *res) { struct process_context *ctx; - if (res->priv) { - ctx = res->priv; - free(ctx); - res->priv = NULL; - } + if (!res) + return; + + ctx = get_resource_privdata(res); + if (!ctx) + return; + + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver process_resource_driver = { diff --git a/src/resource/resource-system.c b/src/resource/resource-system.c index 6f9f2a8..99a22f6 100644 --- a/src/resource/resource-system.c +++ b/src/resource/resource-system.c @@ -84,14 +84,17 @@ static int system_get_avg_cpu_util(const struct resource *res, struct system_resouce_data *sysdata; double *util = (double *)data; - if (!res || !res->priv ||!attr || !data) + if (!res || !attr || !data) return -EINVAL; - sysdata = (struct system_resouce_data *)res->priv; + sysdata = get_resource_privdata(res); + if (!sysdata) + return -EINVAL; *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); + _W("failed to calculate average cpu util (%s: %s)\n", + get_resource_name(res), attr->name); *util = 0.0; } @@ -107,10 +110,12 @@ static int system_get_per_cpu_util(const struct resource *res, double *utils; int i; - if (!res || !res->priv ||!attr || !data) + if (!res || !attr || !data) return -EINVAL; - sysdata = (struct system_resouce_data *)res->priv; + sysdata = get_resource_privdata(res); + if (!sysdata) + return -EINVAL; array->type = DATA_TYPE_DOUBLE; array->length = sysdata->num_possible_cpus; @@ -129,7 +134,8 @@ static int system_get_per_cpu_util(const struct resource *res, &sysdata->prev_cpus[i], &sysdata->curr_cpus[i]); if (utils[i] < 0) { - _W("failed to calculate per-cpu util (%s: %s)\n", res->name, attr->name); + _W("failed to calculate per-cpu util (%s: %s)\n", + get_resource_name(res), attr->name); utils[i] = 0; } } @@ -223,6 +229,7 @@ static const struct resource_attribute system_attrs[] = { static int system_driver_init(struct resource *res) { struct system_resouce_data *sysdata; + const char *res_name = get_resource_name(res); int ret; sysdata = calloc(1, sizeof(struct system_resouce_data)); @@ -231,7 +238,7 @@ static int system_driver_init(struct resource *res) ret = kernel_get_possible_cpu_num(); if (ret < 0) { - _I("failed to get possible cpu on system driver (%s)\n", res->name); + _I("failed to get possible cpu on system driver (%s)\n", res_name); goto err; } sysdata->num_possible_cpus = ret; @@ -239,7 +246,7 @@ static int system_driver_init(struct resource *res) sysdata->prev_cpus = calloc(sysdata->num_possible_cpus, sizeof(struct cpu_stat)); if (!sysdata->prev_cpus) { - _I("failed to allocate memory of prev_cpus (%s)\n", res->name); + _I("failed to allocate memory of prev_cpus (%s)\n", res_name); ret = -ENOMEM; goto err; } @@ -247,12 +254,12 @@ static int system_driver_init(struct resource *res) sysdata->curr_cpus = calloc(sysdata->num_possible_cpus, sizeof(struct cpu_stat)); if (!sysdata->curr_cpus) { - _I("failed to allocate memory of curr_cpus (%s)\n", res->name); + _I("failed to allocate memory of curr_cpus (%s)\n", res_name); ret = -ENOMEM; goto err_prev_cpus; } - res->priv = (void *)sysdata; + set_resource_privdata(res, (void *)sysdata); return 0; @@ -267,26 +274,32 @@ err: static void system_driver_exit(struct resource *res) { - struct system_resouce_data *sysdata - = (struct system_resouce_data *)res->priv; + struct system_resouce_data *sysdata; + + if (!res) + return; + + sysdata = get_resource_privdata(res); + if (!sysdata) + return; free(sysdata->prev_cpus); free(sysdata->curr_cpus); free(sysdata); - res->priv = NULL; + set_resource_privdata(res, NULL); } static int system_driver_prepare_update(struct resource *res) { - struct system_resouce_data *sysdata - = (struct system_resouce_data *)res->priv; + struct system_resouce_data *sysdata = get_resource_privdata(res); + const char *res_name = get_resource_name(res); int ret; /* Get the average cpu utilization of all cpus */ memcpy(&sysdata->prev_avg, &sysdata->curr_avg, sizeof(sysdata->prev_avg)); ret = kernel_get_total_cpu_stat(&sysdata->curr_avg); if (ret < 0) { - _I("failed to calculate average cpu util (%s)\n", res->name); + _I("failed to calculate average cpu util (%s)\n", res_name); return ret; } @@ -297,7 +310,7 @@ static int system_driver_prepare_update(struct resource *res) sysdata->num_possible_cpus, &sysdata->num_online_cpus); if (ret < 0) { - _I("failed to calculate per-cpu util (%s)\n", res->name); + _I("failed to calculate per-cpu util (%s)\n", res_name); return ret; }