CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BlockWipedAllocator.cc
Go to the documentation of this file.
3 
5  std::size_t blockSize,
6  std::size_t maxRecycle):
7  m_typeSize(typeSize), m_blockSize(blockSize), m_maxRecycle(maxRecycle), m_alive(0){
8  // if (typeSize<32) abort(); // throw std::bad_alloc();
9  recycled.reserve(m_maxRecycle);
10  wipe();
11 }
12 
13 
15  m_typeSize(rh.m_typeSize), m_blockSize(rh.m_blockSize), m_maxRecycle(rh.m_maxRecycle), m_alive(0) {
16  recycled.reserve(m_maxRecycle);
17  wipe();
18 }
19 
22  recycled.reserve(m_maxRecycle);
23  m_alive=0;
24  wipe();
25  return *this;
26 }
27 
28 
30  clear();
31 }
32 
33 
34 // cannot keep the count as dealloc is never called...
35 
37  m_alive++;
38  if likely(!recycled.empty()) {
39  void * ret = recycled.back();
40  recycled.pop_back();
41  return ret;
42  }
43  void * ret = m_next;
45  Block & block = *m_current;
46  ++block.m_allocated;
47  if unlikely(m_next==(&block.m_data.back())+1) nextBlock(true);
48  return ret;
49 }
50 
52  if likely (recycled.size()<m_maxRecycle) recycled.push_back(p);
53  m_alive--;
54 }
55 
57  me().m_blocks.clear();
58  me().wipe();
59 }
60 
61 void BlockWipedAllocator::wipe(bool force) const {
62  if (m_alive>0 && !force) return;
63  // reset caches
64  std::for_each(localCaches.begin(),localCaches.end(),boost::bind(&LocalCache::reset,_1));
65 
66  me().m_current=me().m_blocks.begin();
67  me().nextBlock(false);
68  me().recycled.clear();
69 }
70 
72  return const_cast<BlockWipedAllocator&>(*this);
73 }
74 
76  Stat s = { m_typeSize, m_blockSize, (*m_current).m_allocated,
77  (&*(*m_current).m_data.end()-m_next)/m_typeSize,
78  std::distance(const_iterator(m_current),m_blocks.end()),
79  m_blocks.size(), m_alive};
80  return s;
81 }
82 
83 void BlockWipedAllocator::nextBlock(bool advance) {
84  if likely(advance) m_current++;
85  if unlikely(m_current==m_blocks.end()) {
86  m_blocks.push_back(Block());
87  m_current=m_blocks.end(); --m_current;
88  }
89  m_current->m_data.resize(m_blockSize*m_typeSize);
90  m_current->m_allocated=0;
91  m_next = &(m_current->m_data.front());
92 }
93 
94 
95 BlockWipedPool::BlockWipedPool(std::size_t blockSize, std::size_t maxRecycle) :
96  m_blockSize(blockSize), m_maxRecycle(maxRecycle), m_last(0), m_lastSize(0){}
97 
99 
100 
101 
103  if likely(m_lastSize==typeSize) return *m_last;
104  Pool::iterator p=m_pool.find(typeSize);
105  m_lastSize=typeSize;
106  if likely (p!=m_pool.end()) return *(m_last = &(*p).second);
107  return *(m_last=&(*m_pool.insert(std::make_pair(typeSize,Allocator(typeSize, m_blockSize, m_maxRecycle))).first).second);
108 }
109 
110 void BlockWipedPool::wipe(bool force) {
111  std::for_each(m_pool.begin(),m_pool.end(),boost::bind(&Allocator::wipe,
112  boost::bind(&Pool::value_type::second,_1),force
113  ));
114 }
115 
117  std::for_each(m_pool.begin(),m_pool.end(),boost::bind(&Allocator::clear,
118  boost::bind(&Pool::value_type::second,_1)
119  ));
120 }
121 
122 
123 
124 
126  static BlockWipedPool * local=0;
127  if (p!=0) local=p;
128  return *local;
129 }
130 
131 
134 
136  // throw id s_alive!=0???
137  if (0==s_alive) s_usePool=true;
138 }
139 
140 
141 
143  s_alive++;
144  return (s_usePool) ? allocator(s).alloc() : ::operator new(s);
145 }
146 
147 void * BlockWipedPoolAllocated::operator new(size_t s, void * p) {
148  return p;
149 }
150 
151 #include<typeinfo>
152 #include<iostream>
153 struct AQ {
154  virtual ~AQ(){}
155 };
156 void BlockWipedPoolAllocated::operator delete(void * p, size_t s) {
157  if (0==p) return;
158  // if (s<100) std::cout << typeid(*(BlockWipedPoolAllocated*)(p)).name() << std::endl;
159  s_alive--;
160  (s_usePool) ? allocator(s).dealloc(p) : ::operator delete(p);
161 
162 }
163 
165  return blockWipedPool().allocator(s);
166 }
167 
168 
170  return allocator(s).stat();
171 }
172 
173 
std::vector< void * > recycled
std::vector< LocalCache * > localCaches
std::size_t m_maxRecycle
Allocator & allocator(std::size_t typeSize)
static BlockWipedAllocator & allocator(size_t s)
std::vector< unsigned char > m_data
BlockWipedPool & blockWipedPool(BlockWipedPool *p=0)
U second(std::pair< T, U > const &p)
#define unlikely(x)
Definition: Likely.h:21
BlockWipedAllocator & operator=(BlockWipedAllocator const &rh)
BlockWipedAllocator Allocator
BlockWipedPool(std::size_t blockSize, std::size_t maxRecycle)
std::size_t m_blockSize
static BlockWipedAllocator::Stat stat(size_t s)
void nextBlock(bool advance) dso_internal
block
Formating index page&#39;s pieces.
Definition: Association.py:232
BlockWipedAllocator & me() const
virtual ~AQ()
std::size_t m_lastSize
#define likely(x)
Definition: Likely.h:20
void wipe(bool force=true)
Blocks::const_iterator const_iterator
void wipe(bool force=true) const
BlockWipedAllocator(std::size_t typeSize, std::size_t blockSize, std::size_t maxRecycle)