-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils.cpp
171 lines (154 loc) · 3.86 KB
/
utils.cpp
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
161
162
163
164
165
166
167
168
169
170
171
#include "utils.h"
void Buffers::push_back(char *buf) {
bufs_.insert(buf);
auto s1 = bufs_.size();
auto s2 = all_bufs_.size();
// info("bufs: %ld, all_bufs: %ld", s1, s2);
if (s1 * 4 > s2 * 3 && s2 > 16) {
std::vector<char *> v;
auto it = 0;
for (auto buf : bufs_) {
if (++it > s1 / 2) {
break;
}
v.push_back(buf);
}
for (auto buf : v) {
bufs_.erase(buf);
all_bufs_.erase(buf);
free(buf);
}
}
}
char *Buffers::get() {
for (auto buf : bufs_) {
bufs_.erase(buf);
return buf;
}
char *buf = static_cast<char *>(malloc(n));
all_bufs_.insert(buf);
return buf;
}
static std::unique_ptr<std::unordered_map<std::string, int *>> kvars;
static std::unique_ptr<std::unordered_map<std::string, int>> kvarsRef;
kvar::kvar(const std::string &name) {
if (!kvars) {
kvars = my_make_unique<std::unordered_map<std::string, int *>>();
}
if (!kvarsRef) {
kvarsRef = my_make_unique<std::unordered_map<std::string, int>>();
}
auto it = kvars->find(name);
if (it == kvars->end()) {
p = new int(0);
kvars->insert(std::make_pair(name, p));
kvarsRef->insert(std::make_pair(name, 1));
} else {
p = (*kvars)[name];
(*kvarsRef)[name]++;
}
name_ = name;
}
kvar::~kvar() {}
void printKvars() {
if (!kvars) {
return;
}
std::stringstream log_stream;
for (auto &kvar : *kvars) {
auto name = kvar.first;
if (kvar.second == nullptr) {
continue;
}
auto value = *(kvar.second);
log_stream << name << ":" << value << "\t";
}
LOG(INFO) << log_stream.str();
}
void run_kvar_printer(asio::io_service &service) {
auto timer = std::make_shared<asio::high_resolution_timer>(service, std::chrono::seconds(1));
timer->async_wait([&service, timer](const std::error_code &ec) {
if (ec) {
return;
}
printKvars();
run_kvar_printer(service);
});
}
static Buffers buffersCache(4096);
buffer::buffer() {
init();
}
buffer::buffer(buffer &&other) noexcept {
buf = other.buf;
off = other.off;
len = other.len;
cap = other.cap;
other.buf = nullptr;
other.off = 0;
other.len = 0;
other.cap = 0;
}
buffer::~buffer() {
if (buf == nullptr) {
return;
}
buffersCache.push_back(buf);
}
void buffer::init() {
if (buf != nullptr) {
buffersCache.push_back(buf);
}
buf = buffersCache.get();
cap = 4096;
off = 0;
len = 0;
}
LinearBuffer::LinearBuffer() {}
LinearBuffer::~LinearBuffer() {}
std::size_t LinearBuffer::size() {
auto bufsSize = bufs_.size();
if (bufsSize == 0) {
return 0;
} else if (bufsSize == 1) {
return bufs_.front().size();
} else {
std::size_t n = 0;
n = bufs_.front().size();
n += bufs_.back().size();
n += (bufsSize - 2) * 4096;
return n;
}
}
void LinearBuffer::append(char *buf, std::size_t len) {
if (len == 0 || buf == nullptr) {
return;
}
if (bufs_.empty() || bufs_.back().aval() == 0) {
bufs_.emplace_back(buffer());
}
while (len > 0) {
auto aval = bufs_.back().aval();
if (aval >= len) {
bufs_.back().append(buf, len);
return;
}
bufs_.back().append(buf, aval);
len -= aval;
buf += aval;
bufs_.emplace_back(buffer());
}
}
void LinearBuffer::retrieve(char *buf, std::size_t len) {
while (len > 0 && !bufs_.empty()) {
auto sz = bufs_.front().size();
if (sz > len) {
bufs_.front().retrieve(buf, len);
return;
}
bufs_.front().retrieve(buf, sz);
len -= sz;
buf += sz;
bufs_.pop_front();
}
}