-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Utils/Memory] Added memory profiling capabilities
- Overloaded most of the new & delete operators to hook Tracy's memory profiling
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <tracy/Tracy.hpp> | ||
|
||
// See https://en.cppreference.com/w/cpp/memory/new/operator_new | ||
|
||
void* operator new(std::size_t size) { | ||
size = std::max(static_cast<std::size_t>(1), size); | ||
|
||
if (void* ptr = std::malloc(size)) { | ||
TracyAlloc(ptr, size); | ||
return ptr; | ||
} | ||
|
||
throw std::bad_alloc(); | ||
} | ||
|
||
void* operator new(std::size_t size, const std::nothrow_t&) noexcept { | ||
try { | ||
return operator new(size); | ||
} catch (...) { | ||
return nullptr; | ||
} | ||
} | ||
|
||
void* operator new[](std::size_t size) { | ||
size = std::max(static_cast<std::size_t>(1), size); | ||
|
||
if (void* ptr = std::malloc(size)) { | ||
TracyAlloc(ptr, size); | ||
return ptr; | ||
} | ||
|
||
throw std::bad_alloc(); | ||
} | ||
|
||
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept { | ||
try { | ||
return operator new[](size); | ||
} catch (...) { | ||
return nullptr; | ||
} | ||
} | ||
|
||
// See https://en.cppreference.com/w/cpp/memory/new/operator_delete | ||
|
||
void operator delete(void* ptr) noexcept { | ||
TracyFree(ptr); | ||
std::free(ptr); | ||
} | ||
|
||
void operator delete(void* ptr, std::size_t) noexcept { | ||
operator delete(ptr); | ||
} | ||
|
||
void operator delete(void* ptr, const std::nothrow_t&) noexcept { | ||
operator delete(ptr); | ||
} | ||
|
||
void operator delete[](void* ptr) noexcept { | ||
TracyFree(ptr); | ||
std::free(ptr); | ||
} | ||
|
||
void operator delete[](void* ptr, std::size_t) noexcept { | ||
operator delete[](ptr); | ||
} | ||
|
||
void operator delete[](void* ptr, const std::nothrow_t&) noexcept { | ||
operator delete[](ptr); | ||
} |