OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "ppapi/shared_impl/var_tracker.h" | 5 #include "ppapi/shared_impl/var_tracker.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 ref_count(0), | 22 ref_count(0), |
23 track_with_no_reference_count(0) { | 23 track_with_no_reference_count(0) { |
24 } | 24 } |
25 | 25 |
26 VarTracker::VarInfo::VarInfo(Var* v, int input_ref_count) | 26 VarTracker::VarInfo::VarInfo(Var* v, int input_ref_count) |
27 : var(v), | 27 : var(v), |
28 ref_count(input_ref_count), | 28 ref_count(input_ref_count), |
29 track_with_no_reference_count(0) { | 29 track_with_no_reference_count(0) { |
30 } | 30 } |
31 | 31 |
32 VarTracker::VarTracker() : last_var_id_(0) { | 32 VarTracker::VarTracker(ThreadMode thread_mode) : last_var_id_(0) { |
| 33 if (thread_mode == SINGLE_THREADED) |
| 34 thread_checker_.reset(new base::ThreadChecker); |
33 } | 35 } |
34 | 36 |
35 VarTracker::~VarTracker() { | 37 VarTracker::~VarTracker() { |
36 } | 38 } |
37 | 39 |
| 40 void VarTracker::CheckThreadingPreconditions() const { |
| 41 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); |
| 42 #ifndef NDEBUG |
| 43 ProxyLock::AssertAcquired(); |
| 44 #endif |
| 45 } |
| 46 |
38 int32 VarTracker::AddVar(Var* var) { | 47 int32 VarTracker::AddVar(Var* var) { |
39 DCHECK(CalledOnValidThread()); | 48 CheckThreadingPreconditions(); |
40 ProxyLock::AssertAcquired(); | |
41 | 49 |
42 return AddVarInternal(var, ADD_VAR_TAKE_ONE_REFERENCE); | 50 return AddVarInternal(var, ADD_VAR_TAKE_ONE_REFERENCE); |
43 } | 51 } |
44 | 52 |
45 Var* VarTracker::GetVar(int32 var_id) const { | 53 Var* VarTracker::GetVar(int32 var_id) const { |
46 DCHECK(CalledOnValidThread()); | 54 CheckThreadingPreconditions(); |
47 ProxyLock::AssertAcquired(); | |
48 | 55 |
49 VarMap::const_iterator result = live_vars_.find(var_id); | 56 VarMap::const_iterator result = live_vars_.find(var_id); |
50 if (result == live_vars_.end()) | 57 if (result == live_vars_.end()) |
51 return NULL; | 58 return NULL; |
52 return result->second.var.get(); | 59 return result->second.var.get(); |
53 } | 60 } |
54 | 61 |
55 Var* VarTracker::GetVar(const PP_Var& var) const { | 62 Var* VarTracker::GetVar(const PP_Var& var) const { |
56 DCHECK(CalledOnValidThread()); | 63 CheckThreadingPreconditions(); |
57 ProxyLock::AssertAcquired(); | |
58 | 64 |
59 if (!IsVarTypeRefcounted(var.type)) | 65 if (!IsVarTypeRefcounted(var.type)) |
60 return NULL; | 66 return NULL; |
61 return GetVar(static_cast<int32>(var.value.as_id)); | 67 return GetVar(static_cast<int32>(var.value.as_id)); |
62 } | 68 } |
63 | 69 |
64 bool VarTracker::AddRefVar(int32 var_id) { | 70 bool VarTracker::AddRefVar(int32 var_id) { |
65 DCHECK(CalledOnValidThread()); | 71 CheckThreadingPreconditions(); |
66 ProxyLock::AssertAcquired(); | |
67 | 72 |
68 DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) | 73 DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) |
69 << var_id << " is not a PP_Var ID."; | 74 << var_id << " is not a PP_Var ID."; |
70 VarMap::iterator found = live_vars_.find(var_id); | 75 VarMap::iterator found = live_vars_.find(var_id); |
71 if (found == live_vars_.end()) { | 76 if (found == live_vars_.end()) { |
72 NOTREACHED(); // Invalid var. | 77 NOTREACHED(); // Invalid var. |
73 return false; | 78 return false; |
74 } | 79 } |
75 | 80 |
76 VarInfo& info = found->second; | 81 VarInfo& info = found->second; |
77 if (info.ref_count == 0) { | 82 if (info.ref_count == 0) { |
78 // All live vars with no refcount should be tracked objects. | 83 // All live vars with no refcount should be tracked objects. |
79 DCHECK(info.track_with_no_reference_count > 0); | 84 DCHECK(info.track_with_no_reference_count > 0); |
80 DCHECK(info.var->GetType() == PP_VARTYPE_OBJECT); | 85 DCHECK(info.var->GetType() == PP_VARTYPE_OBJECT); |
81 | 86 |
82 TrackedObjectGettingOneRef(found); | 87 TrackedObjectGettingOneRef(found); |
83 } | 88 } |
84 | 89 |
85 // Basic refcount increment. | 90 // Basic refcount increment. |
86 info.ref_count++; | 91 info.ref_count++; |
87 return true; | 92 return true; |
88 } | 93 } |
89 | 94 |
90 bool VarTracker::AddRefVar(const PP_Var& var) { | 95 bool VarTracker::AddRefVar(const PP_Var& var) { |
91 DCHECK(CalledOnValidThread()); | 96 CheckThreadingPreconditions(); |
92 ProxyLock::AssertAcquired(); | |
93 | 97 |
94 if (!IsVarTypeRefcounted(var.type)) | 98 if (!IsVarTypeRefcounted(var.type)) |
95 return true; | 99 return true; |
96 return AddRefVar(static_cast<int32>(var.value.as_id)); | 100 return AddRefVar(static_cast<int32>(var.value.as_id)); |
97 } | 101 } |
98 | 102 |
99 bool VarTracker::ReleaseVar(int32 var_id) { | 103 bool VarTracker::ReleaseVar(int32 var_id) { |
100 DCHECK(CalledOnValidThread()); | 104 CheckThreadingPreconditions(); |
101 ProxyLock::AssertAcquired(); | |
102 | 105 |
103 DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) | 106 DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) |
104 << var_id << " is not a PP_Var ID."; | 107 << var_id << " is not a PP_Var ID."; |
105 VarMap::iterator found = live_vars_.find(var_id); | 108 VarMap::iterator found = live_vars_.find(var_id); |
106 if (found == live_vars_.end()) | 109 if (found == live_vars_.end()) |
107 return false; | 110 return false; |
108 | 111 |
109 VarInfo& info = found->second; | 112 VarInfo& info = found->second; |
110 if (info.ref_count == 0) { | 113 if (info.ref_count == 0) { |
111 NOTREACHED() << "Releasing an object with zero ref"; | 114 NOTREACHED() << "Releasing an object with zero ref"; |
(...skipping 10 matching lines...) Expand all Loading... |
122 // All other var types can just be released. | 125 // All other var types can just be released. |
123 DCHECK(info.track_with_no_reference_count == 0); | 126 DCHECK(info.track_with_no_reference_count == 0); |
124 info.var->ResetVarID(); | 127 info.var->ResetVarID(); |
125 live_vars_.erase(found); | 128 live_vars_.erase(found); |
126 } | 129 } |
127 } | 130 } |
128 return true; | 131 return true; |
129 } | 132 } |
130 | 133 |
131 bool VarTracker::ReleaseVar(const PP_Var& var) { | 134 bool VarTracker::ReleaseVar(const PP_Var& var) { |
132 DCHECK(CalledOnValidThread()); | 135 CheckThreadingPreconditions(); |
133 ProxyLock::AssertAcquired(); | |
134 | 136 |
135 if (!IsVarTypeRefcounted(var.type)) | 137 if (!IsVarTypeRefcounted(var.type)) |
136 return false; | 138 return false; |
137 return ReleaseVar(static_cast<int32>(var.value.as_id)); | 139 return ReleaseVar(static_cast<int32>(var.value.as_id)); |
138 } | 140 } |
139 | 141 |
140 int32 VarTracker::AddVarInternal(Var* var, AddVarRefMode mode) { | 142 int32 VarTracker::AddVarInternal(Var* var, AddVarRefMode mode) { |
141 // If the plugin manages to create millions of strings. | 143 // If the plugin manages to create millions of strings. |
142 if (last_var_id_ == std::numeric_limits<int32>::max() >> kPPIdTypeBits) | 144 if (last_var_id_ == std::numeric_limits<int32>::max() >> kPPIdTypeBits) |
143 return 0; | 145 return 0; |
144 | 146 |
145 int32 new_id = MakeTypedId(++last_var_id_, PP_ID_TYPE_VAR); | 147 int32 new_id = MakeTypedId(++last_var_id_, PP_ID_TYPE_VAR); |
146 live_vars_.insert(std::make_pair(new_id, | 148 live_vars_.insert(std::make_pair(new_id, |
147 VarInfo(var, mode == ADD_VAR_TAKE_ONE_REFERENCE ? 1 : 0))); | 149 VarInfo(var, mode == ADD_VAR_TAKE_ONE_REFERENCE ? 1 : 0))); |
148 | 150 |
149 return new_id; | 151 return new_id; |
150 } | 152 } |
151 | 153 |
152 VarTracker::VarMap::iterator VarTracker::GetLiveVar(int32 id) { | 154 VarTracker::VarMap::iterator VarTracker::GetLiveVar(int32 id) { |
153 return live_vars_.find(id); | 155 return live_vars_.find(id); |
154 } | 156 } |
155 | 157 |
156 int VarTracker::GetRefCountForObject(const PP_Var& plugin_object) { | 158 int VarTracker::GetRefCountForObject(const PP_Var& plugin_object) { |
157 DCHECK(CalledOnValidThread()); | 159 CheckThreadingPreconditions(); |
158 ProxyLock::AssertAcquired(); | |
159 | 160 |
160 VarMap::iterator found = GetLiveVar(plugin_object); | 161 VarMap::iterator found = GetLiveVar(plugin_object); |
161 if (found == live_vars_.end()) | 162 if (found == live_vars_.end()) |
162 return -1; | 163 return -1; |
163 return found->second.ref_count; | 164 return found->second.ref_count; |
164 } | 165 } |
165 | 166 |
166 int VarTracker::GetTrackedWithNoReferenceCountForObject( | 167 int VarTracker::GetTrackedWithNoReferenceCountForObject( |
167 const PP_Var& plugin_object) { | 168 const PP_Var& plugin_object) { |
168 DCHECK(CalledOnValidThread()); | 169 CheckThreadingPreconditions(); |
169 ProxyLock::AssertAcquired(); | |
170 | 170 |
171 VarMap::iterator found = GetLiveVar(plugin_object); | 171 VarMap::iterator found = GetLiveVar(plugin_object); |
172 if (found == live_vars_.end()) | 172 if (found == live_vars_.end()) |
173 return -1; | 173 return -1; |
174 return found->second.track_with_no_reference_count; | 174 return found->second.track_with_no_reference_count; |
175 } | 175 } |
176 | 176 |
177 VarTracker::VarMap::iterator VarTracker::GetLiveVar(const PP_Var& var) { | 177 VarTracker::VarMap::iterator VarTracker::GetLiveVar(const PP_Var& var) { |
178 return live_vars_.find(static_cast<int32>(var.value.as_id)); | 178 return live_vars_.find(static_cast<int32>(var.value.as_id)); |
179 } | 179 } |
180 | 180 |
181 VarTracker::VarMap::const_iterator VarTracker::GetLiveVar( | 181 VarTracker::VarMap::const_iterator VarTracker::GetLiveVar( |
182 const PP_Var& var) const { | 182 const PP_Var& var) const { |
183 return live_vars_.find(static_cast<int32>(var.value.as_id)); | 183 return live_vars_.find(static_cast<int32>(var.value.as_id)); |
184 } | 184 } |
185 | 185 |
186 bool VarTracker::IsVarTypeRefcounted(PP_VarType type) const { | 186 bool VarTracker::IsVarTypeRefcounted(PP_VarType type) const { |
187 return type >= PP_VARTYPE_STRING; | 187 return type >= PP_VARTYPE_STRING; |
188 } | 188 } |
189 | 189 |
190 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes) { | 190 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes) { |
191 DCHECK(CalledOnValidThread()); | 191 CheckThreadingPreconditions(); |
192 ProxyLock::AssertAcquired(); | |
193 | 192 |
194 scoped_refptr<ArrayBufferVar> array_buffer(CreateArrayBuffer(size_in_bytes)); | 193 scoped_refptr<ArrayBufferVar> array_buffer(CreateArrayBuffer(size_in_bytes)); |
195 if (!array_buffer) | 194 if (!array_buffer) |
196 return PP_MakeNull(); | 195 return PP_MakeNull(); |
197 return array_buffer->GetPPVar(); | 196 return array_buffer->GetPPVar(); |
198 } | 197 } |
199 | 198 |
200 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes, | 199 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes, |
201 const void* data) { | 200 const void* data) { |
202 DCHECK(CalledOnValidThread()); | 201 CheckThreadingPreconditions(); |
203 ProxyLock::AssertAcquired(); | |
204 | 202 |
205 ArrayBufferVar* array_buffer = MakeArrayBufferVar(size_in_bytes, data); | 203 ArrayBufferVar* array_buffer = MakeArrayBufferVar(size_in_bytes, data); |
206 return array_buffer ? array_buffer->GetPPVar() : PP_MakeNull(); | 204 return array_buffer ? array_buffer->GetPPVar() : PP_MakeNull(); |
207 } | 205 } |
208 | 206 |
209 ArrayBufferVar* VarTracker::MakeArrayBufferVar(uint32 size_in_bytes, | 207 ArrayBufferVar* VarTracker::MakeArrayBufferVar(uint32 size_in_bytes, |
210 const void* data) { | 208 const void* data) { |
211 DCHECK(CalledOnValidThread()); | 209 CheckThreadingPreconditions(); |
212 ProxyLock::AssertAcquired(); | |
213 | 210 |
214 ArrayBufferVar* array_buffer(CreateArrayBuffer(size_in_bytes)); | 211 ArrayBufferVar* array_buffer(CreateArrayBuffer(size_in_bytes)); |
215 if (!array_buffer) | 212 if (!array_buffer) |
216 return NULL; | 213 return NULL; |
217 memcpy(array_buffer->Map(), data, size_in_bytes); | 214 memcpy(array_buffer->Map(), data, size_in_bytes); |
218 return array_buffer; | 215 return array_buffer; |
219 } | 216 } |
220 | 217 |
221 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes, | 218 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes, |
222 base::SharedMemoryHandle handle) { | 219 base::SharedMemoryHandle handle) { |
223 DCHECK(CalledOnValidThread()); | 220 CheckThreadingPreconditions(); |
224 | 221 |
225 scoped_refptr<ArrayBufferVar> array_buffer( | 222 scoped_refptr<ArrayBufferVar> array_buffer( |
226 CreateShmArrayBuffer(size_in_bytes, handle)); | 223 CreateShmArrayBuffer(size_in_bytes, handle)); |
227 if (!array_buffer) | 224 if (!array_buffer) |
228 return PP_MakeNull(); | 225 return PP_MakeNull(); |
229 return array_buffer->GetPPVar(); | 226 return array_buffer->GetPPVar(); |
230 } | 227 } |
231 | 228 |
232 std::vector<PP_Var> VarTracker::GetLiveVars() { | 229 std::vector<PP_Var> VarTracker::GetLiveVars() { |
233 DCHECK(CalledOnValidThread()); | 230 CheckThreadingPreconditions(); |
234 ProxyLock::AssertAcquired(); | |
235 | 231 |
236 std::vector<PP_Var> var_vector; | 232 std::vector<PP_Var> var_vector; |
237 var_vector.reserve(live_vars_.size()); | 233 var_vector.reserve(live_vars_.size()); |
238 for (VarMap::const_iterator iter = live_vars_.begin(); | 234 for (VarMap::const_iterator iter = live_vars_.begin(); |
239 iter != live_vars_.end(); | 235 iter != live_vars_.end(); |
240 ++iter) { | 236 ++iter) { |
241 var_vector.push_back(iter->second.var->GetPPVar()); | 237 var_vector.push_back(iter->second.var->GetPPVar()); |
242 } | 238 } |
243 return var_vector; | 239 return var_vector; |
244 } | 240 } |
(...skipping 10 matching lines...) Expand all Loading... |
255 bool VarTracker::DeleteObjectInfoIfNecessary(VarMap::iterator iter) { | 251 bool VarTracker::DeleteObjectInfoIfNecessary(VarMap::iterator iter) { |
256 if (iter->second.ref_count != 0 || | 252 if (iter->second.ref_count != 0 || |
257 iter->second.track_with_no_reference_count != 0) | 253 iter->second.track_with_no_reference_count != 0) |
258 return false; // Object still alive. | 254 return false; // Object still alive. |
259 iter->second.var->ResetVarID(); | 255 iter->second.var->ResetVarID(); |
260 live_vars_.erase(iter); | 256 live_vars_.erase(iter); |
261 return true; | 257 return true; |
262 } | 258 } |
263 | 259 |
264 } // namespace ppapi | 260 } // namespace ppapi |
OLD | NEW |