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

Unified Diff: runtime/bin/process.h

Issue 22827002: Change the allocation of the stdout and stderr collected by Process.runSync (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fixes Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/io_buffer.cc ('k') | runtime/bin/process.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/process.h
diff --git a/runtime/bin/process.h b/runtime/bin/process.h
index 230b1b7cb2fc4a28a250a4f5b4243486f31c7f79..ccc80c89574e1d00dd054e6bc4a367cc7a199491 100644
--- a/runtime/bin/process.h
+++ b/runtime/bin/process.h
@@ -6,8 +6,10 @@
#define BIN_PROCESS_H_
#include "bin/builtin.h"
+#include "bin/io_buffer.h"
#include "bin/thread.h"
#include "platform/globals.h"
+#include "platform/utils.h"
namespace dart {
@@ -15,33 +17,24 @@ namespace bin {
class ProcessResult {
public:
- ProcessResult()
- : stdout_data_(NULL), stdout_length_(0),
- stderr_data_(NULL), stderr_length_(0), exit_code_(0) {}
+ ProcessResult() : exit_code_(0) {}
- void SetStdoutData(uint8_t* buffer, intptr_t length) {
- stdout_data_ = buffer;
- stdout_length_ = length;
+ void set_stdout_data(Dart_Handle stdout_data) {
+ stdout_data_ = stdout_data;
}
-
- void SetStderrData(uint8_t* buffer, intptr_t length) {
- stderr_data_ = buffer;
- stderr_length_ = length;
+ void set_stderr_data(Dart_Handle stderr_data) {
+ stderr_data_ = stderr_data;
}
void set_exit_code(intptr_t exit_code) { exit_code_ = exit_code; }
- uint8_t* stdout_data() { return stdout_data_; }
- intptr_t stdout_length() { return stdout_length_; }
- uint8_t* stderr_data() { return stderr_data_; }
- intptr_t stderr_length() { return stderr_length_; }
+ Dart_Handle stdout_data() { return stdout_data_; }
+ Dart_Handle stderr_data() { return stderr_data_; }
intptr_t exit_code() { return exit_code_; }
private:
- uint8_t* stdout_data_;
- intptr_t stdout_length_;
- uint8_t* stderr_data_;
- intptr_t stderr_length_;
+ Dart_Handle stdout_data_;
+ Dart_Handle stderr_data_;
intptr_t exit_code_;
DISALLOW_ALLOCATION();
@@ -104,6 +97,101 @@ class Process {
DISALLOW_IMPLICIT_CONSTRUCTORS(Process);
};
+
+// Utility class for collecting the output when running a process
+// synchronously by using Process::Wait. This class is sub-classed in
+// the platform specific files to implement reading into the buffers
+// allocated.
+class BufferListBase {
+ protected:
+ static const intptr_t kBufferSize = 16 * 1024;
+
+ class BufferListNode {
+ public:
+ explicit BufferListNode(intptr_t size) {
+ data_ = new uint8_t[size];
+ if (data_ == NULL) FATAL("Allocation failed");
+ next_ = NULL;
+ }
+
+ ~BufferListNode() {
+ delete[] data_;
+ }
+
+ uint8_t* data_;
+ BufferListNode* next_;
+
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(BufferListNode);
+ };
+
+ public:
+ BufferListBase() : head_(NULL), tail_(NULL), data_size_(0), free_size_(0) {}
+ ~BufferListBase() {
+ ASSERT(head_ == NULL);
+ ASSERT(tail_ == NULL);
+ }
+
+ // Returns the collected data as a Uint8List. If an error occours an
+ // error handle is returned.
+ Dart_Handle GetData() {
+ uint8_t* buffer;
+ intptr_t buffer_position = 0;
+ Dart_Handle result = IOBuffer::Allocate(data_size_, &buffer);
+ if (Dart_IsError(result)) {
+ Free();
+ return result;
+ }
+ for (BufferListNode* current = head_;
+ current != NULL;
+ current = current->next_) {
+ intptr_t to_copy = dart::Utils::Minimum(data_size_, kBufferSize);
+ memmove(buffer + buffer_position, current->data_, to_copy);
+ buffer_position += to_copy;
+ data_size_ -= to_copy;
+ }
+ ASSERT(data_size_ == 0);
+ Free();
+ return result;
+ }
+
+ protected:
+ void Allocate() {
+ ASSERT(free_size_ == 0);
+ BufferListNode* node = new BufferListNode(kBufferSize);
+ if (head_ == NULL) {
+ head_ = node;
+ tail_ = node;
+ } else {
+ ASSERT(tail_->next_ == NULL);
+ tail_->next_ = node;
+ tail_ = node;
+ }
+ free_size_ = kBufferSize;
+ }
+
+ void Free() {
+ for (BufferListNode* current = head_;
+ current != NULL;
+ current = current->next_) {
+ }
+ head_ = NULL;
+ tail_ = NULL;
+ data_size_ = 0;
+ free_size_ = 0;
+ }
+
+ // Linked list for data collected.
+ BufferListNode* head_;
+ BufferListNode* tail_;
+
+ // Number of bytes of data collected in the linked list.
+ intptr_t data_size_;
+
+ // Number of free bytes in the last node in the list.
+ intptr_t free_size_;
+};
+
} // namespace bin
} // namespace dart
« no previous file with comments | « runtime/bin/io_buffer.cc ('k') | runtime/bin/process.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698