Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
MemoryArena.ipp
Go to the documentation of this file.
1#pragma once
2
4
5#include <type_traits>
6
7namespace ph
8{
9
10inline std::size_t MemoryArena::numUsedBytes() const
11{
12 return m_numUsedBytes;
13}
14
15inline std::size_t MemoryArena::numAllocatedBytes() const
16{
18}
19
20inline std::size_t MemoryArena::getBlockSizeInBytes() const
21{
22 return m_blockSizeInBytes;
23}
24
25inline std::size_t MemoryArena::numAllocatedBlocks() const
26{
27 return m_blocks.size();
28}
29
30template<typename T>
32{
33 // User is responsible for managing lifetime. This restriction helps to reduce the risk of
34 // forgetting to end the object's lifetime.
35 static_assert(std::is_trivially_destructible_v<T>);
36
37 // IOC of array of size 1
38 return reinterpret_cast<T*>(allocRaw(sizeof(T), alignof(T)));
39}
40
41template<typename T>
42inline TSpan<T> MemoryArena::allocArray(const std::size_t arraySize)
43{
44 // User is responsible for managing lifetime. This restriction helps to reduce the risk of
45 // forgetting to end the object's lifetime.
46 static_assert(std::is_trivially_destructible_v<T>);
47
48 // IOC of array of size `arraySize`
49 T* const storage = reinterpret_cast<T*>(allocRaw(sizeof(T) * arraySize, alignof(T)));
50
51 return TSpan<T>(storage, arraySize);
52}
53
54template<typename T, typename... Args>
55inline T* MemoryArena::make(Args&&... args)
56{
57 if constexpr(std::is_trivially_destructible_v<T>)
58 {
59 return std::construct_at(alloc<T>(), std::forward<Args>(args)...);
60 }
61 else
62 {
63 T* const objPtr = std::construct_at(
64 reinterpret_cast<T*>(allocRaw(sizeof(T), alignof(T))),
65 std::forward<Args>(args)...);
66
67 // Record the dtor so we can call it later (on clear)
68 m_dtorCallers.push_back(
69 [objPtr]()
70 {
71 objPtr->~T();
72 });
73
74 return objPtr;
75 }
76}
77
78}// end namespace ph
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
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
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
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