C/C++ API Reference
Loading...
Searching...
No Matches
trace_buffer.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17
18#include "pw_bytes/span.h"
21#include "pw_status/status.h"
22#include "pw_trace_tokenized/config.h"
23
24namespace pw {
25
27
28namespace trace {
29namespace internal {
30
31// Forward declaration.
32class TraceBufferImpl;
33
34} // namespace internal
35
41 public:
42 InlineVarLenEntryQueue<>& queue() { return queue_; }
43
54 constexpr Status PeekFront(ByteSpan data, size_t* bytes_read_out) const;
55
62 constexpr Status PopFront();
63
65 constexpr size_t EntryCount() const { return queue_.size(); }
66
72 constexpr Status CheckForCorruption() { return OkStatus(); }
73
75 constexpr void Clear() { queue_.clear(); }
76
78 constexpr size_t TotalUsedBytes() const {
79 return queue_.encoded_size_bytes();
80 }
81
82 private:
83 friend class ::pw::trace::internal::TraceBufferImpl;
84
85 constexpr explicit TraceBuffer(InlineVarLenEntryQueue<>& queue)
86 : queue_(queue) {}
87
89};
90
92void ClearBuffer();
93
95TraceBuffer* GetBuffer();
96
101ConstByteSpan DeringAndViewRawBuffer();
102
104// Constexpr method implementations
105
107 size_t* bytes_read_out) const {
108 if (queue_.empty()) {
109 return Status::OutOfRange();
110 }
111 auto entry = queue_.front();
112 size_t len = std::min(data.size(), entry.size());
113 pw::copy(
114 entry.begin(),
115 entry.begin() +
116 static_cast<
118 len),
119 data.begin());
120 *bytes_read_out = len;
121 return data.size() < entry.size() ? Status::ResourceExhausted() : OkStatus();
122}
123
125 if (queue_.empty()) {
126 return Status::OutOfRange();
127 }
128 queue_.pop();
129 return OkStatus();
130}
131
132} // namespace trace
133
135
136} // namespace pw
Definition: inline_var_len_entry_queue.h:94
Definition: status.h:120
static constexpr Status OutOfRange()
Definition: status.h:267
static constexpr Status ResourceExhausted()
Definition: status.h:230
Definition: trace_buffer.h:40
constexpr void Clear()
Removes all data from the ring buffer.
Definition: trace_buffer.h:75
constexpr size_t TotalUsedBytes() const
Get the size in bytes of all the current entries in the ring buffer.
Definition: trace_buffer.h:78
constexpr Status PopFront()
Definition: trace_buffer.h:124
constexpr Status CheckForCorruption()
Definition: trace_buffer.h:72
constexpr size_t EntryCount() const
Get the number of variable-length entries currently in the ring buffer.
Definition: trace_buffer.h:65
constexpr Status PeekFront(ByteSpan data, size_t *bytes_read_out) const
Definition: trace_buffer.h:106
constexpr OutputIt copy(InputIt first, InputIt last, OutputIt d_first)
constexpr backport of <algorithm>'s std::copy for C++17.
Definition: algorithm.h:348
constexpr Status OkStatus()
Definition: status.h:450
The Pigweed namespace.
Definition: alignment.h:27