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.
2 
3 
5  std::size_t blockSize):
6  m_typeSize(typeSize), m_blockSize(blockSize), m_alive(0){
7  if (typeSize<32) abort(); // throw std::bad_alloc();
8  wipe();
9 }
10 
11 
13  m_typeSize(rh.m_typeSize), m_blockSize(rh.m_blockSize),m_alive(0) {
14  wipe();
15 }
16 
19  m_alive=0;
20  wipe();
21  return *this;
22 }
23 
24 // cannot keep the count as dealloc is never called...
25 
27  m_alive++;
28  void * ret = m_next;
30  Block & block = *m_current;
31  ++block.m_allocated;
32  if(m_next==(&block.m_data.back())+1)
33  nextBlock(true);
34  return ret;
35 }
36 
38  m_alive--;
39 }
40 
42  me().m_blocks.clear();
43  me().wipe();
44 }
45 
47  // reset caches
48  std::for_each(localCaches.begin(),localCaches.end(),boost::bind(&LocalCache::reset,_1));
49 
50  me().m_current=me().m_blocks.begin();
51  me().nextBlock(false);
52 }
53 
55  return const_cast<BlockWipedAllocator&>(*this);
56 }
57 
59  Stat s = { m_typeSize, m_blockSize, (*m_current).m_allocated,
60  (&*(*m_current).m_data.end()-m_next)/m_typeSize,
61  std::distance(const_iterator(m_current),m_blocks.end()),
62  m_blocks.size(), m_alive};
63  return s;
64 }
65 
66 void BlockWipedAllocator::nextBlock(bool advance) {
67  if (advance) m_current++;
68  if (m_current==m_blocks.end()) {
69  m_blocks.push_back(Block());
70  m_current=m_blocks.end(); --m_current;
71  }
72  m_current->m_data.resize(m_blockSize*m_typeSize);
73  m_current->m_allocated=0;
74  m_next = &(m_current->m_data.front());
75 }
76 
77 
78 BlockWipedPool::BlockWipedPool(std::size_t blockSize) : m_blockSize(blockSize){}
79 
80 
82  Pool::iterator p=m_pool.find(typeSize);
83  if (p!=m_pool.end()) return (*p).second;
84  return (*m_pool.insert(std::make_pair(typeSize,Allocator(typeSize, m_blockSize))).first).second;
85 }
86 
88  std::for_each(m_pool.begin(),m_pool.end(),boost::bind(&Allocator::wipe,
89  boost::bind(&Pool::value_type::second,_1)
90  ));
91 }
92 
94  std::for_each(m_pool.begin(),m_pool.end(),boost::bind(&Allocator::clear,
95  boost::bind(&Pool::value_type::second,_1)
96  ));
97 }
98 
99 
100 
101 
103  static BlockWipedPool local(1024);
104  return local;
105 }
106 
107 
110 
112  // throw id s_alive!=0???
113  if (0==s_alive) s_usePool=true;
114 }
115 
116 
117 
119  s_alive++;
120  return (s_usePool) ? allocator(s).alloc() : ::operator new(s);
121 }
122 
123 static void * BlockWipedPoolAllocated::operator new(size_t s, void * p) {
124  return p;
125 }
126 
127 #include<typeinfo>
128 #include<iostream>
129 struct AQ {
130  virtual ~AQ(){}
131 };
132 void BlockWipedPoolAllocated::operator delete(void * p, size_t s) {
133  if (0==p) return;
134  // if (s<100) std::cout << typeid(*(BlockWipedPoolAllocated*)(p)).name() << std::endl;
135  s_alive--;
136  (s_usePool) ? allocator(s).dealloc(p) : ::operator delete(p);
137 
138 }
139 
141  return blockWipedPool().allocator(s);
142 }
143 
144 
146  return allocator(s).stat();
147 }
148 
149 
std::vector< LocalCache * > localCaches
BlockWipedPool(std::size_t blockSize)
Allocator & allocator(std::size_t typeSize)
BlockWipedAllocator(std::size_t typeSize, std::size_t blockSize)
static BlockWipedAllocator & allocator(size_t s)
std::vector< unsigned char > m_data
return((rh^lh)&mask)
U second(std::pair< T, U > const &p)
BlockWipedAllocator & operator=(BlockWipedAllocator const &rh)
std::size_t m_blockSize
static BlockWipedAllocator::Stat stat(size_t s)
block
Formating index page&#39;s pieces.
Definition: Association.py:187
BlockWipedAllocator & me() const
virtual ~AQ()
BlockWipedPool & blockWipedPool()
Blocks::const_iterator const_iterator
void nextBlock(bool advance)
string s
Definition: asciidump.py:422