@@ -25,7 +25,10 @@ namespace node { |
25 | 25 | #define NO_RETURN |
26 | 26 | #endif |
27 | 27 |
|
| 28 | +// The slightly odd function signature for Assert() is to ease |
| 29 | +// instruction cache pressure in calls from ASSERT and CHECK. |
28 | 30 | NO_RETURN void Abort(); |
| 31 | +NO_RETURN void Assert(const char* const (*args)[4]); |
29 | 32 | void DumpBacktrace(FILE* fp); |
30 | 33 |
|
31 | 34 | #ifdef __APPLE__ |
@@ -52,15 +55,40 @@ template <typename T> using remove_reference = std::remove_reference<T>; |
52 | 55 |
|
53 | 56 | #define ABORT() node::Abort() |
54 | 57 |
|
55 | | -#if defined(NDEBUG) |
56 | | -# define ASSERT(expression) |
57 | | -# define CHECK(expression) \ |
| 58 | +#ifdef __GNUC__ |
| 59 | +#define LIKELY(expr) __builtin_expect(!!(expr), 1) |
| 60 | +#define UNLIKELY(expr) __builtin_expect(!!(expr), 0) |
| 61 | +#define PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__ |
| 62 | +#else |
| 63 | +#define LIKELY(expr) expr |
| 64 | +#define UNLIKELY(expr) expr |
| 65 | +#define PRETTY_FUNCTION_NAME "" |
| 66 | +#endif |
| 67 | + |
| 68 | +#define STRINGIFY_(x) #x |
| 69 | +#define STRINGIFY(x) STRINGIFY_(x) |
| 70 | + |
| 71 | +#define CHECK(expr) \ |
58 | 72 | do { \ |
59 | | - if (!(expression)) ABORT(); \ |
| 73 | + if (UNLIKELY(!(expr))) { \ |
| 74 | + static const char* const args[] = { __FILE__, STRINGIFY(__LINE__), \ |
| 75 | + #expr, PRETTY_FUNCTION_NAME }; \ |
| 76 | + node::Assert(&args); \ |
| 77 | + } \ |
60 | 78 | } while (0) |
| 79 | + |
| 80 | +// FIXME(bnoordhuis) cctests don't link in node::Abort() and node::Assert(). |
| 81 | +#ifdef GTEST_DONT_DEFINE_ASSERT_EQ |
| 82 | +#undef ABORT |
| 83 | +#undef CHECK |
| 84 | +#define ABORT ABORT_NO_BACKTRACE |
| 85 | +#define CHECK assert |
| 86 | +#endif |
| 87 | + |
| 88 | +#ifdef NDEBUG |
| 89 | +#define ASSERT(expr) |
61 | 90 | #else |
62 | | -# define ASSERT(expression) assert(expression) |
63 | | -# define CHECK(expression) assert(expression) |
| 91 | +#define ASSERT(expr) CHECK(expr) |
64 | 92 | #endif |
65 | 93 |
|
66 | 94 | #define ASSERT_EQ(a, b) ASSERT((a) == (b)) |
|