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

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: some cleanup 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
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..4a4144e2b0bcaf42e9dbdd7bd39058dfac457c41
--- /dev/null
+++ b/mojo/public/cpp/bindings/lib/pickle_buffer.cc
@@ -0,0 +1,56 @@
+// 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 "mojo/public/cpp/bindings/lib/bindings_serialization.h"
+
+namespace mojo {
+namespace internal {
+
+PickleBuffer::PickleBuffer() : base::Pickle(sizeof(PaddedHeader)) {
+ headerT<PaddedHeader>()->padding = 0;
+}
+
+PickleBuffer::~PickleBuffer() {
+}
+
+void PickleBuffer::ReserveCapacity(uint32_t num_bytes) {
+ ReserveUninitializedCapacity(num_bytes);
+ memset(data(), 0, num_bytes);
+}
+
+void PickleBuffer::ReserveUninitializedCapacity(uint32_t num_bytes) {
+ // Data should only ever be explicitly allocated through ReserveCapacity or
+ // ReserveUninitializedCapacity if the buffer has no existing data in it yet.
+ DCHECK(payload_size() == 0);
+ Resize(num_bytes);
+}
+
+void* PickleBuffer::Allocate(size_t num_bytes) {
+ // 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(payload_size());
+ DCHECK_GE(padded_capacity, payload_size());
+ size_t padding_bytes = padded_capacity - payload_size();
+ size_t available_capacity = capacity_after_header() - payload_size();
+ size_t allocation_size = padding_bytes + num_bytes;
+ const void* previous_data_location = data();
+ if (available_capacity < allocation_size) {
+ NOTREACHED() <<
+ "Message buffers must be fully allocated before serialization.";
+ return nullptr;
+ }
+ char* p = reinterpret_cast<char*>(ClaimBytes(allocation_size));
+ DCHECK_EQ(data(), previous_data_location);
+ return p + padding_bytes;
+}
+
+PickleBuffer* PickleBuffer::AsPickleBuffer() { return this; }
+
+} // namespace internal
+} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698