MeshLib C++ Docs
Loading...
Searching...
No Matches
MRAllocator.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include <memory>
5#include <new>
6#include <type_traits>
7
9#if !defined( __EMSCRIPTEN__ ) && !defined( MRMESH_NO_MIMALLOC )
10#define MR_ALLOCATOR_IS_MIMALLOC
11#endif
12
13namespace MR
14{
17
18
19#ifdef MR_ALLOCATOR_IS_MIMALLOC
22namespace detail
23{
24[[nodiscard]] MR_BIND_IGNORE MRMESH_API void * mimallocNewN( std::size_t count, std::size_t size );
25[[nodiscard]] MR_BIND_IGNORE MRMESH_API void * mimallocNewAligned( std::size_t size, std::size_t alignment );
26MR_BIND_IGNORE MRMESH_API void mimallocFree( void * p ) noexcept;
27}
28#endif
29
31template <typename T, typename = void>
32inline constexpr std::size_t cAllocatorAlignment = alignof( T );
33template <typename T>
34inline constexpr std::size_t cAllocatorAlignment<T, std::void_t<decltype( T::cAllocAlignment )>> =
35 T::cAllocAlignment > alignof( T ) ? T::cAllocAlignment : alignof( T );
36
43template <typename T>
44struct MR_BIND_IGNORE Allocator
45{
46 using value_type = T;
47 template <typename U>
48 struct rebind { using other = Allocator<U>; };
49
50 Allocator() noexcept = default;
51 template <typename U>
52 Allocator( const Allocator<U> & ) noexcept {}
53
55
56#ifdef MR_ALLOCATOR_IS_MIMALLOC
57 [[nodiscard]] T * allocate( std::size_t n )
58 {
59 if constexpr ( cAllocatorAlignment<T> > __STDCPP_DEFAULT_NEW_ALIGNMENT__ )
60 {
61 if ( n > std::size_t( -1 ) / sizeof( T ) )
62 throw std::bad_array_new_length();
63 return static_cast<T *>( detail::mimallocNewAligned( n * sizeof( T ), cAllocatorAlignment<T> ) );
64 }
65 else
66 return static_cast<T *>( detail::mimallocNewN( n, sizeof( T ) ) );
67 }
68 void deallocate( T * p, std::size_t ) noexcept { detail::mimallocFree( p ); }
69#else
70 [[nodiscard]] T * allocate( std::size_t n )
71 {
72 if constexpr ( cAllocatorAlignment<T> > alignof( T ) )
73 {
74 if ( n > std::size_t( -1 ) / sizeof( T ) )
75 throw std::bad_array_new_length();
76 return static_cast<T *>( ::operator new( n * sizeof( T ), std::align_val_t( cAllocatorAlignment<T> ) ) );
77 }
78 else
79 return std::allocator<T>().allocate( n );
80 }
81 void deallocate( T * p, std::size_t n ) noexcept
82 {
83 if constexpr ( cAllocatorAlignment<T> > alignof( T ) )
84 ::operator delete( p, std::align_val_t( cAllocatorAlignment<T> ) );
85 else
86 std::allocator<T>().deallocate( p, n );
87 }
88#endif
89 [[nodiscard]] Allocator select_on_container_copy_construction() const noexcept { return *this; }
90};
91
92template <typename T, typename U>
93[[nodiscard]] inline bool operator ==( const Allocator<T> &, const Allocator<U> & ) noexcept { return true; }
94
95}
#define MRMESH_API
Definition MRMeshFwd.h:85
constexpr std::size_t cAllocatorAlignment
the alignment of the memory Allocator<T> allocates: alignof( T ), or T::cAllocAlignment if T defines ...
Definition MRAllocator.h:32
bool operator==(const Allocator< T > &, const Allocator< U > &) noexcept
Definition MRAllocator.h:93
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:59
void deallocate(T *p, std::size_t) noexcept
Definition MRAllocator.h:68
T * allocate(std::size_t n)
Definition MRAllocator.h:57
void * mimallocNewN(std::size_t count, std::size_t size)
void mimallocFree(void *p) noexcept
std::true_type propagate_on_container_move_assignment
Definition MRAllocator.h:54
Allocator() noexcept=default
T value_type
Definition MRAllocator.h:46
void * mimallocNewAligned(std::size_t size, std::size_t alignment)
Allocator< U > other
Definition MRAllocator.h:48
Allocator select_on_container_copy_construction() const noexcept
Definition MRAllocator.h:89
for internal use: namespace detail is not bound to other languages
Definition MRAllocator.h:23
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
Definition MRAllocator.h:48
Definition MRAllocator.h:45