MeshLib C++ Docs
Loading...
Searching...
No Matches
MRAllocator.h
Go to the documentation of this file.
1#pragma once
2
3#include "config.h"
4#include "MRPch/MRBindingMacros.h"
5#include <memory>
6#include <new>
7
9#if !defined( __EMSCRIPTEN__ ) && !defined( MRMESH_NO_MIMALLOC )
10#define MR_ALLOCATOR_IS_MIMALLOC
11#endif
12
13#ifdef MR_ALLOCATOR_IS_MIMALLOC
14#include <mimalloc.h>
15#if defined( _MSC_VER ) && !defined( MR_USE_CMAKE_CONFIGURE_FILE )
17#ifdef _DEBUG
18#pragma comment( lib, "mimalloc-debug.dll.lib" )
19#else
20#pragma comment( lib, "mimalloc.dll.lib" )
21#endif
22#endif
23#endif
24
25namespace MR
26{
29
30
31#ifdef MR_ALLOCATOR_IS_MIMALLOC
33template <typename T>
34struct MR_BIND_IGNORE AllocatorBase {};
35#else
36template <typename T>
37using AllocatorBase = std::allocator<T>;
38#endif
39
44template <typename T>
45struct MR_BIND_IGNORE Allocator : AllocatorBase<T>
46{
47 using value_type = T;
48 template <typename U>
49 struct rebind { using other = Allocator<U>; };
50
51 Allocator() noexcept = default;
52 template <typename U>
53 Allocator( const Allocator<U> & ) noexcept {}
54
55#ifdef MR_ALLOCATOR_IS_MIMALLOC
57
58 [[nodiscard]] T * allocate( std::size_t n )
59 {
60 if constexpr ( alignof( T ) > __STDCPP_DEFAULT_NEW_ALIGNMENT__ )
61 {
62 if ( n > std::size_t( -1 ) / sizeof( T ) )
63 throw std::bad_array_new_length();
64 return static_cast<T *>( mi_new_aligned( n * sizeof( T ), alignof( T ) ) );
65 }
66 else
67 return static_cast<T *>( mi_new_n( n, sizeof( T ) ) );
68 }
69 void deallocate( T * p, std::size_t ) noexcept { mi_free( p ); }
70#endif
71 [[nodiscard]] Allocator select_on_container_copy_construction() const noexcept { return *this; }
72};
73
74template <typename T, typename U>
75[[nodiscard]] inline bool operator ==( const Allocator<T> &, const Allocator<U> & ) noexcept { return true; }
76
77}
bool operator==(const Allocator< T > &, const Allocator< U > &) noexcept
Definition MRAllocator.h:75
void deallocate(T *p, std::size_t) noexcept
Definition MRAllocator.h:69
T * allocate(std::size_t n)
Definition MRAllocator.h:58
std::true_type propagate_on_container_move_assignment
Definition MRAllocator.h:56
Allocator() noexcept=default
T value_type
Definition MRAllocator.h:47
Allocator< U > other
Definition MRAllocator.h:49
Allocator select_on_container_copy_construction() const noexcept
Definition MRAllocator.h:71
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
not mi_stl_allocator: its construct/destroy members stop std containers from memmove-copying and skip...
Definition MRAllocator.h:34
Definition MRAllocator.h:49
Definition MRAllocator.h:46