mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 16:00:39 +01:00
[v0.98] Fix little bug in get_str_cache_two (spotted by #90) and simplfy get_str_cache_one
This commit is contained in:
@@ -53,71 +53,58 @@ uint32_t get_nsockets(struct topology* topo) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
int32_t get_value_as_smallest_unit(char ** str, uint32_t value) {
|
int32_t get_value_as_smallest_unit(char ** str, uint32_t value) {
|
||||||
int32_t sanity_ret;
|
int32_t ret;
|
||||||
*str = emalloc(sizeof(char)* 11); //8 for digits, 2 for units
|
int max_len = 10; // Max is 8 for digits, 2 for units
|
||||||
|
*str = emalloc(sizeof(char)* (max_len + 1));
|
||||||
|
|
||||||
if(value/1024 >= 1024)
|
if(value/1024 >= 1024)
|
||||||
sanity_ret = snprintf(*str, 10,"%.4g"STRING_MEGABYTES, (double)value/(1<<20));
|
ret = snprintf(*str, max_len, "%.4g"STRING_MEGABYTES, (double)value/(1<<20));
|
||||||
else
|
else
|
||||||
sanity_ret = snprintf(*str, 10,"%.4g"STRING_KILOBYTES, (double)value/(1<<10));
|
ret = snprintf(*str, max_len, "%.4g"STRING_KILOBYTES, (double)value/(1<<10));
|
||||||
|
|
||||||
return sanity_ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// String functions
|
// String functions
|
||||||
char* get_str_cache_two(int32_t cache_size, uint32_t physical_cores) {
|
char* get_str_cache_two(int32_t cache_size, uint32_t physical_cores) {
|
||||||
// 4 for digits, 2 for units, 2 for ' (', 3 digits, 2 for units and 7 for ' Total)'
|
|
||||||
uint32_t max_size = 4+2 + 2 + 4+2 + 7 + 1;
|
|
||||||
int32_t sanity_ret;
|
|
||||||
char* string = emalloc(sizeof(char) * max_size);
|
|
||||||
char* tmp1;
|
char* tmp1;
|
||||||
char* tmp2;
|
char* tmp2;
|
||||||
int32_t tmp1_len = get_value_as_smallest_unit(&tmp1, cache_size);
|
int32_t tmp1_len = get_value_as_smallest_unit(&tmp1, cache_size);
|
||||||
int32_t tmp2_len = get_value_as_smallest_unit(&tmp2, cache_size * physical_cores);
|
int32_t tmp2_len = get_value_as_smallest_unit(&tmp2, cache_size * physical_cores);
|
||||||
|
|
||||||
|
// tmp1_len for first output, 2 for ' (', tmp2_len for second output and 7 for ' Total)'
|
||||||
|
uint32_t size = tmp1_len + 2 + tmp2_len + 7 + 1;
|
||||||
|
char* string = emalloc(sizeof(char) * size);
|
||||||
|
|
||||||
if(tmp1_len < 0) {
|
if(tmp1_len < 0) {
|
||||||
printBug("get_value_as_smallest_unit: snprintf returned a negative value for input: %d\n", cache_size);
|
printBug("get_value_as_smallest_unit: snprintf failed for input: %d\n", cache_size);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if(tmp2_len < 0) {
|
if(tmp2_len < 0) {
|
||||||
printBug("get_value_as_smallest_unit: snprintf returned a negative value for input: %d\n", cache_size * physical_cores);
|
printBug("get_value_as_smallest_unit: snprintf failed for input: %d\n", cache_size * physical_cores);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t size = tmp1_len + 2 + tmp2_len + 7 + 1;
|
if(snprintf(string, size, "%s (%s Total)", tmp1, tmp2) < 0) {
|
||||||
sanity_ret = snprintf(string, size, "%s (%s Total)", tmp1, tmp2);
|
printBug("get_str_cache_two: snprintf failed for input: '%s' and '%s'\n", tmp1, tmp2);
|
||||||
|
|
||||||
if(sanity_ret < 0) {
|
|
||||||
printBug("get_str_cache_two: snprintf returned a negative value for input: '%s' and '%s'\n", tmp1, tmp2);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(tmp1);
|
free(tmp1);
|
||||||
free(tmp2);
|
free(tmp2);
|
||||||
|
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* get_str_cache_one(int32_t cache_size) {
|
char* get_str_cache_one(int32_t cache_size) {
|
||||||
// 4 for digits, 2 for units, 2 for ' (', 3 digits, 2 for units and 7 for ' Total)'
|
char* string;
|
||||||
uint32_t max_size = 4+2 + 1;
|
int32_t str_len = get_value_as_smallest_unit(&string, cache_size);
|
||||||
int32_t sanity_ret;
|
|
||||||
char* string = emalloc(sizeof(char) * max_size);
|
|
||||||
char* tmp;
|
|
||||||
int32_t tmp_len = get_value_as_smallest_unit(&tmp, cache_size);
|
|
||||||
|
|
||||||
if(tmp_len < 0) {
|
if(str_len < 0) {
|
||||||
printBug("get_value_as_smallest_unit: snprintf returned a negative value for input: %d", cache_size);
|
printBug("get_value_as_smallest_unit: snprintf failed for input: %d", cache_size);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t size = tmp_len + 1;
|
|
||||||
sanity_ret = snprintf(string, size, "%s", tmp);
|
|
||||||
|
|
||||||
if(sanity_ret < 0) {
|
|
||||||
printBug("get_str_cache_one: snprintf returned a negative value for input: '%s'", tmp);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
free(tmp);
|
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user