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

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

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/public/cpp/bindings/message.h"
6
7 #include <stdlib.h>
8
9 #include <algorithm>
10
11 #include "mojo/public/cpp/environment/logging.h"
12
13 namespace mojo {
14
15 Message::Message() {
16 Initialize();
17 }
18
19 Message::~Message() {
20 FreeDataAndCloseHandles();
21 }
22
23 void Message::Reset() {
24 FreeDataAndCloseHandles();
25
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 }
41
42 void Message::MoveTo(Message* destination) {
43 MOJO_DCHECK(this != destination);
44
45 destination->FreeDataAndCloseHandles();
46
47 // No copy needed.
48 destination->data_num_bytes_ = data_num_bytes_;
49 destination->data_ = data_;
50 std::swap(destination->handles_, handles_);
51
52 handles_.clear();
53 Initialize();
54 }
55
56 void Message::Initialize() {
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();
65 it != handles_.end(); ++it) {
66 if (it->is_valid())
67 CloseRaw(*it);
68 }
69 }
70
71 MojoResult ReadMessage(MessagePipeHandle handle, Message* message) {
72 MOJO_DCHECK(handle.is_valid());
73 MOJO_DCHECK(message);
74 MOJO_DCHECK(message->handles()->empty());
75 MOJO_DCHECK(message->data_num_bytes() == 0);
76
77 uint32_t num_bytes = 0;
78 uint32_t num_handles = 0;
79 MojoResult rv = ReadMessageRaw(handle, nullptr, &num_bytes, nullptr,
80 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE);
81 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED)
82 return rv;
83
84 message->AllocUninitializedData(num_bytes);
85 message->mutable_handles()->resize(num_handles);
86
87 uint32_t num_bytes_actual = num_bytes;
88 uint32_t num_handles_actual = num_handles;
89 rv = ReadMessageRaw(
90 handle, message->mutable_data(), &num_bytes_actual,
91 message->mutable_handles()->empty()
92 ? nullptr
93 : reinterpret_cast<MojoHandle*>(&message->mutable_handles()->front()),
94 &num_handles_actual, MOJO_READ_MESSAGE_FLAG_NONE);
95
96 MOJO_DCHECK(num_bytes == num_bytes_actual);
97 MOJO_DCHECK(num_handles == num_handles_actual);
98
99 return rv;
100 }
101
102 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle,
103 MessageReceiver* receiver,
104 bool* receiver_result) {
105 Message message;
106 MojoResult rv = ReadMessage(handle, &message);
107 if (receiver && rv == MOJO_RESULT_OK)
108 *receiver_result = receiver->Accept(&message);
109
110 return rv;
111 }
112
113 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_serialization_forward.h ('k') | mojo/public/cpp/bindings/lib/message_builder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698