summaryrefslogtreecommitdiff
path: root/fastbuffer.hpp
blob: d6ac8d53d0be160a930dccf52f061a910fac9aeb (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
#ifndef FASTBUFFER_H
#define FASTBUFFER_H 1

#include <boost/asio/buffer.hpp>
#include <boost/format.hpp>
#include <boost/noncopyable.hpp>
#include <exception>
#include <string>

class BufferException : public std::exception {
public:
  explicit BufferException(const char *what) : m_what(what) {}
  explicit BufferException(const std::string &what) : m_what(what) {}
  virtual ~BufferException() noexcept {}
  virtual const char *what() const noexcept { return m_what.c_str(); }

private:
  std::string m_what;
};

class BufferBase {
public:
  explicit BufferBase(std::size_t size)
      : m_bufferOffset{0}, m_bufferUsed{0}, m_bufferSize{size} {
    m_buffer = new std::uint8_t[size];
  }
  BufferBase(BufferBase &&moveable) {
    m_bufferOffset = std::move(moveable.m_bufferOffset);
    m_bufferUsed = std::move(moveable.m_bufferUsed);
    m_bufferSize = std::move(moveable.m_bufferSize);
    m_buffer = std::move(moveable.m_buffer);
    moveable.m_buffer = nullptr;
    moveable.m_bufferOffset = moveable.m_bufferUsed = moveable.m_bufferSize = 0;
  }
  ~BufferBase() { delete[] m_buffer; }
  void operator+=(std::size_t commit_size) {
    checkFreeSpace(commit_size);
    m_bufferUsed += commit_size;
  }
  void operator+=(const std::initializer_list<uint8_t> &to_add) {
    checkFreeSpace(to_add.size());
    std::copy(to_add.begin(), to_add.end(), &m_buffer[m_bufferUsed]);
    m_bufferUsed += to_add.size();
  }
  void operator-=(std::size_t consume_size) {
    const auto unconsumed_space = unconsumed();
    checkConsumableSpace(consume_size);
    if (consume_size == unconsumed_space) {
      m_bufferUsed = m_bufferOffset = 0;
      return;
    }
    m_bufferOffset += consume_size;
  }
  auto operator+() {
    return boost::asio::buffer(&m_buffer[m_bufferUsed], unused());
  }
  auto operator-() {
    return boost::asio::buffer(&m_buffer[m_bufferOffset], m_bufferUsed);
  }
  auto operator[](std::size_t index) const { return m_buffer[index]; }
  const auto *operator()(std::size_t index) const { return &m_buffer[index]; }
  auto *operator()() { return &m_buffer[m_bufferUsed]; }
  auto size() const { return m_bufferUsed; }
  auto capacity() const { return m_bufferSize; }
  std::size_t unused() const { return m_bufferSize - m_bufferUsed; }
  std::size_t unconsumed() const { return m_bufferUsed - m_bufferOffset; }
  void checkFreeSpace(std::size_t commit_size) const {
    const auto free_space = unused();
    if (commit_size > free_space)
      throw BufferException(
          (boost::format(
               "Buffer overflow: %1% bytes free, %1% bytes required") %
           free_space % commit_size)
              .str());
  }
  void checkConsumableSpace(std::size_t consume_size) const {
    const auto unconsumed_space = unconsumed();
    if (consume_size > unconsumed_space)
      throw BufferException(
          (boost::format(
               "Buffer underflow: %1% bytes used, %1% bytes consumed") %
           unconsumed_space % consume_size)
              .str());
  };
  auto getHealth() const {
    return (static_cast<float>(m_bufferUsed) /
            static_cast<float>(m_bufferSize));
  }
  auto getConsumeHealth() const {
    return (static_cast<float>(unconsumed()) /
            static_cast<float>(m_bufferSize));
  }

private:
  std::size_t m_bufferOffset;
  std::size_t m_bufferUsed;
  std::size_t m_bufferSize;
  std::uint8_t *m_buffer;
};

using ContiguousStreamBuffer = BufferBase;

class ContiguousPacketQueue : public boost::noncopyable {
public:
  struct Element {
    std::size_t size;
  };
  explicit ContiguousPacketQueue(std::size_t max_packets,
                                 std::size_t max_queue_size)
      : m_buffer(max_queue_size), m_packetsOffset{0}, m_packetsUsed{0},
        m_packetsSize{max_packets} {
    m_packets = new Element[max_packets];
  }
  ContiguousPacketQueue(ContiguousPacketQueue &&moveable)
      : m_buffer(std::move(moveable.m_buffer)) {
    m_packetsOffset = std::move(moveable.m_packetsOffset);
    m_packetsUsed = std::move(moveable.m_packetsUsed);
    m_packetsSize = std::move(moveable.m_packetsSize);
    m_packets = std::move(moveable.m_packets);
    moveable.m_packets = nullptr;
    moveable.m_packetsOffset = moveable.m_packetsUsed = moveable.m_packetsSize =
        0;
  }
  ~ContiguousPacketQueue() { delete[] m_packets; }
  void operator+=(std::size_t commit_size) {
    m_packets[m_packetsUsed++].size = commit_size;
    m_buffer += commit_size;
  }
  void operator+=(const std::initializer_list<uint8_t> &to_add) {
    m_packets[m_packetsUsed++].size = to_add.size();
    m_buffer += to_add;
  }
  void operator--() {
    const auto consume_size = m_packets[m_packetsOffset].size;
    m_buffer -= consume_size;
    if (++m_packetsOffset == m_packetsUsed) {
      m_packetsOffset = m_packetsUsed = 0;
      return;
    }
  }
  auto operator+() { return +m_buffer; }
  auto operator-() { return -m_buffer; }
  auto size() const { return m_packetsUsed; }
  auto capacity() const { return m_packetsSize; }
  std::size_t unused() const { return m_packetsSize - m_packetsUsed; }
  std::size_t unconsumed() const { return m_packetsUsed - m_packetsOffset; }
  const auto *GetBuffer() const { return &m_buffer; }

private:
  BufferBase m_buffer;
  std::size_t m_packetsOffset;
  std::size_t m_packetsUsed;
  std::size_t m_packetsSize;
  Element *m_packets;
};

#endif