From d32e20bcaa8c9f4f24b9d5c3885204b6414fc923 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 16 Dec 2020 23:27:58 +0900 Subject: [PATCH] nghttpx: Make sure that Pool gets cleared when all buffers are returned --- src/memchunk.h | 6 +++++- src/shrpx_worker.cc | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/memchunk.h b/src/memchunk.h index f0e24b85..46116c19 100644 --- a/src/memchunk.h +++ b/src/memchunk.h @@ -72,7 +72,7 @@ template struct Memchunk { }; template struct Pool { - Pool() : pool(nullptr), freelist(nullptr), poolsize(0) {} + Pool() : pool(nullptr), freelist(nullptr), poolsize(0), freelistsize(0) {} ~Pool() { clear(); } T *get() { if (freelist) { @@ -80,6 +80,7 @@ template struct Pool { freelist = freelist->next; m->next = nullptr; m->reset(); + freelistsize -= T::size; return m; } @@ -90,9 +91,11 @@ template struct Pool { void recycle(T *m) { m->next = freelist; freelist = m; + freelistsize += T::size; } void clear() { freelist = nullptr; + freelistsize = 0; for (auto p = pool; p;) { auto knext = p->knext; delete p; @@ -105,6 +108,7 @@ template struct Pool { T *pool; T *freelist; size_t poolsize; + size_t freelistsize; }; template struct Memchunks { diff --git a/src/shrpx_worker.cc b/src/shrpx_worker.cc index cc0254d8..88f632b6 100644 --- a/src/shrpx_worker.cc +++ b/src/shrpx_worker.cc @@ -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) { return; } - worker->get_mcpool()->clear(); + auto mcpool = worker->get_mcpool(); + if (mcpool->freelistsize == mcpool->poolsize) { + worker->get_mcpool()->clear(); + } } } // namespace