pass: Use calloc to initialize the memory instead of malloc
authorChanwoo Choi <cw00.choi@samsung.com>
Wed, 1 Feb 2017 08:29:01 +0000 (17:29 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Thu, 2 Feb 2017 23:43:18 +0000 (08:43 +0900)
This patch just uses the calloc() to initialize the memory with zero
insead of malloc.

Change-Id: I1b45cb10101b5e79bad07d73b952a0af31b868b5
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/pass/pass-core.c
src/pass/pass-parser.c
src/pass/pass.c
src/pmqos/pmqos-parser.c
src/pmqos/pmqos.c

index 914281e5ea651cbcedf3fe07353d202837d2661f..548a9437bace553f46167641d096648a263be666 100644 (file)
@@ -213,7 +213,7 @@ struct pass_hotplug* pass_get_hotplug(struct pass_policy *policy,
        switch (type) {
        case PASS_GOV_STEP:
        case PASS_GOV_RADIATION:
-               hotplug = malloc(sizeof(struct pass_hotplug));
+               hotplug = calloc(1, sizeof(struct pass_hotplug));
                if (!hotplug) {
                        _E("cannot allocate the memory of struct pass_hotplug");
                        return NULL;
index f0cce820f504a786cc337d31525c44266d744ae5..563fddebc97e6067bf4809209b8a878988fc03cb 100644 (file)
@@ -72,8 +72,8 @@ static int pass_parse_scenario(struct parse_result *result, void *user_data,
                        scenario->num_scenarios = atoi(result->value);
 
                        if (scenario->num_scenarios > 0 && !scenario->list) {
-                               scenario->list = malloc(sizeof(struct pass_scenario)
-                                                       * scenario->num_scenarios);
+                               scenario->list = calloc(scenario->num_scenarios,
+                                               sizeof(struct pass_scenario));
                                if (!scenario->list) {
                                        _E("cannot allocate memory for Scenario\n");
                                        return -EINVAL;
@@ -223,23 +223,12 @@ static int pass_parse_core(struct parse_result *result, void *user_data)
        }
 
        if (policy->num_levels > 0 && !policy->pass_table) {
-               policy->pass_table = malloc(sizeof(struct pass_table)
-                              * policy->num_levels);
+               policy->pass_table = calloc(policy->num_levels,
+                                               sizeof(struct pass_table));
                if (!policy->pass_table) {
                        _E("cannot allocate memory for pass_table\n");
                        return -EINVAL;
                }
-               /*
-                * TODO: This is a dangerous way to initialize C-style struct type
-                * pointer variables. When we refactor source code of PASS, it is
-                * required to better use consturctor functions.
-                */
-
-               if (!memset(policy->pass_table, 0,
-                                       sizeof(struct pass_table) * policy->num_levels)){
-                       _E("cannot initialize pass_table\n");
-                       return -EINVAL;
-               }
        }
 
        return 0;
index 1c719ae1472a7fb0eee05546955af37fd23683b0..d372ac699987e0a1bc19b8fc229233f875962092 100644 (file)
@@ -157,16 +157,16 @@ static int pass_resource_init(struct pass_policy *policy)
        policy->cpufreq.num_nr_cpus = max_cpu;
 
        /* Allocate memory according to the number of data and cpu */
-       policy->pass_cpu_stats = malloc(sizeof(struct pass_cpu_stats)
-                                               * policy->num_pass_cpu_stats);
+       policy->pass_cpu_stats = calloc(policy->num_pass_cpu_stats,
+                                       sizeof(struct pass_cpu_stats));
 
        for (i = 0; i < policy->num_pass_cpu_stats; i++) {
                policy->pass_cpu_stats[i].load =
-                       malloc(sizeof(unsigned int) * policy->cpufreq.num_nr_cpus);
+                       calloc(policy->cpufreq.num_nr_cpus, sizeof(unsigned int));
                policy->pass_cpu_stats[i].nr_running =
-                       malloc(sizeof(unsigned int) * policy->cpufreq.num_nr_cpus);
+                       calloc(policy->cpufreq.num_nr_cpus, sizeof(unsigned int));
                policy->pass_cpu_stats[i].runnable_load =
-                       malloc(sizeof(unsigned int) * policy->cpufreq.num_nr_cpus);
+                       calloc(policy->cpufreq.num_nr_cpus, sizeof(unsigned int));
        }
 
        /* Get the instance of PASS governor */
@@ -182,8 +182,8 @@ static int pass_resource_init(struct pass_policy *policy)
        if (!policy->hotplug) {
                _E("cannot get the instance of PASS hotplug");
        } else {
-               policy->hotplug->sequence = malloc(sizeof(int)
-                                         * policy->cpufreq.num_nr_cpus);
+               policy->hotplug->sequence = calloc(policy->cpufreq.num_nr_cpus,
+                                               sizeof(int));
                for (i = 0; i < policy->cpufreq.num_nr_cpus; i++)
                        policy->hotplug->sequence[i] = i + pass_res->cdata.cpu;
        }
index f0baf8b9fbd613982be15cfaa0ed886ce80ee79d..e13e01a75157ff3f04a26f8f9360303dc76a9c16 100644 (file)
@@ -51,7 +51,8 @@ static int pmqos_parse_scenario(struct parse_result *result, void *user_data, un
                else if (MATCH(result->name, "scenario_num")) {
                        scenarios->num = atoi(result->value);
                        if (scenarios->num > 0) {
-                               scenarios->list = malloc(sizeof(struct scenario)*scenarios->num);
+                               scenarios->list = calloc(scenarios->num,
+                                               sizeof(struct scenario));
                                if (!scenarios->list) {
                                        _E("failed to allocat memory for scenario");
                                        return -errno;
index a1c5b3f41a062a3b46adf8bc19a30d94882442be..72042d018b7c0f55b60ec6596410110c283335a7 100644 (file)
@@ -205,7 +205,7 @@ static int pmqos_cpu_request(const char *name, int val)
        }
        /* add cpu */
        if (!found) {
-               cpu = malloc(sizeof(struct pmqos_cpu));
+               cpu = calloc(1, sizeof(struct pmqos_cpu));
                if (!cpu)
                        return -ENOMEM;
 
@@ -332,7 +332,7 @@ static int get_methods_from_conf(const char *path, struct edbus_method **edbus_m
                return 0;
 
        /* allocate edbus methods structure */
-       methods = malloc(sizeof(struct edbus_method)*scenarios.num);
+       methods = calloc(scenarios.num, sizeof(struct edbus_method));
        if (!methods) {
                _E("failed to allocate methods memory : %s", strerror(errno));
                /* release scenarios memory */