Merge "free memory and file handle when it is no longer in use"

This commit is contained in:
Zuul 2018-11-15 14:37:15 +00:00 committed by Gerrit Code Review
commit ddefd385c5
2 changed files with 15 additions and 5 deletions

View File

@ -130,14 +130,17 @@ int online_cpu(unsigned cpu)
rc = read(fd, &val, 1);
if (rc != 1){
ERR_LOG("can't read cpu online value: %m");
close(fd);
return -1;
}
if (val == '1') {
ERR_LOG("cpu %d is already online", cpu);
close(fd);
return 0;
}
val = '1';
rc = write(fd, &val, 1);
close(fd);
if (rc != 1){
ERR_LOG("can't set cpu %d online", cpu);
return -1;
@ -160,14 +163,17 @@ int offline_cpu(unsigned cpu)
rc = read(fd, &val, 1);
if (rc != 1){
ERR_LOG("can't read cpu online value: %m");
close(fd);
return -1;
}
if (val == '0') {
ERR_LOG("cpu %d is already offline\n", cpu);
close(fd);
return 0;
}
val = '0';
rc = write(fd, &val, 1);
close(fd);
if (rc != 1){
ERR_LOG("can't set cpu %d offline", cpu);
return -1;
@ -189,6 +195,7 @@ int get_highest_online_cpu(void)
}
rc = read(fd, buf, sizeof(buf));
close(fd);
if (rc < 2) {
ERR_LOG("error parsing /sys/devices/system/cpu/online, too few chars");
return -1;
@ -304,6 +311,7 @@ pick_cpu:
// no need to release jobj_array as its ownership is transferred to jobj_response
struct json_object *jobj_array = new_json_obj_from_array(current_online_cpus);
json_object_object_add(jobj_response, ONLINE_CPUS, jobj_array);
free(current_online_cpus);
return;
failed:
@ -374,7 +382,7 @@ void cpu_scale_up(json_object *jobj_request,
// no need to release jobj_array as its ownership is transferred to jobj_response
struct json_object *jobj_array = new_json_obj_from_array(current_online_cpus);
json_object_object_add(jobj_response, ONLINE_CPUS, jobj_array);
free(current_online_cpus);
return;
failed:

View File

@ -65,15 +65,15 @@ struct online_cpus *range_to_array(const char *range)
struct online_cpus *cpuarray = (struct online_cpus *) malloc(BUFLEN);
int start, end;
int inrange = 0;
char *token, *tmp;
char *token, *tmp, *tobe_free;
int done = 0;
tmp = strdup(range);
strcpy(tmp, range);
token = tmp;
tobe_free = strdup(range);
token = tmp = tobe_free;
if (*tmp == '\0') {
/* empty string, no online cpus */
cpuarray->numcpus = 0;
free(tobe_free);
return cpuarray;
}
@ -125,9 +125,11 @@ struct online_cpus *range_to_array(const char *range)
break;
}
cpuarray->numcpus = end+1;
free(tobe_free);
return cpuarray;
error:
free(cpuarray);
free(tobe_free);
return 0;
}