5#include <Common/assertion.h>
6#include <Common/math_basics.h>
17 return m_blockSizeInBytes;
22 PH_ASSERT_GE(m_blockSizeInBytes, m_remainingBytesInBlock);
23 return m_blockSizeInBytes - m_remainingBytesInBlock;
28 return m_remainingBytesInBlock;
33 m_ptrInBlock = m_blockSource;
34 m_remainingBytesInBlock = m_blockSizeInBytes;
37template<
typename T,
typename... Args>
40 static_assert(std::is_trivially_destructible_v<T>);
43 T*
const storage =
reinterpret_cast<T*
>(allocRaw(
sizeof(
T),
alignof(
T)));
49 return std::construct_at(storage, std::forward<Args>(args)...);
57 using NonConstT = std::remove_const_t<T>;
59 static_assert(std::is_default_constructible_v<NonConstT>);
60 static_assert(std::is_trivially_destructible_v<NonConstT>);
63 NonConstT*
const storage =
reinterpret_cast<NonConstT*
>(
64 allocRaw(
sizeof(NonConstT) * arraySize,
alignof(NonConstT)));
70 std::uninitialized_default_construct_n(storage, arraySize);
73 return TSpan<NonConstT>(storage, arraySize);
76inline std::byte* GraphicsMemoryBlock::allocRaw(
77 const std::size_t numBytes,
78 const std::size_t alignmentInBytes)
80 PH_ASSERT(m_blockSource);
81 PH_ASSERT(math::is_power_of_2(alignmentInBytes));
83 void* ptr = m_ptrInBlock;
84 std::size_t availableBytes = m_remainingBytesInBlock;
85 void* alignedPtr = std::align(alignmentInBytes, numBytes, ptr, availableBytes);
93 PH_ASSERT(alignedPtr);
94 PH_ASSERT_GE(availableBytes, numBytes);
99 m_ptrInBlock =
static_cast<std::byte*
>(ptr) + numBytes;
102 m_remainingBytesInBlock = availableBytes - numBytes;
104 return start_implicit_lifetime_as_array<std::byte>(alignedPtr, numBytes);
std::size_t numAllocatedBytes() const
Definition GraphicsMemoryBlock.ipp:15
T * make(Args &&... args)
Make an object of type T as if by calling its constructor with Args.
Definition GraphicsMemoryBlock.ipp:38
TSpan< T > makeArray(std::size_t arraySize)
Make an array of default constructed objects of type T.
Definition GraphicsMemoryBlock.ipp:53
std::size_t numUsedBytes() const
Definition GraphicsMemoryBlock.ipp:20
void clear()
Reset the usage of the block. All memory handed out are effectively deallocated/deleted after this ca...
Definition GraphicsMemoryBlock.ipp:31
std::size_t numRemainingBytes() const
Definition GraphicsMemoryBlock.ipp:26
Definition PlatformDisplay.h:13