| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 definition of the FencedAllocator class. | 5 // This file contains the definition of the FencedAllocator class. |
| 6 | 6 |
| 7 #ifndef GPU_COMMAND_BUFFER_CLIENT_FENCED_ALLOCATOR_H_ | 7 #ifndef GPU_COMMAND_BUFFER_CLIENT_FENCED_ALLOCATOR_H_ |
| 8 #define GPU_COMMAND_BUFFER_CLIENT_FENCED_ALLOCATOR_H_ | 8 #define GPU_COMMAND_BUFFER_CLIENT_FENCED_ALLOCATOR_H_ |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 // Parameters: | 58 // Parameters: |
| 59 // offset: the offset of the memory block to free. | 59 // offset: the offset of the memory block to free. |
| 60 void Free(Offset offset); | 60 void Free(Offset offset); |
| 61 | 61 |
| 62 // Frees a block of memory, pending the passage of a token. That memory won't | 62 // Frees a block of memory, pending the passage of a token. That memory won't |
| 63 // be re-allocated until the token has passed through the command stream. | 63 // be re-allocated until the token has passed through the command stream. |
| 64 // | 64 // |
| 65 // Parameters: | 65 // Parameters: |
| 66 // offset: the offset of the memory block to free. | 66 // offset: the offset of the memory block to free. |
| 67 // token: the token value to wait for before re-using the memory. | 67 // token: the token value to wait for before re-using the memory. |
| 68 void FreePendingToken(Offset offset, unsigned int token); | 68 void FreePendingToken(Offset offset, int32 token); |
| 69 |
| 70 // Frees any blocks pending a token for which the token has been read. |
| 71 void FreeUnused(); |
| 69 | 72 |
| 70 // Gets the size of the largest free block that is available without waiting. | 73 // Gets the size of the largest free block that is available without waiting. |
| 71 unsigned int GetLargestFreeSize(); | 74 unsigned int GetLargestFreeSize(); |
| 72 | 75 |
| 73 // Gets the size of the largest free block that can be allocated if the | 76 // Gets the size of the largest free block that can be allocated if the |
| 74 // caller can wait. Allocating a block of this size will succeed, but may | 77 // caller can wait. Allocating a block of this size will succeed, but may |
| 75 // block. | 78 // block. |
| 76 unsigned int GetLargestFreeOrPendingSize(); | 79 unsigned int GetLargestFreeOrPendingSize(); |
| 77 | 80 |
| 78 // Checks for consistency inside the book-keeping structures. Used for | 81 // Checks for consistency inside the book-keeping structures. Used for |
| 79 // testing. | 82 // testing. |
| 80 bool CheckConsistency(); | 83 bool CheckConsistency(); |
| 81 | 84 |
| 82 private: | 85 private: |
| 83 // Status of a block of memory, for book-keeping. | 86 // Status of a block of memory, for book-keeping. |
| 84 enum State { | 87 enum State { |
| 85 IN_USE, | 88 IN_USE, |
| 86 FREE, | 89 FREE, |
| 87 FREE_PENDING_TOKEN | 90 FREE_PENDING_TOKEN |
| 88 }; | 91 }; |
| 89 | 92 |
| 90 // Book-keeping sturcture that describes a block of memory. | 93 // Book-keeping sturcture that describes a block of memory. |
| 91 struct Block { | 94 struct Block { |
| 92 State state; | 95 State state; |
| 93 Offset offset; | 96 Offset offset; |
| 94 unsigned int size; | 97 unsigned int size; |
| 95 unsigned int token; // token to wait for in the FREE_PENDING_TOKEN case. | 98 int32 token; // token to wait for in the FREE_PENDING_TOKEN case. |
| 96 }; | 99 }; |
| 97 | 100 |
| 98 // Comparison functor for memory block sorting. | 101 // Comparison functor for memory block sorting. |
| 99 class OffsetCmp { | 102 class OffsetCmp { |
| 100 public: | 103 public: |
| 101 bool operator() (const Block &left, const Block &right) { | 104 bool operator() (const Block &left, const Block &right) { |
| 102 return left.offset < right.offset; | 105 return left.offset < right.offset; |
| 103 } | 106 } |
| 104 }; | 107 }; |
| 105 | 108 |
| 106 typedef std::vector<Block> Container; | 109 typedef std::vector<Block> Container; |
| 107 typedef unsigned int BlockIndex; | 110 typedef unsigned int BlockIndex; |
| 108 | 111 |
| 109 static const unsigned int kUnusedToken = 0; | 112 static const int32 kUnusedToken = 0; |
| 110 | 113 |
| 111 // Gets the index of a memory block, given its offset. | 114 // Gets the index of a memory block, given its offset. |
| 112 BlockIndex GetBlockByOffset(Offset offset); | 115 BlockIndex GetBlockByOffset(Offset offset); |
| 113 | 116 |
| 114 // Collapse a free block with its neighbours if they are free. Returns the | 117 // Collapse a free block with its neighbours if they are free. Returns the |
| 115 // index of the collapsed block. | 118 // index of the collapsed block. |
| 116 // NOTE: this will invalidate block indices. | 119 // NOTE: this will invalidate block indices. |
| 117 BlockIndex CollapseFreeBlock(BlockIndex index); | 120 BlockIndex CollapseFreeBlock(BlockIndex index); |
| 118 | 121 |
| 119 // Waits for a FREE_PENDING_TOKEN block to be usable, and free it. Returns | 122 // Waits for a FREE_PENDING_TOKEN block to be usable, and free it. Returns |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 DCHECK(pointer); | 185 DCHECK(pointer); |
| 183 allocator_.Free(GetOffset(pointer)); | 186 allocator_.Free(GetOffset(pointer)); |
| 184 } | 187 } |
| 185 | 188 |
| 186 // Frees a block of memory, pending the passage of a token. That memory won't | 189 // Frees a block of memory, pending the passage of a token. That memory won't |
| 187 // be re-allocated until the token has passed through the command stream. | 190 // be re-allocated until the token has passed through the command stream. |
| 188 // | 191 // |
| 189 // Parameters: | 192 // Parameters: |
| 190 // pointer: the pointer to the memory block to free. | 193 // pointer: the pointer to the memory block to free. |
| 191 // token: the token value to wait for before re-using the memory. | 194 // token: the token value to wait for before re-using the memory. |
| 192 void FreePendingToken(void *pointer, unsigned int token) { | 195 void FreePendingToken(void *pointer, int32 token) { |
| 193 DCHECK(pointer); | 196 DCHECK(pointer); |
| 194 allocator_.FreePendingToken(GetOffset(pointer), token); | 197 allocator_.FreePendingToken(GetOffset(pointer), token); |
| 195 } | 198 } |
| 196 | 199 |
| 200 // Frees any blocks pending a token for which the token has been read. |
| 201 void FreeUnused() { |
| 202 allocator_.FreeUnused(); |
| 203 } |
| 204 |
| 197 // Gets a pointer to a memory block given the base memory and the offset. | 205 // Gets a pointer to a memory block given the base memory and the offset. |
| 198 // It translates FencedAllocator::kInvalidOffset to NULL. | 206 // It translates FencedAllocator::kInvalidOffset to NULL. |
| 199 void *GetPointer(FencedAllocator::Offset offset) { | 207 void *GetPointer(FencedAllocator::Offset offset) { |
| 200 return (offset == FencedAllocator::kInvalidOffset) ? | 208 return (offset == FencedAllocator::kInvalidOffset) ? |
| 201 NULL : static_cast<char *>(base_) + offset; | 209 NULL : static_cast<char *>(base_) + offset; |
| 202 } | 210 } |
| 203 | 211 |
| 204 // Gets the offset to a memory block given the base memory and the address. | 212 // Gets the offset to a memory block given the base memory and the address. |
| 205 // It translates NULL to FencedAllocator::kInvalidOffset. | 213 // It translates NULL to FencedAllocator::kInvalidOffset. |
| 206 FencedAllocator::Offset GetOffset(void *pointer) { | 214 FencedAllocator::Offset GetOffset(void *pointer) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 229 | 237 |
| 230 private: | 238 private: |
| 231 FencedAllocator allocator_; | 239 FencedAllocator allocator_; |
| 232 void* base_; | 240 void* base_; |
| 233 DISALLOW_IMPLICIT_CONSTRUCTORS(FencedAllocatorWrapper); | 241 DISALLOW_IMPLICIT_CONSTRUCTORS(FencedAllocatorWrapper); |
| 234 }; | 242 }; |
| 235 | 243 |
| 236 } // namespace gpu | 244 } // namespace gpu |
| 237 | 245 |
| 238 #endif // GPU_COMMAND_BUFFER_CLIENT_FENCED_ALLOCATOR_H_ | 246 #endif // GPU_COMMAND_BUFFER_CLIENT_FENCED_ALLOCATOR_H_ |
| OLD | NEW |