Index: components/tracing/core/scattered_stream_writer.h |
diff --git a/components/tracing/core/scattered_stream_writer.h b/components/tracing/core/scattered_stream_writer.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8c61f612674ef1a5df050cace35d1b90f26943a4 |
--- /dev/null |
+++ b/components/tracing/core/scattered_stream_writer.h |
@@ -0,0 +1,75 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_TRACING_CORE_SCATTERED_STREAM_WRITER_H_ |
+#define COMPONENTS_TRACING_CORE_SCATTERED_STREAM_WRITER_H_ |
+ |
+#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)
|
+ |
+#include "base/macros.h" |
+#include "components/tracing/tracing_export.h" |
+ |
+namespace tracing { |
+namespace v2 { |
+ |
+// This class deals with the following problem: append-only proto messages want |
+// to write a stream of bytes, without caring about the implementation of the |
+// underlying buffer (which concretely will be either the trace ring buffer |
+// or a heap-allocated buffer). The main deal is: proto messages don't know in |
+// advance what the size of the message will be. |
+// Due to the tracing buffer being split into fixed-size chunks, in some |
+// 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.
|
+// chunks of memory. Similarly, when the buffer is backed by the heap, we want |
+// to avoid realloc() calls, as they can cause an expensive memcpy to expand. |
+// The purpose of this class is to abtract away the non-contiguous write logic. |
+// This class knows how to deal with writes as long as they fall in the same |
+// ContiguousMemoryRange, and defers the chunk-chaining logic to the Delegate. |
+class TRACING_EXPORT ScatteredStreamWriter { |
+ public: |
+ struct ContiguousMemoryRange { |
+ uint8_t* begin; |
+ uint8_t* end; // STL style: one byte past the end of the buffer. |
+ }; |
+ |
+ class Delegate { |
+ public: |
+ virtual ContiguousMemoryRange GetNewContiguousMemoryBuffer() = 0; |
+ }; |
+ |
+ explicit ScatteredStreamWriter(Delegate* delegate); |
+ ~ScatteredStreamWriter(); |
+ |
+ void WriteByte(uint8_t value); |
+ void WriteBytes(const uint8_t* src, size_t size); |
+ |
+ // Reserves a fixed number of bytes, to be backfilled later through |
+ // WriteReservedBytes(). |
+ ContiguousMemoryRange ReserveBytes(size_t size); |
+ void WriteReservedBytes(const ContiguousMemoryRange& reserved_range, |
+ const uint8_t* src); |
+ |
+ void Reset(ContiguousMemoryRange range); |
+ |
+ // Number of free bytes in the current range that can be written without |
+ // 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.
|
+ size_t bytes_available() const { |
+ return static_cast<size_t>(cur_range_.end - write_ptr_); |
+ } |
+ |
+ uint8_t* write_ptr() const { return write_ptr_; } |
+ |
+ private: |
+ void Expand(); |
+ |
+ Delegate* const delegate_; |
+ ContiguousMemoryRange cur_range_; |
+ uint8_t* write_ptr_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ScatteredStreamWriter); |
+}; |
+ |
+} // namespace v2 |
+} // namespace tracing |
+ |
+#endif // COMPONENTS_TRACING_CORE_SCATTERED_STREAM_WRITER_H_ |