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 return base::SysInfo::AmountOfPhysicalMemory() <= 256LL * 1024 * 1024; |
| 92 } |
| 93 |
89 } // namespace | 94 } // namespace |
90 | 95 |
91 TEST(TCMallocTest, Malloc) { | 96 TEST(TCMallocTest, Malloc) { |
92 // Try allocating data with a bunch of alignments and sizes | 97 // Try allocating data with a bunch of alignments and sizes |
93 for (int size = 1; size < 1048576; size *= 2) { | 98 for (int size = 1; size < 1048576; size *= 2) { |
94 unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size)); | 99 unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size)); |
95 // Should be 2 byte aligned | 100 // Should be 2 byte aligned |
96 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & 1); | 101 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & 1); |
97 Fill(ptr, size); | 102 Fill(ptr, size); |
98 EXPECT_TRUE(Valid(ptr, size)); | 103 EXPECT_TRUE(Valid(ptr, size)); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 unsigned char* dst = | 152 unsigned char* dst = |
148 reinterpret_cast<unsigned char*>(realloc(src, dst_size)); | 153 reinterpret_cast<unsigned char*>(realloc(src, dst_size)); |
149 EXPECT_TRUE(Valid(dst, min(src_size, dst_size))); | 154 EXPECT_TRUE(Valid(dst, min(src_size, dst_size))); |
150 Fill(dst, dst_size); | 155 Fill(dst, dst_size); |
151 EXPECT_TRUE(Valid(dst, dst_size)); | 156 EXPECT_TRUE(Valid(dst, dst_size)); |
152 if (dst != NULL) | 157 if (dst != NULL) |
153 free(dst); | 158 free(dst); |
154 } | 159 } |
155 } | 160 } |
156 | 161 |
| 162 // The logic below tries to allocate kNumEntries * 9000 ~= 130 MB of memory. |
| 163 // This would cause the test to crash on low memory devices with no VM |
| 164 // overcommit (e.g., chromecast). |
| 165 if (IsLowMemoryDevice()) |
| 166 return; |
| 167 |
157 // Now make sure realloc works correctly even when we overflow the | 168 // Now make sure realloc works correctly even when we overflow the |
158 // packed cache, so some entries are evicted from the cache. | 169 // packed cache, so some entries are evicted from the cache. |
159 // The cache has 2^12 entries, keyed by page number. | 170 // The cache has 2^12 entries, keyed by page number. |
160 const int kNumEntries = 1 << 14; | 171 const int kNumEntries = 1 << 14; |
161 int** p = reinterpret_cast<int**>(malloc(sizeof(*p) * kNumEntries)); | 172 int** p = reinterpret_cast<int**>(malloc(sizeof(*p) * kNumEntries)); |
162 int sum = 0; | 173 int sum = 0; |
163 for (int i = 0; i < kNumEntries; i++) { | 174 for (int i = 0; i < kNumEntries; i++) { |
164 // no page size is likely to be bigger than 8192? | 175 // no page size is likely to be bigger than 8192? |
165 p[i] = reinterpret_cast<int*>(malloc(8192)); | 176 p[i] = reinterpret_cast<int*>(malloc(8192)); |
166 p[i][1000] = i; // use memory deep in the heart of p | 177 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(); | 226 const size_t kPageSize = base::GetPageSize(); |
216 for (size_t size = 1; size <= kPageSize; size <<= 1) { | 227 for (size_t size = 1; size <= kPageSize; size <<= 1) { |
217 char* p = reinterpret_cast<char*>(TCMallocDoMallocForTest(size)); | 228 char* p = reinterpret_cast<char*>(TCMallocDoMallocForTest(size)); |
218 ASSERT_DEATH(TCMallocDoFreeForTest(p); TCMallocDoFreeForTest(p), | 229 ASSERT_DEATH(TCMallocDoFreeForTest(p); TCMallocDoFreeForTest(p), |
219 "Circular loop in list detected"); | 230 "Circular loop in list detected"); |
220 } | 231 } |
221 } | 232 } |
222 #endif // NDEBUG | 233 #endif // NDEBUG |
223 | 234 |
224 #endif | 235 #endif |
OLD | NEW |