2024-05-18 20:05:03 +02:00
|
|
|
/**
|
|
|
|
* This file is part of RenderD7
|
|
|
|
* Copyright (C) 2021-2024 NPI-D7, tobid7
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-02-19 19:20:37 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <map>
|
|
|
|
#include <renderd7/Memory.hpp>
|
|
|
|
|
|
|
|
static RenderD7::Memory::memory_metrics metrics;
|
|
|
|
|
2024-07-01 18:06:54 +02:00
|
|
|
bool rd7i_enable_memtrack;
|
2024-02-19 19:20:37 +01:00
|
|
|
|
|
|
|
void *operator new(size_t size) {
|
|
|
|
void *ptr = malloc(size);
|
2024-07-01 18:06:54 +02:00
|
|
|
if (rd7i_enable_memtrack) metrics.t_TotalAllocated += size;
|
2024-02-19 19:20:37 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void *memory, size_t size) {
|
2024-07-01 18:06:54 +02:00
|
|
|
if (rd7i_enable_memtrack) metrics.t_TotalFreed += size;
|
2024-02-19 19:20:37 +01:00
|
|
|
free(memory);
|
|
|
|
}
|
|
|
|
|
|
|
|
int allocations = 0;
|
|
|
|
int total_size = 0;
|
|
|
|
std::map<void *, size_t> sizes;
|
|
|
|
|
|
|
|
void *operator new[](size_t size) {
|
|
|
|
void *ptr = malloc(size);
|
2024-07-01 18:06:54 +02:00
|
|
|
if (rd7i_enable_memtrack) {
|
2024-06-08 21:00:40 +02:00
|
|
|
allocations++;
|
|
|
|
total_size += size;
|
|
|
|
sizes[ptr] = size;
|
|
|
|
metrics.t_TotalAllocated += size;
|
|
|
|
}
|
|
|
|
|
2024-02-19 19:20:37 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void *ptr) {
|
2024-07-01 18:06:54 +02:00
|
|
|
if (rd7i_enable_memtrack) {
|
2024-06-08 21:00:40 +02:00
|
|
|
allocations--;
|
|
|
|
total_size -= sizes[ptr];
|
|
|
|
metrics.t_TotalFreed += sizes[ptr];
|
|
|
|
sizes.erase(ptr);
|
|
|
|
}
|
2024-02-19 19:20:37 +01:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace RenderD7 {
|
|
|
|
|
|
|
|
namespace Memory {
|
|
|
|
|
|
|
|
size_t GetTotalAllocated() { return metrics.t_TotalAllocated; }
|
|
|
|
|
|
|
|
size_t GetTotalFreed() { return metrics.t_TotalFreed; }
|
|
|
|
|
|
|
|
size_t GetCurrent() { return metrics.t_CurrentlyAllocated(); }
|
|
|
|
} // namespace Memory
|
2024-05-17 11:33:09 +02:00
|
|
|
} // namespace RenderD7
|