volatile u32 memoryUsage = 0;
inline __attribute__((__malloc__)) void * MemAlloc(size_t size) {
if((memoryUsage + size) > ACCEPTABLE_LIMIT) {
// your code to trigger the debugger or whatnot goes here
}
void *ptr = malloc(size);
if (ptr == NULL) {
logPrintf("[ WARNING ] malloc returned NULL!\n");
}
memset(ptr, 0, size);
// uses a GCC atomic operation so this is threadsafe
__sync_fetch_and_add(&memoryUsage, size);
return ptr;
}
inline __attribute__((__malloc__)) void * operator new(size_t size) {
return MemAlloc(size);
}
inline __attribute__((__malloc__)) void * operator new [] (size_t size) {
return MemAlloc(size);
}
Are you talking about the working set size or heap memory? Heap memory is easy: The debugging VC++ Runtime has _CrtSetAllocHook, which calls a user supplied function on every memory allocation/reallocation/free call.
http://msdn.microsoft.com/en-us/library/820k4tb8.aspx
You could install the hook and then sum the memory allocations. If you hit your threshold value, you can call _debugbreak() to drop into the debugger.
Users browsing this forum: No registered users and 1 guest