| 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 "bin/file.h" | 5 #include "bin/file.h" |
| 6 | 6 |
| 7 #include "bin/builtin.h" | 7 #include "bin/builtin.h" |
| 8 #include "bin/dartutils.h" | 8 #include "bin/dartutils.h" |
| 9 #include "bin/thread.h" |
| 9 | 10 |
| 10 #include "include/dart_api.h" | 11 #include "include/dart_api.h" |
| 11 | 12 |
| 13 dart::Mutex File::mutex_; |
| 14 int File::service_ports_size_ = 0; |
| 15 Dart_Port* File::service_ports_ = NULL; |
| 16 int File::service_ports_index_ = 0; |
| 12 | 17 |
| 13 bool File::ReadFully(void* buffer, int64_t num_bytes) { | 18 bool File::ReadFully(void* buffer, int64_t num_bytes) { |
| 14 int64_t remaining = num_bytes; | 19 int64_t remaining = num_bytes; |
| 15 char* current_buffer = reinterpret_cast<char*>(buffer); | 20 char* current_buffer = reinterpret_cast<char*>(buffer); |
| 16 while (remaining > 0) { | 21 while (remaining > 0) { |
| 17 int bytes_read = Read(current_buffer, remaining); | 22 int bytes_read = Read(current_buffer, remaining); |
| 18 if (bytes_read <= 0) { | 23 if (bytes_read <= 0) { |
| 19 return false; | 24 return false; |
| 20 } | 25 } |
| 21 remaining -= bytes_read; // Reduce the number of remaining bytes. | 26 remaining -= bytes_read; // Reduce the number of remaining bytes. |
| (...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 718 default: | 723 default: |
| 719 UNREACHABLE(); | 724 UNREACHABLE(); |
| 720 } | 725 } |
| 721 } | 726 } |
| 722 } | 727 } |
| 723 | 728 |
| 724 Dart_PostCObject(reply_port_id, response->AsApiCObject()); | 729 Dart_PostCObject(reply_port_id, response->AsApiCObject()); |
| 725 } | 730 } |
| 726 | 731 |
| 727 | 732 |
| 733 Dart_Port File::GetServicePort() { |
| 734 MutexLocker lock(&mutex_); |
| 735 if (service_ports_size_ == 0) { |
| 736 ASSERT(service_ports_ == NULL); |
| 737 service_ports_size_ = 16; |
| 738 service_ports_ = new Dart_Port[service_ports_size_]; |
| 739 service_ports_index_ = 0; |
| 740 for (int i = 0; i < service_ports_size_; i++) { |
| 741 service_ports_[i] = kIllegalPort; |
| 742 } |
| 743 } |
| 744 |
| 745 Dart_Port result = service_ports_[service_ports_index_]; |
| 746 if (result == kIllegalPort) { |
| 747 result = Dart_NewNativePort("FileService", |
| 748 FileService, |
| 749 true); |
| 750 ASSERT(result != kIllegalPort); |
| 751 service_ports_[service_ports_index_] = result; |
| 752 } |
| 753 service_ports_index_ = (service_ports_index_ + 1) % service_ports_size_; |
| 754 return result; |
| 755 } |
| 756 |
| 757 |
| 728 void FUNCTION_NAME(File_NewServicePort)(Dart_NativeArguments args) { | 758 void FUNCTION_NAME(File_NewServicePort)(Dart_NativeArguments args) { |
| 729 Dart_EnterScope(); | 759 Dart_EnterScope(); |
| 730 Dart_SetReturnValue(args, Dart_Null()); | 760 Dart_SetReturnValue(args, Dart_Null()); |
| 731 Dart_Port service_port = kIllegalPort; | 761 Dart_Port service_port = File::GetServicePort(); |
| 732 service_port = Dart_NewNativePort("FileService", | |
| 733 FileService, | |
| 734 true); | |
| 735 if (service_port != kIllegalPort) { | 762 if (service_port != kIllegalPort) { |
| 736 // Return a send port for the service port. | 763 // Return a send port for the service port. |
| 737 Dart_Handle send_port = Dart_NewSendPort(service_port); | 764 Dart_Handle send_port = Dart_NewSendPort(service_port); |
| 738 Dart_SetReturnValue(args, send_port); | 765 Dart_SetReturnValue(args, send_port); |
| 739 } | 766 } |
| 740 Dart_ExitScope(); | 767 Dart_ExitScope(); |
| 741 } | 768 } |
| OLD | NEW |