1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
//===-- runtime/buffer.h ----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// External file buffering
#ifndef FORTRAN_RUNTIME_BUFFER_H_
#define FORTRAN_RUNTIME_BUFFER_H_
#include "io-error.h"
#include "memory.h"
#include <algorithm>
#include <cinttypes>
#include <cstring>
namespace Fortran::runtime::io {
void LeftShiftBufferCircularly(char *, std::size_t bytes, std::size_t shift);
// Maintains a view of a contiguous region of a file in a memory buffer.
// The valid data in the buffer may be circular, but any active frame
// will also be contiguous in memory. The requirement stems from the need to
// preserve read data that may be reused by means of Tn/TLn edit descriptors
// without needing to position the file (which may not always be possible,
// e.g. a socket) and a general desire to reduce system call counts.
template <typename STORE> class FileFrame {
public:
using FileOffset = std::int64_t;
~FileFrame() { FreeMemoryAndNullify(buffer_); }
// The valid data in the buffer begins at buffer_[start_] and proceeds
// with possible wrap-around for length_ bytes. The current frame
// is offset by frame_ bytes into that region and is guaranteed to
// be contiguous for at least as many bytes as were requested.
FileOffset FrameAt() const { return fileOffset_ + frame_; }
char *Frame() const { return buffer_ + start_ + frame_; }
std::size_t FrameLength() const {
return std::min<std::size_t>(length_ - frame_, size_ - (start_ + frame_));
}
std::size_t BytesBufferedBeforeFrame() const { return frame_ - start_; }
// Returns a short frame at a non-fatal EOF. Can return a long frame as well.
std::size_t ReadFrame(
FileOffset at, std::size_t bytes, IoErrorHandler &handler) {
Flush(handler);
Reallocate(bytes, handler);
if (at < fileOffset_ || at > fileOffset_ + length_) {
Reset(at);
}
frame_ = at - fileOffset_;
if (static_cast<std::int64_t>(start_ + frame_ + bytes) > size_) {
DiscardLeadingBytes(frame_, handler);
if (static_cast<std::int64_t>(start_ + bytes) > size_) {
// Frame would wrap around; shift current data (if any) to force
// contiguity.
RUNTIME_CHECK(handler, length_ < size_);
if (start_ + length_ <= size_) {
// [......abcde..] -> [abcde........]
std::memmove(buffer_, buffer_ + start_, length_);
} else {
// [cde........ab] -> [abcde........]
auto n{start_ + length_ - size_}; // 3 for cde
RUNTIME_CHECK(handler, length_ >= n);
std::memmove(buffer_ + n, buffer_ + start_, length_ - n); // cdeab
LeftShiftBufferCircularly(buffer_, length_, n); // abcde
}
start_ = 0;
}
}
while (FrameLength() < bytes) {
auto next{start_ + length_};
RUNTIME_CHECK(handler, next < size_);
auto minBytes{bytes - FrameLength()};
auto maxBytes{size_ - next};
auto got{Store().Read(
fileOffset_ + length_, buffer_ + next, minBytes, maxBytes, handler)};
length_ += got;
RUNTIME_CHECK(handler, length_ < size_);
if (got < minBytes) {
break; // error or EOF & program can handle it
}
}
return FrameLength();
}
void WriteFrame(FileOffset at, std::size_t bytes, IoErrorHandler &handler) {
if (!dirty_ || at < fileOffset_ || at > fileOffset_ + length_ ||
start_ + (at - fileOffset_) + static_cast<std::int64_t>(bytes) >
size_) {
Flush(handler);
fileOffset_ = at;
Reallocate(bytes, handler);
}
dirty_ = true;
frame_ = at - fileOffset_;
length_ = std::max<std::int64_t>(length_, frame_ + bytes);
}
void Flush(IoErrorHandler &handler) {
if (dirty_) {
while (length_ > 0) {
std::size_t chunk{std::min<std::size_t>(length_, size_ - start_)};
std::size_t put{
Store().Write(fileOffset_, buffer_ + start_, chunk, handler)};
length_ -= put;
start_ += put;
fileOffset_ += put;
if (put < chunk) {
break;
}
}
Reset(fileOffset_);
}
}
private:
STORE &Store() { return static_cast<STORE &>(*this); }
void Reallocate(std::int64_t bytes, const Terminator &terminator) {
if (bytes > size_) {
char *old{buffer_};
auto oldSize{size_};
size_ = std::max<std::int64_t>(bytes, minBuffer);
buffer_ =
reinterpret_cast<char *>(AllocateMemoryOrCrash(terminator, size_));
auto chunk{std::min<std::int64_t>(length_, oldSize - start_)};
std::memcpy(buffer_, old + start_, chunk);
start_ = 0;
std::memcpy(buffer_ + chunk, old, length_ - chunk);
FreeMemory(old);
}
}
void Reset(FileOffset at) {
start_ = length_ = frame_ = 0;
fileOffset_ = at;
dirty_ = false;
}
void DiscardLeadingBytes(std::int64_t n, const Terminator &terminator) {
RUNTIME_CHECK(terminator, length_ >= n);
length_ -= n;
if (length_ == 0) {
start_ = 0;
} else {
start_ += n;
if (start_ >= size_) {
start_ -= size_;
}
}
if (frame_ >= n) {
frame_ -= n;
} else {
frame_ = 0;
}
fileOffset_ += n;
}
static constexpr std::size_t minBuffer{64 << 10};
char *buffer_{nullptr};
std::int64_t size_{0}; // current allocated buffer size
FileOffset fileOffset_{0}; // file offset corresponding to buffer valid data
std::int64_t start_{0}; // buffer_[] offset of valid data
std::int64_t length_{0}; // valid data length (can wrap)
std::int64_t frame_{0}; // offset of current frame in valid data
bool dirty_{false};
};
} // namespace Fortran::runtime::io
#endif // FORTRAN_RUNTIME_BUFFER_H_
|