Bug 1627646 - Avoid creating a Port object when there are no listeners r=mixedpuppy
[gecko.git] / mfbt / Assertions.cpp
blobcedfc07eba4d79b8eab1dca27b53c6238dd1ba7c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/Assertions.h"
7 #include "mozilla/Atomics.h"
9 #include <stdarg.h>
11 MOZ_BEGIN_EXTERN_C
14 * The crash reason is defined as a global variable here rather than in the
15 * crash reporter itself to make it available to all code, even libraries like
16 * JS that don't link with the crash reporter directly. This value will only
17 * be consumed if the crash reporter is used by the target application.
19 MFBT_DATA const char* gMozCrashReason = nullptr;
21 static char sPrintfCrashReason[sPrintfCrashReasonSize] = {};
23 // Accesses to this atomic are not included in web replay recordings, so that
24 // if we crash in an area where recorded events are not allowed the true reason
25 // for the crash is not obscured by a record/replay error.
26 static mozilla::Atomic<bool, mozilla::SequentiallyConsistent> sCrashing(false);
28 MFBT_API MOZ_COLD MOZ_NEVER_INLINE MOZ_FORMAT_PRINTF(1, 2) const
29 char* MOZ_CrashPrintf(const char* aFormat, ...) {
30 if (!sCrashing.compareExchange(false, true)) {
31 // In the unlikely event of a race condition, skip
32 // setting the crash reason and just crash safely.
33 MOZ_RELEASE_ASSERT(false);
35 va_list aArgs;
36 va_start(aArgs, aFormat);
37 int ret =
38 vsnprintf(sPrintfCrashReason, sPrintfCrashReasonSize, aFormat, aArgs);
39 va_end(aArgs);
40 MOZ_RELEASE_ASSERT(
41 ret >= 0 && size_t(ret) < sPrintfCrashReasonSize,
42 "Could not write the explanation string to the supplied buffer!");
43 return sPrintfCrashReason;
46 MOZ_END_EXTERN_C