From 61f47da0e17212e8f8cfa79a702aca16558937af Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Fri, 16 Oct 2015 15:26:24 +0200 Subject: [PATCH 1/3] mx_util: Cleanup mx_malloc_forever() fix indent --- mx_util.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mx_util.c b/mx_util.c index abd53a4..04e5a6d 100644 --- a/mx_util.c +++ b/mx_util.c @@ -556,11 +556,13 @@ int mx_strtoi64(char *str, int64_t *to) void *mx_malloc_forever(size_t size) { void *ret; + do { - ret=malloc(size); + ret = malloc(size); assert(ret || (!ret && errno == ENOMEM)); - } while (!ret); - return ret ; + } while (!ret); + + return ret ; } char *mx_strdup_forever(char *str) From 98030b58941412e3f1ea5b70e1a7e73135ecd4ab Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Fri, 16 Oct 2015 15:27:48 +0200 Subject: [PATCH 2/3] mx_util: Clean up mx_*cpuset*() * fix indent * fix new lines * unnest if statements --- mx_util.c | 94 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/mx_util.c b/mx_util.c index 04e5a6d..70f532f 100644 --- a/mx_util.c +++ b/mx_util.c @@ -9,6 +9,8 @@ #include #include +#include + //#include //#include #include @@ -1173,7 +1175,7 @@ char **mx_strvec_from_str(char *str) return strvec; } -int mx_str_to_cpuset(cpu_set_t* cpuset_ptr,char *str) +int mx_str_to_cpuset(cpu_set_t* cpuset_ptr, char *str) { char c; int cpu_low; @@ -1184,51 +1186,57 @@ int mx_str_to_cpuset(cpu_set_t* cpuset_ptr,char *str) CPU_ZERO(cpuset_ptr); while (1) { - c=*str; - if (c=='\0') { + c = *str; + + if (c == '\0') break; - } else if (c>='0' && c<='9') { - cpu_low=strtol(str,&next,10); - str=next; - } else { + + if (!isdigit(c)) return -(errno=EINVAL); - } - if (cpu_low<0 || cpu_low>=CPU_SETSIZE) { + cpu_low = strtol(str, &next, 10); + str = next; + + if (cpu_low < 0 || cpu_low >= CPU_SETSIZE) + return -(errno=ERANGE); + + c = *str; + + CPU_SET(cpu_low, cpuset_ptr); + + if (c == '\0') { + break; + } else if (c == ',') { + str++; + continue; + } else if (c != '-') { return -(errno=EINVAL); } - c=*str; - if (c=='\0') { - CPU_SET(cpu_low,cpuset_ptr); + str++; + c = *str; + + if (!isdigit(c)) + return -(errno=EINVAL); + + cpu_high = strtol(str, &next, 10); + str = next; + + if (cpu_high < 0 || cpu_high >= CPU_SETSIZE || cpu_high < cpu_low) + return -(errno=ERANGE); + + for (i = cpu_low+1; i <= cpu_high; i++) + CPU_SET(i, cpuset_ptr); + + c = *str; + + if (c == '\0') { break; - } else if (c==',') { - CPU_SET(cpu_low,cpuset_ptr); - str++; - } else if (c=='-') { - c=*++str; - if (c>='0' && c<='9') { - cpu_high=strtol(str,&next,10); - str=next; - if (cpu_high<0 || cpu_high>=CPU_SETSIZE || cpu_high=CPU_SETSIZE) break; + if (cpu>=CPU_SETSIZE) + break; + if (CPU_ISSET(cpu,cpuset_ptr)) { - cpu_low=cpu++; + cpu_low=cpu; while (1) { - if (cpu>=CPU_SETSIZE || !CPU_ISSET(cpu,cpuset_ptr)) break; cpu++; + if (cpu>=CPU_SETSIZE || !CPU_ISSET(cpu,cpuset_ptr)) + break; } cpu_high=cpu-1; if (cpu_low==cpu_high) { From 652ab706dc2761476d2283e2e104f42472be9acd Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Fri, 16 Oct 2015 15:31:52 +0200 Subject: [PATCH 3/3] mx_util: Cleanup mx_strvec_join() * use stpcpy() instead of loops --- mx_util.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/mx_util.c b/mx_util.c index 70f532f..24088b2 100644 --- a/mx_util.c +++ b/mx_util.c @@ -1250,29 +1250,28 @@ char *mx_strvec_join(char *sep,char **strvec) char *p; int i; + assert(sep); + assert(strvec); + for (i=0;(in=strvec[i]);i++) { elements++; - len+=strlen(in); + len += strlen(in); } - if (elements==0) return mx_strdup_forever(""); - len+=strlen(sep)*(elements-1); - out=mx_malloc_forever(len+1); - p=out; + + if (elements == 0) + return mx_strdup_forever(""); + + len += strlen(sep)*(elements-1); + out = mx_malloc_forever(len+1); + p = out; for (i=0;i