1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#ifndef __OTSERV_ALLOCATOR_H
#define __OTSERV_ALLOCATOR_H
#ifdef __OTSERV_ALLOCATOR__
#include <memory>
#include <limits>
#include <cstdlib>
#include <map>
#include <fstream>
#include <ctime>
#include <boost/pool/pool.hpp>
template<typename T>
class dummyallocator
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U>
struct rebind
{
typedef dummyallocator<U> other;
};
dummyallocator( ) throw( )
{}
dummyallocator(const dummyallocator&) throw( )
{}
template <class U>
dummyallocator(const dummyallocator<U>&) throw( )
{}
~dummyallocator( ) throw( )
{}
pointer address(reference x) const
{
return &x;
}
const_pointer address(const_reference x) const
{
return &x;
}
pointer allocate(size_type n, void* hint = 0)
{
return static_cast<T*>(std::malloc(n * sizeof(T)) );
}
void deallocate(pointer p, size_type n)
{
std::free(static_cast<void*>(p));
}
size_type max_size( ) const throw( )
{
return std::numeric_limits<size_type>::max() / sizeof(T);
}
void construct(pointer p, const T& val)
{
new(static_cast<void*>(p)) T(val);
}
void destroy(pointer p)
{
p->~T( );
}
};
void* operator new(size_t bytes, int dummy);
void* operator new(size_t bytes);
void* operator new[](size_t bytes);
void operator delete(void *p);
void operator delete[](void* p);
#ifdef _MSC_VER
void operator delete(void* p, int dummy);
void operator delete[](void* p, int dummy);
#endif
#ifdef __OTSERV_ALLOCATOR_STATS__
OTSYS_THREAD_RETURN allocatorStatsThread(void *a);
#endif
struct poolTag
{
size_t poolbytes;
};
class PoolManager
{
public:
static PoolManager& getInstance()
{
static PoolManager instance;
return instance;
}
void* allocate(size_t size)
{
Pools::iterator it;
OTSYS_THREAD_LOCK(poolLock, NULL);
for(it = pools.begin(); it != pools.end(); ++it)
{
if(it->first >= size + sizeof(poolTag))
{
//poolTag* tag = reinterpret_cast<poolTag*>(it->second->ordered_malloc());
poolTag* tag = reinterpret_cast<poolTag*>(it->second->malloc());
tag->poolbytes = it->first;
#ifdef __OTSERV_ALLOCATOR_STATS__
poolsStats[it->first]->allocations++;
poolsStats[it->first]->unused+= it->first - (size + sizeof(poolTag));
#endif
OTSYS_THREAD_UNLOCK(poolLock, NULL);
return tag + 1;
}
}
poolTag* tag = reinterpret_cast<poolTag*>(std::malloc(size + sizeof(poolTag)));
#ifdef __OTSERV_ALLOCATOR_STATS__
poolsStats[0]->allocations++;
poolsStats[0]->unused += size;
#endif
tag->poolbytes = 0;
OTSYS_THREAD_UNLOCK(poolLock, NULL);
return tag + 1;
}
void deallocate(void* deletable)
{
if(deletable == NULL)
return;
poolTag* const tag = reinterpret_cast<poolTag*>(deletable) - 1U;
OTSYS_THREAD_LOCK(poolLock, NULL);
if(tag->poolbytes)
{
Pools::iterator it;
it = pools.find(tag->poolbytes);
//it->second->ordered_free(tag);
it->second->free(tag);
#ifdef __OTSERV_ALLOCATOR_STATS__
poolsStats[it->first]->deallocations++;
#endif
}
else
{
std::free(tag);
#ifdef __OTSERV_ALLOCATOR_STATS__
poolsStats[0]->deallocations++;
#endif
}
OTSYS_THREAD_UNLOCK(poolLock, NULL);
}
/*
void releaseMemory(){
Pools::iterator it;
for(it = pools.begin(); it != pools.end(); ++it) {
it->second->release_memory();
}
}
*/
#ifdef __OTSERV_ALLOCATOR_STATS__
void dumpStats()
{
time_t rawtime;
time(&rawtime);
std::ofstream output("mem_dump.txt",std::ios_base::app);
output << "Otserv Allocator Stats: " << std::ctime(&rawtime);
PoolsStats::iterator it;
for(it = poolsStats.begin(); it != poolsStats.end(); ++it)
{
output << (int)(it->first) << " alloc: " << (int)(it->second->allocations) <<
" dealloc: " << (int)(it->second->deallocations) <<
" unused: " << (int)(it->second->unused);
if(it->second->allocations != 0 && it->first != 0)
{
output << " avg: " << (int)((it->first) - (it->second->unused)/(it->second->allocations)) <<
" %unused: " << (int)((it->second->unused)*100/(it->second->allocations)/(it->first));
}
output << " N: " << ((int)(it->second->allocations) - (int)(it->second->deallocations)) <<
std::endl;
}
output << std::endl;
output.close();
}
#endif
~PoolManager()
{
Pools::iterator it = pools.begin();
while(it != pools.end())
{
std::free(it->second);
pools.erase(it++);
}
#ifdef __OTSERV_ALLOCATOR_STATS__
PoolsStats::iterator it2;
for(it2 = poolsStats.begin(); it2 != poolsStats.end(); ++it2)
{
std::free(it2->second);
poolsStats.erase(it2++);
}
#endif
}
private:
void addPool(size_t size, size_t next_size)
{
pools[size] = new(0) boost::pool<boost::default_user_allocator_malloc_free>(size, next_size);
#ifdef __OTSERV_ALLOCATOR_STATS__
t_PoolStats * tmp = new(0) t_PoolStats;
tmp->unused = 0;
tmp->allocations = 0;
tmp->deallocations = 0;
poolsStats[size] = tmp;
#endif
}
PoolManager()
{
OTSYS_THREAD_LOCKVARINIT(poolLock);
addPool(32, 16384);
addPool(64, 16384);
addPool(128, 4096);
addPool(256, 256);
addPool(512, 256);
addPool(768, 256);
addPool(2048, 128);
addPool(4096, 128);
addPool(8192, 128);
addPool(18432, 128);
#ifdef __OTSERV_ALLOCATOR_STATS__
t_PoolStats * tmp = new(0) t_PoolStats;
tmp->unused = 0;
tmp->allocations = 0;
tmp->deallocations = 0;
poolsStats[0] = tmp;
#endif
}
PoolManager(const PoolManager&);
const PoolManager& operator=(const PoolManager&);
typedef std::map<size_t, boost::pool<boost::default_user_allocator_malloc_free >*, std::less<size_t >,
dummyallocator<std::pair<const size_t, boost::pool<boost::default_user_allocator_malloc_free>* > > > Pools;
Pools pools;
#ifdef __OTSERV_ALLOCATOR_STATS__
struct t_PoolStats
{
int allocations;
int deallocations;
int unused;
};
typedef std::map<size_t, t_PoolStats*, std::less<size_t >,
dummyallocator<std::pair<const size_t, t_PoolStats* > > > PoolsStats;
PoolsStats poolsStats;
#endif
OTSYS_THREAD_LOCKVAR poolLock;
};
#endif
#endif
|