summaryrefslogtreecommitdiffstats
path: root/src/hardware
diff options
context:
space:
mode:
authorPeter Schiffer <pschiffe@redhat.com>2013-04-08 20:25:59 +0200
committerPeter Schiffer <pschiffe@redhat.com>2013-04-08 20:25:59 +0200
commit699a49933d2b2690888f719a447682d85c9942b0 (patch)
treeabaf80cecec848db033318cfe84bac08b884dbdc /src/hardware
parent9b77270eebd073ab4060ac84e4b40fc0652dff07 (diff)
downloadopenlmi-providers-699a49933d2b2690888f719a447682d85c9942b0.tar.gz
openlmi-providers-699a49933d2b2690888f719a447682d85c9942b0.tar.xz
openlmi-providers-699a49933d2b2690888f719a447682d85c9942b0.zip
Hardware: Code clean up
* Got rid of asprintf() where suitable (used static memory instead) * Removed if conditional before free() * Replaced more hard coded numbers with LMI constants
Diffstat (limited to 'src/hardware')
-rw-r--r--src/hardware/LMI_AssociatedProcessorCacheMemoryProvider.c46
-rw-r--r--src/hardware/LMI_Hardware.h3
-rw-r--r--src/hardware/LMI_ProcessorCacheMemoryProvider.c20
-rw-r--r--src/hardware/LMI_ProcessorCapabilitiesProvider.c57
-rw-r--r--src/hardware/LMI_ProcessorElementCapabilitiesProvider.c40
-rw-r--r--src/hardware/LMI_ProcessorProvider.c48
-rw-r--r--src/hardware/cpuinfo.c9
-rw-r--r--src/hardware/dmidecode.c70
-rw-r--r--src/hardware/lscpu.c4
-rw-r--r--src/hardware/sysfs.c18
-rw-r--r--src/hardware/utils.c12
-rw-r--r--src/hardware/utils.h2
12 files changed, 89 insertions, 240 deletions
diff --git a/src/hardware/LMI_AssociatedProcessorCacheMemoryProvider.c b/src/hardware/LMI_AssociatedProcessorCacheMemoryProvider.c
index b06aa22..1af1bad 100644
--- a/src/hardware/LMI_AssociatedProcessorCacheMemoryProvider.c
+++ b/src/hardware/LMI_AssociatedProcessorCacheMemoryProvider.c
@@ -155,32 +155,26 @@ static CMPIStatus LMI_AssociatedProcessorCacheMemoryEnumInstances(
&lmi_assoc_cache, &lmi_cpu_cache);
cache_level = get_cache_level(dmi_cpu_caches[j].level);
- if (cache_level == 1) {
- char *other_level;
- if (asprintf(&other_level, "%u",
- dmi_cpu_caches[j].level) < 0) {
- other_level = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+ if (cache_level == LMI_AssociatedProcessorCacheMemory_Level_Other) {
+ char other_level[LONG_INT_LEN] = "";
+ snprintf(other_level, LONG_INT_LEN, "%u",
+ dmi_cpu_caches[j].level);
LMI_AssociatedProcessorCacheMemory_Set_OtherLevelDescription(
&lmi_assoc_cache, other_level);
- free(other_level);
- other_level = NULL;
}
write_policy = get_write_policy(dmi_cpu_caches[j].op_mode);
- if (write_policy == 1) {
+ if (write_policy == LMI_AssociatedProcessorCacheMemory_WritePolicy_Other) {
LMI_AssociatedProcessorCacheMemory_Set_OtherWritePolicyDescription(
&lmi_assoc_cache, dmi_cpu_caches[j].op_mode);
}
cache_type = get_cache_type(dmi_cpu_caches[j].type);
- if (cache_type == 1) {
+ if (cache_type == LMI_AssociatedProcessorCacheMemory_CacheType_Other) {
LMI_AssociatedProcessorCacheMemory_Set_OtherCacheTypeDescription(
&lmi_assoc_cache, dmi_cpu_caches[j].type);
}
associativity = get_cache_associativity_dmi(
dmi_cpu_caches[j].associativity);
- if (associativity == 1) {
+ if (associativity == LMI_AssociatedProcessorCacheMemory_Associativity_Other) {
LMI_AssociatedProcessorCacheMemory_Set_OtherAssociativityDescription(
&lmi_assoc_cache,
dmi_cpu_caches[j].associativity);
@@ -232,15 +226,9 @@ static CMPIStatus LMI_AssociatedProcessorCacheMemoryEnumInstances(
if (dmi_cpus_nb > 0) {
LMI_ProcessorRef_Set_DeviceID(&lmi_cpu, dmi_cpus[j].id);
} else {
- char *cpu_id;
- if (asprintf(&cpu_id, "%u", j) < 0) {
- cpu_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+ char cpu_id[LONG_INT_LEN] = "";
+ snprintf(cpu_id, LONG_INT_LEN, "%u", j);
LMI_ProcessorRef_Set_DeviceID(&lmi_cpu, cpu_id);
- free(cpu_id);
- cpu_id = NULL;
}
LMI_AssociatedProcessorCacheMemory_Set_Dependent(
@@ -249,21 +237,15 @@ static CMPIStatus LMI_AssociatedProcessorCacheMemoryEnumInstances(
&lmi_assoc_cache, &lmi_cpu_cache);
cache_level = get_cache_level(sysfs_cpu_caches[i].level);
- if (cache_level == 1) {
- char *other_level;
- if (asprintf(&other_level, "%u",
- sysfs_cpu_caches[i].level) < 0) {
- other_level = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+ if (cache_level == LMI_AssociatedProcessorCacheMemory_Level_Other) {
+ char other_level[LONG_INT_LEN] = "";
+ snprintf(other_level, LONG_INT_LEN, "%u",
+ sysfs_cpu_caches[i].level);
LMI_AssociatedProcessorCacheMemory_Set_OtherLevelDescription(
&lmi_assoc_cache, other_level);
- free(other_level);
- other_level = NULL;
}
cache_type = get_cache_type(sysfs_cpu_caches[i].type);
- if (cache_type == 1) {
+ if (cache_type == LMI_AssociatedProcessorCacheMemory_CacheType_Other) {
LMI_AssociatedProcessorCacheMemory_Set_OtherCacheTypeDescription(
&lmi_assoc_cache, sysfs_cpu_caches[i].type);
}
diff --git a/src/hardware/LMI_Hardware.h b/src/hardware/LMI_Hardware.h
index 16bdf12..ff8848a 100644
--- a/src/hardware/LMI_Hardware.h
+++ b/src/hardware/LMI_Hardware.h
@@ -21,6 +21,9 @@
#ifndef LMI_HARDWARE_H_
#define LMI_HARDWARE_H_
+#define INSTANCE_ID_LEN 128
+#define ELEMENT_NAME_LEN 128
+
#define CPU_CLASS_NAME "Processor"
#define CPU_CAP_CLASS_NAME "ProcessorCapabilities"
#define CPU_CACHE_CLASS_NAME "ProcessorCacheMemory"
diff --git a/src/hardware/LMI_ProcessorCacheMemoryProvider.c b/src/hardware/LMI_ProcessorCacheMemoryProvider.c
index d4c73f6..ca45b67 100644
--- a/src/hardware/LMI_ProcessorCacheMemoryProvider.c
+++ b/src/hardware/LMI_ProcessorCacheMemoryProvider.c
@@ -60,7 +60,7 @@ static CMPIStatus LMI_ProcessorCacheMemoryEnumInstances(
{
LMI_ProcessorCacheMemory lmi_cpu_cache;
const char *ns = KNameSpace(cop);
- char *error_msg = NULL, *instance_id = NULL;
+ char *error_msg = NULL, instance_id[INSTANCE_ID_LEN] = "";
unsigned i, caches = 0;
DmiCpuCache *dmi_cpu_caches = NULL;
unsigned dmi_cpu_caches_nb = 0;
@@ -118,12 +118,9 @@ static CMPIStatus LMI_ProcessorCacheMemoryEnumInstances(
/* do we have dmidecode output? */
if (dmi_cpu_caches_nb > 0) {
- if (asprintf(&instance_id, ORGID ":" CPU_CACHE_CLASS_NAME ":%s",
- dmi_cpu_caches[i].id) < 0) {
- instance_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" CPU_CACHE_CLASS_NAME ":%s",
+ dmi_cpu_caches[i].id);
LMI_ProcessorCacheMemory_Set_DeviceID(&lmi_cpu_cache,
dmi_cpu_caches[i].id);
@@ -137,12 +134,9 @@ static CMPIStatus LMI_ProcessorCacheMemoryEnumInstances(
LMI_ProcessorCacheMemory_Set_EnabledState(&lmi_cpu_cache,
get_cachestatus(dmi_cpu_caches[i].status));
} else {
- if (asprintf(&instance_id, ORGID ":" CPU_CACHE_CLASS_NAME ":%s",
- sysfs_cpu_caches[i].id) < 0) {
- instance_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" CPU_CACHE_CLASS_NAME ":%s",
+ sysfs_cpu_caches[i].id);
LMI_ProcessorCacheMemory_Set_DeviceID(&lmi_cpu_cache,
sysfs_cpu_caches[i].id);
diff --git a/src/hardware/LMI_ProcessorCapabilitiesProvider.c b/src/hardware/LMI_ProcessorCapabilitiesProvider.c
index c7c4ff2..b29e0a9 100644
--- a/src/hardware/LMI_ProcessorCapabilitiesProvider.c
+++ b/src/hardware/LMI_ProcessorCapabilitiesProvider.c
@@ -60,7 +60,8 @@ static CMPIStatus LMI_ProcessorCapabilitiesEnumInstances(
CMPIUint16 cores = 1, threads = 1;
const char *ns = KNameSpace(cop),
*element_name_string = "Capabilities of processor ";
- char *error_msg = NULL, *instance_id = NULL, *element_name = NULL;
+ char *error_msg = NULL, instance_id[INSTANCE_ID_LEN] = "",
+ element_name[ELEMENT_NAME_LEN] = "";
unsigned i, cpus_nb = 0;
DmiProcessor *dmi_cpus = NULL;
unsigned dmi_cpus_nb = 0;
@@ -89,39 +90,19 @@ static CMPIStatus LMI_ProcessorCapabilitiesEnumInstances(
/* do we have output from dmidecode program? */
if (dmi_cpus_nb > 0) {
- if (asprintf(&instance_id, ORGID ":" CPU_CAP_CLASS_NAME ":%s",
- dmi_cpus[i].id) < 0) {
- instance_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
-
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" CPU_CAP_CLASS_NAME ":%s", dmi_cpus[i].id);
+ snprintf(element_name, ELEMENT_NAME_LEN, "%s%s",
+ element_name_string, dmi_cpus[i].id);
cores = dmi_cpus[i].cores;
threads = dmi_cpus[i].threads;
-
- if (asprintf(&element_name, "%s%s",
- element_name_string, dmi_cpus[i].id) < 0) {
- element_name = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
} else {
- if (asprintf(&instance_id, ORGID ":" CPU_CAP_CLASS_NAME ":%u",
- i) < 0) {
- instance_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
-
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" CPU_CAP_CLASS_NAME ":%u", i);
+ snprintf(element_name, ELEMENT_NAME_LEN, "%s%u",
+ element_name_string, i);
cores = lscpu.cores;
threads = lscpu.threads_per_core * lscpu.cores;
-
- if (asprintf(&element_name, "%s%u",
- element_name_string, i) < 0) {
- element_name = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
}
LMI_ProcessorCapabilities_Set_InstanceID(&lmi_cpu_cap, instance_id);
@@ -138,27 +119,9 @@ static CMPIStatus LMI_ProcessorCapabilitiesEnumInstances(
LMI_ProcessorCapabilities_Set_ElementName(&lmi_cpu_cap, element_name);
KReturnInstance(cr, lmi_cpu_cap);
-
- if (instance_id) {
- free(instance_id);
- }
- instance_id = NULL;
- if (element_name) {
- free(element_name);
- }
- element_name = NULL;
}
done:
- if (instance_id) {
- free(instance_id);
- }
- instance_id = NULL;
- if (element_name) {
- free(element_name);
- }
- element_name = NULL;
-
/* free lscpu only if it was used */
if (dmi_cpus_nb < 1) {
lscpu_free_processor(&lscpu);
diff --git a/src/hardware/LMI_ProcessorElementCapabilitiesProvider.c b/src/hardware/LMI_ProcessorElementCapabilitiesProvider.c
index ee9cf18..0ed7354 100644
--- a/src/hardware/LMI_ProcessorElementCapabilitiesProvider.c
+++ b/src/hardware/LMI_ProcessorElementCapabilitiesProvider.c
@@ -62,7 +62,7 @@ static CMPIStatus LMI_ProcessorElementCapabilitiesEnumInstances(
LMI_ProcessorCapabilitiesRef lmi_cpu_cap;
LMI_ProcessorRef lmi_cpu;
const char *ns = KNameSpace(cop);
- char *error_msg = NULL, *instance_id = NULL;
+ char *error_msg = NULL, instance_id[INSTANCE_ID_LEN] = "";
unsigned i, cpus_nb = 0;
DmiProcessor *dmi_cpus = NULL;
unsigned dmi_cpus_nb = 0;
@@ -102,29 +102,15 @@ static CMPIStatus LMI_ProcessorElementCapabilitiesEnumInstances(
if (dmi_cpus_nb > 0) {
LMI_ProcessorRef_Set_DeviceID(&lmi_cpu, dmi_cpus[i].id);
- if (asprintf(&instance_id, ORGID ":" CPU_CAP_CLASS_NAME ":%s",
- dmi_cpus[i].id) < 0) {
- instance_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" CPU_CAP_CLASS_NAME ":%s", dmi_cpus[i].id);
} else {
- char *cpu_id;
- if (asprintf(&cpu_id, "%u", i) < 0) {
- cpu_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+ char cpu_id[LONG_INT_LEN];
+ snprintf(cpu_id, LONG_INT_LEN, "%u", i);
LMI_ProcessorRef_Set_DeviceID(&lmi_cpu, cpu_id);
- free(cpu_id);
- cpu_id = NULL;
-
- if (asprintf(&instance_id, ORGID ":" CPU_CAP_CLASS_NAME ":%u",
- i) < 0) {
- instance_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" CPU_CAP_CLASS_NAME ":%u", i);
}
LMI_ProcessorCapabilitiesRef_Set_InstanceID(&lmi_cpu_cap, instance_id);
@@ -139,19 +125,9 @@ static CMPIStatus LMI_ProcessorElementCapabilitiesEnumInstances(
0, LMI_ProcessorElementCapabilities_Characteristics_Current);
KReturnInstance(cr, lmi_cpu_el_cap);
-
- if (instance_id) {
- free(instance_id);
- }
- instance_id = NULL;
}
done:
- if (instance_id) {
- free(instance_id);
- }
- instance_id = NULL;
-
/* free lscpu only if it was used */
if (dmi_cpus_nb < 1) {
lscpu_free_processor(&lscpu);
diff --git a/src/hardware/LMI_ProcessorProvider.c b/src/hardware/LMI_ProcessorProvider.c
index 6280bbd..4475de2 100644
--- a/src/hardware/LMI_ProcessorProvider.c
+++ b/src/hardware/LMI_ProcessorProvider.c
@@ -74,7 +74,8 @@ static CMPIStatus LMI_ProcessorEnumInstances(
CMPIUint32 current_speed = 0, max_speed = 0, external_clock = 0;
unsigned i, j, cpus_nb = 0;
char *other_family = NULL, *architecture = NULL, *cpu_name = NULL,
- *stepping = NULL, *error_msg = NULL, *instance_id = NULL;
+ *stepping = NULL, *error_msg = NULL,
+ instance_id[INSTANCE_ID_LEN] = "";
struct utsname utsname_buf;
DmiProcessor *dmi_cpus = NULL;
unsigned dmi_cpus_nb = 0;
@@ -126,12 +127,12 @@ static CMPIStatus LMI_ProcessorEnumInstances(
/* do we have output from dmidecode program? */
if (dmi_cpus_nb > 0) {
family = get_family(dmi_cpus[i].family);
- if (family == 1) {
+ if (family == LMI_Processor_Family_Other) {
other_family = dmi_cpus[i].family;
}
cpustatus = get_cpustatus(dmi_cpus[i].status);
enabledstate = get_enabledstate(cpustatus);
- if (enabledstate == 2) {
+ if (enabledstate == LMI_Processor_EnabledState_Enabled) {
current_speed = dmi_cpus[i].current_speed;
max_speed = dmi_cpus[i].max_speed;
external_clock = dmi_cpus[i].external_clock;
@@ -139,12 +140,8 @@ static CMPIStatus LMI_ProcessorEnumInstances(
cpu_name = dmi_cpus[i].name;
enabled_cores = dmi_cpus[i].enabled_cores;
stepping = dmi_cpus[i].stepping;
- if (asprintf(&instance_id, ORGID ":" CPU_CLASS_NAME ":%s",
- dmi_cpus[i].id) < 0) {
- instance_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" CPU_CLASS_NAME ":%s", dmi_cpus[i].id);
LMI_Processor_Set_DeviceID(&lmi_cpu, dmi_cpus[i].id);
LMI_Processor_Set_Family(&lmi_cpu, family);
@@ -175,31 +172,20 @@ static CMPIStatus LMI_ProcessorEnumInstances(
}
}
} else {
- char *cpu_id;
- if (asprintf(&cpu_id, "%u", i) < 0) {
- cpu_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
- if (asprintf(&instance_id, ORGID ":" CPU_CLASS_NAME ":%s",
- cpu_id) < 0) {
- instance_id = NULL;
- error_msg = "Not enough available memory.";
- goto done;
- }
- LMI_Processor_Set_DeviceID(&lmi_cpu, cpu_id);
- free(cpu_id);
- cpu_id = NULL;
-
+ char cpu_id[LONG_INT_LEN];
+ snprintf(cpu_id, LONG_INT_LEN, "%u", i);
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" CPU_CLASS_NAME ":%s", cpu_id);
cpustatus = get_cpustatus("Enabled");
enabledstate = get_enabledstate(cpustatus);
- if (enabledstate == 2) {
+ if (enabledstate == LMI_Processor_EnabledState_Enabled) {
current_speed = lscpu.current_speed;
}
-
cpu_name = proc_cpu.model_name;
enabled_cores = lscpu.cores;
stepping = lscpu.stepping;
+
+ LMI_Processor_Set_DeviceID(&lmi_cpu, cpu_id);
}
LMI_Processor_Set_InstanceID(&lmi_cpu, instance_id);
@@ -250,14 +236,8 @@ static CMPIStatus LMI_ProcessorEnumInstances(
}
done:
- if (architecture) {
- free(architecture);
- }
+ free(architecture);
architecture = NULL;
- if (instance_id) {
- free(instance_id);
- }
- instance_id = NULL;
dmi_free_processors(&dmi_cpus, &dmi_cpus_nb);
lscpu_free_processor(&lscpu);
diff --git a/src/hardware/cpuinfo.c b/src/hardware/cpuinfo.c
index d80e1cb..6634b05 100644
--- a/src/hardware/cpuinfo.c
+++ b/src/hardware/cpuinfo.c
@@ -126,18 +126,15 @@ void cpuinfo_free_processor(CpuinfoProcessor *cpu)
if (cpu->flags_nb > 0) {
unsigned i;
for (i = 0; i < cpu->flags_nb; i++) {
- if (cpu->flags[i]) {
- free(cpu->flags[i]);
- }
+ free(cpu->flags[i]);
cpu->flags[i] = NULL;
}
free(cpu->flags);
}
cpu->flags_nb = 0;
cpu->flags = NULL;
- if (cpu->model_name) {
- free(cpu->model_name);
- }
+
+ free(cpu->model_name);
cpu->model_name = NULL;
}
diff --git a/src/hardware/dmidecode.c b/src/hardware/dmidecode.c
index a50aa8a..04988a9 100644
--- a/src/hardware/dmidecode.c
+++ b/src/hardware/dmidecode.c
@@ -359,56 +359,36 @@ void dmi_free_processors(DmiProcessor **cpus, unsigned *cpus_nb)
if (*cpus_nb > 0) {
for (i = 0; i < *cpus_nb; i++) {
- if ((*cpus)[i].id) {
- free((*cpus)[i].id);
- }
+ free((*cpus)[i].id);
(*cpus)[i].id = NULL;
- if ((*cpus)[i].family) {
- free((*cpus)[i].family);
- }
+ free((*cpus)[i].family);
(*cpus)[i].family = NULL;
- if ((*cpus)[i].status) {
- free((*cpus)[i].status);
- }
+ free((*cpus)[i].status);
(*cpus)[i].status = NULL;
- if ((*cpus)[i].name) {
- free((*cpus)[i].name);
- }
+ free((*cpus)[i].name);
(*cpus)[i].name = NULL;
- if ((*cpus)[i].type) {
- free((*cpus)[i].type);
- }
+ free((*cpus)[i].type);
(*cpus)[i].type = NULL;
- if ((*cpus)[i].stepping) {
- free((*cpus)[i].stepping);
- }
+ free((*cpus)[i].stepping);
(*cpus)[i].stepping = NULL;
- if ((*cpus)[i].upgrade) {
- free((*cpus)[i].upgrade);
- }
+ free((*cpus)[i].upgrade);
(*cpus)[i].upgrade = NULL;
+
if ((*cpus)[i].charact_nb > 0) {
for (j = 0; j < (*cpus)[i].charact_nb; j++) {
- if ((*cpus)[i].characteristics[j]) {
- free((*cpus)[i].characteristics[j]);
- }
+ free((*cpus)[i].characteristics[j]);
(*cpus)[i].characteristics[j] = NULL;
}
free((*cpus)[i].characteristics);
}
(*cpus)[i].charact_nb = 0;
(*cpus)[i].characteristics = NULL;
- if ((*cpus)[i].l1_cache_handle) {
- free((*cpus)[i].l1_cache_handle);
- }
+
+ free((*cpus)[i].l1_cache_handle);
(*cpus)[i].l1_cache_handle = NULL;
- if ((*cpus)[i].l2_cache_handle) {
- free((*cpus)[i].l2_cache_handle);
- }
+ free((*cpus)[i].l2_cache_handle);
(*cpus)[i].l2_cache_handle = NULL;
- if ((*cpus)[i].l3_cache_handle) {
- free((*cpus)[i].l3_cache_handle);
- }
+ free((*cpus)[i].l3_cache_handle);
(*cpus)[i].l3_cache_handle = NULL;
}
free (*cpus);
@@ -645,29 +625,17 @@ void dmi_free_cpu_caches(DmiCpuCache **caches, unsigned *caches_nb)
if (*caches_nb > 0) {
for (i = 0; i < *caches_nb; i++) {
- if ((*caches)[i].id) {
- free((*caches)[i].id);
- }
+ free((*caches)[i].id);
(*caches)[i].id = NULL;
- if ((*caches)[i].name) {
- free((*caches)[i].name);
- }
+ free((*caches)[i].name);
(*caches)[i].name = NULL;
- if ((*caches)[i].status) {
- free((*caches)[i].status);
- }
+ free((*caches)[i].status);
(*caches)[i].status = NULL;
- if ((*caches)[i].op_mode) {
- free((*caches)[i].op_mode);
- }
+ free((*caches)[i].op_mode);
(*caches)[i].op_mode = NULL;
- if ((*caches)[i].type) {
- free((*caches)[i].type);
- }
+ free((*caches)[i].type);
(*caches)[i].type = NULL;
- if ((*caches)[i].associativity) {
- free((*caches)[i].associativity);
- }
+ free((*caches)[i].associativity);
(*caches)[i].associativity = NULL;
}
free (*caches);
diff --git a/src/hardware/lscpu.c b/src/hardware/lscpu.c
index d4546cb..3a2a420 100644
--- a/src/hardware/lscpu.c
+++ b/src/hardware/lscpu.c
@@ -150,9 +150,7 @@ done:
void lscpu_free_processor(LscpuProcessor *cpus)
{
if (cpus) {
- if (cpus->stepping) {
- free(cpus->stepping);
- }
+ free(cpus->stepping);
cpus->stepping = NULL;
}
diff --git a/src/hardware/sysfs.c b/src/hardware/sysfs.c
index c2cc1af..d07df71 100644
--- a/src/hardware/sysfs.c
+++ b/src/hardware/sysfs.c
@@ -148,7 +148,7 @@ short sysfs_get_cpu_caches(SysfsCpuCache **caches, unsigned *caches_nb)
{
short ret = -1;
unsigned i, level;
- char *buf, *format_str, path[PATH_MAX];
+ char *buf = NULL, *format_str, path[PATH_MAX];
DIR *dir;
*caches_nb = 0;
@@ -256,9 +256,7 @@ short sysfs_get_cpu_caches(SysfsCpuCache **caches, unsigned *caches_nb)
ret = 0;
done:
- if (buf) {
- free(buf);
- }
+ free(buf);
buf = NULL;
if (ret != 0) {
@@ -274,17 +272,11 @@ void sysfs_free_cpu_caches(SysfsCpuCache **caches, unsigned *caches_nb)
if (*caches_nb > 0) {
for (i = 0; i < *caches_nb; i++) {
- if ((*caches)[i].id) {
- free((*caches)[i].id);
- }
+ free((*caches)[i].id);
(*caches)[i].id = NULL;
- if ((*caches)[i].name) {
- free((*caches)[i].name);
- }
+ free((*caches)[i].name);
(*caches)[i].name = NULL;
- if ((*caches)[i].type) {
- free((*caches)[i].type);
- }
+ free((*caches)[i].type);
(*caches)[i].type = NULL;
}
free (*caches);
diff --git a/src/hardware/utils.c b/src/hardware/utils.c
index da7bbfa..8c1b63e 100644
--- a/src/hardware/utils.c
+++ b/src/hardware/utils.c
@@ -103,9 +103,7 @@ short read_fp_to_2d_buffer(FILE *fp, char ***buffer, unsigned *buffer_size)
ret = 0;
done:
- if (line) {
- free(line);
- }
+ free(line);
line = NULL;
if (ret != 0) {
@@ -122,9 +120,7 @@ void free_2d_buffer(char ***buffer, unsigned *buffer_size)
if (tmp_buffer_lines > 0) {
for (i = 0; i < tmp_buffer_lines; i++) {
- if (tmp_buffer[i]) {
- free(tmp_buffer[i]);
- }
+ free(tmp_buffer[i]);
tmp_buffer[i] = NULL;
}
free(tmp_buffer);
@@ -369,9 +365,7 @@ short explode(const char *str, const char *delims, char ***buffer, unsigned *buf
ret = 0;
done:
- if (trimmed_str) {
- free(trimmed_str);
- }
+ free(trimmed_str);
trimmed_str = NULL;
if (ret != 0) {
diff --git a/src/hardware/utils.h b/src/hardware/utils.h
index 9f5686f..933c042 100644
--- a/src/hardware/utils.h
+++ b/src/hardware/utils.h
@@ -27,6 +27,8 @@
#include <errno.h>
#include "globals.h"
+#define LONG_INT_LEN 21 /* 64 bit unsigned int can has 20 decimals + \0 */
+
#define WHITESPACES " \f\n\r\t\v"