blob: 68f79720b1de2000718b9c7d90fc21c0dca624a5 (
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
|
// Copyright (c) 2009 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 {
IdAllocator::IdAllocator() {}
IdAllocator::~IdAllocator() {}
ResourceId IdAllocator::AllocateID() {
ResourceId id = FindFirstFree();
MarkAsUsed(id);
return id;
}
ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
GPU_DCHECK_LT(static_cast<ResourceId>(used_ids_.size()),
static_cast<ResourceId>(-1));
for (; InUse(desired_id); ++desired_id) {}
MarkAsUsed(desired_id);
return desired_id;
}
bool IdAllocator::MarkAsUsed(ResourceId id) {
std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id);
return result.second;
}
void IdAllocator::FreeID(ResourceId id) {
used_ids_.erase(id);
}
bool IdAllocator::InUse(ResourceId id) const {
return id == kInvalidResource || used_ids_.find(id) != used_ids_.end();
}
ResourceId IdAllocator::FindFirstFree() 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;
}
} // namespace gpu
|