Advertisement
zacaj

Untitled

Jan 26th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. header:
  2. extern uint usedMemory;
  3. extern int lastAlloc;
  4. extern uint nAlloc;
  5. void* operator new[] (size_t size,char *file,uint line);
  6. void* operator new (size_t size,char *file,uint line);
  7. #define new new(__FILE__,__LINE__)
  8.  
  9.  
  10. source:
  11.  
  12. struct AllocMeta
  13. {
  14. uint line;
  15. char *file;
  16. size_t size;
  17. };
  18. template <class T>
  19. class my_allocator
  20. {
  21. public:
  22. typedef size_t size_type;
  23. typedef ptrdiff_t difference_type;
  24. typedef T* pointer;
  25. typedef const T* const_pointer;
  26. typedef T& reference;
  27. typedef const T& const_reference;
  28. typedef T value_type;
  29.  
  30. my_allocator() {}
  31. my_allocator(const my_allocator&) {}
  32.  
  33.  
  34.  
  35. pointer allocate(size_type n, const void * = 0) {
  36. T* t = (T*) malloc(n * sizeof(T));
  37. return t;
  38. }
  39.  
  40. void deallocate(void* p, size_type) {
  41. if (p) {
  42. free(p);
  43. }
  44. }
  45.  
  46. pointer address(reference x) const { return &x; }
  47. const_pointer address(const_reference x) const { return &x; }
  48. my_allocator<T>& operator=(const my_allocator&) { return *this; }
  49. void construct(pointer p, const T& val)
  50. { new ((T*) p) T(val); }
  51. void destroy(pointer p) { p->~T(); }
  52.  
  53. size_type max_size() const { return size_t(-1); }
  54.  
  55. template <class U>
  56. struct rebind { typedef my_allocator<U> other; };
  57.  
  58. template <class U>
  59. my_allocator(const my_allocator<U>&) {}
  60.  
  61. template <class U>
  62. my_allocator& operator=(const my_allocator<U>&) { return *this; }
  63. };
  64.  
  65. unordered_map<void*,AllocMeta, std::hash < void* > , std::equal_to < void* > , my_allocator < pair <void*,AllocMeta> > > *allocations=NULL;
  66. void printLeakedMemory()
  67. {
  68. if(allocations==NULL)
  69. return;
  70. map<string,set<uint> > leaks;
  71. for(auto it=allocations->begin();it!=allocations->end();it++)
  72. {
  73. if(it->second.line!=-1)
  74. {
  75. char *file=it->second.file;
  76. int line=it->second.line;
  77. void *p=it->first;
  78.  
  79. if(leaks.find(it->second.file)!=leaks.end())
  80. leaks[it->second.file].insert(it->second.line);
  81. else
  82. {
  83. set<uint> lines;
  84. lines.insert(it->second.line);
  85. leaks[it->second.file]=lines;
  86. }
  87. }
  88. }
  89.  
  90. FILE *fp=fopen("memory leaks.txt","w");
  91. for(auto it=leaks.begin();it!=leaks.end();it++)
  92. {
  93. for(auto it2=it->second.begin();it2!=it->second.end();it2++)
  94. fprintf(fp,"%s: %i\n",it->first.c_str(),*it2);
  95. }
  96. fclose(fp);
  97. }
  98.  
  99. void* operator new (size_t size,int i)
  100. {
  101. void *p=malloc(size);
  102. if (p==0) // did malloc succeed?
  103. throw std::bad_alloc(); // ANSI/ISO compliant behavior
  104. return p;
  105. }
  106. bool trackAllocations=false;
  107. void* _malloc(size_t size,char *file,uint line)
  108. {
  109. void *p=malloc(size);
  110. if (p==0) // did malloc succeed?
  111. throw std::bad_alloc(); // ANSI/ISO compliant behavior
  112. if(trackAllocations)
  113. {
  114. AllocMeta am;
  115. am.line=line;
  116. am.file=file;
  117. am.size=size;
  118. if(allocations==NULL)
  119. allocations=new(2) unordered_map<void*,AllocMeta, std::hash < void* > , std::equal_to < void* > , my_allocator < pair <void*,AllocMeta> > > ();
  120. (*allocations)[p]=am;
  121. }
  122. lastAlloc=size;
  123. usedMemory+=size;
  124. nAlloc++;
  125. return p;
  126. }
  127. void* operator new (size_t size)
  128. {
  129. return _malloc(size,"",-1);
  130. }
  131. void* operator new[] (size_t size)
  132. {
  133. return _malloc(size,"",-1);
  134. }
  135. void* operator new (size_t size,char *file,uint line)
  136. {
  137. return _malloc(size,file,line);
  138. }
  139. void* operator new[] (size_t size,char *file,uint line)
  140. {
  141. return _malloc(size,file,line);
  142. }
  143. void _mdealloc(void *p)
  144. {
  145. if(p!=NULL)
  146. {
  147. size_t size=_msize(p);
  148. if(trackAllocations)
  149. {
  150. auto it=allocations->find(p);
  151. errorIf(it==allocations->end(),"%i not allocated",p);
  152. AllocMeta &am=it->second;
  153. errorIf(am.size!=size,"%i most likely reallocated, recorded size %i but real size %i",p,am.size,size);
  154. allocations->erase(it);
  155. }
  156. nAlloc--;
  157. usedMemory-=size;
  158. lastAlloc=-size;
  159. free(p);
  160. }
  161. }
  162. void operator delete (void *p)
  163. {
  164. _mdealloc(p);
  165. }
  166. void operator delete[](void *p)
  167. {
  168. _mdealloc(p);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement