OLD | NEW |
(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 "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
| 11 |
| 12 namespace mojo { |
| 13 namespace internal { |
| 14 |
| 15 PickleBuffer::PickleBuffer() : base::Pickle(sizeof(PaddedHeader)) { |
| 16 headerT<PaddedHeader>()->padding = 0; |
| 17 } |
| 18 |
| 19 PickleBuffer::~PickleBuffer() { |
| 20 } |
| 21 |
| 22 void PickleBuffer::AllocData(uint32_t num_bytes) { |
| 23 AllocUninitializedData(num_bytes); |
| 24 memset(data(), 0, num_bytes); |
| 25 } |
| 26 |
| 27 void PickleBuffer::AllocUninitializedData(uint32_t num_bytes) { |
| 28 // Data should only ever be explicitly allocated through AllocData or |
| 29 // AllocUninitializedData if the buffer has no existing data in it yet. |
| 30 DCHECK(payload_size() == 0); |
| 31 Resize(num_bytes); |
| 32 } |
| 33 |
| 34 void* PickleBuffer::Allocate(size_t num_bytes) { |
| 35 // The last allocation may terminate in between 8-byte boundaries. Pad the |
| 36 // front of this allocation if that's the case. |
| 37 size_t padding_bytes = (8 - (payload_size() % 8)) % 8; |
| 38 size_t previous_capacity = capacity_after_header(); |
| 39 char* p = reinterpret_cast<char*>(ClaimBytes(padding_bytes + num_bytes)); |
| 40 DCHECK_EQ(capacity_after_header(), previous_capacity) |
| 41 << "Message buffers must be fully allocated before serialization."; |
| 42 return p + padding_bytes; |
| 43 } |
| 44 |
| 45 PickleBuffer* PickleBuffer::AsPickleBuffer() { return this; } |
| 46 |
| 47 } // namespace internal |
| 48 } // namespace mojo |
OLD | NEW |