| 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 IdAllocator class. | 5 // This file contains the definition of the IdAllocator class. |
| 6 | 6 |
| 7 #ifndef GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ | 7 #ifndef GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ |
| 8 #define GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ | 8 #define GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include "../common/types.h" | 12 #include "../common/types.h" |
| 13 | 13 |
| 14 namespace gpu { | 14 namespace gpu { |
| 15 | 15 |
| 16 // A resource ID, key to the resource maps. | 16 // A resource ID, key to the resource maps. |
| 17 typedef uint32 ResourceId; | 17 typedef uint32 ResourceId; |
| 18 // Invalid resource ID. | 18 // Invalid resource ID. |
| 19 static const ResourceId kInvalidResource = 0u; | 19 static const ResourceId kInvalidResource = 0u; |
| 20 | 20 |
| 21 // A class to manage the allocation of resource IDs. | 21 // A class to manage the allocation of resource IDs. |
| 22 class IdAllocator { | 22 class IdAllocator { |
| 23 public: | 23 public: |
| 24 IdAllocator(); | 24 IdAllocator(); |
| 25 ~IdAllocator(); |
| 25 | 26 |
| 26 // Allocates a new resource ID. | 27 // Allocates a new resource ID. |
| 27 ResourceId AllocateID() { | 28 ResourceId AllocateID(); |
| 28 ResourceId id = FindFirstFree(); | |
| 29 MarkAsUsed(id); | |
| 30 return id; | |
| 31 } | |
| 32 | 29 |
| 33 // Allocates an Id starting at or above desired_id. | 30 // Allocates an Id starting at or above desired_id. |
| 34 // Note: may wrap if it starts near limit. | 31 // Note: may wrap if it starts near limit. |
| 35 ResourceId AllocateIDAtOrAbove(ResourceId desired_id); | 32 ResourceId AllocateIDAtOrAbove(ResourceId desired_id); |
| 36 | 33 |
| 37 // Marks an id as used. Returns false if id was already used. | 34 // Marks an id as used. Returns false if id was already used. |
| 38 bool MarkAsUsed(ResourceId id) { | 35 bool MarkAsUsed(ResourceId id); |
| 39 std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id); | |
| 40 return result.second; | |
| 41 } | |
| 42 | 36 |
| 43 // Frees a resource ID. | 37 // Frees a resource ID. |
| 44 void FreeID(ResourceId id) { | 38 void FreeID(ResourceId id); |
| 45 used_ids_.erase(id); | |
| 46 } | |
| 47 | 39 |
| 48 // Checks whether or not a resource ID is in use. | 40 // Checks whether or not a resource ID is in use. |
| 49 bool InUse(ResourceId id) const { | 41 bool InUse(ResourceId id) const; |
| 50 return id == kInvalidResource || used_ids_.find(id) != used_ids_.end(); | |
| 51 } | |
| 52 | 42 |
| 53 private: | 43 private: |
| 54 // TODO(gman): This would work much better with ranges or a hash table. | 44 // TODO(gman): This would work much better with ranges or a hash table. |
| 55 typedef std::set<ResourceId> ResourceIdSet; | 45 typedef std::set<ResourceId> ResourceIdSet; |
| 56 | 46 |
| 57 ResourceId FindFirstFree() const; | 47 ResourceId FindFirstFree() const; |
| 58 | 48 |
| 59 ResourceIdSet used_ids_; | 49 ResourceIdSet used_ids_; |
| 60 | 50 |
| 61 DISALLOW_COPY_AND_ASSIGN(IdAllocator); | 51 DISALLOW_COPY_AND_ASSIGN(IdAllocator); |
| 62 }; | 52 }; |
| 63 | 53 |
| 64 } // namespace gpu | 54 } // namespace gpu |
| 65 | 55 |
| 66 #endif // GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ | 56 #endif // GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ |
| OLD | NEW |