| 380 | |
| 381 | extern "C" |
| 382 | DM_DLLEXPORT void *malloc(size_t size) |
| 383 | { |
| 384 | if (!mallocp) |
| 385 | { |
| 386 | CreateHooks(); |
| 387 | } |
| 388 | |
| 389 | void *ptr = mallocp(size); |
| 390 | |
| 391 | if (ptr) |
| 392 | { |
| 393 | size_t usable_size = 0; |
| 394 | #if defined(__linux__) |
| 395 | usable_size = malloc_usable_size(ptr); |
| 396 | #elif defined(__MACH__) |
| 397 | usable_size = malloc_size(ptr); |
| 398 | #else |
| 399 | #error "Unsupported platform" |
| 400 | #endif |
| 401 | |
| 402 | dmMemProfile::DumpBacktrace('M', ptr, usable_size); |
| 403 | |
| 404 | if (dmMemProfile::g_ExtStats) |
| 405 | { |
| 406 | dmAtomicAdd32(&dmMemProfile::g_ExtStats->m_TotalAllocated, (uint32_t) usable_size); |
| 407 | dmAtomicAdd32(&dmMemProfile::g_ExtStats->m_TotalActive, (uint32_t) usable_size); |
| 408 | dmAtomicAdd32(&dmMemProfile::g_ExtStats->m_AllocationCount, 1U); |
| 409 | |
| 410 | if (dmMemProfile::m_PropertyAddU32) |
| 411 | { |
| 412 | dmMemProfile::m_PropertyAddU32(dmMemProfile::g_ProfileAllocations, 1U); |
| 413 | dmMemProfile::m_PropertyAddU32(dmMemProfile::g_ProfileAmount, usable_size); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | dmMemProfile::DumpBacktrace('M', 0, 0); |
| 420 | } |
| 421 | return ptr; |
| 422 | } |
| 423 | |
| 424 | #ifndef __MACH__ |
| 425 | extern "C" |