Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <memory>
- struct AllocationMetrics
- {
- uint32_t TotalAllocated = 0;
- uint32_t TotalFreed = 0;
- uint32_t CurrentUsage() { return TotalAllocated - TotalFreed; }
- };
- static AllocationMetrics s_AllocationMetrics;
- void* operator new(size_t size)
- {
- s_AllocationMetrics.TotalAllocated += size;
- // Allocate appropriate amount of memory to us and then return a pointer to that memory
- return malloc(size);
- }
- void operator delete(void* memory, size_t size)
- {
- s_AllocationMetrics.TotalFreed += size;
- free(memory);
- }
- struct Object
- {
- int x, y, z;
- };
- static void PrintMemoryUsage()
- {
- std::cout << "Memory Usage: " << s_AllocationMetrics.CurrentUsage() << " bytes\n";
- }
- int main()
- {
- PrintMemoryUsage();
- std::string string = "Cherno";
- PrintMemoryUsage();
- {
- std::unique_ptr<Object> obj = std::make_unique<Object>();
- PrintMemoryUsage();
- }
- PrintMemoryUsage();
- //Object* obj = new Object;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement