Bug 1941128 - Turn off network.dns.native_https_query on Mac again
[gecko.git] / memory / build / mozmemory_utils.h
blob713c800d6b541b3ae13f2d58dd31884c5fe590b1
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozjemalloc_utils_h
8 #define mozjemalloc_utils_h
10 #include <optional>
11 #include <type_traits>
13 #if defined(MOZ_MEMORY) && defined(XP_WIN)
14 # include <wtypes.h>
15 # include "mozmemory_wrap.h"
16 #endif
18 namespace mozilla {
20 namespace detail {
21 // Helper for StallAndRetry error messages.
22 template <typename T>
23 constexpr bool is_std_optional = false;
24 template <typename T>
25 constexpr bool is_std_optional<std::optional<T>> = true;
26 } // namespace detail
28 struct StallSpecs {
29 // Maximum number of retry-attempts before giving up.
30 size_t maxAttempts;
31 // Delay time between successive events.
32 size_t delayMs;
34 // Retry a fallible operation until it succeeds or until we've run out of
35 // retries.
37 // Note that this invokes `aDelayFunc` immediately upon being called! It's
38 // intended for use in the unhappy path, after an initial attempt has failed.
40 // The function type here may be read:
41 // ```
42 // fn StallAndRetry<R>(
43 // delay_func: impl Fn(usize) -> (),
44 // operation: impl Fn() -> Option<R>,
45 // ) -> Option<R>;
46 // ```
48 template <typename DelayFunc, typename OpFunc>
49 auto StallAndRetry(DelayFunc&& aDelayFunc,
50 OpFunc&& aOperation) const -> decltype(aOperation()) {
52 // Explicit typecheck for OpFunc, to provide an explicit error message.
53 using detail::is_std_optional;
54 static_assert(is_std_optional<decltype(aOperation())>,
55 "aOperation() must return std::optional");
57 // (clang's existing error messages suffice for aDelayFunc.)
60 for (size_t i = 0; i < maxAttempts; ++i) {
61 aDelayFunc(delayMs);
62 if (const auto opt = aOperation()) {
63 return opt;
66 return std::nullopt;
70 #if defined(MOZ_MEMORY) && defined(XP_WIN)
71 MOZ_JEMALLOC_API StallSpecs GetAllocatorStallSpecs();
72 MOZ_JEMALLOC_API_NODISCARD void* MozVirtualAlloc(LPVOID lpAddress,
73 SIZE_T dwSize,
74 DWORD flAllocationType,
75 DWORD flProtect);
76 #endif
78 } // namespace mozilla
80 #endif // mozjemalloc_utils_h