| 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_
|
|
|