nghttpx: Make sure that Pool gets cleared when all buffers are returned

This commit is contained in:
Tatsuhiro Tsujikawa 2020-12-16 23:27:58 +09:00
parent 8b8ba6b0a6
commit d32e20bcaa
2 changed files with 9 additions and 2 deletions

View File

@ -72,7 +72,7 @@ template <size_t N> struct Memchunk {
}; };
template <typename T> struct Pool { template <typename T> struct Pool {
Pool() : pool(nullptr), freelist(nullptr), poolsize(0) {} Pool() : pool(nullptr), freelist(nullptr), poolsize(0), freelistsize(0) {}
~Pool() { clear(); } ~Pool() { clear(); }
T *get() { T *get() {
if (freelist) { if (freelist) {
@ -80,6 +80,7 @@ template <typename T> struct Pool {
freelist = freelist->next; freelist = freelist->next;
m->next = nullptr; m->next = nullptr;
m->reset(); m->reset();
freelistsize -= T::size;
return m; return m;
} }
@ -90,9 +91,11 @@ template <typename T> struct Pool {
void recycle(T *m) { void recycle(T *m) {
m->next = freelist; m->next = freelist;
freelist = m; freelist = m;
freelistsize += T::size;
} }
void clear() { void clear() {
freelist = nullptr; freelist = nullptr;
freelistsize = 0;
for (auto p = pool; p;) { for (auto p = pool; p;) {
auto knext = p->knext; auto knext = p->knext;
delete p; delete p;
@ -105,6 +108,7 @@ template <typename T> struct Pool {
T *pool; T *pool;
T *freelist; T *freelist;
size_t poolsize; size_t poolsize;
size_t freelistsize;
}; };
template <typename Memchunk> struct Memchunks { template <typename Memchunk> struct Memchunks {

View File

@ -57,7 +57,10 @@ void mcpool_clear_cb(struct ev_loop *loop, ev_timer *w, int revents) {
if (worker->get_worker_stat()->num_connections != 0) { if (worker->get_worker_stat()->num_connections != 0) {
return; return;
} }
worker->get_mcpool()->clear(); auto mcpool = worker->get_mcpool();
if (mcpool->freelistsize == mcpool->poolsize) {
worker->get_mcpool()->clear();
}
} }
} // namespace } // namespace