| 1378 | FileOutputBuffer::~FileOutputBuffer() { delete file_; } |
| 1379 | |
| 1380 | Status FileOutputBuffer::Append(StringPiece data) { |
| 1381 | // In the below, it is critical to calculate the checksum on the actually |
| 1382 | // copied bytes, not the source bytes. This is because "data" typically |
| 1383 | // points to tensor buffers, which may be concurrently written. |
| 1384 | if (data.size() + position_ <= buffer_size_) { |
| 1385 | // Can fit into the current buffer. |
| 1386 | memcpy(&buffer_[position_], data.data(), data.size()); |
| 1387 | crc32c_ = crc32c::Extend(crc32c_, &buffer_[position_], data.size()); |
| 1388 | } else if (data.size() <= buffer_size_) { |
| 1389 | // Cannot fit, but can fit after flushing. |
| 1390 | TF_RETURN_IF_ERROR(FlushBuffer()); |
| 1391 | memcpy(&buffer_[0], data.data(), data.size()); |
| 1392 | crc32c_ = crc32c::Extend(crc32c_, &buffer_[0], data.size()); |
| 1393 | } else { |
| 1394 | // Cannot fit even after flushing. So we break down "data" by chunk, and |
| 1395 | // flush/checksum each chunk. |
| 1396 | TF_RETURN_IF_ERROR(FlushBuffer()); |
| 1397 | for (size_t i = 0; i < data.size(); i += buffer_size_) { |
| 1398 | const size_t nbytes = std::min(data.size() - i, buffer_size_); |
| 1399 | memcpy(&buffer_[0], data.data() + i, nbytes); |
| 1400 | crc32c_ = crc32c::Extend(crc32c_, &buffer_[0], nbytes); |
| 1401 | position_ = nbytes; |
| 1402 | TF_RETURN_IF_ERROR(FlushBuffer()); |
| 1403 | } |
| 1404 | return Status::OK(); |
| 1405 | } |
| 1406 | position_ += data.size(); |
| 1407 | return Status::OK(); |
| 1408 | } |
| 1409 | |
| 1410 | Status FileOutputBuffer::AppendSegment(StringPiece data) { |
| 1411 | TF_RETURN_IF_ERROR(FlushBuffer()); |
no test coverage detected