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