Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/process/process_metrics.h" | 10 #include "base/process/process_metrics.h" |
| 11 #include "base/sys_info.h" | |
| 11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 #if defined(USE_TCMALLOC) | 15 #if defined(USE_TCMALLOC) |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 using std::min; | 18 using std::min; |
| 18 | 19 |
| 19 #ifdef NDEBUG | 20 #ifdef NDEBUG |
| 20 // We wrap malloc and free in noinline functions to ensure that we test the real | 21 // We wrap malloc and free in noinline functions to ensure that we test the real |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 } else { | 80 } else { |
| 80 EXPECT_NE(reinterpret_cast<void*>(NULL), p) | 81 EXPECT_NE(reinterpret_cast<void*>(NULL), p) |
| 81 << "calloc(n, s) should succeed"; | 82 << "calloc(n, s) should succeed"; |
| 82 for (size_t i = 0; i < n * s; i++) { | 83 for (size_t i = 0; i < n * s; i++) { |
| 83 EXPECT_EQ('\0', p[i]); | 84 EXPECT_EQ('\0', p[i]); |
| 84 } | 85 } |
| 85 free(p); | 86 free(p); |
| 86 } | 87 } |
| 87 } | 88 } |
| 88 | 89 |
| 90 bool IsLowMemoryDevice() { | |
| 91 // 256MiB | |
|
Primiano Tucci (use gerrit)
2017/04/04 14:10:51
this is IMHO redundant given the code below, and j
bcf
2017/04/04 18:44:06
Done.
| |
| 92 return base::SysInfo::AmountOfPhysicalMemory() <= 256LL * 1024 * 1024; | |
| 93 } | |
| 94 | |
| 89 } // namespace | 95 } // namespace |
| 90 | 96 |
| 91 TEST(TCMallocTest, Malloc) { | 97 TEST(TCMallocTest, Malloc) { |
| 92 // Try allocating data with a bunch of alignments and sizes | 98 // Try allocating data with a bunch of alignments and sizes |
| 93 for (int size = 1; size < 1048576; size *= 2) { | 99 for (int size = 1; size < 1048576; size *= 2) { |
| 94 unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size)); | 100 unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size)); |
| 95 // Should be 2 byte aligned | 101 // Should be 2 byte aligned |
| 96 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & 1); | 102 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & 1); |
| 97 Fill(ptr, size); | 103 Fill(ptr, size); |
| 98 EXPECT_TRUE(Valid(ptr, size)); | 104 EXPECT_TRUE(Valid(ptr, size)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 for (unsigned d = 0; d < s * 2; ++d) { | 139 for (unsigned d = 0; d < s * 2; ++d) { |
| 134 void* new_p = realloc(p, start_sizes[s] - deltas[d]); | 140 void* new_p = realloc(p, start_sizes[s] - deltas[d]); |
| 135 ASSERT_EQ(p, new_p); // realloc should not allocate new memory | 141 ASSERT_EQ(p, new_p); // realloc should not allocate new memory |
| 136 } | 142 } |
| 137 free(p); | 143 free(p); |
| 138 } | 144 } |
| 139 } | 145 } |
| 140 #endif | 146 #endif |
| 141 | 147 |
| 142 TEST(TCMallocTest, Realloc) { | 148 TEST(TCMallocTest, Realloc) { |
| 149 if (IsLowMemoryDevice()) | |
|
Primiano Tucci (use gerrit)
2017/04/04 14:10:51
to be honest, you could have moved the early retur
bcf
2017/04/04 18:44:06
Done.
| |
| 150 return; | |
| 151 | |
| 143 for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { | 152 for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { |
| 144 for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { | 153 for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { |
| 145 unsigned char* src = reinterpret_cast<unsigned char*>(malloc(src_size)); | 154 unsigned char* src = reinterpret_cast<unsigned char*>(malloc(src_size)); |
| 146 Fill(src, src_size); | 155 Fill(src, src_size); |
| 147 unsigned char* dst = | 156 unsigned char* dst = |
| 148 reinterpret_cast<unsigned char*>(realloc(src, dst_size)); | 157 reinterpret_cast<unsigned char*>(realloc(src, dst_size)); |
| 149 EXPECT_TRUE(Valid(dst, min(src_size, dst_size))); | 158 EXPECT_TRUE(Valid(dst, min(src_size, dst_size))); |
| 150 Fill(dst, dst_size); | 159 Fill(dst, dst_size); |
| 151 EXPECT_TRUE(Valid(dst, dst_size)); | 160 EXPECT_TRUE(Valid(dst, dst_size)); |
| 152 if (dst != NULL) | 161 if (dst != NULL) |
| 153 free(dst); | 162 free(dst); |
| 154 } | 163 } |
| 155 } | 164 } |
| 156 | 165 |
|
Primiano Tucci (use gerrit)
2017/04/04 14:10:51
to be honest, you could have moved the early retur
bcf
2017/04/04 18:44:06
Done.
| |
| 157 // Now make sure realloc works correctly even when we overflow the | 166 // Now make sure realloc works correctly even when we overflow the |
| 158 // packed cache, so some entries are evicted from the cache. | 167 // packed cache, so some entries are evicted from the cache. |
| 159 // The cache has 2^12 entries, keyed by page number. | 168 // The cache has 2^12 entries, keyed by page number. |
| 160 const int kNumEntries = 1 << 14; | 169 const int kNumEntries = 1 << 14; |
| 161 int** p = reinterpret_cast<int**>(malloc(sizeof(*p) * kNumEntries)); | 170 int** p = reinterpret_cast<int**>(malloc(sizeof(*p) * kNumEntries)); |
| 162 int sum = 0; | 171 int sum = 0; |
| 163 for (int i = 0; i < kNumEntries; i++) { | 172 for (int i = 0; i < kNumEntries; i++) { |
| 164 // no page size is likely to be bigger than 8192? | 173 // no page size is likely to be bigger than 8192? |
| 165 p[i] = reinterpret_cast<int*>(malloc(8192)); | 174 p[i] = reinterpret_cast<int*>(malloc(8192)); |
| 166 p[i][1000] = i; // use memory deep in the heart of p | 175 p[i][1000] = i; // use memory deep in the heart of p |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 const size_t kPageSize = base::GetPageSize(); | 224 const size_t kPageSize = base::GetPageSize(); |
| 216 for (size_t size = 1; size <= kPageSize; size <<= 1) { | 225 for (size_t size = 1; size <= kPageSize; size <<= 1) { |
| 217 char* p = reinterpret_cast<char*>(TCMallocDoMallocForTest(size)); | 226 char* p = reinterpret_cast<char*>(TCMallocDoMallocForTest(size)); |
| 218 ASSERT_DEATH(TCMallocDoFreeForTest(p); TCMallocDoFreeForTest(p), | 227 ASSERT_DEATH(TCMallocDoFreeForTest(p); TCMallocDoFreeForTest(p), |
| 219 "Circular loop in list detected"); | 228 "Circular loop in list detected"); |
| 220 } | 229 } |
| 221 } | 230 } |
| 222 #endif // NDEBUG | 231 #endif // NDEBUG |
| 223 | 232 |
| 224 #endif | 233 #endif |
| OLD | NEW |