Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: mojo/public/cpp/bindings/lib/pickle_buffer.cc

Issue 1524613002: [mojo] Use base::Pickle for Message storage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: some cleanup Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698