Armando Montanez | 0bcae73 | 2020-05-27 13:28:11 -0700 | [diff] [blame] | 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 | |
David Rogers | 6d4f630 | 2020-07-22 14:27:41 -0700 | [diff] [blame] | 24 | Status MemoryWriter::DoWrite(ConstByteSpan data) { |
| 25 | if (ConservativeWriteLimit() == 0) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 26 | return Status::OutOfRange(); |
David Rogers | 6d4f630 | 2020-07-22 14:27:41 -0700 | [diff] [blame] | 27 | } |
| 28 | if (ConservativeWriteLimit() < data.size_bytes()) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 29 | return Status::ResourceExhausted(); |
David Rogers | 6d4f630 | 2020-07-22 14:27:41 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | size_t bytes_to_write = data.size_bytes(); |
Ted Pudlik | c2a4cfc | 2021-11-12 22:18:07 +0000 | [diff] [blame] | 33 | if (bytes_to_write == 0) { |
Ted Pudlik | 081499c | 2021-11-15 21:08:43 +0000 | [diff] [blame] | 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. |
Ted Pudlik | c2a4cfc | 2021-11-12 22:18:07 +0000 | [diff] [blame] | 37 | return OkStatus(); |
| 38 | } |
Wyatt Hepler | 7b62e93 | 2021-08-10 23:57:10 -0700 | [diff] [blame] | 39 | std::memmove(dest_.data() + position_, data.data(), bytes_to_write); |
| 40 | position_ += bytes_to_write; |
Armando Montanez | 0bcae73 | 2020-05-27 13:28:11 -0700 | [diff] [blame] | 41 | |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 42 | return OkStatus(); |
Armando Montanez | 0bcae73 | 2020-05-27 13:28:11 -0700 | [diff] [blame] | 43 | } |
| 44 | |
David Rogers | 6d4f630 | 2020-07-22 14:27:41 -0700 | [diff] [blame] | 45 | StatusWithSize MemoryReader::DoRead(ByteSpan dest) { |
Wyatt Hepler | 7b62e93 | 2021-08-10 23:57:10 -0700 | [diff] [blame] | 46 | if (source_.size_bytes() == position_) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 47 | return StatusWithSize::OutOfRange(); |
David Rogers | 6d4f630 | 2020-07-22 14:27:41 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | size_t bytes_to_read = |
Wyatt Hepler | 7b62e93 | 2021-08-10 23:57:10 -0700 | [diff] [blame] | 51 | std::min(dest.size_bytes(), source_.size_bytes() - position_); |
Ted Pudlik | 081499c | 2021-11-15 21:08:43 +0000 | [diff] [blame] | 52 | 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 | return StatusWithSize(0); |
| 57 | } |
David Rogers | 6d4f630 | 2020-07-22 14:27:41 -0700 | [diff] [blame] | 58 | |
Wyatt Hepler | 7b62e93 | 2021-08-10 23:57:10 -0700 | [diff] [blame] | 59 | std::memcpy(dest.data(), source_.data() + position_, bytes_to_read); |
| 60 | position_ += bytes_to_read; |
David Rogers | 6d4f630 | 2020-07-22 14:27:41 -0700 | [diff] [blame] | 61 | |
| 62 | return StatusWithSize(bytes_to_read); |
| 63 | } |
| 64 | |
| 65 | } // namespace pw::stream |