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"
8 #include "gpu/command_buffer/common/logging.h"
12 IdAllocatorInterface::~IdAllocatorInterface() {
15 IdAllocator::IdAllocator() {}
17 IdAllocator::~IdAllocator() {}
19 ResourceId IdAllocator::AllocateID() {
21 ResourceIdSet::iterator iter = free_ids_.begin();
22 if (iter != free_ids_.end()) {
25 id = LastUsedId() + 1;
27 // We wrapped around to 0.
28 id = FindFirstUnusedId();
35 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
37 ResourceIdSet::iterator iter = free_ids_.lower_bound(desired_id);
38 if (iter != free_ids_.end()) {
40 } else if (LastUsedId() < desired_id) {
43 id = LastUsedId() + 1;
45 // We wrapped around to 0.
46 id = FindFirstUnusedId();
53 bool IdAllocator::MarkAsUsed(ResourceId id) {
56 std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id);
60 void IdAllocator::FreeID(ResourceId id) {
67 bool IdAllocator::InUse(ResourceId id) const {
68 return id == kInvalidResource || used_ids_.find(id) != used_ids_.end();
71 ResourceId IdAllocator::LastUsedId() const {
72 if (used_ids_.empty()) {
75 return *used_ids_.rbegin();
79 ResourceId IdAllocator::FindFirstUnusedId() const {
81 for (ResourceIdSet::const_iterator it = used_ids_.begin();
82 it != used_ids_.end(); ++it) {
91 NonReusedIdAllocator::NonReusedIdAllocator() : last_id_(0) {
94 NonReusedIdAllocator::~NonReusedIdAllocator() {
97 ResourceId NonReusedIdAllocator::AllocateID() {
101 ResourceId NonReusedIdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
102 if (desired_id > last_id_)
103 last_id_ = desired_id;
108 bool NonReusedIdAllocator::MarkAsUsed(ResourceId id) {
113 void NonReusedIdAllocator::FreeID(ResourceId id) {
116 bool NonReusedIdAllocator::InUse(ResourceId id) const {