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

Side by Side Diff: gpu/command_buffer/client/ring_buffer_test.cc

Issue 1796010: Fix some memory leaks in GPU tests. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Created 10 years, 7 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
« no previous file with comments | « gpu/command_buffer/client/fenced_allocator_test.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 // This file contains the tests for the RingBuffer class. 5 // This file contains the tests for the RingBuffer class.
6 6
7 #include "gpu/command_buffer/client/ring_buffer.h" 7 #include "gpu/command_buffer/client/ring_buffer.h"
8 #include "base/at_exit.h" 8 #include "base/at_exit.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 api_mock_->set_engine(gpu_processor_.get()); 61 api_mock_->set_engine(gpu_processor_.get());
62 62
63 helper_.reset(new CommandBufferHelper(command_buffer_.get())); 63 helper_.reset(new CommandBufferHelper(command_buffer_.get()));
64 helper_->Initialize(); 64 helper_->Initialize();
65 } 65 }
66 66
67 int32 GetToken() { 67 int32 GetToken() {
68 return command_buffer_->GetState().token; 68 return command_buffer_->GetState().token;
69 } 69 }
70 70
71 virtual void TearDown() {
72 helper_.release();
73 }
74
75 base::ScopedNSAutoreleasePool autorelease_pool_; 71 base::ScopedNSAutoreleasePool autorelease_pool_;
76 base::AtExitManager at_exit_manager_; 72 base::AtExitManager at_exit_manager_;
77 MessageLoop message_loop_; 73 MessageLoop message_loop_;
78 scoped_ptr<AsyncAPIMock> api_mock_; 74 scoped_ptr<AsyncAPIMock> api_mock_;
79 scoped_ptr<CommandBufferService> command_buffer_; 75 scoped_ptr<CommandBufferService> command_buffer_;
80 scoped_ptr<GPUProcessor> gpu_processor_; 76 scoped_ptr<GPUProcessor> gpu_processor_;
81 CommandParser* parser_; 77 CommandParser* parser_;
82 scoped_ptr<CommandBufferHelper> helper_; 78 scoped_ptr<CommandBufferHelper> helper_;
83 }; 79 };
84 80
(...skipping 10 matching lines...) Expand all
95 protected: 91 protected:
96 virtual void SetUp() { 92 virtual void SetUp() {
97 BaseRingBufferTest::SetUp(); 93 BaseRingBufferTest::SetUp();
98 allocator_.reset(new RingBuffer(kBaseOffset, kBufferSize, helper_.get())); 94 allocator_.reset(new RingBuffer(kBaseOffset, kBufferSize, helper_.get()));
99 } 95 }
100 96
101 virtual void TearDown() { 97 virtual void TearDown() {
102 // If the GPUProcessor posts any tasks, this forces them to run. 98 // If the GPUProcessor posts any tasks, this forces them to run.
103 MessageLoop::current()->RunAllPending(); 99 MessageLoop::current()->RunAllPending();
104 100
105 allocator_.release();
106
107 BaseRingBufferTest::TearDown(); 101 BaseRingBufferTest::TearDown();
108 } 102 }
109 103
110 scoped_ptr<RingBuffer> allocator_; 104 scoped_ptr<RingBuffer> allocator_;
111 }; 105 };
112 106
113 // Checks basic alloc and free. 107 // Checks basic alloc and free.
114 TEST_F(RingBufferTest, TestBasic) { 108 TEST_F(RingBufferTest, TestBasic) {
115 const unsigned int kSize = 16; 109 const unsigned int kSize = 16;
116 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeOrPendingSize()); 110 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeOrPendingSize());
117 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeSizeNoWaiting()); 111 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeSizeNoWaiting());
118 RingBuffer::Offset offset = allocator_->Alloc(kSize); 112 RingBuffer::Offset offset = allocator_->Alloc(kSize);
119 EXPECT_GE(kBufferSize, offset - kBaseOffset + kSize); 113 EXPECT_GE(kBufferSize, offset - kBaseOffset + kSize);
120 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeOrPendingSize()); 114 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeOrPendingSize());
121 EXPECT_EQ(kBufferSize - kSize, allocator_->GetLargestFreeSizeNoWaiting()); 115 EXPECT_EQ(kBufferSize - kSize, allocator_->GetLargestFreeSizeNoWaiting());
122 int32 token = helper_.get()->InsertToken(); 116 int32 token = helper_->InsertToken();
123 allocator_->FreePendingToken(offset, token); 117 allocator_->FreePendingToken(offset, token);
124 } 118 }
125 119
126 // Checks the free-pending-token mechanism. 120 // Checks the free-pending-token mechanism.
127 TEST_F(RingBufferTest, TestFreePendingToken) { 121 TEST_F(RingBufferTest, TestFreePendingToken) {
128 const unsigned int kSize = 16; 122 const unsigned int kSize = 16;
129 const unsigned int kAllocCount = kBufferSize / kSize; 123 const unsigned int kAllocCount = kBufferSize / kSize;
130 CHECK(kAllocCount * kSize == kBufferSize); 124 CHECK(kAllocCount * kSize == kBufferSize);
131 125
132 // Allocate several buffers to fill in the memory. 126 // Allocate several buffers to fill in the memory.
133 int32 tokens[kAllocCount]; 127 int32 tokens[kAllocCount];
134 for (unsigned int ii = 0; ii < kAllocCount; ++ii) { 128 for (unsigned int ii = 0; ii < kAllocCount; ++ii) {
135 RingBuffer::Offset offset = allocator_->Alloc(kSize); 129 RingBuffer::Offset offset = allocator_->Alloc(kSize);
136 EXPECT_GE(kBufferSize, offset - kBaseOffset + kSize); 130 EXPECT_GE(kBufferSize, offset - kBaseOffset + kSize);
137 tokens[ii] = helper_.get()->InsertToken(); 131 tokens[ii] = helper_->InsertToken();
138 allocator_->FreePendingToken(offset, tokens[ii]); 132 allocator_->FreePendingToken(offset, tokens[ii]);
139 } 133 }
140 134
141 EXPECT_EQ(kBufferSize - (kSize * kAllocCount), 135 EXPECT_EQ(kBufferSize - (kSize * kAllocCount),
142 allocator_->GetLargestFreeSizeNoWaiting()); 136 allocator_->GetLargestFreeSizeNoWaiting());
143 137
144 // This allocation will need to reclaim the space freed above, so that should 138 // This allocation will need to reclaim the space freed above, so that should
145 // process the commands until a token is passed. 139 // process the commands until a token is passed.
146 RingBuffer::Offset offset1 = allocator_->Alloc(kSize); 140 RingBuffer::Offset offset1 = allocator_->Alloc(kSize);
147 EXPECT_EQ(kBaseOffset, offset1); 141 EXPECT_EQ(kBaseOffset, offset1);
148 142
149 // Check that the token has indeed passed. 143 // Check that the token has indeed passed.
150 EXPECT_LE(tokens[0], GetToken()); 144 EXPECT_LE(tokens[0], GetToken());
151 145
152 allocator_->FreePendingToken(offset1, helper_.get()->InsertToken()); 146 allocator_->FreePendingToken(offset1, helper_->InsertToken());
153 } 147 }
154 148
155 // Tests GetLargestFreeSizeNoWaiting 149 // Tests GetLargestFreeSizeNoWaiting
156 TEST_F(RingBufferTest, TestGetLargestFreeSizeNoWaiting) { 150 TEST_F(RingBufferTest, TestGetLargestFreeSizeNoWaiting) {
157 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeSizeNoWaiting()); 151 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeSizeNoWaiting());
158 152
159 RingBuffer::Offset offset = allocator_->Alloc(kBufferSize); 153 RingBuffer::Offset offset = allocator_->Alloc(kBufferSize);
160 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); 154 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting());
161 allocator_->FreePendingToken(offset, helper_.get()->InsertToken()); 155 allocator_->FreePendingToken(offset, helper_->InsertToken());
162 } 156 }
163 157
164 // Test fixture for RingBufferWrapper test - Creates a 158 // Test fixture for RingBufferWrapper test - Creates a
165 // RingBufferWrapper, using a CommandBufferHelper with a mock 159 // RingBufferWrapper, using a CommandBufferHelper with a mock
166 // AsyncAPIInterface for its interface (calling it directly, not through the 160 // AsyncAPIInterface for its interface (calling it directly, not through the
167 // RPC mechanism), making sure Noops are ignored and SetToken are properly 161 // RPC mechanism), making sure Noops are ignored and SetToken are properly
168 // forwarded to the engine. 162 // forwarded to the engine.
169 class RingBufferWrapperTest : public BaseRingBufferTest { 163 class RingBufferWrapperTest : public BaseRingBufferTest {
170 protected: 164 protected:
171 virtual void SetUp() { 165 virtual void SetUp() {
172 BaseRingBufferTest::SetUp(); 166 BaseRingBufferTest::SetUp();
173 167
174 // Though allocating this buffer isn't strictly necessary, it makes 168 // Though allocating this buffer isn't strictly necessary, it makes
175 // allocations point to valid addresses, so they could be used for 169 // allocations point to valid addresses, so they could be used for
176 // something. 170 // something.
177 buffer_.reset(new int8[kBufferSize + kBaseOffset]); 171 buffer_.reset(new int8[kBufferSize + kBaseOffset]);
178 buffer_start_ = buffer_.get() + kBaseOffset; 172 buffer_start_ = buffer_.get() + kBaseOffset;
179 allocator_.reset(new RingBufferWrapper( 173 allocator_.reset(new RingBufferWrapper(
180 kBaseOffset, kBufferSize, helper_.get(), buffer_start_)); 174 kBaseOffset, kBufferSize, helper_.get(), buffer_start_));
181 } 175 }
182 176
183 virtual void TearDown() { 177 virtual void TearDown() {
184 // If the GPUProcessor posts any tasks, this forces them to run. 178 // If the GPUProcessor posts any tasks, this forces them to run.
185 MessageLoop::current()->RunAllPending(); 179 MessageLoop::current()->RunAllPending();
186 180
187 allocator_.release();
188 buffer_.release();
189
190 BaseRingBufferTest::TearDown(); 181 BaseRingBufferTest::TearDown();
191 } 182 }
192 183
193 scoped_ptr<RingBufferWrapper> allocator_; 184 scoped_ptr<RingBufferWrapper> allocator_;
194 scoped_array<int8> buffer_; 185 scoped_array<int8> buffer_;
195 int8* buffer_start_; 186 int8* buffer_start_;
196 }; 187 };
197 188
198 // Checks basic alloc and free. 189 // Checks basic alloc and free.
199 TEST_F(RingBufferWrapperTest, TestBasic) { 190 TEST_F(RingBufferWrapperTest, TestBasic) {
200 const unsigned int kSize = 16; 191 const unsigned int kSize = 16;
201 void* pointer = allocator_->Alloc(kSize); 192 void* pointer = allocator_->Alloc(kSize);
202 ASSERT_TRUE(pointer); 193 ASSERT_TRUE(pointer);
203 EXPECT_LE(buffer_start_, static_cast<int8*>(pointer)); 194 EXPECT_LE(buffer_start_, static_cast<int8*>(pointer));
204 EXPECT_GE(kBufferSize, static_cast<int8*>(pointer) - buffer_start_ + kSize); 195 EXPECT_GE(kBufferSize, static_cast<int8*>(pointer) - buffer_start_ + kSize);
205 196
206 allocator_->FreePendingToken(pointer, helper_.get()->InsertToken()); 197 allocator_->FreePendingToken(pointer, helper_->InsertToken());
207 198
208 int8* pointer_int8 = allocator_->AllocTyped<int8>(kSize); 199 int8* pointer_int8 = allocator_->AllocTyped<int8>(kSize);
209 ASSERT_TRUE(pointer_int8); 200 ASSERT_TRUE(pointer_int8);
210 EXPECT_LE(buffer_start_, pointer_int8); 201 EXPECT_LE(buffer_start_, pointer_int8);
211 EXPECT_GE(buffer_start_ + kBufferSize, pointer_int8 + kSize); 202 EXPECT_GE(buffer_start_ + kBufferSize, pointer_int8 + kSize);
212 allocator_->FreePendingToken(pointer_int8, helper_.get()->InsertToken()); 203 allocator_->FreePendingToken(pointer_int8, helper_->InsertToken());
213 204
214 unsigned int* pointer_uint = allocator_->AllocTyped<unsigned int>(kSize); 205 unsigned int* pointer_uint = allocator_->AllocTyped<unsigned int>(kSize);
215 ASSERT_TRUE(pointer_uint); 206 ASSERT_TRUE(pointer_uint);
216 EXPECT_LE(buffer_start_, reinterpret_cast<int8*>(pointer_uint)); 207 EXPECT_LE(buffer_start_, reinterpret_cast<int8*>(pointer_uint));
217 EXPECT_GE(buffer_start_ + kBufferSize, 208 EXPECT_GE(buffer_start_ + kBufferSize,
218 reinterpret_cast<int8* >(pointer_uint + kSize)); 209 reinterpret_cast<int8* >(pointer_uint + kSize));
219 210
220 // Check that it did allocate kSize * sizeof(unsigned int). We can't tell 211 // Check that it did allocate kSize * sizeof(unsigned int). We can't tell
221 // directly, except from the remaining size. 212 // directly, except from the remaining size.
222 EXPECT_EQ(kBufferSize - kSize - kSize - kSize * sizeof(*pointer_uint), 213 EXPECT_EQ(kBufferSize - kSize - kSize - kSize * sizeof(*pointer_uint),
223 allocator_->GetLargestFreeSizeNoWaiting()); 214 allocator_->GetLargestFreeSizeNoWaiting());
224 allocator_->FreePendingToken(pointer_uint, helper_.get()->InsertToken()); 215 allocator_->FreePendingToken(pointer_uint, helper_->InsertToken());
225 } 216 }
226 217
227 // Checks the free-pending-token mechanism. 218 // Checks the free-pending-token mechanism.
228 TEST_F(RingBufferWrapperTest, TestFreePendingToken) { 219 TEST_F(RingBufferWrapperTest, TestFreePendingToken) {
229 const unsigned int kSize = 16; 220 const unsigned int kSize = 16;
230 const unsigned int kAllocCount = kBufferSize / kSize; 221 const unsigned int kAllocCount = kBufferSize / kSize;
231 CHECK(kAllocCount * kSize == kBufferSize); 222 CHECK(kAllocCount * kSize == kBufferSize);
232 223
233 // Allocate several buffers to fill in the memory. 224 // Allocate several buffers to fill in the memory.
234 int32 tokens[kAllocCount]; 225 int32 tokens[kAllocCount];
235 for (unsigned int ii = 0; ii < kAllocCount; ++ii) { 226 for (unsigned int ii = 0; ii < kAllocCount; ++ii) {
236 void* pointer = allocator_->Alloc(kSize); 227 void* pointer = allocator_->Alloc(kSize);
237 EXPECT_TRUE(pointer != NULL); 228 EXPECT_TRUE(pointer != NULL);
238 tokens[ii] = helper_.get()->InsertToken(); 229 tokens[ii] = helper_->InsertToken();
239 allocator_->FreePendingToken(pointer, helper_.get()->InsertToken()); 230 allocator_->FreePendingToken(pointer, helper_->InsertToken());
240 } 231 }
241 232
242 EXPECT_EQ(kBufferSize - (kSize * kAllocCount), 233 EXPECT_EQ(kBufferSize - (kSize * kAllocCount),
243 allocator_->GetLargestFreeSizeNoWaiting()); 234 allocator_->GetLargestFreeSizeNoWaiting());
244 235
245 // This allocation will need to reclaim the space freed above, so that should 236 // This allocation will need to reclaim the space freed above, so that should
246 // process the commands until the token is passed. 237 // process the commands until the token is passed.
247 void* pointer1 = allocator_->Alloc(kSize); 238 void* pointer1 = allocator_->Alloc(kSize);
248 EXPECT_EQ(buffer_start_, static_cast<int8*>(pointer1)); 239 EXPECT_EQ(buffer_start_, static_cast<int8*>(pointer1));
249 240
250 // Check that the token has indeed passed. 241 // Check that the token has indeed passed.
251 EXPECT_LE(tokens[0], GetToken()); 242 EXPECT_LE(tokens[0], GetToken());
252 243
253 allocator_->FreePendingToken(pointer1, helper_.get()->InsertToken()); 244 allocator_->FreePendingToken(pointer1, helper_->InsertToken());
254 } 245 }
255 246
256 } // namespace gpu 247 } // namespace gpu
OLDNEW
« no previous file with comments | « gpu/command_buffer/client/fenced_allocator_test.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698