r12473@Kushana: nickm | 2007-03-06 15:49:45 -0500

Excise PREDICT and PREDICT_FALSE in favor of PREDICT_LIKELY and PREDICT_UNLIKELY.


svn:r9781
This commit is contained in:
Nick Mathewson 2007-03-09 21:39:19 +00:00
parent d26a587309
commit d62e37b4a9
2 changed files with 10 additions and 9 deletions

View File

@ -98,15 +98,17 @@ extern INLINE double U64_TO_DBL(uint64_t x) {
#define ATTR_PURE __attribute__((pure)) #define ATTR_PURE __attribute__((pure))
#define ATTR_MALLOC __attribute__((malloc)) #define ATTR_MALLOC __attribute__((malloc))
#define ATTR_NONNULL(x) __attribute__((nonnull x)) #define ATTR_NONNULL(x) __attribute__((nonnull x))
#define PREDICT(exp, val) __builtin_expect((exp), (val)) /** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
#define PREDICT_LIKELY(exp) PREDICT((exp), 1) * of <b>exp</b> will probably be true. */
#define PREDICT_UNLIKELY(exp) PREDICT((exp), 0) #define PREDICT_LIKELY(exp) __builtin_expect((exp), 1)
/** Macro: Evaluates to <b>exp</b> and hints the compiler that the value
* of <b>exp</b> will probably be false. */
#define PREDICT_UNLIKELY(exp) __builtin_expect((exp), 0)
#else #else
#define ATTR_NORETURN #define ATTR_NORETURN
#define ATTR_PURE #define ATTR_PURE
#define ATTR_MALLOC #define ATTR_MALLOC
#define ATTR_NONNULL(x) #define ATTR_NONNULL(x)
#define PREDICT(exp, val) (exp)
#define PREDICT_LIKELY(exp) (exp) #define PREDICT_LIKELY(exp) (exp)
#define PREDICT_UNLIKELY(exp) (exp) #define PREDICT_UNLIKELY(exp) (exp)
#endif #endif

View File

@ -39,17 +39,16 @@
#error "Sorry; we don't support building with NDEBUG." #error "Sorry; we don't support building with NDEBUG."
#else #else
#ifdef __GNUC__ #ifdef __GNUC__
/** Macro: evaluate the expression x, which we expect to be false. /* Give an int-valued version of !x that won't confuse PREDICT_UNLIKELY. */
* Used to hint the compiler that a branch won't be taken. */ #define IS_FALSE_AS_INT(x) ((x) == ((typeof(x)) 0))
#define PREDICT_FALSE(x) PREDICT_UNLIKELY((x) == ((typeof(x)) 0))
#else #else
#define PREDICT_FALSE(x) !(x) #define IS_FALSE_AS_INT(x) !(x)
#endif #endif
/** Like assert(3), but send assertion failures to the log as well as to /** Like assert(3), but send assertion failures to the log as well as to
* stderr. */ * stderr. */
#define tor_assert(expr) do { \ #define tor_assert(expr) do { \
if (PREDICT_FALSE(expr)) { \ if (PREDICT_UNLIKELY(IS_FALSE_AS_INT(expr))) { \
log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \ log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
_SHORT_FILE_, __LINE__, __func__, #expr); \ _SHORT_FILE_, __LINE__, __func__, #expr); \
fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \ fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \