Chromium Code Reviews| 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..b3daa4bbaf12d269e2bb1c8d244ceac42ff2009b |
| --- /dev/null |
| +++ b/mojo/public/cpp/bindings/lib/pickle_buffer.cc |
| @@ -0,0 +1,48 @@ |
| +// 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" |
|
yzshen1
2015/12/15 19:07:02
This is not used. Maybe you want to use internal::
|
| + |
| +namespace mojo { |
| +namespace internal { |
| + |
| +PickleBuffer::PickleBuffer() : base::Pickle(sizeof(PaddedHeader)) { |
| + headerT<PaddedHeader>()->padding = 0; |
| +} |
| + |
| +PickleBuffer::~PickleBuffer() { |
| +} |
| + |
| +void PickleBuffer::AllocData(uint32_t num_bytes) { |
| + AllocUninitializedData(num_bytes); |
| + memset(data(), 0, num_bytes); |
| +} |
| + |
| +void PickleBuffer::AllocUninitializedData(uint32_t num_bytes) { |
| + // Data should only ever be explicitly allocated through AllocData or |
| + // AllocUninitializedData 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 padding_bytes = (8 - (payload_size() % 8)) % 8; |
| + size_t previous_capacity = capacity_after_header(); |
| + char* p = reinterpret_cast<char*>(ClaimBytes(padding_bytes + num_bytes)); |
| + DCHECK_EQ(capacity_after_header(), previous_capacity) |
| + << "Message buffers must be fully allocated before serialization."; |
| + return p + padding_bytes; |
| +} |
| + |
| +PickleBuffer* PickleBuffer::AsPickleBuffer() { return this; } |
| + |
| +} // namespace internal |
| +} // namespace mojo |