Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / gpu / command_buffer / common / id_allocator.h
blob118424f3c9d6025ba8109bb94440297806aa7116
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 <map>
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 // Allocates |range| amount of contiguous ids.
40 // Returns the first id to |first_id| or |kInvalidResource| if
41 // allocation failed.
42 ResourceId AllocateIDRange(uint32_t range);
44 // Marks an id as used. Returns false if id was already used.
45 bool MarkAsUsed(ResourceId id);
47 // Frees a resource ID.
48 void FreeID(ResourceId id);
50 // Frees a |range| amount of contiguous ids, starting from |first_id|.
51 void FreeIDRange(ResourceId first_id, uint32_t range);
53 // Checks whether or not a resource ID is in use.
54 bool InUse(ResourceId id) const;
56 private:
57 // first_id -> last_id mapping.
58 typedef std::map<ResourceId, ResourceId> ResourceIdRangeMap;
60 ResourceIdRangeMap used_ids_;
62 DISALLOW_COPY_AND_ASSIGN(IdAllocator);
65 } // namespace gpu
67 #endif // GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_