• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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 
15 #include "pw_bloat/bloat_this_binary.h"
16 #include "pw_ring_buffer/prefixed_entry_ring_buffer.h"
17 #include "pw_status/status.h"
18 
19 constexpr size_t kRingBufferSize = 1024;
20 constexpr std::byte kValue = (std::byte)0xFF;
21 constexpr std::byte kData[1] = {kValue};
22 
main()23 int main() {
24   pw::bloat::BloatThisBinary();
25 
26   pw::ring_buffer::PrefixedEntryRingBuffer ring(true /* user_preamble */);
27   std::byte buffer[kRingBufferSize];
28 
29   pw::Status status = ring.SetBuffer(buffer);
30   if (!status.ok()) {
31     return 1;
32   }
33 
34   // Push entries until the buffer is full.
35   size_t total_entries = 0;
36   while (true) {
37     status = ring.TryPushBack(kData);
38     if (status == pw::Status::ResourceExhausted()) {
39       break;
40     } else if (!status.ok()) {
41       return 2;
42     }
43     total_entries++;
44   }
45 
46   // Forcefully push an entry.
47   status = ring.PushBack(kData);
48   if (!status.ok()) {
49     return 3;
50   }
51 
52   // Dering the buffer.
53   status = ring.Dering();
54   if (!status.ok()) {
55     return 4;
56   }
57 
58   // Peek and pop all entries.
59   __attribute__((unused)) std::byte value[1];
60   __attribute__((unused)) size_t value_size;
61   for (size_t i = 0; i < total_entries; ++i) {
62     status = ring.PeekFront(value, &value_size);
63     if (!status.ok()) {
64       return 5;
65     }
66     status = ring.PeekFrontWithPreamble(value, &value_size);
67     if (!status.ok()) {
68       return 6;
69     }
70     if (ring.FrontEntryDataSizeBytes() == 0) {
71       return 7;
72     }
73     if (ring.FrontEntryTotalSizeBytes() == 0) {
74       return 8;
75     }
76     if (ring.EntryCount() == 0) {
77       return 9;
78     }
79     status = ring.PopFront();
80     if (!status.ok()) {
81       return 10;
82     }
83   }
84   ring.Clear();
85   return 0;
86 }
87