OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 COMPONENTS_TRACING_CORE_SCATTERED_STREAM_WRITER_H_ | |
6 #define COMPONENTS_TRACING_CORE_SCATTERED_STREAM_WRITER_H_ | |
7 | |
8 #include <inttypes.h> | |
oystein (OOO til 10th of July)
2016/06/15 08:35:09
Can we rely on this? Unsure what the status of C99
Primiano Tucci (use gerrit)
2016/06/29 09:32:02
ehm, here I really meant stdint (for uint32_t etc)
| |
9 | |
10 #include "base/macros.h" | |
11 #include "components/tracing/tracing_export.h" | |
12 | |
13 namespace tracing { | |
14 namespace v2 { | |
15 | |
16 // This class deals with the following problem: append-only proto messages want | |
17 // to write a stream of bytes, without caring about the implementation of the | |
18 // underlying buffer (which concretely will be either the trace ring buffer | |
19 // or a heap-allocated buffer). The main deal is: proto messages don't know in | |
20 // advance what the size of the message will be. | |
21 // Due to the tracing buffer being split into fixed-size chunks, in some | |
22 // occasions these writes nees to be spread over two (or more) non contiguous | |
oystein (OOO til 10th of July)
2016/06/15 08:35:10
nit: need
Primiano Tucci (use gerrit)
2016/06/29 09:32:02
Done.
| |
23 // chunks of memory. Similarly, when the buffer is backed by the heap, we want | |
24 // to avoid realloc() calls, as they can cause an expensive memcpy to expand. | |
25 // The purpose of this class is to abtract away the non-contiguous write logic. | |
26 // This class knows how to deal with writes as long as they fall in the same | |
27 // ContiguousMemoryRange, and defers the chunk-chaining logic to the Delegate. | |
28 class TRACING_EXPORT ScatteredStreamWriter { | |
29 public: | |
30 struct ContiguousMemoryRange { | |
31 uint8_t* begin; | |
32 uint8_t* end; // STL style: one byte past the end of the buffer. | |
33 }; | |
34 | |
35 class Delegate { | |
36 public: | |
37 virtual ContiguousMemoryRange GetNewContiguousMemoryBuffer() = 0; | |
38 }; | |
39 | |
40 explicit ScatteredStreamWriter(Delegate* delegate); | |
41 ~ScatteredStreamWriter(); | |
42 | |
43 void WriteByte(uint8_t value); | |
44 void WriteBytes(const uint8_t* src, size_t size); | |
45 | |
46 // Reserves a fixed number of bytes, to be backfilled later through | |
47 // WriteReservedBytes(). | |
48 ContiguousMemoryRange ReserveBytes(size_t size); | |
49 void WriteReservedBytes(const ContiguousMemoryRange& reserved_range, | |
50 const uint8_t* src); | |
51 | |
52 void Reset(ContiguousMemoryRange range); | |
53 | |
54 // Number of free bytes in the current range that can be written without | |
55 // reuqesting a new contiguous buffer. | |
oystein (OOO til 10th of July)
2016/06/15 08:35:09
nit: requesting
Primiano Tucci (use gerrit)
2016/06/29 09:32:02
Done.
| |
56 size_t bytes_available() const { | |
57 return static_cast<size_t>(cur_range_.end - write_ptr_); | |
58 } | |
59 | |
60 uint8_t* write_ptr() const { return write_ptr_; } | |
61 | |
62 private: | |
63 void Expand(); | |
64 | |
65 Delegate* const delegate_; | |
66 ContiguousMemoryRange cur_range_; | |
67 uint8_t* write_ptr_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(ScatteredStreamWriter); | |
70 }; | |
71 | |
72 } // namespace v2 | |
73 } // namespace tracing | |
74 | |
75 #endif // COMPONENTS_TRACING_CORE_SCATTERED_STREAM_WRITER_H_ | |
OLD | NEW |