From 1b721fc4146c033e3f8136cd49978aa785321cf8 Mon Sep 17 00:00:00 2001 From: Paul Bone Date: Thu, 21 Oct 2021 06:03:45 +0000 Subject: [PATCH] Bug 1735715 - Handle gMaxSubPageClass == 0 properly r=glandium Differential Revision: https://phabricator.services.mozilla.com/D128448 --- memory/build/Utils.h | 14 -------------- memory/build/mozjemalloc.cpp | 14 ++++++++------ mfbt/MathAlgorithms.h | 2 +- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/memory/build/Utils.h b/memory/build/Utils.h index 76d180d22eb8..6032bf23d0c6 100644 --- a/memory/build/Utils.h +++ b/memory/build/Utils.h @@ -20,20 +20,6 @@ struct Log2 : mozilla::tl::CeilingLog2 { }; #define LOG2(N) Log2::value -// Like Log2, but ignores 0. -template -struct Log2Or0 : mozilla::tl::CeilingLog2 { - using mozilla::tl::CeilingLog2::value; - static_assert(1ULL << value == N, "Number is not a power of 2"); -}; -template <> -struct Log2Or0<0> { - // This makes no sense but neither does any other value. It's just enough - // that this can be used on the unused side of a conditional expression. - static const size_t value = 0; -}; -#define LOG2_OR_0(N) Log2Or0::value - enum class Order { eLess = -1, eEqual = 0, diff --git a/memory/build/mozjemalloc.cpp b/memory/build/mozjemalloc.cpp index 022332399c36..583f5570079c 100644 --- a/memory/build/mozjemalloc.cpp +++ b/memory/build/mozjemalloc.cpp @@ -483,21 +483,21 @@ static size_t gPageSize; # define END_GLOBALS # define DEFINE_GLOBAL(type) static const type # define GLOBAL_LOG2 LOG2 -# define GLOBAL_LOG2_OR_0 LOG2_OR_0 # define GLOBAL_ASSERT_HELPER1(x) static_assert(x, # x) # define GLOBAL_ASSERT_HELPER2(x, y) static_assert(x, y) # define GLOBAL_ASSERT(...) \ MACRO_CALL( \ MOZ_PASTE_PREFIX_AND_ARG_COUNT(GLOBAL_ASSERT_HELPER, __VA_ARGS__), \ (__VA_ARGS__)) +# define GLOBAL_CONSTEXPR constexpr #else # define DECLARE_GLOBAL(type, name) static type name; # define DEFINE_GLOBALS static void DefineGlobals() { # define END_GLOBALS } # define DEFINE_GLOBAL(type) # define GLOBAL_LOG2 FloorLog2 -# define GLOBAL_LOG2_OR_0 FloorLog2 # define GLOBAL_ASSERT MOZ_RELEASE_ASSERT +# define GLOBAL_CONSTEXPR #endif DECLARE_GLOBAL(size_t, gMaxSubPageClass) @@ -520,10 +520,12 @@ gMaxSubPageClass = gPageSize / 2 >= kMinSubPageClass ? gPageSize / 2 : 0; // Number of sub-page bins. DEFINE_GLOBAL(uint8_t) -gNumSubPageClasses = - static_cast(gMaxSubPageClass ? GLOBAL_LOG2_OR_0(gMaxSubPageClass) - - LOG2(kMinSubPageClass) + 1 - : 0); +gNumSubPageClasses = []() GLOBAL_CONSTEXPR -> uint8_t { + if GLOBAL_CONSTEXPR (gMaxSubPageClass != 0) { + return FloorLog2(gMaxSubPageClass) - LOG2(kMinSubPageClass) + 1; + } + return 0; +}(); DEFINE_GLOBAL(uint8_t) gPageSize2Pow = GLOBAL_LOG2(gPageSize); DEFINE_GLOBAL(size_t) gPageSizeMask = gPageSize - 1; diff --git a/mfbt/MathAlgorithms.h b/mfbt/MathAlgorithms.h index 8c64685ef1d8..b60dbfa12e6e 100644 --- a/mfbt/MathAlgorithms.h +++ b/mfbt/MathAlgorithms.h @@ -375,7 +375,7 @@ class FloorLog2 { * FloorLog2(8..15) is 3; and so on. */ template -inline uint_fast8_t FloorLog2(const T aValue) { +inline constexpr uint_fast8_t FloorLog2(const T aValue) { return detail::FloorLog2::compute(aValue); }