From 5868cddcf2ffdffeee7d7132ef4f4afc71dde15e Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Mon, 2 Oct 2023 13:54:50 +0200 Subject: [PATCH] Stylistic improvements for `MutexPool`. (#2556) --- src/nrncvode/pool.h | 61 +++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/src/nrncvode/pool.h b/src/nrncvode/pool.h index 5b2f7f5cae..c062e93ad3 100644 --- a/src/nrncvode/pool.h +++ b/src/nrncvode/pool.h @@ -47,9 +47,8 @@ MutexPool::MutexPool(long count, int mkmut) { pool_ = new T[count_]; pool_size_ = count; items_ = new T*[count_]; - { - for (long i = 0; i < count_; ++i) - items_[i] = pool_ + i; + for (long i = 0; i < count_; ++i) { + items_[i] = pool_ + i; } MUTCONSTRUCT(mkmut) } @@ -62,22 +61,15 @@ void MutexPool::grow() { chain_ = p; long newcnt = 2 * count_; T** itms = new T*[newcnt]; - long i, j; put_ += count_; - { - for (i = 0; i < get_; ++i) { - itms[i] = items_[i]; - } + for (long i = 0; i < get_; ++i) { + itms[i] = items_[i]; } - { - for (i = get_, j = 0; j < count_; ++i, ++j) { - itms[i] = p->items_[j]; - } + for (long i = get_, j = 0; j < count_; ++i, ++j) { + itms[i] = p->items_[j]; } - { - for (i = put_, j = get_; j < count_; ++i, ++j) { - itms[i] = items_[j]; - } + for (long i = put_, j = get_; j < count_; ++i, ++j) { + itms[i] = items_[j]; } delete[] items_; delete[] p->items_; @@ -88,35 +80,24 @@ void MutexPool::grow() { template MutexPool::~MutexPool() { - { - if (chain_) { - delete chain_; - } - } + delete chain_; delete[] pool_; - { - if (items_) { - delete[] items_; - } - } + delete[] items_; MUTDESTRUCT } template T* MutexPool::alloc() { - MUTLOCK { - if (nget_ >= count_) { - grow(); - } + MUTLOCK + if (nget_ >= count_) { + grow(); } T* item = items_[get_]; get_ = (get_ + 1) % count_; ++nget_; - { - if (nget_ > maxget_) { - maxget_ = nget_; - } - } + + maxget_ = std::max(nget_, maxget_); + MUTUNLOCK return item; } @@ -139,12 +120,10 @@ void MutexPool::free_all() { nget_ = 0; get_ = 0; put_ = 0; - { - for (pp = this; pp; pp = pp->chain_) { - for (i = 0; i < pp->pool_size_; ++i) { - items_[put_++] = pp->pool_ + i; - pp->pool_[i].clear(); - } + for (pp = this; pp; pp = pp->chain_) { + for (i = 0; i < pp->pool_size_; ++i) { + items_[put_++] = pp->pool_ + i; + pp->pool_[i].clear(); } } assert(put_ == count_);