Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: base/debug/thread_heap_usage_tracker_unittest.cc

Issue 2697123007: base: Add support for malloc zones to the allocator shim (Closed)
Patch Set: Windows compile error. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/debug/thread_heap_usage_tracker.h" 5 #include "base/debug/thread_heap_usage_tracker.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/allocator/allocator_shim.h" 9 #include "base/allocator/allocator_shim.h"
10 #include "base/allocator/features.h" 10 #include "base/allocator/features.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 dispatch_under_test_->next = &g_mock_dispatch; 61 dispatch_under_test_->next = &g_mock_dispatch;
62 } 62 }
63 63
64 void TearDown() override { 64 void TearDown() override {
65 ASSERT_EQ(&g_mock_dispatch, dispatch_under_test_->next); 65 ASSERT_EQ(&g_mock_dispatch, dispatch_under_test_->next);
66 66
67 dispatch_under_test_->next = nullptr; 67 dispatch_under_test_->next = nullptr;
68 } 68 }
69 69
70 void* MockMalloc(size_t size) { 70 void* MockMalloc(size_t size) {
71 return dispatch_under_test_->alloc_function(dispatch_under_test_, size); 71 return dispatch_under_test_->alloc_function(dispatch_under_test_, size,
72 nullptr);
72 } 73 }
73 74
74 void* MockCalloc(size_t n, size_t size) { 75 void* MockCalloc(size_t n, size_t size) {
75 return dispatch_under_test_->alloc_zero_initialized_function( 76 return dispatch_under_test_->alloc_zero_initialized_function(
76 dispatch_under_test_, n, size); 77 dispatch_under_test_, n, size, nullptr);
77 } 78 }
78 79
79 void* MockAllocAligned(size_t alignment, size_t size) { 80 void* MockAllocAligned(size_t alignment, size_t size) {
80 return dispatch_under_test_->alloc_aligned_function(dispatch_under_test_, 81 return dispatch_under_test_->alloc_aligned_function(
81 alignment, size); 82 dispatch_under_test_, alignment, size, nullptr);
82 } 83 }
83 84
84 void* MockRealloc(void* address, size_t size) { 85 void* MockRealloc(void* address, size_t size) {
85 return dispatch_under_test_->realloc_function(dispatch_under_test_, address, 86 return dispatch_under_test_->realloc_function(dispatch_under_test_, address,
86 size); 87 size, nullptr);
87 } 88 }
88 89
89 void MockFree(void* address) { 90 void MockFree(void* address) {
90 dispatch_under_test_->free_function(dispatch_under_test_, address); 91 dispatch_under_test_->free_function(dispatch_under_test_, address, nullptr);
91 } 92 }
92 93
93 size_t MockGetSizeEstimate(void* address) { 94 size_t MockGetSizeEstimate(void* address) {
94 return dispatch_under_test_->get_size_estimate_function( 95 return dispatch_under_test_->get_size_estimate_function(
95 dispatch_under_test_, address); 96 dispatch_under_test_, address, nullptr);
96 } 97 }
97 98
98 private: 99 private:
99 void RecordAlloc(void* address, size_t size) { 100 void RecordAlloc(void* address, size_t size) {
100 if (address != nullptr) 101 if (address != nullptr)
101 allocation_size_map_[address] = size; 102 allocation_size_map_[address] = size;
102 } 103 }
103 104
104 void DeleteAlloc(void* address) { 105 void DeleteAlloc(void* address) {
105 if (address != nullptr) 106 if (address != nullptr)
(...skipping 13 matching lines...) Expand all
119 ret += kAllocationPadding; 120 ret += kAllocationPadding;
120 break; 121 break;
121 case ZERO_SIZE_FUNCTION: 122 case ZERO_SIZE_FUNCTION:
122 ret = 0; 123 ret = 0;
123 break; 124 break;
124 } 125 }
125 126
126 return ret; 127 return ret;
127 } 128 }
128 129
129 static void* OnAllocFn(const AllocatorDispatch* self, size_t size) { 130 static void* OnAllocFn(const AllocatorDispatch* self,
131 size_t size,
132 void* context) {
130 EXPECT_EQ(&g_mock_dispatch, self); 133 EXPECT_EQ(&g_mock_dispatch, self);
131 134
132 void* ret = malloc(size); 135 void* ret = malloc(size);
133 g_self->RecordAlloc(ret, size); 136 g_self->RecordAlloc(ret, size);
134 return ret; 137 return ret;
135 } 138 }
136 139
137 static void* OnAllocZeroInitializedFn(const AllocatorDispatch* self, 140 static void* OnAllocZeroInitializedFn(const AllocatorDispatch* self,
138 size_t n, 141 size_t n,
139 size_t size) { 142 size_t size,
143 void* context) {
140 EXPECT_EQ(&g_mock_dispatch, self); 144 EXPECT_EQ(&g_mock_dispatch, self);
141 145
142 void* ret = calloc(n, size); 146 void* ret = calloc(n, size);
143 g_self->RecordAlloc(ret, n * size); 147 g_self->RecordAlloc(ret, n * size);
144 return ret; 148 return ret;
145 } 149 }
146 150
147 static void* OnAllocAlignedFn(const AllocatorDispatch* self, 151 static void* OnAllocAlignedFn(const AllocatorDispatch* self,
148 size_t alignment, 152 size_t alignment,
149 size_t size) { 153 size_t size,
154 void* context) {
150 EXPECT_EQ(&g_mock_dispatch, self); 155 EXPECT_EQ(&g_mock_dispatch, self);
151 156
152 // This is a cheat as it doesn't return aligned allocations. This has the 157 // This is a cheat as it doesn't return aligned allocations. This has the
153 // advantage of working for all platforms for this test. 158 // advantage of working for all platforms for this test.
154 void* ret = malloc(size); 159 void* ret = malloc(size);
155 g_self->RecordAlloc(ret, size); 160 g_self->RecordAlloc(ret, size);
156 return ret; 161 return ret;
157 } 162 }
158 163
159 static void* OnReallocFn(const AllocatorDispatch* self, 164 static void* OnReallocFn(const AllocatorDispatch* self,
160 void* address, 165 void* address,
161 size_t size) { 166 size_t size,
167 void* context) {
162 EXPECT_EQ(&g_mock_dispatch, self); 168 EXPECT_EQ(&g_mock_dispatch, self);
163 169
164 g_self->DeleteAlloc(address); 170 g_self->DeleteAlloc(address);
165 void* ret = realloc(address, size); 171 void* ret = realloc(address, size);
166 g_self->RecordAlloc(ret, size); 172 g_self->RecordAlloc(ret, size);
167 return ret; 173 return ret;
168 } 174 }
169 175
170 static void OnFreeFn(const AllocatorDispatch* self, void* address) { 176 static void OnFreeFn(const AllocatorDispatch* self,
177 void* address,
178 void* context) {
171 EXPECT_EQ(&g_mock_dispatch, self); 179 EXPECT_EQ(&g_mock_dispatch, self);
172 180
173 g_self->DeleteAlloc(address); 181 g_self->DeleteAlloc(address);
174 free(address); 182 free(address);
175 } 183 }
176 184
177 static size_t OnGetSizeEstimateFn(const AllocatorDispatch* self, 185 static size_t OnGetSizeEstimateFn(const AllocatorDispatch* self,
178 void* address) { 186 void* address,
187 void* context) {
179 EXPECT_EQ(&g_mock_dispatch, self); 188 EXPECT_EQ(&g_mock_dispatch, self);
180 189
181 return g_self->GetSizeEstimate(address); 190 return g_self->GetSizeEstimate(address);
182 } 191 }
183 192
184 using AllocationSizeMap = std::map<void*, size_t>; 193 using AllocationSizeMap = std::map<void*, size_t>;
185 194
186 SizeFunctionKind size_function_kind_; 195 SizeFunctionKind size_function_kind_;
187 AllocationSizeMap allocation_size_map_; 196 AllocationSizeMap allocation_size_map_;
188 AllocatorDispatch* dispatch_under_test_; 197 AllocatorDispatch* dispatch_under_test_;
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 EXPECT_LE(u2.free_ops + 1, u3.free_ops); 591 EXPECT_LE(u2.free_ops + 1, u3.free_ops);
583 592
584 TestingThreadHeapUsageTracker::DisableHeapTrackingForTesting(); 593 TestingThreadHeapUsageTracker::DisableHeapTrackingForTesting();
585 594
586 ASSERT_FALSE(ThreadHeapUsageTracker::IsHeapTrackingEnabled()); 595 ASSERT_FALSE(ThreadHeapUsageTracker::IsHeapTrackingEnabled());
587 } 596 }
588 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) 597 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
589 598
590 } // namespace debug 599 } // namespace debug
591 } // namespace base 600 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698