Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: mojo/edk/system/handle_table.cc

Issue 1412283002: Convert mojo::system::Dispatcher to use our new refcounting stuff (instead of base's). (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: no change Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/handle_table.h ('k') | mojo/edk/system/ipc_support.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "mojo/edk/system/handle_table.h" 5 #include "mojo/edk/system/handle_table.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <utility>
8 9
9 #include "base/logging.h" 10 #include "base/logging.h"
10 #include "mojo/edk/system/configuration.h" 11 #include "mojo/edk/system/configuration.h"
11 #include "mojo/edk/system/dispatcher.h" 12 #include "mojo/edk/system/dispatcher.h"
12 13
13 namespace mojo { 14 namespace mojo {
14 namespace system { 15 namespace system {
15 16
16 HandleTable::Entry::Entry() : busy(false) { 17 HandleTable::Entry::Entry() : busy(false) {
17 } 18 }
18 19
19 HandleTable::Entry::Entry(const scoped_refptr<Dispatcher>& dispatcher) 20 HandleTable::Entry::Entry(RefPtr<Dispatcher>&& dispatcher)
20 : dispatcher(dispatcher), busy(false) { 21 : dispatcher(std::move(dispatcher)), busy(false) {}
21 }
22 22
23 HandleTable::Entry::~Entry() { 23 HandleTable::Entry::~Entry() {
24 DCHECK(!busy); 24 DCHECK(!busy);
25 } 25 }
26 26
27 HandleTable::HandleTable() : next_handle_(MOJO_HANDLE_INVALID + 1) { 27 HandleTable::HandleTable() : next_handle_(MOJO_HANDLE_INVALID + 1) {
28 } 28 }
29 29
30 HandleTable::~HandleTable() { 30 HandleTable::~HandleTable() {
31 // This should usually not be reached (the only instance should be owned by 31 // This should usually not be reached (the only instance should be owned by
32 // the singleton |Core|, which lives forever), except in tests. 32 // the singleton |Core|, which lives forever), except in tests.
33 } 33 }
34 34
35 Dispatcher* HandleTable::GetDispatcher(MojoHandle handle) { 35 Dispatcher* HandleTable::GetDispatcher(MojoHandle handle) {
36 DCHECK_NE(handle, MOJO_HANDLE_INVALID); 36 DCHECK_NE(handle, MOJO_HANDLE_INVALID);
37 37
38 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle); 38 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle);
39 if (it == handle_to_entry_map_.end()) 39 if (it == handle_to_entry_map_.end())
40 return nullptr; 40 return nullptr;
41 return it->second.dispatcher.get(); 41 return it->second.dispatcher.get();
42 } 42 }
43 43
44 MojoResult HandleTable::GetAndRemoveDispatcher( 44 MojoResult HandleTable::GetAndRemoveDispatcher(MojoHandle handle,
45 MojoHandle handle, 45 RefPtr<Dispatcher>* dispatcher) {
46 scoped_refptr<Dispatcher>* dispatcher) {
47 DCHECK_NE(handle, MOJO_HANDLE_INVALID); 46 DCHECK_NE(handle, MOJO_HANDLE_INVALID);
48 DCHECK(dispatcher); 47 DCHECK(dispatcher);
49 48
50 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle); 49 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle);
51 if (it == handle_to_entry_map_.end()) 50 if (it == handle_to_entry_map_.end())
52 return MOJO_RESULT_INVALID_ARGUMENT; 51 return MOJO_RESULT_INVALID_ARGUMENT;
53 if (it->second.busy) 52 if (it->second.busy)
54 return MOJO_RESULT_BUSY; 53 return MOJO_RESULT_BUSY;
55 *dispatcher = it->second.dispatcher; 54 *dispatcher = std::move(it->second.dispatcher);
56 handle_to_entry_map_.erase(it); 55 handle_to_entry_map_.erase(it);
57 56
58 return MOJO_RESULT_OK; 57 return MOJO_RESULT_OK;
59 } 58 }
60 59
61 MojoHandle HandleTable::AddDispatcher( 60 MojoHandle HandleTable::AddDispatcher(Dispatcher* dispatcher) {
62 const scoped_refptr<Dispatcher>& dispatcher) {
63 if (handle_to_entry_map_.size() >= GetConfiguration().max_handle_table_size) 61 if (handle_to_entry_map_.size() >= GetConfiguration().max_handle_table_size)
64 return MOJO_HANDLE_INVALID; 62 return MOJO_HANDLE_INVALID;
65 return AddDispatcherNoSizeCheck(dispatcher); 63 return AddDispatcherNoSizeCheck(RefPtr<Dispatcher>(dispatcher));
66 } 64 }
67 65
68 std::pair<MojoHandle, MojoHandle> HandleTable::AddDispatcherPair( 66 std::pair<MojoHandle, MojoHandle> HandleTable::AddDispatcherPair(
69 const scoped_refptr<Dispatcher>& dispatcher0, 67 Dispatcher* dispatcher0,
70 const scoped_refptr<Dispatcher>& dispatcher1) { 68 Dispatcher* dispatcher1) {
71 if (handle_to_entry_map_.size() + 1 >= 69 if (handle_to_entry_map_.size() + 1 >=
72 GetConfiguration().max_handle_table_size) 70 GetConfiguration().max_handle_table_size)
73 return std::make_pair(MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID); 71 return std::make_pair(MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID);
74 return std::make_pair(AddDispatcherNoSizeCheck(dispatcher0), 72 return std::make_pair(
75 AddDispatcherNoSizeCheck(dispatcher1)); 73 AddDispatcherNoSizeCheck(RefPtr<Dispatcher>(dispatcher0)),
74 AddDispatcherNoSizeCheck(RefPtr<Dispatcher>(dispatcher1)));
76 } 75 }
77 76
78 bool HandleTable::AddDispatcherVector(const DispatcherVector& dispatchers, 77 bool HandleTable::AddDispatcherVector(const DispatcherVector& dispatchers,
79 MojoHandle* handles) { 78 MojoHandle* handles) {
80 size_t max_message_num_handles = GetConfiguration().max_message_num_handles; 79 size_t max_message_num_handles = GetConfiguration().max_message_num_handles;
81 size_t max_handle_table_size = GetConfiguration().max_handle_table_size; 80 size_t max_handle_table_size = GetConfiguration().max_handle_table_size;
82 81
83 DCHECK_LE(dispatchers.size(), max_message_num_handles); 82 DCHECK_LE(dispatchers.size(), max_message_num_handles);
84 DCHECK(handles); 83 DCHECK(handles);
85 DCHECK_LT( 84 DCHECK_LT(
86 static_cast<uint64_t>(max_handle_table_size) + max_message_num_handles, 85 static_cast<uint64_t>(max_handle_table_size) + max_message_num_handles,
87 std::numeric_limits<size_t>::max()) 86 std::numeric_limits<size_t>::max())
88 << "Addition may overflow"; 87 << "Addition may overflow";
89 88
90 if (handle_to_entry_map_.size() + dispatchers.size() > max_handle_table_size) 89 if (handle_to_entry_map_.size() + dispatchers.size() > max_handle_table_size)
91 return false; 90 return false;
92 91
93 for (size_t i = 0; i < dispatchers.size(); i++) { 92 for (size_t i = 0; i < dispatchers.size(); i++) {
94 if (dispatchers[i]) { 93 if (dispatchers[i]) {
95 handles[i] = AddDispatcherNoSizeCheck(dispatchers[i]); 94 handles[i] = AddDispatcherNoSizeCheck(dispatchers[i].Clone());
96 } else { 95 } else {
97 LOG(WARNING) << "Invalid dispatcher at index " << i; 96 LOG(WARNING) << "Invalid dispatcher at index " << i;
98 handles[i] = MOJO_HANDLE_INVALID; 97 handles[i] = MOJO_HANDLE_INVALID;
99 } 98 }
100 } 99 }
101 return true; 100 return true;
102 } 101 }
103 102
104 MojoResult HandleTable::MarkBusyAndStartTransport( 103 MojoResult HandleTable::MarkBusyAndStartTransport(
105 MojoHandle disallowed_handle, 104 MojoHandle disallowed_handle,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 entries[j]->busy = false; 179 entries[j]->busy = false;
181 (*transports)[j].End(); 180 (*transports)[j].End();
182 } 181 }
183 return error_result; 182 return error_result;
184 } 183 }
185 184
186 return MOJO_RESULT_OK; 185 return MOJO_RESULT_OK;
187 } 186 }
188 187
189 MojoHandle HandleTable::AddDispatcherNoSizeCheck( 188 MojoHandle HandleTable::AddDispatcherNoSizeCheck(
190 const scoped_refptr<Dispatcher>& dispatcher) { 189 RefPtr<Dispatcher>&& dispatcher) {
191 DCHECK(dispatcher); 190 DCHECK(dispatcher);
192 DCHECK_LT(handle_to_entry_map_.size(), 191 DCHECK_LT(handle_to_entry_map_.size(),
193 GetConfiguration().max_handle_table_size); 192 GetConfiguration().max_handle_table_size);
194 DCHECK_NE(next_handle_, MOJO_HANDLE_INVALID); 193 DCHECK_NE(next_handle_, MOJO_HANDLE_INVALID);
195 194
196 // TODO(vtl): Maybe we want to do something different/smarter. (Or maybe try 195 // TODO(vtl): Maybe we want to do something different/smarter. (Or maybe try
197 // assigning randomly?) 196 // assigning randomly?)
198 while (handle_to_entry_map_.find(next_handle_) != 197 while (handle_to_entry_map_.find(next_handle_) !=
199 handle_to_entry_map_.end()) { 198 handle_to_entry_map_.end()) {
200 next_handle_++; 199 next_handle_++;
201 if (next_handle_ == MOJO_HANDLE_INVALID) 200 if (next_handle_ == MOJO_HANDLE_INVALID)
202 next_handle_++; 201 next_handle_++;
203 } 202 }
204 203
205 MojoHandle new_handle = next_handle_; 204 MojoHandle new_handle = next_handle_;
206 handle_to_entry_map_[new_handle] = Entry(dispatcher); 205 handle_to_entry_map_[new_handle] = Entry(std::move(dispatcher));
207 206
208 next_handle_++; 207 next_handle_++;
209 if (next_handle_ == MOJO_HANDLE_INVALID) 208 if (next_handle_ == MOJO_HANDLE_INVALID)
210 next_handle_++; 209 next_handle_++;
211 210
212 return new_handle; 211 return new_handle;
213 } 212 }
214 213
215 void HandleTable::RemoveBusyHandles(const MojoHandle* handles, 214 void HandleTable::RemoveBusyHandles(const MojoHandle* handles,
216 uint32_t num_handles) { 215 uint32_t num_handles) {
(...skipping 17 matching lines...) Expand all
234 for (uint32_t i = 0; i < num_handles; i++) { 233 for (uint32_t i = 0; i < num_handles; i++) {
235 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]); 234 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]);
236 DCHECK(it != handle_to_entry_map_.end()); 235 DCHECK(it != handle_to_entry_map_.end());
237 DCHECK(it->second.busy); 236 DCHECK(it->second.busy);
238 it->second.busy = false; 237 it->second.busy = false;
239 } 238 }
240 } 239 }
241 240
242 } // namespace system 241 } // namespace system
243 } // namespace mojo 242 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/handle_table.h ('k') | mojo/edk/system/ipc_support.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698