25#ifdef JPL_COMPILER_MSVC
33#include <memory_resource>
61 std::pmr::memory_resource* mPrevious;
68 template<
class T,
class AllocatorType = std::allocator<T>>
71 using AForT =
typename std::allocator_traits<AllocatorType>::template rebind_alloc<T>;
72 using Traits = std::allocator_traits<AForT>;
76 explicit
AllocatorDeleter(const AllocatorType& allocator) noexcept(
std::is_nothrow_constructible_v<
AForT, const AllocatorType&>)
77 : mAllocator(allocator)
82 explicit AllocatorDeleter(
const AForT& allcoator)
noexcept(std::is_nothrow_copy_constructible_v<AForT>)
requires (!std::same_as<AllocatorType, AForT>)
83 : mAllocator(allcoator)
91 Traits::destroy(mAllocator, ptr);
92 Traits::deallocate(mAllocator, ptr, 1);
96 [[no_unique_address]]
AForT mAllocator{};
107 using Traits = std::allocator_traits<Allocator>;
110 explicit PmrDeleter(std::pmr::memory_resource* resource) noexcept
111 : mResource(resource)
120 Traits::destroy(allocator, p);
121 Traits::deallocate(allocator, p, 1);
127 static_assert(std::is_move_assignable_v<PmrDeleter<int>>);
131 template<
class Allocator>
134 typename std::allocator_traits<Allocator>::template rebind_alloc<std::byte>,
135 std::pmr::polymorphic_allocator<std::byte>
143 template<
class T,
class Allocator,
class... Args>
146 using AForT =
typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
147 using Traits = std::allocator_traits<AForT>;
148 AForT rebound(allocator);
150 T* raw = Traits::allocate(rebound, 1);
152 if constexpr (std::is_nothrow_constructible_v<T, Args...>)
154 Traits::construct(rebound, raw, std::forward<Args>(args)...);
160 Traits::construct(rebound, raw, std::forward<Args>(args)...);
163 Traits::deallocate(rebound, raw, 1);
171 return std::unique_ptr<T, Deleter>(raw, Deleter(allocator.resource()));
176 return std::unique_ptr<T, Deleter>(raw, Deleter(rebound));
185 template<
class T = std::
byte>
190 template<
class T,
class ...Args>
205 template<
class T,
class... Args>
220 template<
class T,
class Y>
236 template<
class T,
class... Args>
255 [[nodiscard]] JPL_INLINE
void* do_allocate(std::size_t bytes, std::size_t align)
override
257 void* p =
Upstream->allocate(bytes, align);
265 JPL_INLINE
void do_deallocate(
void* p, std::size_t bytes, std::size_t align)
override
268 Upstream->deallocate(p, bytes, align);
271 [[nodiscard]] JPL_INLINE
bool do_is_equal(
const std::pmr::memory_resource& other)
const noexcept override
273 return this == &other;
286 [[nodiscard]] JPL_INLINE
static void*
Allocate(std::size_t inSize)
289 return std::malloc(inSize);
292 [[nodiscard]] JPL_INLINE
static void*
Reallocate(
void* inBlock, [[maybe_unused]] std::size_t inOldSize, std::size_t inNewSize)
295 return std::realloc(inBlock, inNewSize);
298 JPL_INLINE
static void Free(
void* inBlock)
303 [[nodiscard]] JPL_INLINE
static void*
AlignedAllocate(std::size_t inSize, std::size_t inAlignment)
307#if defined(JPL_PLATFORM_WINDOWS)
309 return _aligned_malloc(inSize, inAlignment);
311 void* block =
nullptr;
312 JPL_SUPPRESS_WARNING_PUSH
315 (void)posix_memalign(&block, inAlignment, inSize);
316 JPL_SUPPRESS_WARNING_POP
323#if defined(JPL_PLATFORM_WINDOWS)
324 _aligned_free(inBlock);
338 [[nodiscard]] JPL_INLINE
void* do_allocate(std::size_t bytes, std::size_t align)
override
346 JPL_INLINE
void do_deallocate(
void* p, std::size_t bytes, std::size_t align)
override
354 [[nodiscard]] JPL_INLINE
bool do_is_equal(
const std::pmr::memory_resource& other)
const noexcept override
356 return this == &other;
#define JPL_DEFAULT_NEW_ALIGNMENT
Definition Core.h:357
#define JPL_GCC_SUPPRESS_WARNING(w)
Definition Core.h:283
#define JPL_CLANG_SUPPRESS_WARNING(w)
Definition Core.h:272
#define JPL_ASSERT(inExpression,...)
Main assert macro, usage: JPL_ASSERT(condition, message) or JPL_ASSERT(condition)
Definition ErrorReporting.h:80
std::size_t MaxUsage
Definition Memory.h:279
CountingResource(std::pmr::memory_resource *upstream)
Definition Memory.h:249
std::pmr::memory_resource * Upstream
Definition Memory.h:277
std::size_t InUse
Definition Memory.h:278
static JPL_INLINE void * Allocate(std::size_t inSize)
Definition Memory.h:286
static JPL_INLINE void AlignedFree(void *inBlock)
Definition Memory.h:321
static JPL_INLINE void Free(void *inBlock)
Definition Memory.h:298
static JPL_INLINE void * AlignedAllocate(std::size_t inSize, std::size_t inAlignment)
Definition Memory.h:303
static JPL_INLINE void * Reallocate(void *inBlock, std::size_t inOldSize, std::size_t inNewSize)
Definition Memory.h:292
std::pmr::polymorphic_allocator< T > Allocator
Definition Memory.h:106
JPL_INLINE void operator()(T *p) noexcept
Definition Memory.h:115
std::allocator_traits< Allocator > Traits
Definition Memory.h:107
PmrDeleter(std::pmr::memory_resource *resource) noexcept
Definition Memory.h:110
PmrMallocResource()=default
RAII override library-wide default memory resource.
Definition Memory.h:47
ScopedGlobalMemoryResource(std::pmr::memory_resource *resource)
Definition Memory.h:49
~ScopedGlobalMemoryResource()
Definition Memory.h:55
constexpr bool is_pmr_alloc_v
Definition Memory.h:132
Definition AcousticMaterial.h:36
JPL_INLINE T * DefaultNew(Args &&...args)
Allocate new object from deafult global memory resource used in JPLSpatial.
Definition Memory.h:191
JPL_INLINE void reset_pmr_shared(std::shared_ptr< T > &sharedPtr, Y *ptr)
Definition Memory.h:221
std::pmr::polymorphic_allocator< T > PmrAllocator
Just a shorter alias.
Definition Memory.h:183
JPL_INLINE std::shared_ptr< T > make_pmr_shared(Args &&... args)
std::make_shared wrapper using our global memory resource
Definition Memory.h:206
std::unique_ptr< T, PmrDeleter< T > > pmr_unique_ptr
Alias for unique ptr using pmr allocator and deleter.
Definition Memory.h:233
JPL_INLINE void DefaultDelete(T *object)
Delete object allcoated from deafult global memory resource. The object must had been allocated by ca...
Definition Memory.h:198
std::pmr::memory_resource * GetDefaultMemoryResource() noexcept
Definition Memory.h:42
std::pmr::memory_resource * gDefaultMemoryResource
Definition Memory.h:41
auto allocate_unique(const Allocator &allocator, Args &&... args)
Definition Memory.h:144
JPL_INLINE PmrAllocator< T > GetDefaultPmrAllocator()
Definition Memory.h:186
JPL_INLINE pmr_unique_ptr< T > make_pmr_unique(Args &&... args)
std::make_unique wrapper using our global memory resource
Definition Memory.h:237
Definition ChannelMap.h:272
typename std::allocator_traits< AllocatorType >::template rebind_alloc< T > AForT
Definition Memory.h:71
AllocatorDeleter() noexcept(std::is_nothrow_default_constructible_v< AForT >)=default
JPL_INLINE void operator()(T *ptr) noexcept
Definition Memory.h:87
std::allocator_traits< AForT > Traits
Definition Memory.h:72
AllocatorDeleter(const AForT &allcoator) noexcept(std::is_nothrow_copy_constructible_v< AForT >)
Definition Memory.h:82