My Project
PoolBase.h
1 #pragma once
2 #include "util/mutex.h"
3 #include <boost/pool/object_pool.hpp>
4 
5 namespace ParaEngine
6 {
7  // forward declare
8  template <typename UserAllocator = boost::default_user_allocator_new_delete, typename Mutex=ParaEngine::mutex>
10 
17  template <typename UserAllocator, typename Mutex>
18  class PoolThreadSafe : protected boost::pool<UserAllocator>
19  {
20  public:
21  typedef typename UserAllocator::size_type size_type;
22  typedef typename UserAllocator::difference_type difference_type;
23  typedef Mutex mutex_type;
24  typedef typename boost::pool<UserAllocator> pool_type;
25 
26  // The second parameter here is an extension!
27  // pre: npartition_size != 0 && nnext_size != 0
28  explicit PoolThreadSafe(const size_type nrequested_size,
29  const size_type nnext_size = 32)
30  :pool_type(nrequested_size, nnext_size) { }
31 
32  // Returns 0 if out-of-memory
33  void * malloc()
34  {
35  ParaEngine::Lock lock_(s_mutex);
36  return pool_type::malloc();
37  }
38  void free(void * const chunk)
39  {
40  ParaEngine::Lock lock_(s_mutex);
41  return pool_type::free(chunk);
42  }
43  private:
44  static mutex_type s_mutex;
45  };
46  template <typename UserAllocator, typename Mutex>
48 
59  template <class T>
60  class PoolBase {
61  public:
62  static void* operator new(size_t size) {
63  return s_memPool.malloc();
64  }
65 
66  static void operator delete(void *p) {
67  s_memPool.free(p);
68  }
69 
70  private:
71  static PoolThreadSafe<> s_memPool;
72  };
73 
74  template <class T>
76 
77 
90  template <class T>
92  public:
93  static void* operator new(size_t size) {
94  return s_memPool.malloc();
95  }
96 
97  static void operator delete(void *p) {
98  s_memPool.free(p);
99  }
100 
101  private:
102  static boost::pool<> s_memPool;
103  };
104 
105 }
different physics engine has different winding order.
Definition: EventBinding.h:32
simple scoped lock function
Definition: mutex.h:12
if one wants to create and delete many objects of the same type per frame, derive your class from Poo...
Definition: PoolBase.h:60
cross platform mutex
Definition: Mutex.hpp:88
if one wants to create and delete many objects of the same type per frame, derive your class from Poo...
Definition: PoolBase.h:91
this is a thread safe version of boost::pool.
Definition: PoolBase.h:9