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

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

Issue 1932083002: Mojo: Use new message APIs to reduce copying (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 2016 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/lib/message_buffer.h"
6
7 #include <limits>
8
9 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
10
11 namespace mojo {
12 namespace internal {
13
14 MessageBuffer::MessageBuffer(size_t capacity, bool zero_initialized) {
15 DCHECK_LE(capacity, std::numeric_limits<uint32_t>::max());
16 data_num_bytes_ = static_cast<uint32_t>(capacity);
17
18 MojoResult rv = AllocMessage(capacity, nullptr, 0,
19 MOJO_ALLOC_MESSAGE_FLAG_NONE, &message_);
20 CHECK_EQ(rv, MOJO_RESULT_OK);
21
22 if (capacity == 0) {
23 buffer_ = nullptr;
24 } else {
25 rv = GetMessageBuffer(message_.get(), &buffer_);
26 CHECK_EQ(rv, MOJO_RESULT_OK);
27
28 if (zero_initialized)
29 memset(buffer_, 0, capacity);
30 }
31 }
32
33 MessageBuffer::MessageBuffer(ScopedMessageHandle message, uint32_t num_bytes) {
34 message_ = std::move(message);
35 data_num_bytes_ = num_bytes;
36
37 MojoResult rv = GetMessageBuffer(message_.get(), &buffer_);
yzshen1 2016/04/29 16:50:20 I noticed that the previous method explicitly set
Ken Rockot(use gerrit already) 2016/04/29 17:37:13 done
38 CHECK_EQ(rv, MOJO_RESULT_OK);
39 }
40
41 MessageBuffer::~MessageBuffer() {}
42
43 void* MessageBuffer::Allocate(size_t delta) {
44 delta = internal::Align(delta);
45
46 DCHECK_LE(delta, static_cast<size_t>(data_num_bytes_));
47 DCHECK_GT(bytes_claimed_ + static_cast<uint32_t>(delta), bytes_claimed_);
48
49 uint32_t new_bytes_claimed = bytes_claimed_ + static_cast<uint32_t>(delta);
50 if (new_bytes_claimed > data_num_bytes_) {
51 NOTREACHED();
52 return nullptr;
53 }
54
55 char* start = static_cast<char*>(buffer_) + bytes_claimed_;
yzshen1 2016/04/29 16:50:20 It relies on the fact that |buffer_| itself is 8-b
Ken Rockot(use gerrit already) 2016/04/29 17:37:13 done
56 bytes_claimed_ = new_bytes_claimed;
57 return static_cast<void*>(start);
58 }
59
60 } // namespace internal
61 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698