My Project
NPLMemPool.h
1 #pragma once
2 #include "util/mutex.h"
3 #include "math/ParaMath.h"
4 #include "util/PoolBase.h"
5 #include <boost/pool/object_pool.hpp>
6 
7 namespace ParaEngine
8 {
9  // forward declare
10  template <typename UserAllocator = boost::default_user_allocator_new_delete, typename Mutex = ParaEngine::mutex>
12 
15 
21  typedef std::basic_string<char, std::char_traits<char>, ParaEngine::CNPLPool_Char_alloc<> > NPLString;
22 
24 #define NPL_char_pool_init_size_bytes 32
25 
26 #define NPL_char_pool_init_size 5
27 
28 #define NPL_char_pool_count 7
29 
47  template <typename UserAllocator,
48  typename Mutex>
50  {
51  public:
52  typedef char value_type;
53  typedef UserAllocator user_allocator;
54  typedef Mutex mutex_type;
55 
56  typedef value_type * pointer;
57  typedef const value_type * const_pointer;
58  typedef value_type & reference;
59  typedef const value_type & const_reference;
60  typedef typename ParaEngine::PoolThreadSafe<UserAllocator, Mutex> pool_type;
61  typedef typename pool_type::size_type size_type;
62  typedef typename pool_type::difference_type difference_type;
63 
64  template <typename U>
65  struct rebind
66  {
68  };
69  protected:
71  {
73  {
74  int i=0;
75  m_mem_pools[i++] = new pool_type(NPL_char_pool_init_size_bytes,1024); // 32
76  m_mem_pools[i++] = new pool_type(NPL_char_pool_init_size_bytes*2,512); // 64
77  m_mem_pools[i++] = new pool_type(NPL_char_pool_init_size_bytes*2*2,256); // 128
78  m_mem_pools[i++] = new pool_type(NPL_char_pool_init_size_bytes*2*2*2,128); // 256
79  m_mem_pools[i++] = new pool_type(NPL_char_pool_init_size_bytes*2*2*2*2,64); // 512
80  m_mem_pools[i++] = new pool_type(NPL_char_pool_init_size_bytes*2*2*2*2*2,32); // 1024
81  m_mem_pools[i++] = new pool_type(NPL_char_pool_init_size_bytes*2*2*2*2*2*2,32); // 2048
82  PE_ASSERT(i==NPL_char_pool_count);
83  }
85  {
86  for(int i=0;i<NPL_char_pool_count;++i)
87  {
88  delete m_mem_pools[i];
89  }
90  }
91  pool_type& operator [] (int nIndex)
92  {
93  return *(m_mem_pools[nIndex]);
94  }
95  private:
96  pool_type* m_mem_pools[NPL_char_pool_count];
97  };
99  public:
101  {
102  }
103 
104  // default copy constructor
105 
106  // default assignment operator
107 
108  // not explicit, mimicking std::allocator [20.4.1]
110  {
111  }
113 
114  static pointer address(reference r)
115  { return &r; }
116  static const_pointer address(const_reference s)
117  { return &s; }
118  static size_type max_size()
119  { return (std::numeric_limits<size_type>::max)(); }
120 
121  bool operator==(const CNPLPool_Char_alloc &) const
122  { return true; }
123  bool operator!=(const CNPLPool_Char_alloc &) const
124  { return false; }
125 
126  static pointer allocate(const size_type n)
127  {
128  int nIndex = Math::log2_ceil(n) - NPL_char_pool_init_size;
129  if(nIndex<0)
130  nIndex = 0;
131  pointer ret = 0;
132  if(nIndex < NPL_char_pool_count)
133  {
134  ret = static_cast<pointer>(s_mem_pools[nIndex].malloc());
135  }
136  else
137  {
138  ret = UserAllocator::malloc(n);
139  }
140  if (ret == 0)
141  boost::throw_exception(std::bad_alloc());
142  return ret;
143  }
144  static pointer allocate(const size_type n, const void * const)
145  { return allocate(n); }
146  static void deallocate(const pointer ptr, const size_type n)
147  {
148  if (ptr == 0 || n == 0)
149  return;
150  int nIndex = Math::log2_ceil(n) - NPL_char_pool_init_size;
151  if(nIndex<0)
152  nIndex = 0;
153  if(nIndex < NPL_char_pool_count)
154  {
155  s_mem_pools[nIndex].free(ptr);
156  }
157  else
158  {
159  UserAllocator::free(ptr);
160  }
161  }
162  };
163 
165  template <typename UserAllocator, typename Mutex>
167 }
168 
169 // required by DLL interface
170 //EXPIMP_TEMPLATE template class PE_CORE_DECL ParaEngine::CNPLPool_Char_alloc<>;
171 //EXPIMP_TEMPLATE template class PE_CORE_DECL std::basic_string<char, std::char_traits<char>, ParaEngine::CNPLPool_Char_alloc<> >;
different physics engine has different winding order.
Definition: EventBinding.h:32
std::basic_string< char, std::char_traits< char >, ParaEngine::CNPLPool_Char_alloc<> > NPLString
NPL String can be used instead of std::string, for strings which are created and deleted very regular...
Definition: NPLMemPool.h:21
Definition: NPLMemPool.h:65
cross platform mutex
Definition: Mutex.hpp:88
This class can be used as a custom allocator for stl containers, which constantly create and delete s...
Definition: NPLMemPool.h:11
static SingletonMemPool_Type s_mem_pools
predefined memory pool (free lists)
Definition: NPLMemPool.h:98
static int log2_ceil(unsigned int x)
this is a fast version of log2.
Definition: ParaMath.h:458
CNPLPool_Char_alloc CNPLPool_Char_allocator
a quick define
Definition: NPLMemPool.h:11
this is a thread safe version of boost::pool.
Definition: PoolBase.h:9