Skip to content

Commit

Permalink
Support variable arguments for ResourcePool and ObjectPool
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Jan 2, 2025
1 parent 5c03414 commit 73902a7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 52 deletions.
19 changes: 4 additions & 15 deletions src/butil/object_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 6 additions & 11 deletions src/butil/object_pool_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
// 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; \
Expand All @@ -164,7 +164,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
/* 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; \
Expand All @@ -176,7 +176,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
_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; \
Expand All @@ -191,14 +191,9 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
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
Expand Down
19 changes: 4 additions & 15 deletions src/butil/resource_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 6 additions & 11 deletions src/butil/resource_pool_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
// 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]; \
Expand All @@ -187,7 +187,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
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; \
Expand All @@ -200,7 +200,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
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; \
Expand All @@ -215,14 +215,9 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
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
Expand Down

0 comments on commit 73902a7

Please sign in to comment.