| 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
|
|
|