Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
dequeue_buffer.h
1 #ifndef UPTANE_DEQUEUE_BUFFER_H_
2 #define UPTANE_DEQUEUE_BUFFER_H_
3 
4 #include <array>
5 #include <cstddef>
6 
7 /**
8  * A dequeue based on a contiguous buffer in memory. Used for buffering
9  * data between recv() and ber_decode()
10  */
12  public:
13  /**
14  * A pointer to the first element that has not been Consumed().
15  */
16  char *Head();
17 
18  /**
19  * The number of elements that are valid (have been written) after Head()
20  */
21  size_t Size() const;
22 
23  /**
24  * Called after bytes have been read from Head(). Remove them from the head
25  * of the queue.
26  */
27  void Consume(size_t bytes);
28 
29  /**
30  * A pointer to the next place to write data to
31  */
32  char *Tail();
33 
34  /**
35  * The number of bytes beyond Tail() that are allocated and may be written to.
36  */
37  size_t TailSpace();
38 
39  /**
40  * Call to indicate that bytes have been written in the range
41  * Tail() ... Tail() + TailSpace().
42  */
43  void HaveEnqueued(size_t bytes);
44 
45  private:
46  static constexpr int kSentinel = 1337161494;
47 
48  /**
49  * buffer_[0..written_bytes_] contains to contents of this dequeue
50  */
51  size_t written_bytes_{0};
52  std::array<char, 4096> buffer_{}; // Zero initialise as a security pesimisation
53  // NOLINTNEXTLINE (should be just for clang-diagnostic-unused-private-field but it won't work)
54  int sentinel_{kSentinel}; // Sentinel to check for writers overflowing buffer_
55 };
56 
57 #endif // UPTANE_DEQUEUE_BUFFER_H_
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
A dequeue based on a contiguous buffer in memory.
Definition: dequeue_buffer.h:11
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