blob: e3fbdd57dd448bb7e4e122086e212e37748a413e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file contains the implementation of IdAllocator.
#include "../common/id_allocator.h"
#include "../common/logging.h"
namespace gpu {
IdAllocatorInterface::~IdAllocatorInterface() {
}
IdAllocator::IdAllocator() {}
IdAllocator::~IdAllocator() {}
ResourceId IdAllocator::AllocateID() {
ResourceId id;
ResourceIdSet::iterator iter = free_ids_.begin();
if (iter != free_ids_.end()) {
id = *iter;
} else {
id = LastUsedId() + 1;
if (!id) {
// We wrapped around to 0.
id = FindFirstUnusedId();
}
}
MarkAsUsed(id);
return id;
}
ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
ResourceId id;
ResourceIdSet::iterator iter = free_ids_.lower_bound(desired_id);
if (iter != free_ids_.end()) {
id = *iter;
} else if (LastUsedId() < desired_id) {
id = desired_id;
} else {
id = LastUsedId() + 1;
if (!id) {
// We wrapped around to 0.
id = FindFirstUnusedId();
}
}
MarkAsUsed(id);
return id;
}
bool IdAllocator::MarkAsUsed(ResourceId id) {
GPU_DCHECK(id);
free_ids_.erase(id);
std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id);
return result.second;
}
void IdAllocator::FreeID(ResourceId id) {
if (id) {
used_ids_.erase(id);
free_ids_.insert(id);
}
}
bool IdAllocator::InUse(ResourceId id) const {
return id == kInvalidResource || used_ids_.find(id) != used_ids_.end();
}
ResourceId IdAllocator::LastUsedId() const {
if (used_ids_.empty()) {
return 0u;
} else {
return *used_ids_.rbegin();
}
}
ResourceId IdAllocator::FindFirstUnusedId() const {
ResourceId id = 1;
for (ResourceIdSet::const_iterator it = used_ids_.begin();
it != used_ids_.end(); ++it) {
if ((*it) != id) {
return id;
}
++id;
}
return id;
}
NonReusedIdAllocator::NonReusedIdAllocator() : last_id_(0) {
}
NonReusedIdAllocator::~NonReusedIdAllocator() {
}
ResourceId NonReusedIdAllocator::AllocateID() {
return ++last_id_;
}
ResourceId NonReusedIdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
if (desired_id > last_id_)
last_id_ = desired_id;
return ++last_id_;
}
bool NonReusedIdAllocator::MarkAsUsed(ResourceId id) {
GPU_NOTREACHED();
return false;
}
void NonReusedIdAllocator::FreeID(ResourceId id) {
}
bool NonReusedIdAllocator::InUse(ResourceId id) const {
GPU_NOTREACHED();
return false;
}
} // namespace gpu
|