Bug 1414155 - Define pagesize_2pow in terms of pagesize, not the opposite. r?njn
At the same time, add user-defined literals to make those constants more
legible.
--- a/memory/build/Utils.h
+++ b/memory/build/Utils.h
@@ -25,9 +25,20 @@ int
CompareAddr(T* aAddr1, T* aAddr2)
{
uintptr_t addr1 = reinterpret_cast<uintptr_t>(aAddr1);
uintptr_t addr2 = reinterpret_cast<uintptr_t>(aAddr2);
return (addr1 > addr2) - (addr1 < addr2);
}
+// User-defined literals to make constants more legible
+constexpr unsigned long long int operator"" _KiB(unsigned long long int aNum)
+{
+ return aNum * 1024;
+}
+
+constexpr unsigned long long int operator"" _MiB(unsigned long long int aNum)
+{
+ return aNum * 1024_KiB;
+}
+
#endif
--- a/memory/build/mozjemalloc.cpp
+++ b/memory/build/mozjemalloc.cpp
@@ -417,23 +417,23 @@ static const unsigned nqbins = unsigned(
#ifdef MALLOC_STATIC_PAGESIZE
// VM page size. It must divide the runtime CPU page size or the code
// will abort.
// Platform specific page size conditions copied from js/public/HeapAPI.h
#if (defined(SOLARIS) || defined(__FreeBSD__)) && \
(defined(__sparc) || defined(__sparcv9) || defined(__ia64))
-#define pagesize_2pow (size_t(13))
+static const size_t pagesize = 8_KiB;
#elif defined(__powerpc64__)
-#define pagesize_2pow (size_t(16))
+static const size_t pagesize = 64_KiB;
#else
-#define pagesize_2pow (size_t(12))
+static const size_t pagesize = 4_KiB;
#endif
-#define pagesize (size_t(1) << pagesize_2pow)
+#define pagesize_2pow LOG2(pagesize)
#define pagesize_mask (pagesize - 1)
// Max size class for bins.
static const size_t bin_maxclass = pagesize >> 1;
// Number of (2^n)-spaced sub-page bins.
static const unsigned nsbins =
unsigned(pagesize_2pow - SMALL_MAX_2POW_DEFAULT - 1);
--- a/memory/gtest/TestJemalloc.cpp
+++ b/memory/gtest/TestJemalloc.cpp
@@ -3,16 +3,17 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/mozalloc.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Vector.h"
#include "mozmemory.h"
+#include "Utils.h"
#include "gtest/gtest.h"
using namespace mozilla;
static inline void
TestOne(size_t size)
{
@@ -30,33 +31,30 @@ TestOne(size_t size)
static inline void
TestThree(size_t size)
{
ASSERT_NO_FATAL_FAILURE(TestOne(size - 1));
ASSERT_NO_FATAL_FAILURE(TestOne(size));
ASSERT_NO_FATAL_FAILURE(TestOne(size + 1));
}
-#define K * 1024
-#define M * 1024 * 1024
-
TEST(Jemalloc, UsableSizeInAdvance)
{
/*
* Test every size up to a certain point, then (N-1, N, N+1) triplets for a
* various sizes beyond that.
*/
- for (size_t n = 0; n < 16 K; n++)
+ for (size_t n = 0; n < 16_KiB; n++)
ASSERT_NO_FATAL_FAILURE(TestOne(n));
- for (size_t n = 16 K; n < 1 M; n += 4 K)
+ for (size_t n = 16_KiB; n < 1_MiB; n += 4_KiB)
ASSERT_NO_FATAL_FAILURE(TestThree(n));
- for (size_t n = 1 M; n < 8 M; n += 128 K)
+ for (size_t n = 1_MiB; n < 8_MiB; n += 128_KiB)
ASSERT_NO_FATAL_FAILURE(TestThree(n));
}
static int gStaticVar;
bool InfoEq(jemalloc_ptr_info_t& aInfo, PtrInfoTag aTag, void* aAddr,
size_t aSize)
{
@@ -94,28 +92,28 @@ TEST(Jemalloc, PtrInfo)
ASSERT_TRUE(small.append(p));
for (size_t j = 0; j < usable; j++) {
jemalloc_ptr_info(&p[j], &info);
ASSERT_TRUE(InfoEq(info, TagLiveSmall, p, usable));
}
}
// Similar for large (2KiB + 1 KiB .. 1MiB - 8KiB) allocations.
- for (size_t n = small_max + 1 K; n <= stats.large_max; n += 1 K) {
+ for (size_t n = small_max + 1_KiB; n <= stats.large_max; n += 1_KiB) {
auto p = (char*)malloc(n);
size_t usable = moz_malloc_size_of(p);
ASSERT_TRUE(large.append(p));
for (size_t j = 0; j < usable; j += 347) {
jemalloc_ptr_info(&p[j], &info);
ASSERT_TRUE(InfoEq(info, TagLiveLarge, p, usable));
}
}
// Similar for huge (> 1MiB - 8KiB) allocations.
- for (size_t n = stats.chunksize; n <= 10 M; n += 512 K) {
+ for (size_t n = stats.chunksize; n <= 10_MiB; n += 512_KiB) {
auto p = (char*)malloc(n);
size_t usable = moz_malloc_size_of(p);
ASSERT_TRUE(huge.append(p));
for (size_t j = 0; j < usable; j += 567) {
jemalloc_ptr_info(&p[j], &info);
ASSERT_TRUE(InfoEq(info, TagLiveHuge, p, usable));
}
}
@@ -220,11 +218,8 @@ TEST(Jemalloc, PtrInfo)
// Entire chunk. It's impossible to check what is put into |info| for all of
// these addresses; this is more about checking that we don't crash.
for (size_t i = 0; i < stats.chunksize; i += 256) {
jemalloc_ptr_info(&chunk[i], &info);
}
jemalloc_thread_local_arena(false);
}
-
-#undef K
-#undef M
--- a/memory/gtest/moz.build
+++ b/memory/gtest/moz.build
@@ -4,8 +4,12 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
UNIFIED_SOURCES += [
'TestJemalloc.cpp',
]
FINAL_LIBRARY = 'xul-gtest'
+
+LOCAL_INCLUDES += [
+ '../build',
+]