| 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 #ifndef BIN_PROCESS_H_ | 5 #ifndef BIN_PROCESS_H_ |
| 6 #define BIN_PROCESS_H_ | 6 #define BIN_PROCESS_H_ |
| 7 | 7 |
| 8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/io_buffer.h" |
| 9 #include "bin/thread.h" | 10 #include "bin/thread.h" |
| 10 #include "platform/globals.h" | 11 #include "platform/globals.h" |
| 12 #include "platform/utils.h" |
| 11 | 13 |
| 12 | 14 |
| 13 namespace dart { | 15 namespace dart { |
| 14 namespace bin { | 16 namespace bin { |
| 15 | 17 |
| 16 class ProcessResult { | 18 class ProcessResult { |
| 17 public: | 19 public: |
| 18 ProcessResult() | 20 ProcessResult() : exit_code_(0) {} |
| 19 : stdout_data_(NULL), stdout_length_(0), | |
| 20 stderr_data_(NULL), stderr_length_(0), exit_code_(0) {} | |
| 21 | 21 |
| 22 void SetStdoutData(uint8_t* buffer, intptr_t length) { | 22 void set_stdout_data(Dart_Handle stdout_data) { |
| 23 stdout_data_ = buffer; | 23 stdout_data_ = stdout_data; |
| 24 stdout_length_ = length; | |
| 25 } | 24 } |
| 26 | 25 void set_stderr_data(Dart_Handle stderr_data) { |
| 27 void SetStderrData(uint8_t* buffer, intptr_t length) { | 26 stderr_data_ = stderr_data; |
| 28 stderr_data_ = buffer; | |
| 29 stderr_length_ = length; | |
| 30 } | 27 } |
| 31 | 28 |
| 32 void set_exit_code(intptr_t exit_code) { exit_code_ = exit_code; } | 29 void set_exit_code(intptr_t exit_code) { exit_code_ = exit_code; } |
| 33 | 30 |
| 34 uint8_t* stdout_data() { return stdout_data_; } | 31 Dart_Handle stdout_data() { return stdout_data_; } |
| 35 intptr_t stdout_length() { return stdout_length_; } | 32 Dart_Handle stderr_data() { return stderr_data_; } |
| 36 uint8_t* stderr_data() { return stderr_data_; } | |
| 37 intptr_t stderr_length() { return stderr_length_; } | |
| 38 intptr_t exit_code() { return exit_code_; } | 33 intptr_t exit_code() { return exit_code_; } |
| 39 | 34 |
| 40 private: | 35 private: |
| 41 uint8_t* stdout_data_; | 36 Dart_Handle stdout_data_; |
| 42 intptr_t stdout_length_; | 37 Dart_Handle stderr_data_; |
| 43 uint8_t* stderr_data_; | |
| 44 intptr_t stderr_length_; | |
| 45 intptr_t exit_code_; | 38 intptr_t exit_code_; |
| 46 | 39 |
| 47 DISALLOW_ALLOCATION(); | 40 DISALLOW_ALLOCATION(); |
| 48 }; | 41 }; |
| 49 | 42 |
| 50 | 43 |
| 51 class Process { | 44 class Process { |
| 52 public: | 45 public: |
| 53 // Start a new process providing access to stdin, stdout, stderr and | 46 // Start a new process providing access to stdin, stdout, stderr and |
| 54 // process exit streams. | 47 // process exit streams. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 intptr_t pid); | 90 intptr_t pid); |
| 98 | 91 |
| 99 private: | 92 private: |
| 100 static int global_exit_code_; | 93 static int global_exit_code_; |
| 101 static dart::Mutex* global_exit_code_mutex_; | 94 static dart::Mutex* global_exit_code_mutex_; |
| 102 | 95 |
| 103 DISALLOW_ALLOCATION(); | 96 DISALLOW_ALLOCATION(); |
| 104 DISALLOW_IMPLICIT_CONSTRUCTORS(Process); | 97 DISALLOW_IMPLICIT_CONSTRUCTORS(Process); |
| 105 }; | 98 }; |
| 106 | 99 |
| 100 |
| 101 // Utility class for collecting the output when running a process |
| 102 // synchronously by using Process::Wait. This class is sub-classed in |
| 103 // the platform specific files to implement reading into the buffers |
| 104 // allocated. |
| 105 class BufferListBase { |
| 106 protected: |
| 107 static const intptr_t kBufferSize = 16 * 1024; |
| 108 |
| 109 class BufferListNode { |
| 110 public: |
| 111 explicit BufferListNode(intptr_t size) { |
| 112 data_ = new uint8_t[size]; |
| 113 if (data_ == NULL) FATAL("Allocation failed"); |
| 114 next_ = NULL; |
| 115 } |
| 116 |
| 117 ~BufferListNode() { |
| 118 delete[] data_; |
| 119 } |
| 120 |
| 121 uint8_t* data_; |
| 122 BufferListNode* next_; |
| 123 |
| 124 private: |
| 125 DISALLOW_IMPLICIT_CONSTRUCTORS(BufferListNode); |
| 126 }; |
| 127 |
| 128 public: |
| 129 BufferListBase() : head_(NULL), tail_(NULL), data_size_(0), free_size_(0) {} |
| 130 ~BufferListBase() { |
| 131 ASSERT(head_ == NULL); |
| 132 ASSERT(tail_ == NULL); |
| 133 } |
| 134 |
| 135 // Returns the collected data as a Uint8List. If an error occours an |
| 136 // error handle is returned. |
| 137 Dart_Handle GetData() { |
| 138 uint8_t* buffer; |
| 139 intptr_t buffer_position = 0; |
| 140 Dart_Handle result = IOBuffer::Allocate(data_size_, &buffer); |
| 141 if (Dart_IsError(result)) { |
| 142 Free(); |
| 143 return result; |
| 144 } |
| 145 for (BufferListNode* current = head_; |
| 146 current != NULL; |
| 147 current = current->next_) { |
| 148 intptr_t to_copy = dart::Utils::Minimum(data_size_, kBufferSize); |
| 149 memmove(buffer + buffer_position, current->data_, to_copy); |
| 150 buffer_position += to_copy; |
| 151 data_size_ -= to_copy; |
| 152 } |
| 153 ASSERT(data_size_ == 0); |
| 154 Free(); |
| 155 return result; |
| 156 } |
| 157 |
| 158 protected: |
| 159 void Allocate() { |
| 160 ASSERT(free_size_ == 0); |
| 161 BufferListNode* node = new BufferListNode(kBufferSize); |
| 162 if (head_ == NULL) { |
| 163 head_ = node; |
| 164 tail_ = node; |
| 165 } else { |
| 166 ASSERT(tail_->next_ == NULL); |
| 167 tail_->next_ = node; |
| 168 tail_ = node; |
| 169 } |
| 170 free_size_ = kBufferSize; |
| 171 } |
| 172 |
| 173 void Free() { |
| 174 for (BufferListNode* current = head_; |
| 175 current != NULL; |
| 176 current = current->next_) { |
| 177 } |
| 178 head_ = NULL; |
| 179 tail_ = NULL; |
| 180 data_size_ = 0; |
| 181 free_size_ = 0; |
| 182 } |
| 183 |
| 184 // Linked list for data collected. |
| 185 BufferListNode* head_; |
| 186 BufferListNode* tail_; |
| 187 |
| 188 // Number of bytes of data collected in the linked list. |
| 189 intptr_t data_size_; |
| 190 |
| 191 // Number of free bytes in the last node in the list. |
| 192 intptr_t free_size_; |
| 193 }; |
| 194 |
| 107 } // namespace bin | 195 } // namespace bin |
| 108 } // namespace dart | 196 } // namespace dart |
| 109 | 197 |
| 110 #endif // BIN_PROCESS_H_ | 198 #endif // BIN_PROCESS_H_ |
| OLD | NEW |