diff options
author | Nick Mathewson <nickm@torproject.org> | 2007-03-06 20:25:44 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2007-03-06 20:25:44 +0000 |
commit | 5d1bee87ffe3dcdaa9e7960b0fed836aa1d01b9b (patch) | |
tree | 5f9aea61df183fd3a5b88223789babc1fb859a67 /src/common | |
parent | c9e2766e7598a669d3acf3ae0e45e8be8b5945b0 (diff) | |
download | tor-5d1bee87ffe3dcdaa9e7960b0fed836aa1d01b9b.tar.gz tor-5d1bee87ffe3dcdaa9e7960b0fed836aa1d01b9b.zip |
r12468@Kushana: nickm | 2007-03-06 15:24:00 -0500
More unit tests: gcov is fun.
svn:r9748
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/compat.h | 4 | ||||
-rw-r--r-- | src/common/log.h | 8 | ||||
-rw-r--r-- | src/common/test.h | 33 | ||||
-rw-r--r-- | src/common/util.c | 15 | ||||
-rw-r--r-- | src/common/util.h | 18 |
5 files changed, 47 insertions, 31 deletions
diff --git a/src/common/compat.h b/src/common/compat.h index f578c128f8..07f194282f 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -99,12 +99,16 @@ extern INLINE double U64_TO_DBL(uint64_t x) { #define ATTR_MALLOC __attribute__((malloc)) #define ATTR_NONNULL(x) __attribute__((nonnull x)) #define PREDICT(exp, val) __builtin_expect((exp), (val)) +#define PREDICT_LIKELY(exp) PREDICT((exp), 1) +#define PREDICT_UNLIKELY(exp) PREDICT((exp), 0) #else #define ATTR_NORETURN #define ATTR_PURE #define ATTR_MALLOC #define ATTR_NONNULL(x) #define PREDICT(exp, val) (exp) +#define PREDICT_LIKELY(exp) (exp) +#define PREDICT_UNLIKELY(exp) (exp) #endif /* ===== String compatibility */ diff --git a/src/common/log.h b/src/common/log.h index d01305b90a..af17e2e2c2 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -129,10 +129,10 @@ void _log_fn(int severity, uint32_t domain, * of the current function name. */ #define log_fn(severity, domain, args...) \ _log_fn(severity, domain, __PRETTY_FUNCTION__, args) -#define log_debug(domain, args...) \ - do { \ - if (PREDICT(_log_global_min_severity == LOG_DEBUG, 0)) \ - _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \ +#define log_debug(domain, args...) \ + do { \ + if (PREDICT_UNLIKELY(_log_global_min_severity == LOG_DEBUG)) \ + _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \ } while (0) #define log_info(domain, args...) \ _log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args) diff --git a/src/common/test.h b/src/common/test.h index 9edda8d296..41b795a89a 100644 --- a/src/common/test.h +++ b/src/common/test.h @@ -71,21 +71,28 @@ extern int have_failed; #define test_eq_ptr(expr1, expr2) \ test_eq_type(void*, "%p", expr1, expr2) -#define test_neq(expr1, expr2) \ - STMT_BEGIN \ - long v1=(long)(expr1), v2=(long)(expr2); \ - if (v1!=v2) { printf("."); fflush(stdout); } else { \ - have_failed = 1; \ - printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\ - " (%ld == %ld)\n", \ - _SHORT_FILE_, \ - __LINE__, \ - PRETTY_FUNCTION, \ - #expr1, #expr2, \ - v1, v2); \ - return; \ +#define test_neq_type(tp, fmt, expr1, expr2) \ + STMT_BEGIN \ + tp v1=(tp)(expr1); \ + tp v2=(tp)(expr2); \ + if (v1!=v2) { printf("."); fflush(stdout); } else { \ + have_failed = 1; \ + printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n" \ + " ("fmt" == "fmt")\n", \ + _SHORT_FILE_, \ + __LINE__, \ + PRETTY_FUNCTION, \ + #expr1, #expr2, \ + v1, v2); \ + return; \ } STMT_END +#define test_neq(expr1, expr2) \ + test_neq_type(long, "%ld", expr1, expr2) + +#define test_neq_ptr(expr1, expr2) \ + test_neq_type(void *, "%p", expr1, expr2) + #define test_streq(expr1, expr2) \ STMT_BEGIN \ const char *v1=(expr1), *v2=(expr2); \ diff --git a/src/common/util.c b/src/common/util.c index 6c1ff3bc3b..d21d0c07d1 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -115,7 +115,7 @@ _tor_malloc(size_t size DMALLOC_PARAMS) #endif result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0); - if (PREDICT(result == NULL, 0)) { + if (PREDICT_UNLIKELY(result == NULL)) { log_err(LD_MM,"Out of memory on malloc(). Dying."); /* If these functions die within a worker process, they won't call * spawn_exit, but that's ok, since the parent will run out of memory soon @@ -147,7 +147,7 @@ _tor_realloc(void *ptr, size_t size DMALLOC_PARAMS) void *result; result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0); - if (PREDICT(result == NULL, 0)) { + if (PREDICT_UNLIKELY(result == NULL)) { log_err(LD_MM,"Out of memory on realloc(). Dying."); exit(1); } @@ -165,7 +165,7 @@ _tor_strdup(const char *s DMALLOC_PARAMS) tor_assert(s); dup = dmalloc_strdup(file, line, s, 0); - if (PREDICT(dup == NULL, 0)) { + if (PREDICT_UNLIKELY(dup == NULL)) { log_err(LD_MM,"Out of memory on strdup(). Dying."); exit(1); } @@ -517,7 +517,7 @@ tor_parse_long(const char *s, int base, long min, long max, CHECK_STRTOX_RESULT(); } -/** As tor_parse_log, but return an unsigned long. */ +/** As tor_parse_long, but return an unsigned long. */ unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next) @@ -615,6 +615,7 @@ int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen) { const char *end; + int v1,v2; if ((srclen % 2) != 0) return -1; @@ -812,7 +813,7 @@ wrap_string(smartlist_t *out, const char *string, size_t width, /** Return the number of microseconds elapsed between *start and *end. */ long -tv_udiff(struct timeval *start, struct timeval *end) +tv_udiff(const struct timeval *start, const struct timeval *end) { long udiff; long secdiff = end->tv_sec - start->tv_sec; @@ -829,7 +830,7 @@ tv_udiff(struct timeval *start, struct timeval *end) /** Return -1 if *a \< *b, 0 if *a==*b, and 1 if *a \> *b. */ int -tv_cmp(struct timeval *a, struct timeval *b) +tv_cmp(const struct timeval *a, const struct timeval *b) { if (a->tv_sec > b->tv_sec) return 1; @@ -845,7 +846,7 @@ tv_cmp(struct timeval *a, struct timeval *b) /** Increment *a by the number of seconds and microseconds in *b. */ void -tv_add(struct timeval *a, struct timeval *b) +tv_add(struct timeval *a, const struct timeval *b) { a->tv_usec += b->tv_usec; a->tv_sec += b->tv_sec + (a->tv_usec / 1000000); diff --git a/src/common/util.h b/src/common/util.h index 5d9a16030d..9f37e6ad3f 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -41,7 +41,7 @@ #ifdef __GNUC__ /** Macro: evaluate the expression x, which we expect to be false. * Used to hint the compiler that a branch won't be taken. */ -#define PREDICT_FALSE(x) PREDICT((x) == ((typeof(x)) 0), 0) +#define PREDICT_FALSE(x) PREDICT_UNLIKELY((x) == ((typeof(x)) 0)) #else #define PREDICT_FALSE(x) !(x) #endif @@ -85,14 +85,18 @@ void _tor_free(void *mem); extern int dmalloc_free(const char *file, const int line, void *pnt, const int func_id); #define tor_free(p) do { \ - if (PREDICT((p)!=NULL, 1)) { \ + if (PREDICT_LIKELY((p)!=NULL)) { \ dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \ (p)=NULL; \ } \ } while (0) #else -#define tor_free(p) do { if (PREDICT((p)!=NULL,1)) { free(p); (p)=NULL;} } \ - while (0) +#define tor_free(p) do { \ + if (PREDICT_LIKELY((p)!=NULL)) { \ + free(p); \ + (p)=NULL; \ + } \ + } while (0) #endif #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS) @@ -172,10 +176,10 @@ void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen); int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen); /* Time helpers */ -long tv_udiff(struct timeval *start, struct timeval *end); +long tv_udiff(const struct timeval *start, const struct timeval *end); void tv_addms(struct timeval *a, long ms); -void tv_add(struct timeval *a, struct timeval *b); -int tv_cmp(struct timeval *a, struct timeval *b); +void tv_add(struct timeval *a, const struct timeval *b); +int tv_cmp(const struct timeval *a, const struct timeval *b); time_t tor_timegm(struct tm *tm); #define RFC1123_TIME_LEN 29 void format_rfc1123_time(char *buf, time_t t); |