| 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 <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 #include <stdlib.h> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/pickle.h" | |
| 14 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | |
| 15 | |
| 16 namespace mojo { | |
| 17 namespace internal { | |
| 18 | |
| 19 class PickleBuffer::Storage : public base::Pickle { | |
| 20 public: | |
| 21 explicit Storage(size_t num_bytes); | |
| 22 ~Storage() override {} | |
| 23 | |
| 24 size_t available_capacity() const { | |
| 25 return capacity_after_header() - payload_size(); | |
| 26 } | |
| 27 | |
| 28 void* GetData() { return static_cast<void*>(mutable_payload()); } | |
| 29 void* Claim(size_t num_bytes) { return ClaimBytes(num_bytes); } | |
| 30 | |
| 31 private: | |
| 32 // TODO(rockot): Stop wasting 8 bytes per buffer. | |
| 33 // | |
| 34 // We don't use Pickle's header at all, but its base header type consumes 4 | |
| 35 // bytes. We waste another 4 bytes to keep our actual buffer aligned to an | |
| 36 // 8-byte boundary. | |
| 37 // | |
| 38 // The reason we don't use base::Pickle's header is that it stores payload | |
| 39 // length in the first 4 bytes. Mojo Messages are packed like mojom structs, | |
| 40 // where the first 4 bytes are header size rather than payload size. | |
| 41 struct PaddedHeader : public base::Pickle::Header { | |
| 42 uint32_t padding; | |
| 43 }; | |
| 44 | |
| 45 static_assert(sizeof(PaddedHeader) % 8 == 0, | |
| 46 "PickleBuffer requires a Pickle header size with 8-byte alignment."); | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(Storage); | |
| 49 }; | |
| 50 | |
| 51 PickleBuffer::Storage::Storage(size_t num_bytes) | |
| 52 : base::Pickle(sizeof(PaddedHeader)) { | |
| 53 headerT<PaddedHeader>()->padding = 0; | |
| 54 Resize(num_bytes); | |
| 55 } | |
| 56 | |
| 57 PickleBuffer::PickleBuffer(size_t num_bytes, bool zero_initialized) | |
| 58 : storage_(new Storage(num_bytes)) { | |
| 59 if (zero_initialized) | |
| 60 memset(storage_->GetData(), 0, num_bytes); | |
| 61 } | |
| 62 | |
| 63 PickleBuffer::~PickleBuffer() { | |
| 64 } | |
| 65 | |
| 66 const void* PickleBuffer::data() const { return storage_->GetData(); } | |
| 67 | |
| 68 size_t PickleBuffer::data_num_bytes() const { return storage_->payload_size(); } | |
| 69 | |
| 70 base::Pickle* PickleBuffer::pickle() const { | |
| 71 return storage_.get(); | |
| 72 } | |
| 73 | |
| 74 void* PickleBuffer::Allocate(size_t num_bytes) { | |
| 75 DCHECK(storage_); | |
| 76 | |
| 77 // The last allocation may terminate in between 8-byte boundaries. Pad the | |
| 78 // front of this allocation if that's the case. | |
| 79 size_t padded_capacity = Align(storage_->payload_size()); | |
| 80 DCHECK_GE(padded_capacity, storage_->payload_size()); | |
| 81 | |
| 82 size_t padding_bytes = padded_capacity - storage_->payload_size(); | |
| 83 size_t allocation_size = padding_bytes + num_bytes; | |
| 84 const void* previous_data_location = storage_->GetData(); | |
| 85 if (storage_->available_capacity() < allocation_size) { | |
| 86 NOTREACHED() << | |
| 87 "Message buffers must be fully allocated before serialization."; | |
| 88 return nullptr; | |
| 89 } | |
| 90 char* p = static_cast<char*>(storage_->Claim(allocation_size)); | |
| 91 DCHECK_EQ(storage_->GetData(), previous_data_location); | |
| 92 return p + padding_bytes; | |
| 93 } | |
| 94 | |
| 95 PickleBuffer* PickleBuffer::AsPickleBuffer() { return this; } | |
| 96 | |
| 97 } // namespace internal | |
| 98 } // namespace mojo | |
| OLD | NEW |