1#include "MemoryContext.h"
3#include "Foundation/Logging/Logger.h"
4#include "Foundation/Memory/Allocator.h"
5#include "Foundation/Platform/Atomic.h"
6#include "Foundation/StringView.h"
21 baseAllocator(baseAllocator),
29 if (!base && tag !=
"Strings") {
30 if (allocationCount || allocationSize) {
31 LOG_WARNING(logger,
"[%.*s] Leaked %zu allocations, %zu bytes.", StringViewFormat(tag), allocationCount.load(), allocationSize.load());
32 assert(allocationSize == 0 &&
"Core memory leaked.");
37 size_t size()
const {
return allocationSize; }
38 size_t count()
const {
return allocationCount; }
40 size_t peakSize()
const {
return peakAllocationSize; }
41 size_t peakCount()
const {
return peakAllocationCount; }
43 void *
allocate(
size_t size,
size_t alignment, MemBlockType type = MemBlockType::CoreAllocator)
override
45 allocationSize += size;
46 peakAllocationSize = std::max(peakAllocationSize.load(), allocationSize.load());
48 peakAllocationCount = std::max(peakAllocationCount.load(), allocationCount.load());
50 return baseAllocator->
allocate(size, alignment, type);
53 void deallocate(
void * ptr,
size_t size, MemBlockType type = MemBlockType::CoreAllocator)
override
55 allocationSize -= size;
69 Atomic<size_t> allocationSize{ 0 };
70 Atomic<size_t> allocationCount{ 0 };
71 Atomic<size_t> peakAllocationSize{ 0 };
72 Atomic<size_t> peakAllocationCount{ 0 };
82 resourceAllocator(&baseAllocator,
"Resources"),
83 componentAllocator(&baseAllocator,
"Components"),
84 ioAllocator(&baseAllocator,
"IO"),
85 scriptAllocator(&baseAllocator,
"Script")
104 return ((CoreAllocator *)allocator)->count();
107 size_t getPeakBytes(
const Memory::Allocator * allocator)
109 return ((CoreAllocator *)allocator)->peakSize();
112 size_t getPeakCount(
const Memory::Allocator * allocator)
114 return ((CoreAllocator *)allocator)->peakCount();
121 return &cSringsAllocator;
124Cogs::Core::MemoryContext::MemoryContext() :
125 storage(
std::make_unique<MemoryContextStorage>()),
126 baseAllocator(&storage->baseAllocator),
127 resourceAllocator(&storage->resourceAllocator),
128 ioAllocator(&storage->ioAllocator),
129 componentAllocator(&storage->componentAllocator),
130 scriptAllocator(&storage->scriptAllocator)
135Cogs::Core::MemoryContext::~MemoryContext()
void * allocate(size_t size, size_t alignment, MemBlockType type=MemBlockType::CoreAllocator) override
Allocate raw memory.
void deallocate(void *ptr, size_t size, MemBlockType type=MemBlockType::CoreAllocator) override
Deallocate the memory block at the given pointer, with the given size.
size_t getAllocationCount() const override
Return the number of allocations currently allocated by this allocator.
size_t getAllocatedBytes() const override
Return the number of bytes currently allocated by this allocator.
Log implementation class.
Base allocator implementation.
static Allocator * defaultAllocator()
Gets the system default allocator.
virtual void deallocate(void *ptr, size_t size, MemBlockType type=MemBlockType::Block)
Deallocate the memory block at the given pointer, with the given size.
virtual void * allocate(size_t size, size_t alignment=0, MemBlockType type=MemBlockType::Block)
Allocate raw memory.
Provides a weakly referenced view over the contents of a string.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
constexpr Log getLogger(const char(&name)[LEN]) noexcept