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
11 #include <type_traits>
13 #if defined(MOZ_MEMORY) && defined(XP_WIN)
15 # include "mozmemory_wrap.h"
21 // Helper for StallAndRetry error messages.
23 constexpr bool is_std_optional
= false;
25 constexpr bool is_std_optional
<std::optional
<T
>> = true;
29 // Maximum number of retry-attempts before giving up.
31 // Delay time between successive events.
34 // Retry a fallible operation until it succeeds or until we've run out of
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:
42 // fn StallAndRetry<R>(
43 // delay_func: impl Fn(usize) -> (),
44 // operation: impl Fn() -> Option<R>,
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
) {
62 if (const auto opt
= aOperation()) {
70 #if defined(MOZ_MEMORY) && defined(XP_WIN)
71 MOZ_JEMALLOC_API StallSpecs
GetAllocatorStallSpecs();
72 MOZ_JEMALLOC_API_NODISCARD
void* MozVirtualAlloc(LPVOID lpAddress
,
74 DWORD flAllocationType
,
78 } // namespace mozilla
80 #endif // mozjemalloc_utils_h