Cogs.Foundation
Loading...
Searching...
No Matches
Allocator.h
Go to the documentation of this file.
1#pragma once
2
3
4#include "../FoundationBase.h"
5#include "../Platform/Atomic.h"
6
7#include <OsoMemoryProfiler/CogsMemoryProfile.h>
8
9#ifndef _WIN32
10#include <utility>
11#endif
12
13namespace Cogs {
14 namespace Memory {
15#ifdef EMSCRIPTEN
16 constexpr size_t kDefaultAlignment = 8;
17#else
18 // For MSVC/GCC/Clang on Windows/Linux
19#if defined(_M_X64) || defined(__amd64__)
20 constexpr size_t kDefaultAlignment = 16;
21#else
22 constexpr size_t kDefaultAlignment = 8;
23#endif
24#endif
25
30 {
31 public:
32 Allocator() = default;
33 virtual ~Allocator() = default;
34
36 static Allocator * defaultAllocator();
37
40 static Allocator * overflowAllocator();
41
50 virtual void * allocate(size_t size, size_t alignment = 0, MemBlockType type = MemBlockType::Block);
51
60 virtual void deallocate(void * ptr, size_t size, MemBlockType type = MemBlockType::Block);
61
62 virtual void changeType(void* ptr, size_t size, MemBlockType oldType, MemBlockType newType);
63
65 virtual size_t getAllocatedBytes() const { return allocated; }
66
68 virtual size_t getAllocationCount() const { return allocations; }
69
70 protected:
72 Cogs::Atomic<size_t> allocations{0};
73 };
74
82 template<typename T>
83 T * create(Allocator * allocator)
84 {
85 void * memory = allocator->allocate(sizeof(T), alignof(T));
86
87 return new (memory) T();
88 }
89
100 template<typename T, typename... Args>
101 T * create(Allocator * allocator, Args&&... args)
102 {
103 void * memory = allocator->allocate(sizeof(T), alignof(T));
104
105 return new (memory) T(std::forward<Args>(args)...);
106 }
107
116 template<typename T>
117 void destroy(T * t, Allocator * allocator)
118 {
119 t->~T();
120
121 allocator->deallocate(t, sizeof(T));
122 }
123 }
124}
#define COGSFOUNDATION_API
Definition: FoundationBase.h:31
Base allocator implementation.
Definition: Allocator.h:30
virtual ~Allocator()=default
virtual size_t getAllocatedBytes() const
Return the number of bytes currently allocated by this allocator.
Definition: Allocator.h:65
virtual void deallocate(void *ptr, size_t size, MemBlockType type=MemBlockType::Block)
Deallocate the memory block at the given pointer, with the given size.
Definition: Allocator.cpp:58
virtual void * allocate(size_t size, size_t alignment=0, MemBlockType type=MemBlockType::Block)
Allocate raw memory.
Definition: Allocator.cpp:29
virtual size_t getAllocationCount() const
Return the number of allocations currently allocated by this allocator.
Definition: Allocator.h:68
T * create(Allocator *allocator)
Allocate storage and construct an object of the type T with the given allocator.
Definition: Allocator.h:83
void destroy(T *t, Allocator *allocator)
Destroy the given object and free the allocated memory using the given allocator.
Definition: Allocator.h:117
constexpr size_t kDefaultAlignment
Definition: Allocator.h:22
Main Cogs namespace.
Definition: MortonCode.h:5
std::atomic< T > Atomic
Definition: Atomic.h:107