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

Side by Side Diff: mojo/public/cpp/bindings/lib/message.cc

Issue 1524613002: [mojo] Use base::Pickle for Message storage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: static_cast, initialization-is-allocation, and split out Pickle impl from PickleBuffer Created 5 years 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/cpp/bindings/message.h" 5 #include "mojo/public/cpp/bindings/message.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "mojo/public/cpp/environment/logging.h" 11 #include "mojo/public/cpp/environment/logging.h"
12 12
13 namespace mojo { 13 namespace mojo {
14 14
15 Message::Message() { 15 Message::Message() {
16 Initialize();
17 } 16 }
18 17
19 Message::~Message() { 18 Message::~Message() {
20 FreeDataAndCloseHandles(); 19 CloseHandles();
21 } 20 }
22 21
23 void Message::Reset() { 22 void Message::Reset() {
24 FreeDataAndCloseHandles(); 23 CloseHandles();
25 24
26 handles_.clear(); 25 handles_.clear();
27 Initialize(); 26 buffer_.reset();
yzshen1 2015/12/16 00:29:27 Is there anyone calling Reset()? If not, maybe it
28 } 27 }
29 28
30 void Message::AllocData(uint32_t num_bytes) { 29 void Message::Initialize(size_t capacity, bool zero_initialized) {
31 MOJO_DCHECK(!data_); 30 DCHECK(!buffer_);
32 data_num_bytes_ = num_bytes; 31 buffer_.reset(new internal::PickleBuffer(capacity, zero_initialized));
33 data_ = static_cast<internal::MessageData*>(calloc(num_bytes, 1));
34 }
35
36 void Message::AllocUninitializedData(uint32_t num_bytes) {
37 MOJO_DCHECK(!data_);
38 data_num_bytes_ = num_bytes;
39 data_ = static_cast<internal::MessageData*>(malloc(num_bytes));
40 } 32 }
41 33
42 void Message::MoveTo(Message* destination) { 34 void Message::MoveTo(Message* destination) {
43 MOJO_DCHECK(this != destination); 35 MOJO_DCHECK(this != destination);
44 36
45 destination->FreeDataAndCloseHandles();
46
47 // No copy needed. 37 // No copy needed.
48 destination->data_num_bytes_ = data_num_bytes_; 38 std::swap(destination->buffer_, buffer_);
49 destination->data_ = data_;
50 std::swap(destination->handles_, handles_); 39 std::swap(destination->handles_, handles_);
51 40
52 handles_.clear(); 41 Reset();
53 Initialize();
54 } 42 }
55 43
56 void Message::Initialize() { 44 void Message::CloseHandles() {
57 data_num_bytes_ = 0;
58 data_ = nullptr;
59 }
60
61 void Message::FreeDataAndCloseHandles() {
62 free(data_);
63
64 for (std::vector<Handle>::iterator it = handles_.begin(); 45 for (std::vector<Handle>::iterator it = handles_.begin();
65 it != handles_.end(); ++it) { 46 it != handles_.end(); ++it) {
66 if (it->is_valid()) 47 if (it->is_valid())
67 CloseRaw(*it); 48 CloseRaw(*it);
68 } 49 }
69 } 50 }
70 51
71 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, 52 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle,
72 MessageReceiver* receiver, 53 MessageReceiver* receiver,
73 bool* receiver_result) { 54 bool* receiver_result) {
74 MojoResult rv; 55 MojoResult rv;
75 56
76 uint32_t num_bytes = 0, num_handles = 0; 57 uint32_t num_bytes = 0, num_handles = 0;
77 rv = ReadMessageRaw(handle, 58 rv = ReadMessageRaw(handle,
78 nullptr, 59 nullptr,
79 &num_bytes, 60 &num_bytes,
80 nullptr, 61 nullptr,
81 &num_handles, 62 &num_handles,
82 MOJO_READ_MESSAGE_FLAG_NONE); 63 MOJO_READ_MESSAGE_FLAG_NONE);
83 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED) 64 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED)
84 return rv; 65 return rv;
85 66
86 Message message; 67 Message message;
87 message.AllocUninitializedData(num_bytes); 68 message.Initialize(num_bytes, false /* zero_initialized */);
69
70 void* mutable_data = message.buffer()->Allocate(num_bytes);
88 message.mutable_handles()->resize(num_handles); 71 message.mutable_handles()->resize(num_handles);
89 72
90 rv = ReadMessageRaw( 73 rv = ReadMessageRaw(
91 handle, 74 handle,
92 message.mutable_data(), 75 mutable_data,
93 &num_bytes, 76 &num_bytes,
94 message.mutable_handles()->empty() 77 message.mutable_handles()->empty()
95 ? nullptr 78 ? nullptr
96 : reinterpret_cast<MojoHandle*>(&message.mutable_handles()->front()), 79 : reinterpret_cast<MojoHandle*>(message.mutable_handles()->data()),
97 &num_handles, 80 &num_handles,
98 MOJO_READ_MESSAGE_FLAG_NONE); 81 MOJO_READ_MESSAGE_FLAG_NONE);
99 if (receiver && rv == MOJO_RESULT_OK) 82 if (receiver && rv == MOJO_RESULT_OK)
100 *receiver_result = receiver->Accept(&message); 83 *receiver_result = receiver->Accept(&message);
101 84
102 return rv; 85 return rv;
103 } 86 }
104 87
105 } // namespace mojo 88 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698