OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/port.h" | 5 #include "vm/port.h" |
6 | 6 |
7 #include "platform/utils.h" | 7 #include "platform/utils.h" |
8 #include "vm/dart_api_impl.h" | 8 #include "vm/dart_api_impl.h" |
9 #include "vm/isolate.h" | 9 #include "vm/isolate.h" |
10 #include "vm/message_handler.h" | 10 #include "vm/message_handler.h" |
11 #include "vm/thread.h" | 11 #include "vm/thread.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
15 DECLARE_FLAG(bool, trace_isolates); | 15 DECLARE_FLAG(bool, trace_isolates); |
16 | 16 |
17 Mutex* PortMap::mutex_ = NULL; | 17 Mutex* PortMap::mutex_ = NULL; |
18 PortMap::Entry* PortMap::map_ = NULL; | 18 PortMap::Entry* PortMap::map_ = NULL; |
19 MessageHandler* PortMap::deleted_entry_ = reinterpret_cast<MessageHandler*>(1); | 19 MessageHandler* PortMap::deleted_entry_ = reinterpret_cast<MessageHandler*>(1); |
20 intptr_t PortMap::capacity_ = 0; | 20 intptr_t PortMap::capacity_ = 0; |
21 intptr_t PortMap::used_ = 0; | 21 intptr_t PortMap::used_ = 0; |
22 intptr_t PortMap::deleted_ = 0; | 22 intptr_t PortMap::deleted_ = 0; |
23 Dart_Port PortMap::next_port_ = 7111; | 23 Random* PortMap::prng_ = NULL; |
24 | 24 |
25 | 25 |
26 intptr_t PortMap::FindPort(Dart_Port port) { | 26 intptr_t PortMap::FindPort(Dart_Port port) { |
27 intptr_t index = port % capacity_; | 27 intptr_t index = port % capacity_; |
28 intptr_t start_index = index; | 28 intptr_t start_index = index; |
29 Entry entry = map_[index]; | 29 Entry entry = map_[index]; |
30 while (entry.handler != NULL) { | 30 while (entry.handler != NULL) { |
31 if (entry.port == port) { | 31 if (entry.port == port) { |
32 return index; | 32 return index; |
33 } | 33 } |
(...skipping 22 matching lines...) Expand all Loading... | |
56 } | 56 } |
57 } | 57 } |
58 delete[] map_; | 58 delete[] map_; |
59 map_ = new_ports; | 59 map_ = new_ports; |
60 capacity_ = new_capacity; | 60 capacity_ = new_capacity; |
61 deleted_ = 0; | 61 deleted_ = 0; |
62 } | 62 } |
63 | 63 |
64 | 64 |
65 Dart_Port PortMap::AllocatePort() { | 65 Dart_Port PortMap::AllocatePort() { |
66 Dart_Port result = next_port_; | 66 Dart_Port result = prng_->NextUInt32() & kSmiMax; |
67 | 67 |
68 do { | 68 while ((result == 0) && (FindPort(result) >= 0)) { |
kasperl
2014/03/26 11:39:03
Shouldn't this be ||? Otherwise, you'll just get t
hausner
2014/03/28 23:13:20
What Kasper says. And you could turn this into a d
| |
69 // TODO(iposva): Use an approved hashing function to have less predictable | 69 result = prng_->NextUInt32() & kSmiMax; |
70 // port ids, or make them not accessible from Dart code or both. | 70 } |
71 next_port_++; | |
72 } while (FindPort(next_port_) >= 0); | |
73 | 71 |
74 ASSERT(result != 0); | 72 ASSERT(result != 0); |
75 return result; | 73 return result; |
76 } | 74 } |
77 | 75 |
78 | 76 |
79 void PortMap::SetLive(Dart_Port port) { | 77 void PortMap::SetLive(Dart_Port port) { |
80 MutexLocker ml(mutex_); | 78 MutexLocker ml(mutex_); |
81 intptr_t index = FindPort(port); | 79 intptr_t index = FindPort(port); |
82 ASSERT(index >= 0); | 80 ASSERT(index >= 0); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 return NULL; | 249 return NULL; |
252 } | 250 } |
253 | 251 |
254 MessageHandler* handler = map_[index].handler; | 252 MessageHandler* handler = map_[index].handler; |
255 return handler->GetIsolate(); | 253 return handler->GetIsolate(); |
256 } | 254 } |
257 | 255 |
258 | 256 |
259 void PortMap::InitOnce() { | 257 void PortMap::InitOnce() { |
260 mutex_ = new Mutex(); | 258 mutex_ = new Mutex(); |
259 prng_ = new Random(); | |
261 | 260 |
262 static const intptr_t kInitialCapacity = 8; | 261 static const intptr_t kInitialCapacity = 8; |
263 // TODO(iposva): Verify whether we want to keep exponentially growing. | 262 // TODO(iposva): Verify whether we want to keep exponentially growing. |
264 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); | 263 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); |
265 map_ = new Entry[kInitialCapacity]; | 264 map_ = new Entry[kInitialCapacity]; |
266 memset(map_, 0, kInitialCapacity * sizeof(Entry)); | 265 memset(map_, 0, kInitialCapacity * sizeof(Entry)); |
267 capacity_ = kInitialCapacity; | 266 capacity_ = kInitialCapacity; |
268 used_ = 0; | 267 used_ = 0; |
269 deleted_ = 0; | 268 deleted_ = 0; |
270 } | 269 } |
271 | 270 |
272 } // namespace dart | 271 } // namespace dart |
OLD | NEW |