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 implementation of IdAllocator. | 5 // This file contains the implementation of IdAllocator. |
6 | 6 |
7 #include "../common/id_allocator.h" | 7 #include "../common/id_allocator.h" |
8 #include "../common/logging.h" | 8 #include "../common/logging.h" |
9 | 9 |
10 namespace gpu { | 10 namespace gpu { |
11 | 11 |
12 IdAllocator::IdAllocator() { | 12 IdAllocator::IdAllocator() {} |
| 13 |
| 14 IdAllocator::~IdAllocator() {} |
| 15 |
| 16 ResourceId IdAllocator::AllocateID() { |
| 17 ResourceId id = FindFirstFree(); |
| 18 MarkAsUsed(id); |
| 19 return id; |
| 20 } |
| 21 |
| 22 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) { |
| 23 GPU_DCHECK_LT(static_cast<ResourceId>(used_ids_.size()), |
| 24 static_cast<ResourceId>(-1)); |
| 25 for (; InUse(desired_id); ++desired_id) {} |
| 26 MarkAsUsed(desired_id); |
| 27 return desired_id; |
| 28 } |
| 29 |
| 30 bool IdAllocator::MarkAsUsed(ResourceId id) { |
| 31 std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id); |
| 32 return result.second; |
| 33 } |
| 34 |
| 35 void IdAllocator::FreeID(ResourceId id) { |
| 36 used_ids_.erase(id); |
| 37 } |
| 38 |
| 39 bool IdAllocator::InUse(ResourceId id) const { |
| 40 return id == kInvalidResource || used_ids_.find(id) != used_ids_.end(); |
13 } | 41 } |
14 | 42 |
15 ResourceId IdAllocator::FindFirstFree() const { | 43 ResourceId IdAllocator::FindFirstFree() const { |
16 ResourceId id = 1; | 44 ResourceId id = 1; |
17 for (ResourceIdSet::const_iterator it = used_ids_.begin(); | 45 for (ResourceIdSet::const_iterator it = used_ids_.begin(); |
18 it != used_ids_.end(); ++it) { | 46 it != used_ids_.end(); ++it) { |
19 if ((*it) != id) { | 47 if ((*it) != id) { |
20 return id; | 48 return id; |
21 } | 49 } |
22 ++id; | 50 ++id; |
23 } | 51 } |
24 return id; | 52 return id; |
25 } | 53 } |
26 | 54 |
27 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) { | |
28 GPU_DCHECK_LT(static_cast<ResourceId>(used_ids_.size()), | |
29 static_cast<ResourceId>(-1)); | |
30 for (; InUse(desired_id); ++desired_id) {} | |
31 MarkAsUsed(desired_id); | |
32 return desired_id; | |
33 } | |
34 | |
35 } // namespace gpu | 55 } // namespace gpu |
OLD | NEW |