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