Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1794)

Unified Diff: base/trace_event/v2/scattered_buffer.h

Issue 1947373002: Tracing V2 prototype [NOT FOR REVIEW] Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: WORKS Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/trace_event/v2/ring_buffer.cc ('k') | base/trace_event/v2/scattered_buffer.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/trace_event/v2/scattered_buffer.h
diff --git a/base/trace_event/v2/scattered_buffer.h b/base/trace_event/v2/scattered_buffer.h
new file mode 100644
index 0000000000000000000000000000000000000000..5a06f49ba876ac5eaa6b283fababd0cb80afb985
--- /dev/null
+++ b/base/trace_event/v2/scattered_buffer.h
@@ -0,0 +1,74 @@
+// 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 BASE_TRACE_EVENT_V2_SCATTERED_BUFFER_H_
+#define BASE_TRACE_EVENT_V2_SCATTERED_BUFFER_H_
+
+#include <inttypes.h>
+
+#include <array>
+#include <memory>
+
+#include "base/base_export.h"
+#include "base/macros.h"
+
+namespace base {
+namespace trace_event {
+namespace v2 {
+
+// This class deals with the following problem: AppendOnlyProtoMessage wants
+// to write a stream of bytes, without caring about the implementation of the
+// underlying buffer, which concretely will be either the trace RingBuffer
+// or a heap-allocated buffer. The main deal is: AppendOnlyProtoMessage does not
+// 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
+// chunks of memory. Similarly, when the buffer is backed by the heap, we want
+// to avoid realloc() calls (which can require a memcpy of the full contents).
+// 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 BASE_EXPORT ScatteredBuffer {
+ 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;
+ };
+
+ ScatteredBuffer(Delegate* delegate);
+ virtual ~ScatteredBuffer();
+
+ void Reset(ContiguousMemoryRange range);
+ void WriteByte(uint8_t value);
+ void WriteBytes(const uint8_t* src, size_t size);
+ ContiguousMemoryRange ReserveBytes(size_t size);
+ void WriteReservedBytes(const ContiguousMemoryRange& reserved_range, const uint8_t* src, size_t size);
+
+ // Number of free bytes in the current range that can be written without
+ // reuqesting a new contiguous buffer.
+ size_t contiguous_bytes_available() const { return static_cast<size_t>(cur_range_.end - wrptr_); }
+
+ // ContiguousMemoryRange cur_range() const { return cur_range_; }
+ uint8_t* write_ptr() const { return wrptr_; }
+
+ private:
+ void Expand();
+
+ Delegate* delegate_;
+ ContiguousMemoryRange cur_range_;
+ uint8_t* wrptr_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScatteredBuffer);
+};
+
+} // namespace v2
+} // namespace trace_event
+} // namespace base
+
+#endif // BASE_TRACE_EVENT_V2_SCATTERED_BUFFER_H_
« no previous file with comments | « base/trace_event/v2/ring_buffer.cc ('k') | base/trace_event/v2/scattered_buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698