SHARE
TWEET

Untitled

a guest Jul 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef IG_SHARED_OBJECT_POOL_H_
  2. #define IG_SHARED_OBJECT_POOL_H_
  3.  
  4. #include <cstdlib>
  5. #include <queue>
  6. #include <vector>
  7. #include <list>
  8. #include <memory>
  9.  
  10. namespace ig {
  11.  
  12. static const size_t kObjectPoolDefaultSize = 100;
  13. static const size_t kObjectPoolDefaultExtendSize = 10;
  14.  
  15. template <typename ObjectType>
  16. struct ObjectPoolDefaultInitializer {
  17.   void operator()(ObjectType *object) const {
  18.   }
  19. };
  20.  
  21. template <typename ObjectType, size_t N = kObjectPoolDefaultSize,
  22.     typename Initializer = ObjectPoolDefaultInitializer<ObjectType> >
  23. class SharedObjectPool {
  24.  public:
  25.   static SharedObjectPool& Instance() {
  26.     static SharedObjectPool pool(N);
  27.     return pool;
  28.   }
  29.  
  30.   std::shared_ptr<ObjectType> Get() {
  31.     ObjectType *ptr = nullptr;
  32.     if (queue_.empty()) {
  33.       Extend(kObjectPoolDefaultExtendSize);
  34.     }
  35.     ptr = queue_.front();
  36.     queue_.pop();
  37.     kInitializer(ptr);
  38.     return std::shared_ptr<ObjectType>(ptr, [&](ObjectType *p) {
  39.       queue_.push(p);
  40.     });
  41.   }
  42.  
  43.   int capacity() const {
  44.     return capacity_;
  45.   }
  46.  
  47.   void set_capacity(const size_t capacity) {
  48.     if (capacity_ < capacity) {
  49.       Extend(capacity - capacity_);
  50.     }
  51.   }
  52.  
  53.  private:
  54.   explicit SharedObjectPool(const size_t pool_size) :
  55.       kDefaultCacheSize(pool_size) {
  56.     cache_ = new ObjectType[kDefaultCacheSize];
  57.     for (size_t i = 0; i < kDefaultCacheSize; ++i) {
  58.       queue_.push(&cache_[i]);
  59.       kInitializer(&cache_[i]);
  60.     }
  61.     capacity_ = kDefaultCacheSize;
  62.   }
  63.  
  64.   ~SharedObjectPool() {
  65.     if (cache_) {
  66.       delete[] cache_;
  67.       cache_ = nullptr;
  68.     }
  69.     for (auto &ptr : extended_cache_) {
  70.       delete ptr;
  71.     }
  72.     extended_cache_.clear();
  73.   }
  74.  
  75.   void Extend(const size_t num) {
  76.     for (size_t i = 0; i < num; ++i) {
  77.       ObjectType *ptr = new ObjectType;
  78.       extended_cache_.push_back(ptr);
  79.       queue_.push(ptr);
  80.       kInitializer(ptr);
  81.     }
  82.     capacity_ = kDefaultCacheSize + extended_cache_.size();
  83.   }
  84.  
  85.  private:
  86.   size_t capacity_ = 0;
  87.  
  88.   std::queue<ObjectType*> queue_;
  89.  
  90.   ObjectType *cache_ = nullptr;
  91.  
  92.   std::list<ObjectType*> extended_cache_;
  93.  
  94.   const size_t kDefaultCacheSize;
  95.  
  96.   static const Initializer kInitializer;
  97. };
  98.  
  99. template <typename ObjectType, size_t N, typename Initializer>
  100. const Initializer
  101.     SharedObjectPool<ObjectType, N, Initializer>::kInitializer;
  102.  
  103. }  // namespace ig
  104.  
  105. #endif  // IG_SHARED_OBJECT_POOL_H_
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top