| 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 Random* PortMap::prng_ = NULL; | 23 Dart_Port PortMap::next_port_ = 7111; |
| 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 = prng_->NextUInt32() & kSmiMax; | 66 Dart_Port result = next_port_; |
| 67 | 67 |
| 68 while ((result == 0) && (FindPort(result) >= 0)) { | 68 do { |
| 69 result = prng_->NextUInt32() & kSmiMax; | 69 // TODO(iposva): Use an approved hashing function to have less predictable |
| 70 } | 70 // port ids, or make them not accessible from Dart code or both. |
| 71 next_port_++; |
| 72 } while (FindPort(next_port_) >= 0); |
| 71 | 73 |
| 72 ASSERT(result != 0); | 74 ASSERT(result != 0); |
| 73 return result; | 75 return result; |
| 74 } | 76 } |
| 75 | 77 |
| 76 | 78 |
| 77 void PortMap::SetLive(Dart_Port port) { | 79 void PortMap::SetLive(Dart_Port port) { |
| 78 MutexLocker ml(mutex_); | 80 MutexLocker ml(mutex_); |
| 79 intptr_t index = FindPort(port); | 81 intptr_t index = FindPort(port); |
| 80 ASSERT(index >= 0); | 82 ASSERT(index >= 0); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 return NULL; | 251 return NULL; |
| 250 } | 252 } |
| 251 | 253 |
| 252 MessageHandler* handler = map_[index].handler; | 254 MessageHandler* handler = map_[index].handler; |
| 253 return handler->GetIsolate(); | 255 return handler->GetIsolate(); |
| 254 } | 256 } |
| 255 | 257 |
| 256 | 258 |
| 257 void PortMap::InitOnce() { | 259 void PortMap::InitOnce() { |
| 258 mutex_ = new Mutex(); | 260 mutex_ = new Mutex(); |
| 259 prng_ = new Random(); | |
| 260 | 261 |
| 261 static const intptr_t kInitialCapacity = 8; | 262 static const intptr_t kInitialCapacity = 8; |
| 262 // TODO(iposva): Verify whether we want to keep exponentially growing. | 263 // TODO(iposva): Verify whether we want to keep exponentially growing. |
| 263 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); | 264 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); |
| 264 map_ = new Entry[kInitialCapacity]; | 265 map_ = new Entry[kInitialCapacity]; |
| 265 memset(map_, 0, kInitialCapacity * sizeof(Entry)); | 266 memset(map_, 0, kInitialCapacity * sizeof(Entry)); |
| 266 capacity_ = kInitialCapacity; | 267 capacity_ = kInitialCapacity; |
| 267 used_ = 0; | 268 used_ = 0; |
| 268 deleted_ = 0; | 269 deleted_ = 0; |
| 269 } | 270 } |
| 270 | 271 |
| 271 } // namespace dart | 272 } // namespace dart |
| OLD | NEW |