blob: 1192905ecec815dfdbaa872ab00a7f45e27a7479 [file] [log] [blame]
Wyatt Hepler1927c282020-02-11 16:45:02 -08001// 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 Rogersb6b14b82020-09-11 16:27:27 -070017#include "pw_status/try.h"
Wyatt Hepler28904602020-03-09 08:45:51 -070018
Wyatt Hepler1927c282020-02-11 16:45:02 -080019namespace pw {
20
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070021StatusWithSize AlignedWriter::Write(std::span<const std::byte> data) {
Wyatt Hepler1927c282020-02-11 16:45:02 -080022 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 Rogersc4dc8642020-09-14 10:52:36 -070026 PW_TRY_WITH_SIZE(AddBytesToBuffer(to_copy));
Wyatt Hepler1927c282020-02-11 16:45:02 -080027 data = data.subspan(to_copy);
Wyatt Hepler1927c282020-02-11 16:45:02 -080028 }
29
David Rogers6592d292020-02-14 14:19:26 -080030 return StatusWithSize(bytes_written_);
Wyatt Hepler1927c282020-02-11 16:45:02 -080031}
32
33StatusWithSize AlignedWriter::Flush() {
Wyatt Hepler0f2ad9f2020-02-25 16:58:55 -080034 Status status;
35
Wyatt Hepler1927c282020-02-11 16:45:02 -080036 // 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 Hepler0f2ad9f2020-02-25 16:58:55 -080043 status = output_.Write(buffer_, remaining_bytes).status();
Wyatt Hepler1927c282020-02-11 16:45:02 -080044
45 bytes_written_ += remaining_bytes; // Include padding in the total.
Wyatt Hepler0f2ad9f2020-02-25 16:58:55 -080046 bytes_in_buffer_ = 0;
Wyatt Hepler1927c282020-02-11 16:45:02 -080047 }
48
Wyatt Hepler0f2ad9f2020-02-25 16:58:55 -080049 const StatusWithSize result(status, bytes_written_);
Wyatt Hepler1927c282020-02-11 16:45:02 -080050 bytes_written_ = 0;
Wyatt Hepler1927c282020-02-11 16:45:02 -080051 return result;
52}
53
Wyatt Hepler28904602020-03-09 08:45:51 -070054StatusWithSize 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 Rogersc4dc8642020-09-14 10:52:36 -070061 PW_TRY_WITH_SIZE(AddBytesToBuffer(to_read));
Wyatt Hepler28904602020-03-09 08:45:51 -070062 size -= result.size();
63 }
64
65 return StatusWithSize(bytes_written_);
66}
67
68StatusWithSize 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 Hepler1927c282020-02-11 16:45:02 -080088} // namespace pw