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