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