| 170 | }, |
| 171 | |
| 172 | onMalloc(ptr, size, loc) { |
| 173 | if (!ptr) return; |
| 174 | if (emscriptenMemoryProfiler.sizeOfAllocatedPtr[ptr]) |
| 175 | { |
| 176 | // Uncomment to debug internal workings of tracing: |
| 177 | // console.error(`Allocation error in onMalloc! Pointer ${ptr} had already been tracked as allocated!`); |
| 178 | // console.error(`Previous site of allocation: ${emscriptenMemoryProfiler.allocationSitePtrs[ptr]}`); |
| 179 | // console.error(`This doubly attempted site of allocation: ${new Error().stack.toString()}`); |
| 180 | // abort('malloc internal inconsistency!'); |
| 181 | return; |
| 182 | } |
| 183 | var self = emscriptenMemoryProfiler; |
| 184 | // Gather global stats. |
| 185 | self.totalMemoryAllocated += size; |
| 186 | ++self.totalTimesMallocCalled; |
| 187 | |
| 188 | self.recordStackWatermark(); |
| 189 | |
| 190 | // Remember the size of the allocated block to know how much will be _free()d later. |
| 191 | self.sizeOfAllocatedPtr[ptr] = size; |
| 192 | // Also track if this was a _malloc performed at preRun time. |
| 193 | if (!self.pagePreRunIsFinished) self.sizeOfPreRunAllocatedPtr[ptr] = size; |
| 194 | |
| 195 | #if ASSERTIONS |
| 196 | assert(loc) |
| 197 | #endif |
| 198 | self.allocationsAtLoc[loc] ||= [0, 0, self.filterCallstackForMalloc(loc)]; |
| 199 | self.allocationsAtLoc[loc][0] += 1; |
| 200 | self.allocationsAtLoc[loc][1] += size; |
| 201 | self.allocationSitePtrs[ptr] = loc; |
| 202 | }, |
| 203 | |
| 204 | onFree(ptr) { |
| 205 | if (!ptr) return; |