Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [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_kvs/alignment.h" |
| 16 | |
David Rogers | b6b14b8 | 2020-09-11 16:27:27 -0700 | [diff] [blame] | 17 | #include "pw_status/try.h" |
Wyatt Hepler | 2890460 | 2020-03-09 08:45:51 -0700 | [diff] [blame] | 18 | |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 19 | namespace pw { |
| 20 | |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 21 | StatusWithSize AlignedWriter::Write(std::span<const std::byte> data) { |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 22 | while (!data.empty()) { |
| 23 | size_t to_copy = std::min(write_size_ - bytes_in_buffer_, data.size()); |
| 24 | |
| 25 | std::memcpy(&buffer_[bytes_in_buffer_], data.data(), to_copy); |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 26 | PW_TRY_WITH_SIZE(AddBytesToBuffer(to_copy)); |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 27 | data = data.subspan(to_copy); |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 28 | } |
| 29 | |
David Rogers | 6592d29 | 2020-02-14 14:19:26 -0800 | [diff] [blame] | 30 | return StatusWithSize(bytes_written_); |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | StatusWithSize AlignedWriter::Flush() { |
Wyatt Hepler | 0f2ad9f | 2020-02-25 16:58:55 -0800 | [diff] [blame] | 34 | Status status; |
| 35 | |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 36 | // If data remains in the buffer, pad it to the alignment size and flush the |
| 37 | // remaining data. |
| 38 | if (bytes_in_buffer_ != 0u) { |
| 39 | const size_t remaining_bytes = AlignUp(bytes_in_buffer_, alignment_bytes_); |
| 40 | std::memset(&buffer_[bytes_in_buffer_], |
| 41 | int(kPadByte), |
| 42 | remaining_bytes - bytes_in_buffer_); |
Wyatt Hepler | 0f2ad9f | 2020-02-25 16:58:55 -0800 | [diff] [blame] | 43 | status = output_.Write(buffer_, remaining_bytes).status(); |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 44 | |
| 45 | bytes_written_ += remaining_bytes; // Include padding in the total. |
Wyatt Hepler | 0f2ad9f | 2020-02-25 16:58:55 -0800 | [diff] [blame] | 46 | bytes_in_buffer_ = 0; |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Wyatt Hepler | 0f2ad9f | 2020-02-25 16:58:55 -0800 | [diff] [blame] | 49 | const StatusWithSize result(status, bytes_written_); |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 50 | bytes_written_ = 0; |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 51 | return result; |
| 52 | } |
| 53 | |
Wyatt Hepler | 2890460 | 2020-03-09 08:45:51 -0700 | [diff] [blame] | 54 | StatusWithSize AlignedWriter::Write(Input& input, size_t size) { |
| 55 | while (size > 0u) { |
| 56 | const size_t to_read = std::min(write_size_ - bytes_in_buffer_, size); |
| 57 | StatusWithSize result = input.Read(buffer_ + bytes_in_buffer_, to_read); |
| 58 | if (!result.ok()) { |
| 59 | return StatusWithSize(result.status(), bytes_written_); |
| 60 | } |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 61 | PW_TRY_WITH_SIZE(AddBytesToBuffer(to_read)); |
Wyatt Hepler | 2890460 | 2020-03-09 08:45:51 -0700 | [diff] [blame] | 62 | size -= result.size(); |
| 63 | } |
| 64 | |
| 65 | return StatusWithSize(bytes_written_); |
| 66 | } |
| 67 | |
| 68 | StatusWithSize AlignedWriter::AddBytesToBuffer(size_t bytes_added) { |
| 69 | bytes_in_buffer_ += bytes_added; |
| 70 | |
| 71 | // If the buffer is full, write it out. |
| 72 | if (bytes_in_buffer_ == write_size_) { |
| 73 | StatusWithSize result = output_.Write(buffer_, write_size_); |
| 74 | |
| 75 | // Always use write_size_ for the bytes written. If there was an error |
| 76 | // assume the space was written or at least disturbed. |
| 77 | bytes_written_ += write_size_; |
| 78 | bytes_in_buffer_ = 0; |
| 79 | |
| 80 | if (!result.ok()) { |
| 81 | return StatusWithSize(result.status(), bytes_written_); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return StatusWithSize(bytes_written_); |
| 86 | } |
| 87 | |
Wyatt Hepler | 1927c28 | 2020-02-11 16:45:02 -0800 | [diff] [blame] | 88 | } // namespace pw |