| 95 | #endif |
| 96 | |
| 97 | void *MyAllocWithStats(size_t size, const char *file, int line) |
| 98 | { |
| 99 | // Avoid compiler warning when variables aren't used |
| 100 | UNUSED_VAR(line); |
| 101 | UNUSED_VAR(file); |
| 102 | |
| 103 | // Allocate the memory |
| 104 | void *ptr = malloc(size); |
| 105 | #if !defined(__psp2__) && !defined(__CELLOS_LV2__) |
| 106 | // Count number of allocations made |
| 107 | numAllocs++; |
| 108 | |
| 109 | // Count total amount of memory allocated |
| 110 | sumAllocSize += size; |
| 111 | |
| 112 | // Update currently allocated memory |
| 113 | currentMemAlloc += size; |
| 114 | if( currentMemAlloc > maxMemAlloc ) maxMemAlloc = currentMemAlloc; |
| 115 | |
| 116 | // Remember the size of the memory allocated at this pointer |
| 117 | memSize.insert(map<void*,size_t>::value_type(ptr,size)); |
| 118 | |
| 119 | // Remember the currently allocated memory blocks, with the allocation number so that we can debug later |
| 120 | memCount.insert(map<void*,int>::value_type(ptr,numAllocs)); |
| 121 | |
| 122 | // Determine the maximum number of allocations at the same time |
| 123 | if( numAllocs - numFrees > maxNumAllocsSameTime ) |
| 124 | maxNumAllocsSameTime = numAllocs - numFrees; |
| 125 | |
| 126 | #ifdef TRACK_SIZES |
| 127 | // Determine the mean size of the memory allocations |
| 128 | map<size_t,int>::iterator i = meanSize.find(size); |
| 129 | if( i != meanSize.end() ) |
| 130 | i->second++; |
| 131 | else |
| 132 | meanSize.insert(map<size_t,int>::value_type(size,1)); |
| 133 | #endif |
| 134 | |
| 135 | #ifdef TRACK_LOCATIONS |
| 136 | // Count the number of allocations for each location in the library |
| 137 | loc l = {file, line}; |
| 138 | map<loc, counters>::iterator i2 = locCount.find(l); |
| 139 | if (i2 != locCount.end()) |
| 140 | { |
| 141 | i2->second.allocs++; |
| 142 | i2->second.totalMemAlloced += size; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | counters c = { 1,0,size,0 }; |
| 147 | locCount.insert(map<loc, counters>::value_type(l, c)); |
| 148 | } |
| 149 | |
| 150 | // Remember where the allocation is from |
| 151 | memAllocedFrom.insert(map<void*, loc>::value_type(ptr, l)); |
| 152 | #endif |
| 153 | #endif |
| 154 | return ptr; |