| 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 BASE_TRACE_EVENT_V2_SCATTERED_BUFFER_H_ |
| 6 #define BASE_TRACE_EVENT_V2_SCATTERED_BUFFER_H_ |
| 7 |
| 8 #include <inttypes.h> |
| 9 |
| 10 #include <array> |
| 11 #include <memory> |
| 12 |
| 13 #include "base/base_export.h" |
| 14 #include "base/macros.h" |
| 15 |
| 16 namespace base { |
| 17 namespace trace_event { |
| 18 namespace v2 { |
| 19 |
| 20 // This class deals with the following problem: AppendOnlyProtoMessage wants |
| 21 // to write a stream of bytes, without caring about the implementation of the |
| 22 // underlying buffer, which concretely will be either the trace RingBuffer |
| 23 // or a heap-allocated buffer. The main deal is: AppendOnlyProtoMessage does not |
| 24 // know in advance what the size of the message will be. |
| 25 // Due to the tracing buffer being split into fixed-size chunks, in some |
| 26 // occasions these writes nees to be spread over two (or more) non contiguous |
| 27 // chunks of memory. Similarly, when the buffer is backed by the heap, we want |
| 28 // to avoid realloc() calls (which can require a memcpy of the full contents). |
| 29 // The purpose of this class is to abtract away the non-contiguous write logic. |
| 30 // This class knows how to deal with writes as long as they fall in the same |
| 31 // ContiguousMemoryRange, and defers the chunk-chaining logic to the Delegate. |
| 32 class BASE_EXPORT ScatteredBuffer { |
| 33 public: |
| 34 struct ContiguousMemoryRange { |
| 35 uint8_t* begin; |
| 36 uint8_t* end; // STL style: one byte past the end of the buffer. |
| 37 }; |
| 38 |
| 39 class Delegate { |
| 40 public: |
| 41 virtual ContiguousMemoryRange GetNewContiguousMemoryBuffer() = 0; |
| 42 }; |
| 43 |
| 44 ScatteredBuffer(Delegate* delegate); |
| 45 virtual ~ScatteredBuffer(); |
| 46 |
| 47 void Reset(ContiguousMemoryRange range); |
| 48 void WriteByte(uint8_t value); |
| 49 void WriteBytes(const uint8_t* src, size_t size); |
| 50 ContiguousMemoryRange ReserveBytes(size_t size); |
| 51 void WriteReservedBytes(const ContiguousMemoryRange& reserved_range, const uin
t8_t* src, size_t size); |
| 52 |
| 53 // Number of free bytes in the current range that can be written without |
| 54 // reuqesting a new contiguous buffer. |
| 55 size_t contiguous_bytes_available() const { return static_cast<size_t>(cur_ran
ge_.end - wrptr_); } |
| 56 |
| 57 // ContiguousMemoryRange cur_range() const { return cur_range_; } |
| 58 uint8_t* write_ptr() const { return wrptr_; } |
| 59 |
| 60 private: |
| 61 void Expand(); |
| 62 |
| 63 Delegate* delegate_; |
| 64 ContiguousMemoryRange cur_range_; |
| 65 uint8_t* wrptr_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(ScatteredBuffer); |
| 68 }; |
| 69 |
| 70 } // namespace v2 |
| 71 } // namespace trace_event |
| 72 } // namespace base |
| 73 |
| 74 #endif // BASE_TRACE_EVENT_V2_SCATTERED_BUFFER_H_ |
| OLD | NEW |