Fix crash on app list start page keyboard navigation with <4 apps.
[chromium-blink-merge.git] / gpu / command_buffer / common / id_allocator.h
blobb877083008ad724f515799283d66173215d03344
1 // Copyright (c) 2012 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 definition of the IdAllocator class.
7 #ifndef GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_
8 #define GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_
10 #include <stdint.h>
12 #include <set>
13 #include <utility>
15 #include "base/compiler_specific.h"
16 #include "base/macros.h"
17 #include "gpu/gpu_export.h"
19 namespace gpu {
21 // A resource ID, key to the resource maps.
22 typedef uint32_t ResourceId;
23 // Invalid resource ID.
24 static const ResourceId kInvalidResource = 0u;
26 // A class to manage the allocation of resource IDs.
27 class GPU_EXPORT IdAllocator {
28 public:
29 IdAllocator();
30 ~IdAllocator();
32 // Allocates a new resource ID.
33 ResourceId AllocateID();
35 // Allocates an Id starting at or above desired_id.
36 // Note: may wrap if it starts near limit.
37 ResourceId AllocateIDAtOrAbove(ResourceId desired_id);
39 // Marks an id as used. Returns false if id was already used.
40 bool MarkAsUsed(ResourceId id);
42 // Frees a resource ID.
43 void FreeID(ResourceId id);
45 // Checks whether or not a resource ID is in use.
46 bool InUse(ResourceId id) const;
48 private:
49 // TODO(gman): This would work much better with ranges or a hash table.
50 typedef std::set<ResourceId> ResourceIdSet;
52 // The highest ID on the used list.
53 ResourceId LastUsedId() const;
55 // Lowest ID that isn't on the used list. This is slow, use as a last resort.
56 ResourceId FindFirstUnusedId() const;
58 ResourceIdSet used_ids_;
59 ResourceIdSet free_ids_;
61 DISALLOW_COPY_AND_ASSIGN(IdAllocator);
64 } // namespace gpu
66 #endif // GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_