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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/lib/pickle_buffer.cc
diff --git a/mojo/public/cpp/bindings/lib/pickle_buffer.cc b/mojo/public/cpp/bindings/lib/pickle_buffer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cb481a9146b1bcac1809c9418c3928c52748ca01
--- /dev/null
+++ b/mojo/public/cpp/bindings/lib/pickle_buffer.cc
@@ -0,0 +1,96 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/public/cpp/bindings/lib/pickle_buffer.h"
+
+#include <stdlib.h>
+
+#include "base/logging.h"
+#include "base/macros.h"
+#include "base/pickle.h"
+#include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
+
+namespace mojo {
+namespace internal {
+
+class PickleBuffer::Storage : public base::Pickle {
+ public:
+ explicit Storage(size_t num_bytes);
+ ~Storage() override {}
+
+ size_t available_capacity() const {
+ return capacity_after_header() - payload_size();
+ }
+
+ void* GetData() { return static_cast<void*>(mutable_payload()); }
+ void* Claim(size_t num_bytes) { return ClaimBytes(num_bytes); }
+
+ private:
+ // TODO(rockot): Stop wasting 8 bytes per buffer.
+ //
+ // We don't use Pickle's header at all, but its base header type consumes 4
+ // bytes. We waste another 4 bytes to keep our actual buffer aligned to an
+ // 8-byte boundary.
+ //
+ // The reason we don't use base::Pickle's header is that it stores payload
+ // length in the first 4 bytes. Mojo Messages are packed like mojom structs,
+ // where the first 4 bytes are header size rather than payload size.
+ struct PaddedHeader : public base::Pickle::Header {
+ uint32_t padding;
+ };
+
+ static_assert(sizeof(PaddedHeader) % 8 == 0,
+ "PickleBuffer requires a Pickle header size with 8-byte alignment.");
+
+ DISALLOW_COPY_AND_ASSIGN(Storage);
+};
+
+PickleBuffer::Storage::Storage(size_t num_bytes)
+ : base::Pickle(sizeof(PaddedHeader)) {
+ headerT<PaddedHeader>()->padding = 0;
+ Resize(num_bytes);
+}
+
+PickleBuffer::PickleBuffer(size_t num_bytes, bool zero_initialized)
+ : storage_(new Storage(num_bytes)) {
+ if (zero_initialized)
+ memset(storage_->GetData(), 0, num_bytes);
+}
+
+PickleBuffer::~PickleBuffer() {
+}
+
+const void* PickleBuffer::data() const { return storage_->GetData(); }
+
+size_t PickleBuffer::data_num_bytes() const { return storage_->payload_size(); }
+
+base::Pickle* PickleBuffer::pickle() const {
+ return storage_.get();
+}
+
+void* PickleBuffer::Allocate(size_t num_bytes) {
+ DCHECK(storage_);
+
+ // The last allocation may terminate in between 8-byte boundaries. Pad the
+ // front of this allocation if that's the case.
+ size_t padded_capacity = Align(storage_->payload_size());
+ DCHECK_GE(padded_capacity, storage_->payload_size());
+
+ size_t padding_bytes = padded_capacity - storage_->payload_size();
+ size_t allocation_size = padding_bytes + num_bytes;
+ const void* previous_data_location = storage_->GetData();
+ if (storage_->available_capacity() < allocation_size) {
+ NOTREACHED() <<
+ "Message buffers must be fully allocated before serialization.";
+ return nullptr;
+ }
+ char* p = static_cast<char*>(storage_->Claim(allocation_size));
+ DCHECK_EQ(storage_->GetData(), previous_data_location);
+ return p + padding_bytes;
+}
+
+PickleBuffer* PickleBuffer::AsPickleBuffer() { return this; }
+
+} // namespace internal
+} // namespace mojo
« 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