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

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: 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 {
yzshen1 2015/12/15 19:07:02 Does it make sense to let PickleBuffer has a Pickl
19 public:
20 PickleBuffer();
21 ~PickleBuffer() override;
22
23 const void* data() const {
24 return reinterpret_cast<const void*>(
yzshen1 2015/12/15 19:07:02 nit: I think both reinterpret_cast can be static_c
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 AllocData(uint32_t num_bytes);
37
38 // Resizes the underlying Pickle's capacity without zeroing.
39 void AllocUninitializedData(uint32_t num_bytes);
40
41 // Buffer:
42 PickleBuffer* AsPickleBuffer() override;
43
44 private:
45 // Buffer implementation. Note that this may grow the Pickle's capacity if
46 // necessary, but you can avoid this by calling AllocData to preallocate when
yzshen1 2015/12/15 19:07:02 I noticed that there is a DCHECK in the code, but
47 // the final buffer size is known in advance.
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 //
62 // We can adopt Pickle's header format for Mojo messages (or do any number of
yzshen1 2015/12/15 19:07:02 I personally prefer not to change our wire format
63 // other things to clean this up), but wasting 8 bytes is for now the most
64 // efficient path toward blending existing IPC traits with Mojo bindings.
65 struct PaddedHeader : public base::Pickle::Header {
66 uint32_t padding;
67 };
68
69 static_assert(sizeof(PaddedHeader) % 8 == 0,
70 "PickleBuffer requires a Pickle header size with 8-byte alignment.");
71
72 // The index into the message payload where the next Buffer::Allocate should
73 // claim memory.
74 size_t allocation_cursor_ = 0;
75
76 DISALLOW_COPY_AND_ASSIGN(PickleBuffer);
77 };
78
79 } // namespace internal
80 } // namespace mojo
81
82 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_PICKLE_BUFFER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698