Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
dequeue_buffer.cc
1 #include "utilities/dequeue_buffer.h"
2 
3 #include <cassert>
4 #include <cstring>
5 #include <stdexcept>
7  assert(sentinel_ == kSentinel);
8  return buffer_.data();
9 }
10 
11 size_t DequeueBuffer::Size() const {
12  assert(sentinel_ == kSentinel);
13  return written_bytes_;
14 }
15 
16 void DequeueBuffer::Consume(size_t bytes) {
17  assert(sentinel_ == kSentinel);
18  // It would be possible to have a smarter algorithm here that
19  // only shuffles bytes down when Tail() is called and we are getting
20  // close to the end of the buffer, or when the buffer is nearly empty
21  // the memmove operation is cheap. This isn't performance critical code.
22  if (written_bytes_ < bytes) {
23  throw std::logic_error("Attempt to DequeueBuffer::Consume() more bytes than are valid");
24  }
25  // Shuffle up the buffer
26  auto* next_unconsumed_byte = buffer_.begin() + bytes;
27  auto* end_of_written_area = buffer_.begin() + written_bytes_;
28  std::copy(next_unconsumed_byte, end_of_written_area, buffer_.begin());
29  written_bytes_ -= bytes;
30 }
31 
33  assert(sentinel_ == kSentinel);
34  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
35  return buffer_.data() + written_bytes_;
36 }
37 
39  assert(sentinel_ == kSentinel);
40  return buffer_.size() - written_bytes_;
41 }
42 
43 void DequeueBuffer::HaveEnqueued(size_t bytes) {
44  // Assert first. If we have corrupted memory, there is
45  // no amount of exception handling that will save us. Abort rather
46  // than risk a security hole
47  assert(sentinel_ == kSentinel);
48  if (buffer_.size() < written_bytes_ + bytes) {
49  throw std::logic_error("Wrote bytes beyond the end of the buffer");
50  }
51  written_bytes_ += bytes;
52 }
DequeueBuffer::Tail
char * Tail()
A pointer to the next place to write data to.
Definition: dequeue_buffer.cc:32
DequeueBuffer::Consume
void Consume(size_t bytes)
Called after bytes have been read from Head().
Definition: dequeue_buffer.cc:16
DequeueBuffer::Size
size_t Size() const
The number of elements that are valid (have been written) after Head()
Definition: dequeue_buffer.cc:11
DequeueBuffer::TailSpace
size_t TailSpace()
The number of bytes beyond Tail() that are allocated and may be written to.
Definition: dequeue_buffer.cc:38
DequeueBuffer::Head
char * Head()
A pointer to the first element that has not been Consumed().
Definition: dequeue_buffer.cc:6
DequeueBuffer::HaveEnqueued
void HaveEnqueued(size_t bytes)
Call to indicate that bytes have been written in the range Tail() ...
Definition: dequeue_buffer.cc:43