From a686ba19753e23df690930c5ebdb035efd90f5a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Fri, 8 Jan 2016 09:43:04 +0100 Subject: [PATCH 1/3] Add gettokens() string function --- include/ucore/string.h | 3 ++- src/gettokens.c | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/gettokens.c diff --git a/include/ucore/string.h b/include/ucore/string.h index ccdb53d..a8f22be 100644 --- a/include/ucore/string.h +++ b/include/ucore/string.h @@ -96,7 +96,8 @@ char* uc_sprintbs(char *result, unsigned short value); /** Like http://swtch.com/plan9port/man/man3/getfields.html */ -int getfields(char *str, char **args, int max, int mflag, const char *set); +int getfields(char *str, char **args, int max, int mflag, const char *sep); +int gettokens(char *str, char **args, int max, const char *sep); //Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.) ///result should be at least of size 12, diff --git a/src/gettokens.c b/src/gettokens.c new file mode 100644 index 0000000..7c6c9a4 --- /dev/null +++ b/src/gettokens.c @@ -0,0 +1,58 @@ +#include +#include "ucore/string.h" + +//tokenize string, and exclude the quotes +static char *qtoken(char *s, const char *sep) +{ + int inquote = 0; + char *t = s; + while (*t != 0 && (inquote || strchr(sep, *t) == NULL)) { + if (*t != '"') { + *s++ = *t++; + continue; + } + + if (!inquote) { + inquote = 1; + t++; + continue; + } + + if (t[1] != '"') { + t++; + inquote = 0; + continue; + } + //double quotes, merged to 1 quote + t++; + *s++ = *t++; + } + + if (*s != 0) { + *s = 0; + if (t == s) { + t++; + } + } + + return t; +} + +int gettokens(char *str, char **args, int maxargs, const char *sep) +{ + int nargs; + + for (nargs = 0; nargs < maxargs; nargs++) { + while (*str != 0 && strchr(sep, *str) != NULL) { + *str++ = 0; + } + if (*str == 0) { + break; + } + args[nargs] = str; + str = qtoken(str, sep); + } + + return nargs; +} + From d70ab1fe09d1139699d4dda71511d2874696fb21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Mon, 8 Feb 2016 13:34:22 +0100 Subject: [PATCH 2/3] Use ck_assert_int_eq instead of ck_assert_uint_eq for compatibility with older check version --- test/test_restart_file.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/test_restart_file.c b/test/test_restart_file.c index 14792b6..55749dc 100644 --- a/test/test_restart_file.c +++ b/test/test_restart_file.c @@ -26,12 +26,12 @@ START_TEST (test_restart_counter) rc = uc_restart_counter_init(&cnt, FILE_NAME, 0); fail_if(rc != UC_RC_OK); - ck_assert_uint_eq(0, uc_restart_counter_get(&cnt)); + ck_assert_int_eq(0, uc_restart_counter_get(&cnt)); rc = uc_restart_counter_init(&cnt, FILE_NAME, 0); fail_if(rc != UC_RC_OK); - ck_assert_uint_eq(1, uc_restart_counter_get(&cnt)); + ck_assert_int_eq(1, uc_restart_counter_get(&cnt)); END_TEST @@ -48,7 +48,7 @@ START_TEST (test_restart_counter_existing) rc = uc_restart_counter_init(&cnt, FILE_NAME, 0); fail_if(rc != UC_RC_OK); - ck_assert_uint_eq(100000, uc_restart_counter_get(&cnt)); + ck_assert_int_eq(100000, uc_restart_counter_get(&cnt)); END_TEST @@ -72,11 +72,11 @@ START_TEST (test_restart_counter_garble) rc = uc_restart_counter_init(&cnt, FILE_NAME, 0); fail_if(rc != UC_RC_ERR_FILE_CONTENT_ERROR); - ck_assert_uint_eq(0, uc_restart_counter_get(&cnt)); + ck_assert_int_eq(0, uc_restart_counter_get(&cnt)); rc = uc_restart_counter_init(&cnt, FILE_NAME, 0); fail_if(rc != UC_RC_OK, "%d", rc); - ck_assert_uint_eq(1, uc_restart_counter_get(&cnt)); + ck_assert_int_eq(1, uc_restart_counter_get(&cnt)); END_TEST @@ -103,7 +103,7 @@ START_TEST (test_restart_counter_lock_file) rc = uc_restart_counter_init(&cnt, FILE_NAME, UC_RC_FL_LOCK_FILE); fail_if(rc != UC_RC_OK); - ck_assert_uint_eq(0, uc_restart_counter_get(&cnt)); + ck_assert_int_eq(0, uc_restart_counter_get(&cnt)); rc = uc_restart_counter_close(&cnt); fail_if(rc != UC_RC_OK); rc = uc_restart_counter_close(&cnt); @@ -118,7 +118,7 @@ START_TEST (test_restart_counter_lock_file) sem_post(&sem[1]); printf("rc = %d\n", rc); fail_if(rc != UC_RC_ERR_FILE_LOCKED); - ck_assert_uint_eq(0, uc_restart_counter_get(&cnt)); + ck_assert_int_eq(0, uc_restart_counter_get(&cnt)); } else { uc_restart_counter_init(&cnt, FILE_NAME, UC_RC_FL_LOCK_FILE); sem_post(&sem[0]); From 53079305353cd5631b42264bef29f6d465ec71a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Fri, 4 Mar 2016 11:38:51 +0100 Subject: [PATCH 3/3] Use gcc builtins (if gcc 5 or newer) for saturating math --- include/ucore/saturating_math.h | 35 ++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/include/ucore/saturating_math.h b/include/ucore/saturating_math.h index 6b9228f..84f9a62 100644 --- a/include/ucore/saturating_math.h +++ b/include/ucore/saturating_math.h @@ -10,10 +10,34 @@ * * multiplication, subtraction and addition operations are provided.*/ +//Use gcc builtins, supported since GCC 5 +#if __GNUC__ >= 5 + +#define UC_SAT_ADD(a, b, type, max_val)\ +({type res ;\ +if (__builtin_add_overflow(a, b, &res)) {\ + res = max_val;}\ +res;}) + + +#define UC_SAT_SUB(a, b, type)\ +({type res ;\ +if (__builtin_sub_overflow(a, b, &res)) {\ + res = 0;}\ +res;}) + +#define UC_SAT_MULT(a, b, type, max_val)\ +({type res ;\ +if (__builtin_mul_overflow(a, b, &res)) {\ + res = max_val;}\ +res;}) + +#else + #define UC_SAT_ADD(a, b, type, max_val)\ ((type)((a) + (b)) >= (a) ? (a) + (b) : (max_val)) -#define UC_SAT_SUB(a, b)\ +#define UC_SAT_SUB(a, b, type)\ ((a) >= (b) ? (a) - (b) : 0) #define UC_SAT_MULT(a, b, type, max_val)\ @@ -21,6 +45,7 @@ (a) <= (max_val) / (b) ? (type) (a) * (type) (b) \ : (max_val)) +#endif static inline uint8_t uc_sat_addu8(uint8_t a, uint8_t b) { @@ -29,7 +54,7 @@ static inline uint8_t uc_sat_addu8(uint8_t a, uint8_t b) static inline uint8_t uc_sat_subu8(uint8_t a, uint8_t b) { - return UC_SAT_SUB(a, b); + return UC_SAT_SUB(a, b, uint8_t); } static inline uint8_t uc_sat_multu8(uint8_t a, uint8_t b) @@ -44,7 +69,7 @@ static inline uint16_t uc_sat_addu16(uint16_t a, uint16_t b) static inline uint16_t uc_sat_subu16(uint16_t a, uint16_t b) { - return UC_SAT_SUB(a, b); + return UC_SAT_SUB(a, b, uint16_t); } static inline uint16_t uc_sat_multu16(uint16_t a, uint16_t b) @@ -59,7 +84,7 @@ static inline uint32_t uc_sat_addu32(uint32_t a, uint32_t b) static inline uint32_t uc_sat_subu32(uint32_t a, uint32_t b) { - return UC_SAT_SUB(a, b); + return UC_SAT_SUB(a, b, uint32_t); } static inline uint32_t uc_sat_multu32(uint32_t a, uint32_t b) @@ -74,7 +99,7 @@ static inline uint64_t uc_sat_addu64(uint64_t a, uint64_t b) static inline uint64_t uc_sat_subu64(uint64_t a, uint64_t b) { - return UC_SAT_SUB(a, b); + return UC_SAT_SUB(a, b, uint64_t); } static inline uint64_t uc_sat_multu64(uint64_t a, uint64_t b)