Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
MemoryArena.h
Go to the documentation of this file.
1#pragma once
2
3#include "Utility/IMoveOnly.h"
4#include "Utility/TFunction.h"
5#include "Utility/TSpan.h"
6
7#include <Common/primitive_type.h>
8#include <Common/memory.h>
9
10#include <cstddef>
11#include <vector>
12#include <utility>
13#include <memory>
14
15namespace ph
16{
17
20class MemoryArena final : private IMoveOnly
21{
22public:
26
33 MemoryArena(std::size_t blockSizeHintInBytes, std::size_t numDefaultBlocks);
34
41 inline MemoryArena(MemoryArena&& other) = default;
42 inline MemoryArena& operator = (MemoryArena&& rhs) = default;
44
46
57 std::byte* allocRaw(std::size_t numBytes, std::size_t alignmentInBytes = alignof(std::max_align_t));
58
64 void clear();
65
66 std::size_t numUsedBytes() const;
67 std::size_t numAllocatedBytes() const;
68 std::size_t getBlockSizeInBytes() const;
69 std::size_t numAllocatedBlocks() const;
70
79 template<typename T>
80 T* alloc();
81
90 template<typename T>
91 TSpan<T> allocArray(const std::size_t arraySize);
92
100 template<typename T, typename... Args>
101 T* make(Args&&... args);
102
103private:
104 std::vector<TAlignedMemoryUniquePtr<std::byte>> m_blocks;
105
106 std::size_t m_blockSizeInBytes;
107 std::size_t m_currentBlockIdx;
108 std::byte* m_blockPtr;
109 std::size_t m_remainingBytesInBlock;
110 std::size_t m_numUsedBytes;
111
112 // Destructors of non-trivially-copyable objects. Simply specify `MIN_SIZE_HINT = 0` for
113 // smallest possible `TFunction`; increase it if compilation failed (this is unlikely,
114 // should investigate first).
115 std::vector<TFunction<void(void), 0>> m_dtorCallers;
116};
117
118}// end namespace ph
119
Marks the derived class as move only.
Definition IMoveOnly.h:23
A general purpose bump allocator.
Definition MemoryArena.h:21
~MemoryArena()
Definition MemoryArena.cpp:71
std::size_t numAllocatedBytes() const
Definition MemoryArena.ipp:15
TSpan< T > allocArray(const std::size_t arraySize)
Allocate raw memory for array of type T. Convenient method for allocating raw memory for array of typ...
Definition MemoryArena.ipp:42
T * alloc()
Allocate raw memory for type T. Convenient method for allocating raw memory for object of type T....
Definition MemoryArena.ipp:31
std::size_t numUsedBytes() const
Definition MemoryArena.ipp:10
std::size_t getBlockSizeInBytes() const
Definition MemoryArena.ipp:20
MemoryArena(MemoryArena &&other)=default
Move another arena into this one. The moved-from arena cannot be used unless:
std::size_t numAllocatedBlocks() const
Definition MemoryArena.ipp:25
std::byte * allocRaw(std::size_t numBytes, std::size_t alignmentInBytes=alignof(std::max_align_t))
Allocate raw memory. Generally speaking, the memory returned contains no object–placement new is requ...
Definition MemoryArena.cpp:78
MemoryArena & operator=(MemoryArena &&rhs)=default
T * make(Args &&... args)
Make an object of type T. Convenient method for creating an object without needing a placement new la...
Definition MemoryArena.ipp:55
MemoryArena()
Empty arena without any allocation performed yet.
Definition MemoryArena.cpp:44
void clear()
Reset the usage of the arena. All memory handed out are effectively deallocated/deleted after this ca...
Definition MemoryArena.cpp:141
Definition TFunction.h:23
The root for all renderer implementations.
Definition EEngineProject.h:6
std::span< T, EXTENT > TSpan
A contiguous sequence of objects of type T. Effectively the same as std::span.
Definition TSpan.h:12