From 2e486462e0f79a1ba436fa23495a9ac992fa4d53 Mon Sep 17 00:00:00 2001 From: kwiberg Date: Tue, 23 Aug 2016 05:54:25 -0700 Subject: [PATCH] RTC_CHECK and RTC_DCHECK macros for C So that we don't have to use assert(). Includes one sample call site. NOTRY=true BUG=chromium:617124 Review-Url: https://codereview.webrtc.org/2262173002 Cr-Commit-Position: refs/heads/master@{#13862} --- webrtc/base/checks.cc | 5 ++ webrtc/base/checks.h | 59 +++++++++++++++++-- .../audio_coding/codecs/opus/opus_interface.c | 5 +- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/webrtc/base/checks.cc b/webrtc/base/checks.cc index d9dc8f2d63..6aaaf9ff83 100644 --- a/webrtc/base/checks.cc +++ b/webrtc/base/checks.cc @@ -133,3 +133,8 @@ template std::string* MakeCheckOpString( #endif } // namespace rtc + +// Function to call from the C version of the RTC_CHECK and RTC_DCHECK macros. +NO_RETURN void rtc_FatalMessage(const char* file, int line, const char* msg) { + rtc::FatalMessage(file, line).stream() << msg; +} diff --git a/webrtc/base/checks.h b/webrtc/base/checks.h index e66c061186..a48971fd29 100644 --- a/webrtc/base/checks.h +++ b/webrtc/base/checks.h @@ -11,11 +11,28 @@ #ifndef WEBRTC_BASE_CHECKS_H_ #define WEBRTC_BASE_CHECKS_H_ +#include "webrtc/typedefs.h" + +#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) +#define RTC_DCHECK_IS_ON 1 +#else +#define RTC_DCHECK_IS_ON 0 +#endif + +#ifdef __cplusplus +extern "C" { +#endif +NO_RETURN void rtc_FatalMessage(const char* file, int line, const char* msg); +#ifdef __cplusplus +} // extern "C" +#endif + +#ifdef __cplusplus +// C++ version. + #include #include -#include "webrtc/typedefs.h" - // The macros here print a message to stderr and abort under various // conditions. All will accept additional stream messages. For example: // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar."; @@ -163,8 +180,7 @@ DEFINE_RTC_CHECK_OP_IMPL(GT, > ) // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates // code in debug builds. It does reference the condition parameter in all cases, // though, so callers won't risk getting warnings about unused variables. -#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) -#define RTC_DCHECK_IS_ON 1 +#if RTC_DCHECK_IS_ON #define RTC_DCHECK(condition) RTC_CHECK(condition) #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2) #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2) @@ -173,7 +189,6 @@ DEFINE_RTC_CHECK_OP_IMPL(GT, > ) #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2) #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2) #else -#define RTC_DCHECK_IS_ON 0 #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition) #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) == (v2)) #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) != (v2)) @@ -227,4 +242,38 @@ inline T CheckedDivExact(T a, T b) { } // namespace rtc +#else // __cplusplus not defined +// C version. Lacks many features compared to the C++ version, but usage +// guidelines are the same. + +#define RTC_CHECK(condition) \ + do { \ + if (!(condition)) { \ + rtc_FatalMessage(__FILE__, __LINE__, "CHECK failed: " #condition); \ + } \ + } while (0) + +#define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b)) +#define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b)) +#define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b)) +#define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b)) +#define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b)) +#define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b)) + +#define RTC_DCHECK(condition) \ + do { \ + if (RTC_DCHECK_IS_ON && !(condition)) { \ + rtc_FatalMessage(__FILE__, __LINE__, "DCHECK failed: " #condition); \ + } \ + } while (0) + +#define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b)) +#define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b)) +#define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b)) +#define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b)) +#define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b)) +#define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b)) + +#endif // __cplusplus + #endif // WEBRTC_BASE_CHECKS_H_ diff --git a/webrtc/modules/audio_coding/codecs/opus/opus_interface.c b/webrtc/modules/audio_coding/codecs/opus/opus_interface.c index 9905735533..d79a9ad290 100644 --- a/webrtc/modules/audio_coding/codecs/opus/opus_interface.c +++ b/webrtc/modules/audio_coding/codecs/opus/opus_interface.c @@ -9,9 +9,10 @@ */ #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" + +#include "webrtc/base/checks.h" #include "webrtc/modules/audio_coding/codecs/opus/opus_inst.h" -#include #include #include @@ -51,7 +52,7 @@ int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst, } OpusEncInst* state = calloc(1, sizeof(OpusEncInst)); - assert(state); + RTC_DCHECK(state); int error; state->encoder = opus_encoder_create(48000, (int)channels, opus_app,