Merge branch 'dev' of ssh://box/var/lib/git/libucore into dev

This commit is contained in:
Nils O. Selåsdal
2016-03-07 22:14:07 +01:00
4 changed files with 97 additions and 13 deletions
+30 -5
View File
@@ -10,10 +10,34 @@
* *
* multiplication, subtraction and addition operations are provided.*/ * 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)\ #define UC_SAT_ADD(a, b, type, max_val)\
((type)((a) + (b)) >= (a) ? (a) + (b) : (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) ((a) >= (b) ? (a) - (b) : 0)
#define UC_SAT_MULT(a, b, type, max_val)\ #define UC_SAT_MULT(a, b, type, max_val)\
@@ -21,6 +45,7 @@
(a) <= (max_val) / (b) ? (type) (a) * (type) (b) \ (a) <= (max_val) / (b) ? (type) (a) * (type) (b) \
: (max_val)) : (max_val))
#endif
static inline uint8_t uc_sat_addu8(uint8_t a, uint8_t b) 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) 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) 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) 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) 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) 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) 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) 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) static inline uint64_t uc_sat_multu64(uint64_t a, uint64_t b)
+2 -1
View File
@@ -96,7 +96,8 @@ char* uc_sprintbs(char *result, unsigned short value);
/** Like http://swtch.com/plan9port/man/man3/getfields.html /** 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.) //Converts bytes to a human representable test, (i.e. 12kB, 160 MB 1.78 GB etc.)
///result should be at least of size 12, ///result should be at least of size 12,
+58
View File
@@ -0,0 +1,58 @@
#include <string.h>
#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;
}
+7 -7
View File
@@ -26,12 +26,12 @@ START_TEST (test_restart_counter)
rc = uc_restart_counter_init(&cnt, FILE_NAME, 0); rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_OK); 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); rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_OK); 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 END_TEST
@@ -48,7 +48,7 @@ START_TEST (test_restart_counter_existing)
rc = uc_restart_counter_init(&cnt, FILE_NAME, 0); rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_OK); 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 END_TEST
@@ -72,11 +72,11 @@ START_TEST (test_restart_counter_garble)
rc = uc_restart_counter_init(&cnt, FILE_NAME, 0); rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_ERR_FILE_CONTENT_ERROR); 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); rc = uc_restart_counter_init(&cnt, FILE_NAME, 0);
fail_if(rc != UC_RC_OK, "%d", rc); 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 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); rc = uc_restart_counter_init(&cnt, FILE_NAME, UC_RC_FL_LOCK_FILE);
fail_if(rc != UC_RC_OK); 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); rc = uc_restart_counter_close(&cnt);
fail_if(rc != UC_RC_OK); fail_if(rc != UC_RC_OK);
rc = uc_restart_counter_close(&cnt); rc = uc_restart_counter_close(&cnt);
@@ -118,7 +118,7 @@ START_TEST (test_restart_counter_lock_file)
sem_post(&sem[1]); sem_post(&sem[1]);
printf("rc = %d\n", rc); printf("rc = %d\n", rc);
fail_if(rc != UC_RC_ERR_FILE_LOCKED); 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 { } else {
uc_restart_counter_init(&cnt, FILE_NAME, UC_RC_FL_LOCK_FILE); uc_restart_counter_init(&cnt, FILE_NAME, UC_RC_FL_LOCK_FILE);
sem_post(&sem[0]); sem_post(&sem[0]);