| 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 "base/memory/shared_memory.h" | 12 #include "base/memory/shared_memory.h" |
| 13 #include "ppapi/shared_impl/host_resource.h" | 13 #include "ppapi/shared_impl/host_resource.h" |
| 14 #include "ppapi/shared_impl/id_assignment.h" | 14 #include "ppapi/shared_impl/id_assignment.h" |
| 15 #include "ppapi/shared_impl/proxy_lock.h" | 15 #include "ppapi/shared_impl/proxy_lock.h" |
| 16 #include "ppapi/shared_impl/resource_var.h" | 16 #include "ppapi/shared_impl/resource_var.h" |
| 17 #include "ppapi/shared_impl/var.h" | 17 #include "ppapi/shared_impl/var.h" |
| 18 | 18 |
| 19 namespace ppapi { | 19 namespace ppapi { |
| 20 | 20 |
| 21 VarTracker::VarInfo::VarInfo() | 21 VarTracker::VarInfo::VarInfo() |
| 22 : var(), | 22 : var(), ref_count(0), track_with_no_reference_count(0) {} |
| 23 ref_count(0), | |
| 24 track_with_no_reference_count(0) { | |
| 25 } | |
| 26 | 23 |
| 27 VarTracker::VarInfo::VarInfo(Var* v, int input_ref_count) | 24 VarTracker::VarInfo::VarInfo(Var* v, int input_ref_count) |
| 28 : var(v), | 25 : var(v), ref_count(input_ref_count), track_with_no_reference_count(0) {} |
| 29 ref_count(input_ref_count), | |
| 30 track_with_no_reference_count(0) { | |
| 31 } | |
| 32 | 26 |
| 33 VarTracker::VarTracker(ThreadMode thread_mode) : last_var_id_(0) { | 27 VarTracker::VarTracker(ThreadMode thread_mode) : last_var_id_(0) { |
| 34 if (thread_mode == SINGLE_THREADED) | 28 if (thread_mode == SINGLE_THREADED) |
| 35 thread_checker_.reset(new base::ThreadChecker); | 29 thread_checker_.reset(new base::ThreadChecker); |
| 36 } | 30 } |
| 37 | 31 |
| 38 VarTracker::~VarTracker() { | 32 VarTracker::~VarTracker() {} |
| 39 } | |
| 40 | 33 |
| 41 void VarTracker::CheckThreadingPreconditions() const { | 34 void VarTracker::CheckThreadingPreconditions() const { |
| 42 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); | 35 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); |
| 43 #ifndef NDEBUG | 36 #ifndef NDEBUG |
| 44 ProxyLock::AssertAcquired(); | 37 ProxyLock::AssertAcquired(); |
| 45 #endif | 38 #endif |
| 46 } | 39 } |
| 47 | 40 |
| 48 int32 VarTracker::AddVar(Var* var) { | 41 int32 VarTracker::AddVar(Var* var) { |
| 49 CheckThreadingPreconditions(); | 42 CheckThreadingPreconditions(); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 return ReleaseVar(static_cast<int32>(var.value.as_id)); | 137 return ReleaseVar(static_cast<int32>(var.value.as_id)); |
| 145 } | 138 } |
| 146 | 139 |
| 147 int32 VarTracker::AddVarInternal(Var* var, AddVarRefMode mode) { | 140 int32 VarTracker::AddVarInternal(Var* var, AddVarRefMode mode) { |
| 148 // If the plugin manages to create millions of strings. | 141 // If the plugin manages to create millions of strings. |
| 149 if (last_var_id_ == std::numeric_limits<int32>::max() >> kPPIdTypeBits) | 142 if (last_var_id_ == std::numeric_limits<int32>::max() >> kPPIdTypeBits) |
| 150 return 0; | 143 return 0; |
| 151 | 144 |
| 152 int32 new_id = MakeTypedId(++last_var_id_, PP_ID_TYPE_VAR); | 145 int32 new_id = MakeTypedId(++last_var_id_, PP_ID_TYPE_VAR); |
| 153 std::pair<VarMap::iterator, bool> was_inserted = | 146 std::pair<VarMap::iterator, bool> was_inserted = |
| 154 live_vars_.insert(std::make_pair(new_id, | 147 live_vars_.insert(std::make_pair( |
| 155 VarInfo(var, mode == ADD_VAR_TAKE_ONE_REFERENCE ? 1 : 0))); | 148 new_id, VarInfo(var, mode == ADD_VAR_TAKE_ONE_REFERENCE ? 1 : 0))); |
| 156 // We should never insert an ID that already exists. | 149 // We should never insert an ID that already exists. |
| 157 DCHECK(was_inserted.second); | 150 DCHECK(was_inserted.second); |
| 158 | 151 |
| 159 return new_id; | 152 return new_id; |
| 160 } | 153 } |
| 161 | 154 |
| 162 VarTracker::VarMap::iterator VarTracker::GetLiveVar(int32 id) { | 155 VarTracker::VarMap::iterator VarTracker::GetLiveVar(int32 id) { |
| 163 return live_vars_.find(id); | 156 return live_vars_.find(id); |
| 164 } | 157 } |
| 165 | 158 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 184 | 177 |
| 185 // static | 178 // static |
| 186 bool VarTracker::IsVarTypeRefcounted(PP_VarType type) { | 179 bool VarTracker::IsVarTypeRefcounted(PP_VarType type) { |
| 187 return type >= PP_VARTYPE_STRING; | 180 return type >= PP_VARTYPE_STRING; |
| 188 } | 181 } |
| 189 | 182 |
| 190 VarTracker::VarMap::iterator VarTracker::GetLiveVar(const PP_Var& var) { | 183 VarTracker::VarMap::iterator VarTracker::GetLiveVar(const PP_Var& var) { |
| 191 return live_vars_.find(static_cast<int32>(var.value.as_id)); | 184 return live_vars_.find(static_cast<int32>(var.value.as_id)); |
| 192 } | 185 } |
| 193 | 186 |
| 194 VarTracker::VarMap::const_iterator VarTracker::GetLiveVar( | 187 VarTracker::VarMap::const_iterator VarTracker::GetLiveVar(const PP_Var& var) |
| 195 const PP_Var& var) const { | 188 const { |
| 196 return live_vars_.find(static_cast<int32>(var.value.as_id)); | 189 return live_vars_.find(static_cast<int32>(var.value.as_id)); |
| 197 } | 190 } |
| 198 | 191 |
| 199 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes) { | 192 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes) { |
| 200 CheckThreadingPreconditions(); | 193 CheckThreadingPreconditions(); |
| 201 | 194 |
| 202 scoped_refptr<ArrayBufferVar> array_buffer(CreateArrayBuffer(size_in_bytes)); | 195 scoped_refptr<ArrayBufferVar> array_buffer(CreateArrayBuffer(size_in_bytes)); |
| 203 if (!array_buffer.get()) | 196 if (!array_buffer.get()) |
| 204 return PP_MakeNull(); | 197 return PP_MakeNull(); |
| 205 return array_buffer->GetPPVar(); | 198 return array_buffer->GetPPVar(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 bool VarTracker::DeleteObjectInfoIfNecessary(VarMap::iterator iter) { | 260 bool VarTracker::DeleteObjectInfoIfNecessary(VarMap::iterator iter) { |
| 268 if (iter->second.ref_count != 0 || | 261 if (iter->second.ref_count != 0 || |
| 269 iter->second.track_with_no_reference_count != 0) | 262 iter->second.track_with_no_reference_count != 0) |
| 270 return false; // Object still alive. | 263 return false; // Object still alive. |
| 271 iter->second.var->ResetVarID(); | 264 iter->second.var->ResetVarID(); |
| 272 live_vars_.erase(iter); | 265 live_vars_.erase(iter); |
| 273 return true; | 266 return true; |
| 274 } | 267 } |
| 275 | 268 |
| 276 } // namespace ppapi | 269 } // namespace ppapi |
| OLD | NEW |