Skip to content

Commit

Permalink
Merge pull request nghttp2#1544 from nghttp2/nghttpx-clear-mcpool
Browse files Browse the repository at this point in the history
nghttpx: Make sure that Pool gets cleared when all buffers are returned
  • Loading branch information
tatsuhiro-t authored Dec 16, 2020
2 parents 1c04ca8 + d32e20b commit 563c117
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/memchunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ template <size_t N> struct Memchunk {
};

template <typename T> struct Pool {
Pool() : pool(nullptr), freelist(nullptr), poolsize(0) {}
Pool() : pool(nullptr), freelist(nullptr), poolsize(0), freelistsize(0) {}
~Pool() { clear(); }
T *get() {
if (freelist) {
auto m = freelist;
freelist = freelist->next;
m->next = nullptr;
m->reset();
freelistsize -= T::size;
return m;
}

Expand All @@ -90,9 +91,11 @@ template <typename T> 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;
Expand All @@ -105,6 +108,7 @@ template <typename T> struct Pool {
T *pool;
T *freelist;
size_t poolsize;
size_t freelistsize;
};

template <typename Memchunk> struct Memchunks {
Expand Down
5 changes: 4 additions & 1 deletion src/shrpx_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 563c117

Please sign in to comment.