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

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

Issue 11419280: Make FencedAlloctor fail on size = 0. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add separate zero allocation tracking Created 8 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 implementation of the FencedAllocator class. 5 // This file contains the implementation of the FencedAllocator class.
6 6
7 #include "../client/fenced_allocator.h" 7 #include "../client/fenced_allocator.h"
8 #include <algorithm> 8 #include <algorithm>
9 #include "../client/cmd_buffer_helper.h" 9 #include "../client/cmd_buffer_helper.h"
10 10
11 namespace gpu { 11 namespace gpu {
12 12
13 #ifndef _MSC_VER 13 #ifndef _MSC_VER
14 const FencedAllocator::Offset FencedAllocator::kInvalidOffset; 14 const FencedAllocator::Offset FencedAllocator::kInvalidOffset;
15 #endif 15 #endif
16 16
17 FencedAllocator::FencedAllocator(unsigned int size, 17 FencedAllocator::FencedAllocator(unsigned int size,
18 CommandBufferHelper *helper) 18 CommandBufferHelper *helper)
19 : helper_(helper) { 19 : helper_(helper),
20 zero_offset_(size) {
20 Block block = { FREE, 0, size, kUnusedToken }; 21 Block block = { FREE, 0, size, kUnusedToken };
21 blocks_.push_back(block); 22 blocks_.push_back(block);
22 } 23 }
23 24
24 FencedAllocator::~FencedAllocator() { 25 FencedAllocator::~FencedAllocator() {
25 // Free blocks pending tokens. 26 // Free blocks pending tokens.
26 for (unsigned int i = 0; i < blocks_.size(); ++i) { 27 for (unsigned int i = 0; i < blocks_.size(); ++i) {
27 if (blocks_[i].state == FREE_PENDING_TOKEN) { 28 if (blocks_[i].state == FREE_PENDING_TOKEN) {
28 i = WaitForTokenAndFreeBlock(i); 29 i = WaitForTokenAndFreeBlock(i);
29 } 30 }
30 } 31 }
31 // These checks are not valid if the service has crashed or lost the context. 32 // These checks are not valid if the service has crashed or lost the context.
32 // GPU_DCHECK_EQ(blocks_.size(), 1u); 33 // GPU_DCHECK_EQ(blocks_.size(), 1u);
33 // GPU_DCHECK_EQ(blocks_[0].state, FREE); 34 // GPU_DCHECK_EQ(blocks_[0].state, FREE);
34 } 35 }
35 36
36 // Looks for a non-allocated block that is big enough. Search in the FREE 37 // Looks for a non-allocated block that is big enough. Search in the FREE
37 // blocks first (for direct usage), first-fit, then in the FREE_PENDING_TOKEN 38 // blocks first (for direct usage), first-fit, then in the FREE_PENDING_TOKEN
38 // blocks, waiting for them. The current implementation isn't smart about 39 // blocks, waiting for them. The current implementation isn't smart about
39 // optimizing what to wait for, just looks inside the block in order (first-fit 40 // optimizing what to wait for, just looks inside the block in order (first-fit
40 // as well). 41 // as well).
41 FencedAllocator::Offset FencedAllocator::Alloc(unsigned int size) { 42 FencedAllocator::Offset FencedAllocator::Alloc(unsigned int size) {
42 // Similarly to malloc, an allocation of 0 allocates at least 1 byte, to 43 // Allocate zero size allocations outside the block. This is to satisfy 2
43 // return different pointers every time. 44 // issues. (1) that like malloc all ptrs are different (2) that
44 if (size == 0) size = 1; 45 // Alloc(GetLargestFreeSize()) always works.
46 if (size == 0) {
47 for (size_t ii = 0; ii < zero_allocs_.size(); ++ii) {
apatrick 2012/12/03 20:45:43 How common is allocating with zero size? You can't
48 if (!zero_allocs_[ii]) {
49 return zero_offset_ + ii;
50 }
51 }
52 zero_allocs_.push_back(true);
53 return zero_offset_ + zero_allocs_.size() - 1;
54 }
45 55
46 // Try first to allocate in a free block. 56 // Try first to allocate in a free block.
47 for (unsigned int i = 0; i < blocks_.size(); ++i) { 57 for (unsigned int i = 0; i < blocks_.size(); ++i) {
48 Block &block = blocks_[i]; 58 Block &block = blocks_[i];
49 if (block.state == FREE && block.size >= size) { 59 if (block.state == FREE && block.size >= size) {
50 return AllocInBlock(i, size); 60 return AllocInBlock(i, size);
51 } 61 }
52 } 62 }
53 63
54 // No free block is available. Look for blocks pending tokens, and wait for 64 // No free block is available. Look for blocks pending tokens, and wait for
55 // them to be re-usable. 65 // them to be re-usable.
56 for (unsigned int i = 0; i < blocks_.size(); ++i) { 66 for (unsigned int i = 0; i < blocks_.size(); ++i) {
57 if (blocks_[i].state != FREE_PENDING_TOKEN) 67 if (blocks_[i].state != FREE_PENDING_TOKEN)
58 continue; 68 continue;
59 i = WaitForTokenAndFreeBlock(i); 69 i = WaitForTokenAndFreeBlock(i);
60 if (blocks_[i].size >= size) 70 if (blocks_[i].size >= size)
61 return AllocInBlock(i, size); 71 return AllocInBlock(i, size);
62 } 72 }
63 return kInvalidOffset; 73 return kInvalidOffset;
64 } 74 }
65 75
76 void FencedAllocator::FreeZeroAlloc(FencedAllocator::Offset offset) {
77 size_t index = offset - zero_offset_;
78 zero_allocs_[index] = false;
apatrick 2012/12/03 20:45:43 Would DCHECK(zero_allocs_[index]) make sense here?
79 }
80
66 // Looks for the corresponding block, mark it FREE, and collapse it if 81 // Looks for the corresponding block, mark it FREE, and collapse it if
67 // necessary. 82 // necessary.
68 void FencedAllocator::Free(FencedAllocator::Offset offset) { 83 void FencedAllocator::Free(FencedAllocator::Offset offset) {
84 if (offset >= zero_offset_) {
85 FreeZeroAlloc(offset);
86 return;
87 }
69 BlockIndex index = GetBlockByOffset(offset); 88 BlockIndex index = GetBlockByOffset(offset);
70 GPU_DCHECK_NE(blocks_[index].state, FREE); 89 GPU_DCHECK_NE(blocks_[index].state, FREE);
71 blocks_[index].state = FREE; 90 blocks_[index].state = FREE;
72 CollapseFreeBlock(index); 91 CollapseFreeBlock(index);
73 } 92 }
74 93
75 // Looks for the corresponding block, mark it FREE_PENDING_TOKEN. 94 // Looks for the corresponding block, mark it FREE_PENDING_TOKEN.
76 void FencedAllocator::FreePendingToken( 95 void FencedAllocator::FreePendingToken(
77 FencedAllocator::Offset offset, int32 token) { 96 FencedAllocator::Offset offset, int32 token) {
97 if (offset >= zero_offset_) {
98 FreeZeroAlloc(offset);
99 return;
100 }
78 BlockIndex index = GetBlockByOffset(offset); 101 BlockIndex index = GetBlockByOffset(offset);
79 Block &block = blocks_[index]; 102 Block &block = blocks_[index];
80 block.state = FREE_PENDING_TOKEN; 103 block.state = FREE_PENDING_TOKEN;
81 block.token = token; 104 block.token = token;
82 } 105 }
83 106
84 // Gets the max of the size of the blocks marked as free. 107 // Gets the max of the size of the blocks marked as free.
85 unsigned int FencedAllocator::GetLargestFreeSize() { 108 unsigned int FencedAllocator::GetLargestFreeSize() {
86 unsigned int max_size = 0; 109 unsigned int max_size = 0;
87 for (unsigned int i = 0; i < blocks_.size(); ++i) { 110 for (unsigned int i = 0; i < blocks_.size(); ++i) {
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 // The blocks are in offset order, so we can do a binary search. 226 // The blocks are in offset order, so we can do a binary search.
204 FencedAllocator::BlockIndex FencedAllocator::GetBlockByOffset(Offset offset) { 227 FencedAllocator::BlockIndex FencedAllocator::GetBlockByOffset(Offset offset) {
205 Block templ = { IN_USE, offset, 0, kUnusedToken }; 228 Block templ = { IN_USE, offset, 0, kUnusedToken };
206 Container::iterator it = std::lower_bound(blocks_.begin(), blocks_.end(), 229 Container::iterator it = std::lower_bound(blocks_.begin(), blocks_.end(),
207 templ, OffsetCmp()); 230 templ, OffsetCmp());
208 GPU_DCHECK(it != blocks_.end() && it->offset == offset); 231 GPU_DCHECK(it != blocks_.end() && it->offset == offset);
209 return it-blocks_.begin(); 232 return it-blocks_.begin();
210 } 233 }
211 234
212 } // namespace gpu 235 } // namespace gpu
OLDNEW
« no previous file with comments | « gpu/command_buffer/client/fenced_allocator.h ('k') | gpu/command_buffer/client/fenced_allocator_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698