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 their size will be. |
| 26 // Due to the tracing buffer being split into fixed-size chunks, on some |
| 27 // occasions, these writes need to be spread over two (or more) non-contiguous |
| 28 // chunks of memory. Similarly, when the buffer is backed by the heap, we want |
| 29 // to avoid realloc() calls, as they might cause a full copy of the contents |
| 30 // of the buffer. |
| 31 // The purpose of this class is to abtract away the non-contiguous write logic. |
| 32 // This class knows how to deal with writes as long as they fall in the same |
| 33 // ContiguousMemoryRange and defers the chunk-chaining logic to the Delegate. |
| 34 class TRACING_EXPORT ScatteredStreamWriter { |
| 35 public: |
| 36 class Delegate { |
| 37 public: |
| 38 virtual ContiguousMemoryRange GetNewBuffer() = 0; |
| 39 }; |
| 40 |
| 41 explicit ScatteredStreamWriter(Delegate* delegate); |
| 42 ~ScatteredStreamWriter(); |
| 43 |
| 44 void WriteByte(uint8_t value); |
| 45 void WriteBytes(const uint8_t* src, size_t size); |
| 46 |
| 47 // Reserves a fixed amount of bytes to be backfilled later. The reserved range |
| 48 // is guaranteed to be contiguous and not span across chunks. |
| 49 ContiguousMemoryRange ReserveBytes(size_t size); |
| 50 |
| 51 // Resets the buffer boundaries and the write pointer to the given |range|. |
| 52 // Subsequent WriteByte(s) will write into |range|. |
| 53 void Reset(ContiguousMemoryRange range); |
| 54 |
| 55 // Number of contiguous free bytes in |cur_range_| that can be written without |
| 56 // requesting a new buffer. |
| 57 size_t bytes_available() const { |
| 58 return static_cast<size_t>(cur_range_.end - write_ptr_); |
| 59 } |
| 60 |
| 61 uint8_t* write_ptr() const { return write_ptr_; } |
| 62 |
| 63 private: |
| 64 void Extend(); |
| 65 |
| 66 Delegate* const delegate_; |
| 67 ContiguousMemoryRange cur_range_; |
| 68 uint8_t* write_ptr_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(ScatteredStreamWriter); |
| 71 }; |
| 72 |
| 73 } // namespace v2 |
| 74 } // namespace tracing |
| 75 |
| 76 #endif // COMPONENTS_TRACING_CORE_SCATTERED_STREAM_WRITER_H_ |
OLD | NEW |