• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INCLUDE_PERFETTO_PROTOZERO_STATIC_BUFFER_H_
18 #define INCLUDE_PERFETTO_PROTOZERO_STATIC_BUFFER_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "perfetto/base/export.h"
25 #include "perfetto/protozero/scattered_stream_writer.h"
26 
27 namespace protozero {
28 
29 class Message;
30 
31 // A simple implementation of ScatteredStreamWriter::Delegate backed by a
32 // fixed-size buffer. It doesn't support expansion. The caller needs to ensure
33 // to never write more than the size of the buffer. Will CHECK() otherwise.
34 class PERFETTO_EXPORT StaticBufferDelegate
35     : public ScatteredStreamWriter::Delegate {
36  public:
StaticBufferDelegate(uint8_t * buf,size_t len)37   StaticBufferDelegate(uint8_t* buf, size_t len) : range_{buf, buf + len} {}
38   ~StaticBufferDelegate() override;
39 
40   // ScatteredStreamWriter::Delegate implementation.
41   ContiguousMemoryRange GetNewBuffer() override;
42 
43   ContiguousMemoryRange const range_;
44   bool get_new_buffer_called_once_ = false;
45 };
46 
47 // Helper function to create protozero messages backed by a fixed-size buffer
48 // in one line. You can write:
49 //   protozero::Static<protozero::MyMessage> msg(buf.data(), buf.size());
50 //   msg->set_stuff(...);
51 //   size_t bytes_encoded = msg.Finalize();
52 template <typename T /* protozero::Message */>
53 class StaticBuffered {
54  public:
StaticBuffered(void * buf,size_t len)55   StaticBuffered(void* buf, size_t len)
56       : delegate_(reinterpret_cast<uint8_t*>(buf), len), writer_(&delegate_) {
57     msg_.Reset(&writer_);
58   }
59 
60   // This can't be neither copied nor moved because Message hands out pointers
61   // to itself when creating submessages.
62   StaticBuffered(const StaticBuffered&) = delete;
63   StaticBuffered& operator=(const StaticBuffered&) = delete;
64   StaticBuffered(StaticBuffered&&) = delete;
65   StaticBuffered& operator=(StaticBuffered&&) = delete;
66 
get()67   T* get() { return &msg_; }
68   T* operator->() { return &msg_; }
69 
70   // The lack of a size() method is deliberate. It's to prevent that one
71   // accidentally calls size() before Finalize().
72 
73   // Returns the number of encoded bytes (<= the size passed in the ctor).
Finalize()74   size_t Finalize() {
75     msg_.Finalize();
76     return static_cast<size_t>(writer_.write_ptr() - delegate_.range_.begin);
77   }
78 
79  private:
80   StaticBufferDelegate delegate_;
81   ScatteredStreamWriter writer_;
82   T msg_;
83 };
84 
85 // Helper function to create stack-based protozero messages in one line.
86 // You can write:
87 //   protozero::StackBuffered<protozero::MyMessage, 16> msg;
88 //   msg->set_stuff(...);
89 //   size_t bytes_encoded = msg.Finalize();
90 template <typename T /* protozero::Message */, size_t N>
91 class StackBuffered : public StaticBuffered<T> {
92  public:
StackBuffered()93   StackBuffered() : StaticBuffered<T>(&buf_[0], N) {}
94 
95  private:
96   uint8_t buf_[N];  // Deliberately not initialized.
97 };
98 
99 }  // namespace protozero
100 
101 #endif  // INCLUDE_PERFETTO_PROTOZERO_STATIC_BUFFER_H_
102