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 IdAllocatorInterface::~IdAllocatorInterface() {
16 IdAllocator::IdAllocator() {}
18 IdAllocator::~IdAllocator() {}
20 ResourceId IdAllocator::AllocateID() {
22 ResourceIdSet::iterator iter = free_ids_.begin();
23 if (iter != free_ids_.end()) {
26 id = LastUsedId() + 1;
28 // We wrapped around to 0.
29 id = FindFirstUnusedId();
36 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
38 ResourceIdSet::iterator iter = free_ids_.lower_bound(desired_id);
39 if (iter != free_ids_.end()) {
41 } else if (LastUsedId() < desired_id) {
44 id = LastUsedId() + 1;
46 // We wrapped around to 0.
47 id = FindFirstUnusedId();
54 bool IdAllocator::MarkAsUsed(ResourceId id) {
57 std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id);
61 void IdAllocator::FreeID(ResourceId id) {
68 bool IdAllocator::InUse(ResourceId id) const {
69 return id == kInvalidResource || used_ids_.find(id) != used_ids_.end();
72 ResourceId IdAllocator::LastUsedId() const {
73 if (used_ids_.empty()) {
76 return *used_ids_.rbegin();
80 ResourceId IdAllocator::FindFirstUnusedId() const {
82 for (ResourceIdSet::const_iterator it = used_ids_.begin();
83 it != used_ids_.end(); ++it) {
92 NonReusedIdAllocator::NonReusedIdAllocator() : last_id_(0) {
95 NonReusedIdAllocator::~NonReusedIdAllocator() {
98 ResourceId NonReusedIdAllocator::AllocateID() {
102 ResourceId NonReusedIdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
103 if (desired_id > last_id_)
104 last_id_ = desired_id;
109 bool NonReusedIdAllocator::MarkAsUsed(ResourceId id) {
114 void NonReusedIdAllocator::FreeID(ResourceId id) {
117 bool NonReusedIdAllocator::InUse(ResourceId id) const {