OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_ID_MAP_H_ | 5 #ifndef BASE_ID_MAP_H_ |
6 #define BASE_ID_MAP_H_ | 6 #define BASE_ID_MAP_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/hash_tables.h" | 12 #include "base/hash_tables.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/threading/non_thread_safe.h" |
14 | 15 |
15 // Ownership semantics - own pointer means the pointer is deleted in Remove() | 16 // Ownership semantics - own pointer means the pointer is deleted in Remove() |
16 // & during destruction | 17 // & during destruction |
17 enum IDMapOwnershipSemantics { | 18 enum IDMapOwnershipSemantics { |
18 IDMapExternalPointer, | 19 IDMapExternalPointer, |
19 IDMapOwnPointer | 20 IDMapOwnPointer |
20 }; | 21 }; |
21 | 22 |
22 // This object maintains a list of IDs that can be quickly converted to | 23 // This object maintains a list of IDs that can be quickly converted to |
23 // pointers to objects. It is implemented as a hash table, optimized for | 24 // pointers to objects. It is implemented as a hash table, optimized for |
24 // relatively small data sets (in the common case, there will be exactly one | 25 // relatively small data sets (in the common case, there will be exactly one |
25 // item in the list). | 26 // item in the list). |
26 // | 27 // |
27 // Items can be inserted into the container with arbitrary ID, but the caller | 28 // Items can be inserted into the container with arbitrary ID, but the caller |
28 // must ensure they are unique. Inserting IDs and relying on automatically | 29 // must ensure they are unique. Inserting IDs and relying on automatically |
29 // generated ones is not allowed because they can collide. | 30 // generated ones is not allowed because they can collide. |
30 // | 31 // |
31 // This class does not have a virtual destructor, do not inherit from it when | 32 // This class does not have a virtual destructor, do not inherit from it when |
32 // ownership semantics are set to own because pointers will leak. | 33 // ownership semantics are set to own because pointers will leak. |
33 template<typename T, IDMapOwnershipSemantics OS = IDMapExternalPointer> | 34 template<typename T, IDMapOwnershipSemantics OS = IDMapExternalPointer> |
34 class IDMap { | 35 class IDMap : public base::NonThreadSafe { |
35 private: | 36 private: |
36 typedef int32 KeyType; | 37 typedef int32 KeyType; |
37 typedef base::hash_map<KeyType, T*> HashTable; | 38 typedef base::hash_map<KeyType, T*> HashTable; |
38 | 39 |
39 public: | 40 public: |
40 IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { | 41 IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { |
| 42 // A number of consumers of IDMap create it on one thread but always access |
| 43 // it from a different, but consitent, thread post-construction. |
| 44 DetachFromThread(); |
41 } | 45 } |
42 | 46 |
43 ~IDMap() { | 47 ~IDMap() { |
| 48 // Many IDMap's are static, and hence will be destroyed on the main thread. |
| 49 // However, all the accesses may take place on another thread, such as the |
| 50 // IO thread. Detaching again to clean this up. |
| 51 DetachFromThread(); |
44 Releaser<OS, 0>::release_all(&data_); | 52 Releaser<OS, 0>::release_all(&data_); |
45 } | 53 } |
46 | 54 |
47 // Sets whether Add should CHECK if passed in NULL data. Default is false. | 55 // Sets whether Add should CHECK if passed in NULL data. Default is false. |
48 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } | 56 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
49 | 57 |
50 // Adds a view with an automatically generated unique ID. See AddWithID. | 58 // Adds a view with an automatically generated unique ID. See AddWithID. |
51 KeyType Add(T* data) { | 59 KeyType Add(T* data) { |
| 60 DCHECK(CalledOnValidThread()); |
52 CHECK(!check_on_null_data_ || data); | 61 CHECK(!check_on_null_data_ || data); |
53 KeyType this_id = next_id_; | 62 KeyType this_id = next_id_; |
54 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; | 63 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
55 data_[this_id] = data; | 64 data_[this_id] = data; |
56 next_id_++; | 65 next_id_++; |
57 return this_id; | 66 return this_id; |
58 } | 67 } |
59 | 68 |
60 // Adds a new data member with the specified ID. The ID must not be in | 69 // Adds a new data member with the specified ID. The ID must not be in |
61 // the list. The caller either must generate all unique IDs itself and use | 70 // the list. The caller either must generate all unique IDs itself and use |
62 // this function, or allow this object to generate IDs and call Add. These | 71 // this function, or allow this object to generate IDs and call Add. These |
63 // two methods may not be mixed, or duplicate IDs may be generated | 72 // two methods may not be mixed, or duplicate IDs may be generated |
64 void AddWithID(T* data, KeyType id) { | 73 void AddWithID(T* data, KeyType id) { |
| 74 DCHECK(CalledOnValidThread()); |
65 CHECK(!check_on_null_data_ || data); | 75 CHECK(!check_on_null_data_ || data); |
66 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; | 76 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
67 data_[id] = data; | 77 data_[id] = data; |
68 } | 78 } |
69 | 79 |
70 void Remove(KeyType id) { | 80 void Remove(KeyType id) { |
| 81 DCHECK(CalledOnValidThread()); |
71 typename HashTable::iterator i = data_.find(id); | 82 typename HashTable::iterator i = data_.find(id); |
72 if (i == data_.end()) { | 83 if (i == data_.end()) { |
73 NOTREACHED() << "Attempting to remove an item not in the list"; | 84 NOTREACHED() << "Attempting to remove an item not in the list"; |
74 return; | 85 return; |
75 } | 86 } |
76 | 87 |
77 if (iteration_depth_ == 0) { | 88 if (iteration_depth_ == 0) { |
78 Releaser<OS, 0>::release(i->second); | 89 Releaser<OS, 0>::release(i->second); |
79 data_.erase(i); | 90 data_.erase(i); |
80 } else { | 91 } else { |
81 removed_ids_.insert(id); | 92 removed_ids_.insert(id); |
82 } | 93 } |
83 } | 94 } |
84 | 95 |
85 bool IsEmpty() const { | 96 bool IsEmpty() const { |
| 97 DCHECK(CalledOnValidThread()); |
86 return size() == 0u; | 98 return size() == 0u; |
87 } | 99 } |
88 | 100 |
89 T* Lookup(KeyType id) const { | 101 T* Lookup(KeyType id) const { |
| 102 DCHECK(CalledOnValidThread()); |
90 typename HashTable::const_iterator i = data_.find(id); | 103 typename HashTable::const_iterator i = data_.find(id); |
91 if (i == data_.end()) | 104 if (i == data_.end()) |
92 return NULL; | 105 return NULL; |
93 return i->second; | 106 return i->second; |
94 } | 107 } |
95 | 108 |
96 size_t size() const { | 109 size_t size() const { |
| 110 DCHECK(CalledOnValidThread()); |
97 return data_.size() - removed_ids_.size(); | 111 return data_.size() - removed_ids_.size(); |
98 } | 112 } |
99 | 113 |
100 // It is safe to remove elements from the map during iteration. All iterators | 114 // It is safe to remove elements from the map during iteration. All iterators |
101 // will remain valid. | 115 // will remain valid. |
102 template<class ReturnType> | 116 template<class ReturnType> |
103 class Iterator { | 117 class Iterator { |
104 public: | 118 public: |
105 Iterator(IDMap<T, OS>* map) | 119 Iterator(IDMap<T, OS>* map) |
106 : map_(map), | 120 : map_(map), |
107 iter_(map_->data_.begin()) { | 121 iter_(map_->data_.begin()) { |
| 122 DCHECK(map->CalledOnValidThread()); |
108 ++map_->iteration_depth_; | 123 ++map_->iteration_depth_; |
109 SkipRemovedEntries(); | 124 SkipRemovedEntries(); |
110 } | 125 } |
111 | 126 |
112 ~Iterator() { | 127 ~Iterator() { |
| 128 DCHECK(map_->CalledOnValidThread()); |
113 if (--map_->iteration_depth_ == 0) | 129 if (--map_->iteration_depth_ == 0) |
114 map_->Compact(); | 130 map_->Compact(); |
115 } | 131 } |
116 | 132 |
117 bool IsAtEnd() const { | 133 bool IsAtEnd() const { |
| 134 DCHECK(map_->CalledOnValidThread()); |
118 return iter_ == map_->data_.end(); | 135 return iter_ == map_->data_.end(); |
119 } | 136 } |
120 | 137 |
121 KeyType GetCurrentKey() const { | 138 KeyType GetCurrentKey() const { |
| 139 DCHECK(map_->CalledOnValidThread()); |
122 return iter_->first; | 140 return iter_->first; |
123 } | 141 } |
124 | 142 |
125 ReturnType* GetCurrentValue() const { | 143 ReturnType* GetCurrentValue() const { |
| 144 DCHECK(map_->CalledOnValidThread()); |
126 return iter_->second; | 145 return iter_->second; |
127 } | 146 } |
128 | 147 |
129 void Advance() { | 148 void Advance() { |
| 149 DCHECK(map_->CalledOnValidThread()); |
130 ++iter_; | 150 ++iter_; |
131 SkipRemovedEntries(); | 151 SkipRemovedEntries(); |
132 } | 152 } |
133 | 153 |
134 private: | 154 private: |
135 void SkipRemovedEntries() { | 155 void SkipRemovedEntries() { |
136 while (iter_ != map_->data_.end() && | 156 while (iter_ != map_->data_.end() && |
137 map_->removed_ids_.find(iter_->first) != | 157 map_->removed_ids_.find(iter_->first) != |
138 map_->removed_ids_.end()) { | 158 map_->removed_ids_.end()) { |
139 ++iter_; | 159 ++iter_; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 | 210 |
191 HashTable data_; | 211 HashTable data_; |
192 | 212 |
193 // See description above setter. | 213 // See description above setter. |
194 bool check_on_null_data_; | 214 bool check_on_null_data_; |
195 | 215 |
196 DISALLOW_COPY_AND_ASSIGN(IDMap); | 216 DISALLOW_COPY_AND_ASSIGN(IDMap); |
197 }; | 217 }; |
198 | 218 |
199 #endif // BASE_ID_MAP_H_ | 219 #endif // BASE_ID_MAP_H_ |
OLD | NEW |