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::ReserveCapacity(uint32_t num_bytes) { |
| 23 ReserveUninitializedCapacity(num_bytes); |
| 24 memset(data(), 0, num_bytes); |
| 25 } |
| 26 |
| 27 void PickleBuffer::ReserveUninitializedCapacity(uint32_t num_bytes) { |
| 28 // Data should only ever be explicitly allocated through ReserveCapacity or |
| 29 // ReserveUninitializedCapacity 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 padded_capacity = Align(payload_size()); |
| 38 DCHECK_GE(padded_capacity, payload_size()); |
| 39 size_t padding_bytes = padded_capacity - payload_size(); |
| 40 size_t available_capacity = capacity_after_header() - payload_size(); |
| 41 size_t allocation_size = padding_bytes + num_bytes; |
| 42 const void* previous_data_location = data(); |
| 43 if (available_capacity < allocation_size) { |
| 44 NOTREACHED() << |
| 45 "Message buffers must be fully allocated before serialization."; |
| 46 return nullptr; |
| 47 } |
| 48 char* p = reinterpret_cast<char*>(ClaimBytes(allocation_size)); |
| 49 DCHECK_EQ(data(), previous_data_location); |
| 50 return p + padding_bytes; |
| 51 } |
| 52 |
| 53 PickleBuffer* PickleBuffer::AsPickleBuffer() { return this; } |
| 54 |
| 55 } // namespace internal |
| 56 } // namespace mojo |
OLD | NEW |