aboutsummaryrefslogtreecommitdiff
path: root/pw_ring_buffer/size_report/ring_buffer_multi.cc
blob: c4e9232d15fefa899fd71ef89a6aaca4c8b7ce77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_bloat/bloat_this_binary.h"
#include "pw_ring_buffer/prefixed_entry_ring_buffer.h"
#include "pw_status/status.h"

constexpr size_t kRingBufferSize = 1024;
constexpr size_t kReaderCount = 4;
constexpr std::byte kValue = (std::byte)0xFF;
constexpr std::byte kData[1] = {kValue};

int main() {
  pw::bloat::BloatThisBinary();

  pw::ring_buffer::PrefixedEntryRingBufferMulti ring(true /* user_preamble */);
  std::byte buffer[kRingBufferSize];

  pw::Status status = ring.SetBuffer(buffer);
  if (!status.ok()) {
    return 1;
  }

  // Attach readers.
  pw::ring_buffer::PrefixedEntryRingBufferMulti::Reader readers[kReaderCount];
  for (auto& reader : readers) {
    ring.AttachReader(reader)
        .IgnoreError();  // TODO(b/242598609): Handle Status properly
  }

  // Push entries until the buffer is full.
  size_t total_entries = 0;
  while (true) {
    status = ring.TryPushBack(kData);
    if (status == pw::Status::ResourceExhausted()) {
      break;
    } else if (!status.ok()) {
      return 2;
    }
    total_entries++;
  }

  // Forcefully push an entry.
  status = ring.PushBack(kData);
  if (!status.ok()) {
    return 3;
  }

  // Dering the buffer.
  status = ring.Dering();
  if (!status.ok()) {
    return 4;
  }

  // Peek and pop all entries.
  __attribute__((unused)) std::byte value[1];
  __attribute__((unused)) size_t value_size;
  for (size_t i = 0; i < total_entries; ++i) {
    for (auto& reader : readers) {
      status = reader.PeekFront(value, &value_size);
      if (!status.ok()) {
        return 5;
      }
      status = reader.PeekFrontWithPreamble(value, &value_size);
      if (!status.ok()) {
        return 6;
      }
      if (reader.FrontEntryDataSizeBytes() == 0) {
        return 7;
      }
      if (reader.FrontEntryTotalSizeBytes() == 0) {
        return 8;
      }
      if (reader.EntryCount() == 0) {
        return 9;
      }
      status = reader.PopFront();
      if (!status.ok()) {
        return 10;
      }
    }
  }

  for (auto& reader : readers) {
    ring.DetachReader(reader)
        .IgnoreError();  // TODO(b/242598609): Handle Status properly
  }
  ring.Clear();
  return 0;
}