Photon Common Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1#pragma once
2
7#include "Common/assertion.h"
8
9#include <cstddef>
10#include <memory>
11#include <array>
12
13namespace ph
14{
15
16namespace detail
17{
18
27[[nodiscard]]
28void* allocate_aligned_memory(std::size_t numBytes, std::size_t alignmentInBytes);
29
35void free_aligned_memory(void* ptr);
36
38{
39 inline void operator () (void* const ptr) const
40 {
42 }
43
44 inline void operator () (const void* const ptr) const
45 {
46 // `const_cast` to support const overload. This is fine as aligned memory is only returned as
47 // a non-const pointer (see `allocate_aligned_memory()`), and casting an originally non-const
48 // type is safe.
49 free_aligned_memory(const_cast<void*>(ptr));
50 }
51};
52
53}// end namespace detail
54
55template<typename T>
56using TAlignedMemoryUniquePtr = std::unique_ptr<T, detail::AlignedMemoryDeleter>;
57
58// Note that `detail::AlignedMemoryDeleter` is for empty base optimization on `std::unique_ptr`,
59// see https://stackoverflow.com/questions/42715492/stdunique-ptr-and-custom-deleters.
60// This would reduce the size of the resulting `unique_ptr` to the size of a single pointer.
61// Reference: https://stackoverflow.com/questions/45341371/memory-efficient-custom-deleter-for-stdunique-ptr
62//
63// The following test will ensure this is true:
64static_assert(sizeof(TAlignedMemoryUniquePtr<void>) == sizeof(void*));
65
72template<typename T, std::size_t N, std::size_t ALIGNMENT_IN_BYTES = sizeof(T)>
73struct alignas(ALIGNMENT_IN_BYTES) TAlignedArray : public std::array<T, N>
74{
76 inline static constexpr std::size_t ALIGNMENT = ALIGNMENT_IN_BYTES;
77
78 // Do not add any other member here. This struct must be a standard-layout type and nothing else
79 // but being an aligned alias to `std::array`.
80};
81
96template<typename T = void>
97inline auto make_aligned_memory(std::size_t numBytes, std::size_t alignmentInBytes)
99
100template<typename T>
101void from_bytes(const std::byte* srcBytes, T* out_dstValue);
102
103template<typename T>
104void to_bytes(const T& srcValue, std::byte* out_dstBytes);
105
106template<std::size_t N>
107void reverse_bytes(std::byte* bytes);
108
115template<typename T>
116T* start_implicit_lifetime_as(void* ptr) noexcept;
117
124template<typename T>
125T* start_implicit_lifetime_as_array(void* ptr, std::size_t numArrayElements) noexcept;
126
127}// end namespace ph
128
129#include "Common/memory.ipp"
void free_aligned_memory(void *ptr)
Definition memory.cpp:54
void * allocate_aligned_memory(std::size_t numBytes, std::size_t alignmentInBytes)
Definition memory.cpp:27
The root for all renderer implementations.
Definition assertion.h:9
T * start_implicit_lifetime_as_array(void *ptr, std::size_t numArrayElements) noexcept
Wrapper for std::start_lifetime_as_array(). Primarily a fallback when C++23 is not available....
Definition memory.ipp:132
T * start_implicit_lifetime_as(void *ptr) noexcept
Wrapper for std::start_lifetime_as(). Primarily a fallback when C++23 is not available....
Definition memory.ipp:122
void from_bytes(const std::byte *srcBytes, T *out_dstValue)
Definition memory.ipp:34
std::unique_ptr< T, detail::AlignedMemoryDeleter > TAlignedMemoryUniquePtr
Definition memory.h:56
auto make_aligned_memory(std::size_t numBytes, std::size_t alignmentInBytes) -> TAlignedMemoryUniquePtr< T >
Create an aligned memory resource.
Definition memory.ipp:17
void reverse_bytes(std::byte *bytes)
Definition memory.ipp:61
void to_bytes(const T &srcValue, std::byte *out_dstBytes)
Definition memory.ipp:48
Aligned version of std::array. This type shares the same traits as std::array. [class....
Definition memory.h:74
Definition memory.h:38
void operator()(void *const ptr) const
Definition memory.h:39