This is an automated email from the ASF dual-hosted git repository. wwbmmm pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brpc.git
The following commit(s) were added to refs/heads/master by this push: new 4158a17f Support variable arguments for ResourcePool and ObjectPool (#2859) 4158a17f is described below commit 4158a17f0ea4368d5136ea364e901bc85d447f68 Author: Bright Chen <chenguangmin...@foxmail.com> AuthorDate: Thu Jan 2 22:13:21 2025 +0800 Support variable arguments for ResourcePool and ObjectPool (#2859) --- src/butil/object_pool.h | 19 ++++--------------- src/butil/object_pool_inl.h | 17 ++++++----------- src/butil/resource_pool.h | 19 ++++--------------- src/butil/resource_pool_inl.h | 17 ++++++----------- 4 files changed, 20 insertions(+), 52 deletions(-) diff --git a/src/butil/object_pool.h b/src/butil/object_pool.h index c8690821..5bb312b9 100644 --- a/src/butil/object_pool.h +++ b/src/butil/object_pool.h @@ -75,21 +75,10 @@ template <typename T> inline bool local_pool_free_empty() { } // Get an object typed |T|. The object should be cleared before usage. -// NOTE: T must be default-constructible. -template <typename T> inline T* get_object() { - return ObjectPool<T>::singleton()->get_object(); -} - -// Get an object whose constructor is T(arg1) -template <typename T, typename A1> -inline T* get_object(const A1& arg1) { - return ObjectPool<T>::singleton()->get_object(arg1); -} - -// Get an object whose constructor is T(arg1, arg2) -template <typename T, typename A1, typename A2> -inline T* get_object(const A1& arg1, const A2& arg2) { - return ObjectPool<T>::singleton()->get_object(arg1, arg2); +// NOTE: If there are no arguments, T must be default-constructible. +template <typename T, typename... Args> +inline T* get_object(Args... args) { + return ObjectPool<T>::singleton()->get_object(std::forward<Args>(args)...); } // Return the object |ptr| back. The object is NOT destructed and will be diff --git a/src/butil/object_pool_inl.h b/src/butil/object_pool_inl.h index 0055239d..ab8d88d1 100644 --- a/src/butil/object_pool_inl.h +++ b/src/butil/object_pool_inl.h @@ -147,7 +147,7 @@ public: // which may include parenthesis because when T is POD, "new T()" // and "new T" are different: former one sets all fields to 0 which // we don't want. -#define BAIDU_OBJECT_POOL_GET(...) \ +#define BAIDU_OBJECT_POOL_GET(CTOR_ARGS) \ /* Fetch local free ptr */ \ if (_cur_free.nfree) { \ BAIDU_OBJECT_POOL_FREE_ITEM_NUM_SUB1; \ @@ -164,7 +164,7 @@ public: /* Fetch memory from local block */ \ if (_cur_block && _cur_block->nitem < BLOCK_NITEM) { \ auto item = _cur_block->items + _cur_block->nitem; \ - obj = new (item->void_data()) T(__VA_ARGS__); \ + obj = new (item->void_data()) T CTOR_ARGS; \ if (!ObjectPoolValidator<T>::validate(obj)) { \ obj->~T(); \ return NULL; \ @@ -176,7 +176,7 @@ public: _cur_block = add_block(&_cur_block_index); \ if (_cur_block != NULL) { \ auto item = _cur_block->items + _cur_block->nitem; \ - obj = new (item->void_data()) T(__VA_ARGS__); \ + obj = new (item->void_data()) T CTOR_ARGS; \ if (!ObjectPoolValidator<T>::validate(obj)) { \ obj->~T(); \ return NULL; \ @@ -191,14 +191,9 @@ public: BAIDU_OBJECT_POOL_GET(); } - template <typename A1> - inline T* get(const A1& a1) { - BAIDU_OBJECT_POOL_GET(a1); - } - - template <typename A1, typename A2> - inline T* get(const A1& a1, const A2& a2) { - BAIDU_OBJECT_POOL_GET(a1, a2); + template<typename... Args> + inline T* get(Args... args) { + BAIDU_OBJECT_POOL_GET((std::forward<Args>(args)...)); } #undef BAIDU_OBJECT_POOL_GET diff --git a/src/butil/resource_pool.h b/src/butil/resource_pool.h index cc0fb000..84da8052 100644 --- a/src/butil/resource_pool.h +++ b/src/butil/resource_pool.h @@ -92,21 +92,10 @@ namespace butil { // Get an object typed |T| and write its identifier into |id|. // The object should be cleared before usage. -// NOTE: T must be default-constructible. -template <typename T> inline T* get_resource(ResourceId<T>* id) { - return ResourcePool<T>::singleton()->get_resource(id); -} - -// Get an object whose constructor is T(arg1) -template <typename T, typename A1> -inline T* get_resource(ResourceId<T>* id, const A1& arg1) { - return ResourcePool<T>::singleton()->get_resource(id, arg1); -} - -// Get an object whose constructor is T(arg1, arg2) -template <typename T, typename A1, typename A2> -inline T* get_resource(ResourceId<T>* id, const A1& arg1, const A2& arg2) { - return ResourcePool<T>::singleton()->get_resource(id, arg1, arg2); +// NOTE: If there are no arguments, T must be default-constructible. +template <typename T, typename... Args> +inline T* get_resource(ResourceId<T>* id, Args... args) { + return ResourcePool<T>::singleton()->get_resource(id, std::forward<Args>(args)...); } // Return the object associated with identifier |id| back. The object is NOT diff --git a/src/butil/resource_pool_inl.h b/src/butil/resource_pool_inl.h index 316e37f3..115bceda 100644 --- a/src/butil/resource_pool_inl.h +++ b/src/butil/resource_pool_inl.h @@ -164,7 +164,7 @@ public: // which may include parenthesis because when T is POD, "new T()" // and "new T" are different: former one sets all fields to 0 which // we don't want. -#define BAIDU_RESOURCE_POOL_GET(...) \ +#define BAIDU_RESOURCE_POOL_GET(CTOR_ARGS) \ /* Fetch local free id */ \ if (_cur_free.nfree) { \ const ResourceId<T> free_id = _cur_free.ids[--_cur_free.nfree]; \ @@ -187,7 +187,7 @@ public: if (_cur_block && _cur_block->nitem < BLOCK_NITEM) { \ id->value = _cur_block_index * BLOCK_NITEM + _cur_block->nitem; \ auto item = _cur_block->items + _cur_block->nitem; \ - p = new (item->void_data()) T(__VA_ARGS__); \ + p = new (item->void_data()) T CTOR_ARGS; \ if (!ResourcePoolValidator<T>::validate(p)) { \ p->~T(); \ return NULL; \ @@ -200,7 +200,7 @@ public: if (_cur_block != NULL) { \ id->value = _cur_block_index * BLOCK_NITEM + _cur_block->nitem; \ auto item = _cur_block->items + _cur_block->nitem; \ - p = new (item->void_data()) T(__VA_ARGS__); \ + p = new (item->void_data()) T CTOR_ARGS; \ if (!ResourcePoolValidator<T>::validate(p)) { \ p->~T(); \ return NULL; \ @@ -215,14 +215,9 @@ public: BAIDU_RESOURCE_POOL_GET(); } - template <typename A1> - inline T* get(ResourceId<T>* id, const A1& a1) { - BAIDU_RESOURCE_POOL_GET(a1); - } - - template <typename A1, typename A2> - inline T* get(ResourceId<T>* id, const A1& a1, const A2& a2) { - BAIDU_RESOURCE_POOL_GET(a1, a2); + template<typename... Args> + inline T* get(ResourceId<T>* id, Args... args) { + BAIDU_RESOURCE_POOL_GET((std::forward<Args>(args)...)); } #undef BAIDU_RESOURCE_POOL_GET --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org