Files
Seele/src/Engine/Containers/MemoryResource.h
T

50 lines
1.8 KiB
C++
Raw Normal View History

2025-03-20 20:15:38 +01:00
#pragma once
2026-03-16 16:52:43 +01:00
#include <string>
2025-03-20 20:15:38 +01:00
#include <memory_resource>
2025-03-25 09:17:08 +01:00
#include <map>
2026-02-22 20:26:25 +01:00
#include <string>
2025-03-26 13:38:48 +01:00
#include "MinimalEngine.h"
2025-03-20 20:15:38 +01:00
namespace Seele {
class MemoryManager {
public:
2025-03-26 13:38:48 +01:00
static RefPtr<uint64> getAllocationCounter(const std::string& name);
2025-03-20 20:15:38 +01:00
static void printAllocations();
private:
2025-03-25 09:17:08 +01:00
static MemoryManager& getInstance();
std::mutex allocationMutex;
2025-03-26 13:38:48 +01:00
std::map<std::string, OwningPtr<uint64>> allocationCounters;
2025-03-20 20:15:38 +01:00
};
class debug_resource : public std::pmr::memory_resource {
public:
static debug_resource* get(const std::string& name);
debug_resource() : name(""), upstream(std::pmr::get_default_resource()), counter(MemoryManager::getAllocationCounter(name)) {}
debug_resource(std::string name, std::pmr::memory_resource* up = std::pmr::get_default_resource())
: name(name), upstream(up), counter(MemoryManager::getAllocationCounter(name)) {}
debug_resource(const debug_resource& other) = delete;
2025-03-25 09:17:08 +01:00
debug_resource(debug_resource&& other) = default;
2025-03-20 20:15:38 +01:00
debug_resource& operator=(const debug_resource& other) = delete;
2025-03-25 09:17:08 +01:00
debug_resource& operator=(debug_resource&& other) = default;
2025-03-20 20:15:38 +01:00
void* do_allocate(size_t bytes, size_t alignment) override {
2025-03-26 13:38:48 +01:00
// std::cout << name << " + " << bytes << std::endl;
*counter.getHandle() += bytes;
2025-03-20 20:15:38 +01:00
return upstream->allocate(bytes, alignment);
}
void do_deallocate(void* ptr, size_t bytes, size_t alignment) override {
upstream->deallocate(ptr, bytes, alignment);
2025-03-26 13:38:48 +01:00
// std::cout << name << " - " << bytes << std::endl;
*counter.getHandle() -= bytes;
2025-03-20 20:15:38 +01:00
}
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { return this == &other; }
private:
std::string name;
std::pmr::memory_resource* upstream;
2025-03-26 13:38:48 +01:00
RefPtr<uint64> counter;
2025-03-20 20:15:38 +01:00
};
} // namespace Seele