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

Side by Side Diff: mojo/public/cpp/bindings/lib/pickle_buffer.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
« no previous file with comments | « mojo/public/cpp/bindings/lib/pickle_buffer.h ('k') | mojo/public/cpp/bindings/message.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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/pickle_buffer.h"
6
7 #include <stdlib.h>
8
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "base/pickle.h"
12 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
13
14 namespace mojo {
15 namespace internal {
16
17 class PickleBuffer::Storage : public base::Pickle {
18 public:
19 explicit Storage(size_t num_bytes);
20 ~Storage() override {}
21
22 size_t available_capacity() const {
23 return capacity_after_header() - payload_size();
24 }
25
26 void* GetData() { return static_cast<void*>(mutable_payload()); }
27 void* Claim(size_t num_bytes) { return ClaimBytes(num_bytes); }
28
29 private:
30 // TODO(rockot): Stop wasting 8 bytes per buffer.
31 //
32 // We don't use Pickle's header at all, but its base header type consumes 4
33 // bytes. We waste another 4 bytes to keep our actual buffer aligned to an
34 // 8-byte boundary.
35 //
36 // The reason we don't use base::Pickle's header is that it stores payload
37 // length in the first 4 bytes. Mojo Messages are packed like mojom structs,
38 // where the first 4 bytes are header size rather than payload size.
39 struct PaddedHeader : public base::Pickle::Header {
40 uint32_t padding;
41 };
42
43 static_assert(sizeof(PaddedHeader) % 8 == 0,
44 "PickleBuffer requires a Pickle header size with 8-byte alignment.");
45
46 DISALLOW_COPY_AND_ASSIGN(Storage);
47 };
48
49 PickleBuffer::Storage::Storage(size_t num_bytes)
50 : base::Pickle(sizeof(PaddedHeader)) {
51 headerT<PaddedHeader>()->padding = 0;
52 Resize(num_bytes);
53 }
54
55 PickleBuffer::PickleBuffer(size_t num_bytes, bool zero_initialized)
56 : storage_(new Storage(num_bytes)) {
57 if (zero_initialized)
58 memset(storage_->GetData(), 0, num_bytes);
59 }
60
61 PickleBuffer::~PickleBuffer() {
62 }
63
64 const void* PickleBuffer::data() const { return storage_->GetData(); }
65
66 size_t PickleBuffer::data_num_bytes() const { return storage_->payload_size(); }
67
68 base::Pickle* PickleBuffer::pickle() const {
69 return storage_.get();
70 }
71
72 void* PickleBuffer::Allocate(size_t num_bytes) {
73 DCHECK(storage_);
74
75 // The last allocation may terminate in between 8-byte boundaries. Pad the
76 // front of this allocation if that's the case.
77 size_t padded_capacity = Align(storage_->payload_size());
78 DCHECK_GE(padded_capacity, storage_->payload_size());
79
80 size_t padding_bytes = padded_capacity - storage_->payload_size();
81 size_t allocation_size = padding_bytes + num_bytes;
82 const void* previous_data_location = storage_->GetData();
83 if (storage_->available_capacity() < allocation_size) {
84 NOTREACHED() <<
85 "Message buffers must be fully allocated before serialization.";
86 return nullptr;
87 }
88 char* p = static_cast<char*>(storage_->Claim(allocation_size));
89 DCHECK_EQ(storage_->GetData(), previous_data_location);
90 return p + padding_bytes;
91 }
92
93 PickleBuffer* PickleBuffer::AsPickleBuffer() { return this; }
94
95 } // namespace internal
96 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/pickle_buffer.h ('k') | mojo/public/cpp/bindings/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698