Index: mojo/public/cpp/bindings/lib/message.cc |
diff --git a/mojo/public/cpp/bindings/lib/message.cc b/mojo/public/cpp/bindings/lib/message.cc |
index 6b563e779aca7d196284726c0a899fe03ce2cdde..23ef162b40f69b36b0bc3f5a859b8b39d9e5acda 100644 |
--- a/mojo/public/cpp/bindings/lib/message.cc |
+++ b/mojo/public/cpp/bindings/lib/message.cc |
@@ -13,54 +13,30 @@ |
namespace mojo { |
Message::Message() { |
- Initialize(); |
} |
Message::~Message() { |
- FreeDataAndCloseHandles(); |
+ CloseHandles(); |
} |
-void Message::Reset() { |
- FreeDataAndCloseHandles(); |
- |
- handles_.clear(); |
- Initialize(); |
-} |
- |
-void Message::AllocData(uint32_t num_bytes) { |
- MOJO_DCHECK(!data_); |
- data_num_bytes_ = num_bytes; |
- data_ = static_cast<internal::MessageData*>(calloc(num_bytes, 1)); |
-} |
- |
-void Message::AllocUninitializedData(uint32_t num_bytes) { |
- MOJO_DCHECK(!data_); |
- data_num_bytes_ = num_bytes; |
- data_ = static_cast<internal::MessageData*>(malloc(num_bytes)); |
+void Message::Initialize(size_t capacity, bool zero_initialized) { |
+ DCHECK(!buffer_); |
+ buffer_.reset(new internal::PickleBuffer(capacity, zero_initialized)); |
} |
void Message::MoveTo(Message* destination) { |
MOJO_DCHECK(this != destination); |
- destination->FreeDataAndCloseHandles(); |
- |
// No copy needed. |
- destination->data_num_bytes_ = data_num_bytes_; |
- destination->data_ = data_; |
+ std::swap(destination->buffer_, buffer_); |
std::swap(destination->handles_, handles_); |
+ CloseHandles(); |
handles_.clear(); |
- Initialize(); |
-} |
- |
-void Message::Initialize() { |
- data_num_bytes_ = 0; |
- data_ = nullptr; |
+ buffer_.reset(); |
} |
-void Message::FreeDataAndCloseHandles() { |
- free(data_); |
- |
+void Message::CloseHandles() { |
for (std::vector<Handle>::iterator it = handles_.begin(); |
it != handles_.end(); ++it) { |
if (it->is_valid()) |
@@ -84,16 +60,18 @@ MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, |
return rv; |
Message message; |
- message.AllocUninitializedData(num_bytes); |
+ message.Initialize(num_bytes, false /* zero_initialized */); |
+ |
+ void* mutable_data = message.buffer()->Allocate(num_bytes); |
message.mutable_handles()->resize(num_handles); |
rv = ReadMessageRaw( |
handle, |
- message.mutable_data(), |
+ mutable_data, |
&num_bytes, |
message.mutable_handles()->empty() |
? nullptr |
- : reinterpret_cast<MojoHandle*>(&message.mutable_handles()->front()), |
+ : reinterpret_cast<MojoHandle*>(message.mutable_handles()->data()), |
&num_handles, |
MOJO_READ_MESSAGE_FLAG_NONE); |
if (receiver && rv == MOJO_RESULT_OK) |