Coverage Report

Created: 2024-07-09 06:09

/proc/self/cwd/pw_stream/memory_stream.cc
Line
Count
Source (jump to first uncovered line)
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
15
#include "pw_stream/memory_stream.h"
16
17
#include <cstddef>
18
#include <cstring>
19
20
#include "pw_status/status_with_size.h"
21
22
namespace pw::stream {
23
24
272k
Status MemoryWriter::DoWrite(ConstByteSpan data) {
25
272k
  if (ConservativeWriteLimit() == 0) {
26
10
    return Status::OutOfRange();
27
10
  }
28
272k
  if (ConservativeWriteLimit() < data.size_bytes()) {
29
0
    return Status::ResourceExhausted();
30
0
  }
31
32
272k
  size_t bytes_to_write = data.size_bytes();
33
272k
  if (bytes_to_write == 0) {
34
    // Calling memmove with a null pointer is undefined behavior, even when zero
35
    // bytes are moved. We must return early here to avoid performing such a
36
    // call when data is an empty span.
37
13.8k
    return OkStatus();
38
13.8k
  }
39
258k
  std::memmove(dest_.data() + position_, data.data(), bytes_to_write);
40
258k
  position_ += bytes_to_write;
41
42
258k
  return OkStatus();
43
272k
}
44
45
49.6k
StatusWithSize MemoryReader::DoRead(ByteSpan dest) {
46
49.6k
  if (source_.size_bytes() == position_) {
47
460
    return StatusWithSize::OutOfRange();
48
460
  }
49
50
49.2k
  size_t bytes_to_read =
51
49.2k
      std::min(dest.size_bytes(), source_.size_bytes() - position_);
52
49.2k
  if (bytes_to_read == 0) {
53
    // Calling memcpy with a null pointer is undefined behavior, even when zero
54
    // bytes are copied. We must return early here to avoid performing such a
55
    // call when the dest span is empty.
56
2.49k
    return StatusWithSize(0);
57
2.49k
  }
58
59
46.7k
  std::memcpy(dest.data(), source_.data() + position_, bytes_to_read);
60
46.7k
  position_ += bytes_to_read;
61
62
46.7k
  return StatusWithSize(bytes_to_read);
63
49.2k
}
64
65
}  // namespace pw::stream