From c5271d8115653da5c07025a7116cb300df2aeac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bargull?= Date: Fri, 13 Mar 2020 18:26:38 +0000 Subject: [PATCH] Bug 1622260: Replace POOLED_COLLECTION_PTR_METHODS macro with C++ code. r=mgaudet `acquireCollection` and `releaseCollection` act like an interface subclasses need to implement for `PooledCollectionPtr`. Differential Revision: https://phabricator.services.mozilla.com/D66762 --HG-- extra : moz-landing-system : lando --- js/src/frontend/NameCollections.h | 110 +++++++++++++++++++----------- 1 file changed, 71 insertions(+), 39 deletions(-) diff --git a/js/src/frontend/NameCollections.h b/js/src/frontend/NameCollections.h index d51761d946cd..f5eac161d47d 100644 --- a/js/src/frontend/NameCollections.h +++ b/js/src/frontend/NameCollections.h @@ -240,49 +240,83 @@ class NameCollectionPool { } }; -#define POOLED_COLLECTION_PTR_METHODS(N, T) \ - NameCollectionPool& pool_; \ - T* collection_; \ - \ - T& collection() { \ - MOZ_ASSERT(collection_); \ - return *collection_; \ - } \ - \ - const T& collection() const { \ - MOZ_ASSERT(collection_); \ - return *collection_; \ - } \ - \ - public: \ - explicit N(NameCollectionPool& pool) : pool_(pool), collection_(nullptr) {} \ - \ - ~N() { pool_.release##T(&collection_); } \ - \ - bool acquire(JSContext* cx) { \ - MOZ_ASSERT(!collection_); \ - collection_ = pool_.acquire##T(cx); \ - return !!collection_; \ - } \ - \ - explicit operator bool() const { return !!collection_; } \ - \ - T* operator->() { return &collection(); } \ - \ - const T* operator->() const { return &collection(); } \ - \ - T& operator*() { return collection(); } \ - \ +template typename Impl> +class PooledCollectionPtr { + NameCollectionPool& pool_; + T* collection_ = nullptr; + + protected: + ~PooledCollectionPtr() { Impl::releaseCollection(pool_, &collection_); } + + T& collection() { + MOZ_ASSERT(collection_); + return *collection_; + } + + const T& collection() const { + MOZ_ASSERT(collection_); + return *collection_; + } + + public: + explicit PooledCollectionPtr(NameCollectionPool& pool) : pool_(pool) {} + + bool acquire(JSContext* cx) { + MOZ_ASSERT(!collection_); + collection_ = Impl::acquireCollection(cx, pool_); + return !!collection_; + } + + explicit operator bool() const { return !!collection_; } + + T* operator->() { return &collection(); } + + const T* operator->() const { return &collection(); } + + T& operator*() { return collection(); } + const T& operator*() const { return collection(); } +}; template -class PooledMapPtr { - POOLED_COLLECTION_PTR_METHODS(PooledMapPtr, Map) +class PooledMapPtr : public PooledCollectionPtr { + friend class PooledCollectionPtr; + + static Map* acquireCollection(JSContext* cx, NameCollectionPool& pool) { + return pool.acquireMap(cx); + } + + static void releaseCollection(NameCollectionPool& pool, Map** ptr) { + pool.releaseMap(ptr); + } + + using Base = PooledCollectionPtr; + + public: + using Base::Base; + + ~PooledMapPtr() = default; }; template -class PooledVectorPtr { - POOLED_COLLECTION_PTR_METHODS(PooledVectorPtr, Vector) +class PooledVectorPtr : public PooledCollectionPtr { + friend class PooledCollectionPtr; + + static Vector* acquireCollection(JSContext* cx, NameCollectionPool& pool) { + return pool.acquireVector(cx); + } + + static void releaseCollection(NameCollectionPool& pool, Vector** ptr) { + pool.releaseVector(ptr); + } + + using Base = PooledCollectionPtr; + using Base::collection; + + public: + using Base::Base; + + ~PooledVectorPtr() = default; typename Vector::ElementType& operator[](size_t index) { return collection()[index]; @@ -293,8 +327,6 @@ class PooledVectorPtr { } }; -#undef POOLED_COLLECTION_PTR_METHODS - } // namespace frontend } // namespace js