-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcirbuf.h
95 lines (80 loc) · 2.39 KB
/
cirbuf.h
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
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#ifndef CIRBUF_H
#define CIRBUF_H
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <cassert>
#include <algorithm>
#include <vector>
// Optimized circular buffer, works only with sizes power of two.
class CirBuf
{
#ifdef TESTING
friend class MainTests;
#endif
char *buf = NULL;
uint32_t head = 0;
uint32_t tail = 0;
uint32_t size = 0;
bool primedForSizeReset = false;
public:
CirBuf(const CirBuf &other) = delete;
CirBuf(CirBuf &&other) = delete;
CirBuf(size_t size);
~CirBuf();
CirBuf &operator=(const CirBuf &other) = delete;
CirBuf &operator=(CirBuf &&other) = delete;
uint32_t usedBytes() const;
uint32_t freeSpace() const;
uint32_t maxWriteSize() const;
uint32_t maxReadSize() const;
char *headPtr();
char *tailPtr();
void advanceHead(uint32_t n);
void advanceTail(uint32_t n);
char peakAhead(uint32_t offset) const;
void ensureFreeSpace(const size_t n, const size_t max = UINT_MAX);
void doubleSize(uint factor = 2);
uint32_t getSize() const;
void resetSizeIfEligable(size_t size);
void resetSize(size_t size);
void reset();
void write(uint8_t b);
void write(uint8_t b, uint8_t b2);
void write(const void *buf, size_t count);
std::vector<char> peekAllToVector();
std::vector<char> readToVector(const uint32_t max);
std::vector<char> readAllToVector();
/**
* Write the whole range into the buf, making space as needed.
*/
template<class InputIt>
void writerange(InputIt begin, InputIt end)
{
const auto input_size = end - begin;
ensureFreeSpace(input_size);
size_t len_left = input_size;
int guard = 0;
auto pos = begin;
while (len_left > 0 && guard++ < 10)
{
const size_t len = std::min<size_t>(len_left, maxWriteSize());
std::copy(pos, pos + len, headPtr());
advanceHead(len);
pos += len;
len_left -= len;
}
assert(len_left == 0);
assert(pos == end);
}
bool operator==(const CirBuf &other) const;
};
#endif // CIRBUF_H