2011-07-12 21:27:37 +02:00
|
|
|
#ifndef __ALLOCATOR_H__
|
|
|
|
#define __ALLOCATOR_H__
|
|
|
|
|
2011-07-12 23:59:45 +02:00
|
|
|
#ifdef _DEBUG_MEMORY
|
2011-07-12 21:27:37 +02:00
|
|
|
|
2011-07-12 23:59:45 +02:00
|
|
|
#include <unordered_set>
|
2011-07-13 00:29:06 +02:00
|
|
|
#include <boost/functional/hash.hpp>
|
2011-07-12 21:27:37 +02:00
|
|
|
|
2011-07-12 23:59:45 +02:00
|
|
|
#ifdef _REENTRANT
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#endif
|
2011-07-12 21:27:37 +02:00
|
|
|
|
2011-07-13 06:19:12 +02:00
|
|
|
#define MAGIC_NUMBER 0xA1B2C3D4
|
|
|
|
|
2011-07-12 23:59:45 +02:00
|
|
|
struct AllocationBlock
|
|
|
|
{
|
|
|
|
unsigned int bytes;
|
2011-07-12 21:27:37 +02:00
|
|
|
void** backtraceBuffer;
|
2011-07-12 23:59:45 +02:00
|
|
|
unsigned char backtraceSize;
|
|
|
|
unsigned int records;
|
|
|
|
};
|
2011-07-12 21:27:37 +02:00
|
|
|
|
2011-07-13 06:19:12 +02:00
|
|
|
struct AllocationHead
|
|
|
|
{
|
|
|
|
unsigned int magicNumber;
|
|
|
|
AllocationBlock *block;
|
|
|
|
};
|
|
|
|
|
2011-07-12 23:59:45 +02:00
|
|
|
struct block_hash : std::unary_function<AllocationBlock *, std::size_t> {
|
|
|
|
std::size_t operator()(const AllocationBlock *block) const {
|
2011-07-13 00:29:06 +02:00
|
|
|
std::size_t seed = 0;
|
2011-07-12 23:59:45 +02:00
|
|
|
|
2011-07-13 00:29:06 +02:00
|
|
|
boost::hash_combine(seed, block->bytes);
|
2011-07-13 23:12:36 +02:00
|
|
|
for(int i=0;i<4;++i) {
|
2011-07-12 23:59:45 +02:00
|
|
|
if(i < block->backtraceSize)
|
2011-07-13 00:29:06 +02:00
|
|
|
boost::hash_combine(seed, block->backtraceBuffer[i]);
|
2011-07-12 23:59:45 +02:00
|
|
|
else
|
2011-07-13 00:29:06 +02:00
|
|
|
boost::hash_combine(seed, 0);
|
2011-07-12 23:59:45 +02:00
|
|
|
}
|
2011-07-13 00:29:06 +02:00
|
|
|
boost::hash_combine(seed, block->backtraceSize);
|
|
|
|
return seed;
|
2011-07-12 23:59:45 +02:00
|
|
|
}
|
2011-07-12 21:27:37 +02:00
|
|
|
};
|
|
|
|
|
2011-07-12 23:59:45 +02:00
|
|
|
struct block_equal_to : std::binary_function<AllocationBlock *, AllocationBlock *, bool> {
|
|
|
|
bool operator()(const AllocationBlock* a, const AllocationBlock *b) const
|
|
|
|
{
|
|
|
|
if(a->bytes != b->bytes || a->backtraceSize != b->backtraceSize)
|
|
|
|
return false;
|
|
|
|
for(int i=0;i<a->backtraceSize;++i)
|
|
|
|
if(a->backtraceBuffer[i] != b->backtraceBuffer[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-12 21:27:37 +02:00
|
|
|
class Allocator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Allocator();
|
|
|
|
~Allocator();
|
|
|
|
|
|
|
|
void *allocate(size_t bytes);
|
2011-07-12 23:59:45 +02:00
|
|
|
void deallocate(void *p);
|
2011-07-12 21:27:37 +02:00
|
|
|
|
|
|
|
void dumpLeaks();
|
|
|
|
|
|
|
|
private:
|
2011-07-12 23:59:45 +02:00
|
|
|
AllocationBlock* findBlock(void** backtraceBuffer, int backtraceSize, unsigned int bytes);
|
2011-07-12 21:27:37 +02:00
|
|
|
|
2011-07-12 23:59:45 +02:00
|
|
|
typedef std::unordered_set<AllocationBlock*, block_hash, block_equal_to> AllocationBlocksList;
|
2011-07-12 21:27:37 +02:00
|
|
|
AllocationBlocksList m_allocationsBlocks;
|
|
|
|
|
2011-07-12 23:59:45 +02:00
|
|
|
#ifdef _REENTRANT
|
2011-07-12 21:27:37 +02:00
|
|
|
boost::recursive_mutex m_allocatorLock;
|
2011-07-12 23:59:45 +02:00
|
|
|
#endif
|
2011-07-12 21:27:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2011-07-12 23:59:45 +02:00
|
|
|
void operator delete[](void *p);
|
|
|
|
|
|
|
|
#endif // _DEBUG_MEMORY
|
2011-07-12 21:27:37 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|