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

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: happy compiler happy robots 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::Initialize(size_t capacity, bool zero_initialized) {
24 FreeDataAndCloseHandles(); 23 DCHECK(!buffer_);
25 24 buffer_.reset(new internal::PickleBuffer(capacity, zero_initialized));
26 handles_.clear();
27 Initialize();
28 }
29
30 void Message::AllocData(uint32_t num_bytes) {
31 MOJO_DCHECK(!data_);
32 data_num_bytes_ = num_bytes;
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 } 25 }
41 26
42 void Message::MoveTo(Message* destination) { 27 void Message::MoveTo(Message* destination) {
43 MOJO_DCHECK(this != destination); 28 MOJO_DCHECK(this != destination);
44 29
45 destination->FreeDataAndCloseHandles();
46
47 // No copy needed. 30 // No copy needed.
48 destination->data_num_bytes_ = data_num_bytes_; 31 std::swap(destination->buffer_, buffer_);
49 destination->data_ = data_;
50 std::swap(destination->handles_, handles_); 32 std::swap(destination->handles_, handles_);
51 33
34 CloseHandles();
52 handles_.clear(); 35 handles_.clear();
53 Initialize(); 36 buffer_.reset();
54 } 37 }
55 38
56 void Message::Initialize() { 39 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(); 40 for (std::vector<Handle>::iterator it = handles_.begin();
65 it != handles_.end(); ++it) { 41 it != handles_.end(); ++it) {
66 if (it->is_valid()) 42 if (it->is_valid())
67 CloseRaw(*it); 43 CloseRaw(*it);
68 } 44 }
69 } 45 }
70 46
71 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, 47 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle,
72 MessageReceiver* receiver, 48 MessageReceiver* receiver,
73 bool* receiver_result) { 49 bool* receiver_result) {
74 MojoResult rv; 50 MojoResult rv;
75 51
76 uint32_t num_bytes = 0, num_handles = 0; 52 uint32_t num_bytes = 0, num_handles = 0;
77 rv = ReadMessageRaw(handle, 53 rv = ReadMessageRaw(handle,
78 nullptr, 54 nullptr,
79 &num_bytes, 55 &num_bytes,
80 nullptr, 56 nullptr,
81 &num_handles, 57 &num_handles,
82 MOJO_READ_MESSAGE_FLAG_NONE); 58 MOJO_READ_MESSAGE_FLAG_NONE);
83 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED) 59 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED)
84 return rv; 60 return rv;
85 61
86 Message message; 62 Message message;
87 message.AllocUninitializedData(num_bytes); 63 message.Initialize(num_bytes, false /* zero_initialized */);
64
65 void* mutable_data = message.buffer()->Allocate(num_bytes);
88 message.mutable_handles()->resize(num_handles); 66 message.mutable_handles()->resize(num_handles);
89 67
90 rv = ReadMessageRaw( 68 rv = ReadMessageRaw(
91 handle, 69 handle,
92 message.mutable_data(), 70 mutable_data,
93 &num_bytes, 71 &num_bytes,
94 message.mutable_handles()->empty() 72 message.mutable_handles()->empty()
95 ? nullptr 73 ? nullptr
96 : reinterpret_cast<MojoHandle*>(&message.mutable_handles()->front()), 74 : reinterpret_cast<MojoHandle*>(message.mutable_handles()->data()),
97 &num_handles, 75 &num_handles,
98 MOJO_READ_MESSAGE_FLAG_NONE); 76 MOJO_READ_MESSAGE_FLAG_NONE);
99 if (receiver && rv == MOJO_RESULT_OK) 77 if (receiver && rv == MOJO_RESULT_OK)
100 *receiver_result = receiver->Accept(&message); 78 *receiver_result = receiver->Accept(&message);
101 79
102 return rv; 80 return rv;
103 } 81 }
104 82
105 } // namespace mojo 83 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/fixed_buffer.cc ('k') | mojo/public/cpp/bindings/lib/message_builder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698