Chromium Code Reviews| 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 <stdint.h> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "components/tracing/tracing_export.h" | |
| 12 | |
| 13 namespace tracing { | |
| 14 namespace v2 { | |
| 15 | |
| 16 struct ContiguousMemoryRange { | |
| 17 uint8_t* begin; | |
| 18 uint8_t* end; // STL style: one byte past the end of the buffer. | |
| 19 }; | |
| 20 | |
| 21 // This class deals with the following problem: append-only proto messages want | |
| 22 // to write a stream of bytes, without caring about the implementation of the | |
| 23 // underlying buffer (which concretely will be either the trace ring buffer | |
| 24 // or a heap-allocated buffer). The main deal is: proto messages don't know in | |
| 25 // advance what the size of the message will be. | |
|
petrcermak
2016/06/29 10:27:48
nit: The subject of this sentence is "proto messag
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Done.
| |
| 26 // Due to the tracing buffer being split into fixed-size chunks, in some | |
|
petrcermak
2016/06/29 10:27:48
nit: I think that the correct wording is "ON ... o
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
but.. there are 6 ""in some occasions" vs 1 "on so
| |
| 27 // occasions these writes need to be spread over two (or more) non contiguous | |
|
petrcermak
2016/06/29 10:27:48
nit: s/non contiguous/non-contiguous/
petrcermak
2016/06/29 10:27:48
nit: I think there should be a comma after "occasi
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Done.
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Done.
| |
| 28 // chunks of memory. Similarly, when the buffer is backed by the heap, we want | |
| 29 // to avoid realloc() calls, as they can cause an expensive memcpy to extend. | |
|
petrcermak
2016/06/29 10:27:48
FYI, I have no idea what it means when "an expensi
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Done.
| |
| 30 // The purpose of this class is to abtract away the non-contiguous write logic. | |
| 31 // This class knows how to deal with writes as long as they fall in the same | |
| 32 // ContiguousMemoryRange, and defers the chunk-chaining logic to the Delegate. | |
|
petrcermak
2016/06/29 10:27:48
nit: there shouldn't be a comma here
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Done.
| |
| 33 class TRACING_EXPORT ScatteredStreamWriter { | |
| 34 public: | |
| 35 class Delegate { | |
| 36 public: | |
| 37 virtual ContiguousMemoryRange GetNewBuffer() = 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. | |
|
petrcermak
2016/06/29 10:27:48
nit: the comma here is confusing since it incorrec
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Ups. Yeah I think it's a refuse from an older long
| |
| 47 ContiguousMemoryRange ReserveBytes(size_t size); | |
|
petrcermak
2016/06/29 10:27:49
Even though it's technically already in the type n
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
It's funny because the previous name was ReserveCo
| |
| 48 | |
| 49 // Resets the buffer boundaries and the write pointer to the given |range|. | |
| 50 // Subsequent WriteByte(s) will write into |range|. | |
| 51 void Reset(ContiguousMemoryRange range); | |
| 52 | |
| 53 // Number of contiguous free bytes in |cur_range_| that can be written without | |
| 54 // requesting a new buffer. | |
| 55 size_t bytes_available() const { | |
| 56 return static_cast<size_t>(cur_range_.end - write_ptr_); | |
| 57 } | |
| 58 | |
| 59 uint8_t* write_ptr() const { return write_ptr_; } | |
| 60 | |
| 61 private: | |
| 62 void Extend(); | |
| 63 | |
| 64 Delegate* const delegate_; | |
| 65 ContiguousMemoryRange cur_range_; | |
| 66 uint8_t* write_ptr_; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(ScatteredStreamWriter); | |
| 69 }; | |
| 70 | |
| 71 } // namespace v2 | |
| 72 } // namespace tracing | |
| 73 | |
| 74 #endif // COMPONENTS_TRACING_CORE_SCATTERED_STREAM_WRITER_H_ | |
| OLD | NEW |