Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- header:
- extern uint usedMemory;
- extern int lastAlloc;
- extern uint nAlloc;
- void* operator new[] (size_t size,char *file,uint line);
- void* operator new (size_t size,char *file,uint line);
- #define new new(__FILE__,__LINE__)
- source:
- struct AllocMeta
- {
- uint line;
- char *file;
- size_t size;
- };
- template <class T>
- class my_allocator
- {
- public:
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef T* pointer;
- typedef const T* const_pointer;
- typedef T& reference;
- typedef const T& const_reference;
- typedef T value_type;
- my_allocator() {}
- my_allocator(const my_allocator&) {}
- pointer allocate(size_type n, const void * = 0) {
- T* t = (T*) malloc(n * sizeof(T));
- return t;
- }
- void deallocate(void* p, size_type) {
- if (p) {
- free(p);
- }
- }
- pointer address(reference x) const { return &x; }
- const_pointer address(const_reference x) const { return &x; }
- my_allocator<T>& operator=(const my_allocator&) { return *this; }
- void construct(pointer p, const T& val)
- { new ((T*) p) T(val); }
- void destroy(pointer p) { p->~T(); }
- size_type max_size() const { return size_t(-1); }
- template <class U>
- struct rebind { typedef my_allocator<U> other; };
- template <class U>
- my_allocator(const my_allocator<U>&) {}
- template <class U>
- my_allocator& operator=(const my_allocator<U>&) { return *this; }
- };
- unordered_map<void*,AllocMeta, std::hash < void* > , std::equal_to < void* > , my_allocator < pair <void*,AllocMeta> > > *allocations=NULL;
- void printLeakedMemory()
- {
- if(allocations==NULL)
- return;
- map<string,set<uint> > leaks;
- for(auto it=allocations->begin();it!=allocations->end();it++)
- {
- if(it->second.line!=-1)
- {
- char *file=it->second.file;
- int line=it->second.line;
- void *p=it->first;
- if(leaks.find(it->second.file)!=leaks.end())
- leaks[it->second.file].insert(it->second.line);
- else
- {
- set<uint> lines;
- lines.insert(it->second.line);
- leaks[it->second.file]=lines;
- }
- }
- }
- FILE *fp=fopen("memory leaks.txt","w");
- for(auto it=leaks.begin();it!=leaks.end();it++)
- {
- for(auto it2=it->second.begin();it2!=it->second.end();it2++)
- fprintf(fp,"%s: %i\n",it->first.c_str(),*it2);
- }
- fclose(fp);
- }
- void* operator new (size_t size,int i)
- {
- void *p=malloc(size);
- if (p==0) // did malloc succeed?
- throw std::bad_alloc(); // ANSI/ISO compliant behavior
- return p;
- }
- bool trackAllocations=false;
- void* _malloc(size_t size,char *file,uint line)
- {
- void *p=malloc(size);
- if (p==0) // did malloc succeed?
- throw std::bad_alloc(); // ANSI/ISO compliant behavior
- if(trackAllocations)
- {
- AllocMeta am;
- am.line=line;
- am.file=file;
- am.size=size;
- if(allocations==NULL)
- allocations=new(2) unordered_map<void*,AllocMeta, std::hash < void* > , std::equal_to < void* > , my_allocator < pair <void*,AllocMeta> > > ();
- (*allocations)[p]=am;
- }
- lastAlloc=size;
- usedMemory+=size;
- nAlloc++;
- return p;
- }
- void* operator new (size_t size)
- {
- return _malloc(size,"",-1);
- }
- void* operator new[] (size_t size)
- {
- return _malloc(size,"",-1);
- }
- void* operator new (size_t size,char *file,uint line)
- {
- return _malloc(size,file,line);
- }
- void* operator new[] (size_t size,char *file,uint line)
- {
- return _malloc(size,file,line);
- }
- void _mdealloc(void *p)
- {
- if(p!=NULL)
- {
- size_t size=_msize(p);
- if(trackAllocations)
- {
- auto it=allocations->find(p);
- errorIf(it==allocations->end(),"%i not allocated",p);
- AllocMeta &am=it->second;
- errorIf(am.size!=size,"%i most likely reallocated, recorded size %i but real size %i",p,am.size,size);
- allocations->erase(it);
- }
- nAlloc--;
- usedMemory-=size;
- lastAlloc=-size;
- free(p);
- }
- }
- void operator delete (void *p)
- {
- _mdealloc(p);
- }
- void operator delete[](void *p)
- {
- _mdealloc(p);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement