| 554 | |
| 555 | extern "C" |
| 556 | DM_DLLEXPORT void *realloc(void* ptr, size_t size) |
| 557 | { |
| 558 | // This simplifies the code below a bit |
| 559 | if (ptr == 0) |
| 560 | return malloc(size); |
| 561 | |
| 562 | if (!reallocp) |
| 563 | { |
| 564 | CreateHooks(); |
| 565 | } |
| 566 | |
| 567 | size_t old_usable_size = 0; |
| 568 | #if defined(__linux__) |
| 569 | old_usable_size = malloc_usable_size(ptr); |
| 570 | #elif defined(__MACH__) |
| 571 | old_usable_size = malloc_size(ptr); |
| 572 | #else |
| 573 | #error "Unsupported platform" |
| 574 | #endif |
| 575 | |
| 576 | void* old_ptr = ptr; |
| 577 | ptr = reallocp(ptr, size); |
| 578 | if (ptr) |
| 579 | { |
| 580 | size_t usable_size = 0; |
| 581 | #if defined(__linux__) |
| 582 | usable_size = malloc_usable_size(ptr); |
| 583 | #elif defined(__MACH__) |
| 584 | usable_size = malloc_size(ptr); |
| 585 | #else |
| 586 | #error "Unsupported platform" |
| 587 | #endif |
| 588 | |
| 589 | dmMemProfile::DumpBacktrace('F', old_ptr, old_usable_size); |
| 590 | dmMemProfile::DumpBacktrace('M', ptr, usable_size); |
| 591 | |
| 592 | if (dmMemProfile::g_ExtStats) |
| 593 | { |
| 594 | dmAtomicSub32(&dmMemProfile::g_ExtStats->m_TotalActive, (uint32_t) old_usable_size); |
| 595 | |
| 596 | dmAtomicAdd32(&dmMemProfile::g_ExtStats->m_TotalAllocated, (uint32_t) usable_size); |
| 597 | dmAtomicAdd32(&dmMemProfile::g_ExtStats->m_TotalActive, (uint32_t) usable_size); |
| 598 | dmAtomicAdd32(&dmMemProfile::g_ExtStats->m_AllocationCount, 1U); |
| 599 | |
| 600 | if (dmMemProfile::m_PropertyAddU32) |
| 601 | { |
| 602 | dmMemProfile::m_PropertyAddU32(dmMemProfile::g_ProfileAllocations, 1U); |
| 603 | dmMemProfile::m_PropertyAddU32(dmMemProfile::g_ProfileAmount, usable_size); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | // Nothing happended |
| 610 | } |
| 611 | return ptr; |
| 612 | } |
| 613 | |