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

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

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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_PICKLE_BUFFER_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_PICKLE_BUFFER_H_
7
8 #include "base/macros.h"
9 #include "base/pickle.h"
10 #include "mojo/public/cpp/bindings/lib/buffer.h"
11
12 namespace mojo {
13 namespace internal {
14
15 // An implementation of Buffer which uses base::Pickle for its backing. Note
16 // that this does not use Pickle's header structure at all, instead storing
17 // the complete Message (including header) in the Pickle's payload.
18 class PickleBuffer : public base::Pickle, public Buffer {
19 public:
20 PickleBuffer();
21 ~PickleBuffer() override;
22
23 const void* data() const {
24 return reinterpret_cast<const void*>(
25 reinterpret_cast<const char*>(base::Pickle::data())
26 + sizeof(PaddedHeader));
27 }
28
29 void* data() {
30 return const_cast<void*>(static_cast<const PickleBuffer*>(this)->data());
31 }
32
33 size_t data_num_bytes() const { return payload_size(); }
34
35 // Resizes and zeroes the underlying Pickle's capacity.
36 void ReserveCapacity(uint32_t num_bytes);
yzshen1 2015/12/15 22:27:08 "Reserve capacity" doesn't sound like it zero out
Ken Rockot(use gerrit already) 2015/12/16 00:09:33 PickleBuffer doesn't actually need to be resizable
37
38 // Resizes the underlying Pickle's capacity without zeroing.
39 void ReserveUninitializedCapacity(uint32_t num_bytes);
40
41 // Buffer:
42 PickleBuffer* AsPickleBuffer() override;
43
44 private:
45 // Buffer implementation. Note that this cannot grow the Pickle's capacity and
46 // it is an error to Allocate() more bytes in total than have been
47 // pre-allocated using ReserveCapacity() or ReserveUninitializedCapacity().
48 //
49 // This guarantees that the returned data is aligned on an 8-byte boundary.
50 void* Allocate(size_t num_bytes) override;
51
52 // TODO(rockot): Stop wasting 8 bytes per buffer.
53 //
54 // We don't use Pickle's header at all, but its base header type consumes 4
55 // bytes. We waste another 4 bytes to keep our actual buffer aligned to an
56 // 8-byte boundary.
57 //
58 // The reason we don't use base::Pickle's header (yet) is that it stores
59 // payload length in the first 4 bytes. Mojo Messages are packed like structs,
60 // where the first 4 bytes are header size rather than payload size.
61 struct PaddedHeader : public base::Pickle::Header {
62 uint32_t padding;
63 };
64
65 static_assert(sizeof(PaddedHeader) % 8 == 0,
66 "PickleBuffer requires a Pickle header size with 8-byte alignment.");
67
68 // The index into the message payload where the next Buffer::Allocate should
69 // claim memory.
70 size_t allocation_cursor_ = 0;
71
72 DISALLOW_COPY_AND_ASSIGN(PickleBuffer);
73 };
74
75 } // namespace internal
76 } // namespace mojo
77
78 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_PICKLE_BUFFER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698