aboutsummaryrefslogtreecommitdiff
path: root/src/base/io/BufferStream.cpp
blob: fdcd25ffb45052e21b5f8e7d8297584e3e7a2ae3 (plain)
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
#include "cru/base/io/BufferStream.h"
#include "cru/base/io/Stream.h"

namespace cru::io {
BufferStream::BufferStream(const BufferStreamOptions& options)
    : Stream(false, true, true) {
  block_size_ = options.GetBlockSizeOrDefault();
  max_block_count_ = options.GetMaxBlockCount();
  eof_written_ = false;
}

BufferStream::~BufferStream() {}

Index BufferStream::DoRead(std::byte* buffer, Index offset, Index size) {
  std::unique_lock lock(mutex_);

  read_cv_.wait(lock, [this] {
    return eof_written_ || !buffer_list_.empty() || IsClosed();
  });

  CheckClosed();

  if (buffer_list_.empty() && eof_written_) {
    return kEOF;
  }

  auto full_previously = max_block_count_ > 0 &&
                         buffer_list_.size() == max_block_count_ &&
                         buffer_list_.back().IsFull();

  Index read = 0;

  while (!buffer_list_.empty()) {
    auto& stream_buffer = buffer_list_.front();
    auto this_read = stream_buffer.Read(buffer + offset + read, size - read);
    if (stream_buffer.IsEmpty()) {
      buffer_list_.pop_front();
    }
    read += this_read;
    if (read == size) {
      break;
    }
  }

  if (full_previously && buffer_list_.size() < max_block_count_) {
    write_cv_.notify_all();
  }

  return read;
}

Index BufferStream::DoWrite(const std::byte* buffer, Index offset, Index size) {
  std::unique_lock lock(mutex_);

  write_cv_.wait(lock, [this] {
    return eof_written_ || max_block_count_ <= 0 ||
           buffer_list_.size() < max_block_count_ ||
           !buffer_list_.back().IsFull() || IsClosed();
  });

  CheckClosed();

  if (eof_written_) {
    throw StreamIOException(
        this, "Stream has been set eof. Can't write to it any more.");
  }

  auto empty_previously = buffer_list_.empty();

  Index written = 0;

  if (empty_previously) {
    buffer_list_.push_back(Block(block_size_));
  }

  while (true) {
    if (buffer_list_.back().IsFull()) {
      if (max_block_count_ > 0 && buffer_list_.size() == max_block_count_) {
        break;
      }
      buffer_list_.push_back(Block(block_size_));
    }
    auto& stream_buffer = buffer_list_.back();
    auto this_written =
        stream_buffer.Write(buffer + offset + written, size - written);
    written += this_written;
    if (written == size) {
      break;
    }
  }

  if (empty_previously) {
    read_cv_.notify_all();
  }

  return written;
}

void BufferStream::DoClose() {
  std::unique_lock lock(mutex_);

  buffer_list_.clear();
  read_cv_.notify_all();
  write_cv_.notify_all();
}

void BufferStream::WriteEof() {
  std::unique_lock lock(mutex_);

  eof_written_ = true;
  read_cv_.notify_all();
  write_cv_.notify_all();
}

BufferStream::Block::Block(Index size)
    : buffer(new std::byte[size]), size(size), start(0), end(0) {}

BufferStream::Block::Block(Block&& other) noexcept
    : buffer(other.buffer),
      size(other.size),
      start(other.start),
      end(other.end) {
  other.buffer = nullptr;
  other.size = other.start = other.end = 0;
}

BufferStream::Block& BufferStream::Block::operator=(Block&& other) noexcept {
  if (this != &other) {
    delete[] buffer;
    buffer = other.buffer;
    size = other.size;
    start = other.start;
    end = other.end;
    other.buffer = nullptr;
    other.size = other.start = other.end = 0;
  }
  return *this;
}

BufferStream::Block::~Block() { delete[] buffer; }

Index BufferStream::Block::Read(std::byte* des, Index si) {
  si = std::min(si, end - start);
  std::memcpy(des, buffer + start, si);
  start += si;
  return si;
}

Index BufferStream::Block::Write(const std::byte* src, Index si) {
  si = std::min(si, size - end);
  std::memcpy(buffer + end, src, si);
  end += si;
  return si;
}

bool BufferStream::Block::IsFull() const { return end == size; }

bool BufferStream::Block::IsEmpty() const { return end == start; }

}  // namespace cru::io