Skip to content

Commit

Permalink
Code format - (Clang-format)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jan 11, 2025
1 parent 6668241 commit 12c4f73
Showing 1 changed file with 60 additions and 60 deletions.
120 changes: 60 additions & 60 deletions src/utils/object_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,72 +26,72 @@
template <typename T>
class ObjectPool {
public:
/**
* @brief Alias for the smart pointer type used by the pool.
*/
using Ptr = std::shared_ptr<T>;
/**
* @brief Alias for the smart pointer type used by the pool.
*/
using Ptr = std::shared_ptr<T>;

/**
* @brief Acquires an object from the pool or creates a new one if the pool is empty.
*
* The object is constructed in place using the provided arguments.
* The `std::shared_ptr` includes a custom deleter that returns the object
* to the pool when it is no longer needed.
*
* @tparam Args The types of the arguments used to construct the object.
* @param args The arguments forwarded to the constructor of the object.
* @return A `std::shared_ptr` managing the allocated object.
*/
template <typename... Args>
static Ptr acquireObject(Args&&... args) {
std::lock_guard<std::mutex> lock(mutex_);
if (!pool_.empty()) {
T* obj = pool_.back();
pool_.pop_back();
/**
* @brief Acquires an object from the pool or creates a new one if the pool is empty.
*
* The object is constructed in place using the provided arguments.
* The `std::shared_ptr` includes a custom deleter that returns the object
* to the pool when it is no longer needed.
*
* @tparam Args The types of the arguments used to construct the object.
* @param args The arguments forwarded to the constructor of the object.
* @return A `std::shared_ptr` managing the allocated object.
*/
template <typename... Args>
static Ptr acquireObject(Args &&... args) {
std::lock_guard<std::mutex> lock(mutex_);
if (!pool_.empty()) {
T* obj = pool_.back();
pool_.pop_back();

// Construct the object in place with new arguments
new (obj) T(std::forward<Args>(args)...);
return Ptr(obj, [](T* ptr) {
ptr->~T(); // Destroy the object
releaseObject(ptr); // Return to the pool
});
}
// Construct the object in place with new arguments
new (obj) T(std::forward<Args>(args)...);
return Ptr(obj, [](T* ptr) {
ptr->~T(); // Destroy the object
releaseObject(ptr); // Return to the pool
});
}

// Allocate a new object if the pool is empty
T* rawPtr = allocator_.allocate(1);
new (rawPtr) T(std::forward<Args>(args)...);
return Ptr(rawPtr, [](T* ptr) {
ptr->~T(); // Destroy the object
releaseObject(ptr); // Return to the pool
});
}
// Allocate a new object if the pool is empty
T* rawPtr = allocator_.allocate(1);
new (rawPtr) T(std::forward<Args>(args)...);
return Ptr(rawPtr, [](T* ptr) {
ptr->~T(); // Destroy the object
releaseObject(ptr); // Return to the pool
});
}

private:
/**
* @brief Returns an object to the pool for future reuse.
*
* This method is called automatically by the custom deleter when the
* `std::shared_ptr` is destroyed.
*
* @param obj The object to be returned to the pool.
*/
static void releaseObject(T* obj) {
std::lock_guard<std::mutex> lock(mutex_);
pool_.push_back(obj);
}
/**
* @brief Returns an object to the pool for future reuse.
*
* This method is called automatically by the custom deleter when the
* `std::shared_ptr` is destroyed.
*
* @param obj The object to be returned to the pool.
*/
static void releaseObject(T* obj) {
std::lock_guard<std::mutex> lock(mutex_);
pool_.push_back(obj);
}

/**
* @brief The internal pool of objects available for reuse.
*/
static inline std::vector<T*> pool_;
/**
* @brief The internal pool of objects available for reuse.
*/
static inline std::vector<T*> pool_;

/**
* @brief The allocator instance used to allocate and deallocate objects.
*/
static inline std::allocator<T> allocator_;
/**
* @brief The allocator instance used to allocate and deallocate objects.
*/
static inline std::allocator<T> allocator_;

/**
* @brief A mutex to ensure thread safety when accessing the pool.
*/
static inline std::mutex mutex_;
/**
* @brief A mutex to ensure thread safety when accessing the pool.
*/
static inline std::mutex mutex_;
};

0 comments on commit 12c4f73

Please sign in to comment.