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
3 // found in the LICENSE file.
5 // This file contains the implementation of IdAllocator.
7 #include "gpu/command_buffer/common/id_allocator.h"
9 #include "base/logging.h"
13 IdAllocator::IdAllocator() {}
15 IdAllocator::~IdAllocator() {}
17 ResourceId IdAllocator::AllocateID() {
19 ResourceIdSet::iterator iter = free_ids_.begin();
20 if (iter != free_ids_.end()) {
23 id = LastUsedId() + 1;
25 // We wrapped around to 0.
26 id = FindFirstUnusedId();
33 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
35 ResourceIdSet::iterator iter = free_ids_.lower_bound(desired_id);
36 if (iter != free_ids_.end()) {
38 } else if (LastUsedId() < desired_id) {
41 id = LastUsedId() + 1;
43 // We wrapped around to 0.
44 id = FindFirstUnusedId();
51 bool IdAllocator::MarkAsUsed(ResourceId id) {
54 std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id);
58 void IdAllocator::FreeID(ResourceId id) {
65 bool IdAllocator::InUse(ResourceId id) const {
66 return id == kInvalidResource || used_ids_.find(id) != used_ids_.end();
69 ResourceId IdAllocator::LastUsedId() const {
70 if (used_ids_.empty()) {
73 return *used_ids_.rbegin();
77 ResourceId IdAllocator::FindFirstUnusedId() const {
79 for (ResourceIdSet::const_iterator it = used_ids_.begin();
80 it != used_ids_.end(); ++it) {