GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
ScopedHeap.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
// See LICENSE for details.
4
5
#ifndef GPR_MEMORY_SCOPEDHEAP_H_
6
#define GPR_MEMORY_SCOPEDHEAP_H_
7
8
#include <cstddef>
9
#include <limits>
10
#include <memory>
11
#include <new>
12
#include <type_traits>
13
#include <utility>
14
#include <vector>
15
16
#ifdef USE_MIMALLOC
17
#include <mimalloc.h>
18
#endif
19
20
namespace
gpr::memory
{
21
22
#ifdef USE_MIMALLOC
23
namespace
detail {
24
25
inline
std::shared_ptr<mi_heap_t> makeHeap()
26
{
27
auto
* heap = mi_heap_new();
28
if
(!heap)
throw
std::bad_alloc();
29
return
{heap, mi_heap_destroy};
30
}
31
32
}
// namespace detail
33
34
class
ScopedHeap
;
35
36
// Container copies and allocator rebindings share ownership of the heap.
37
// Individual buffers stay allocated until the last owner releases the heap;
38
// containers still run every element destructor and own nested allocations.
39
template
<
typename
T>
40
class
HeapAllocator
{
41
public
:
42
using
value_type = T;
43
using
size_type = std::size_t;
44
using
difference_type = std::ptrdiff_t;
45
using
is_always_equal = std::false_type;
46
using
propagate_on_container_move_assignment = std::true_type;
47
using
propagate_on_container_swap = std::true_type;
48
49
HeapAllocator
() : heap_(detail::makeHeap()) { }
50
51
template
<
typename
U>
52
HeapAllocator
(
const
HeapAllocator<U>
& other) noexcept : heap_(other.heap_)
53
{
54
}
55
56
[[nodiscard]] T* allocate(std::size_t count)
57
{
58
if
(count > max_size())
throw
std::bad_array_new_length();
59
if
(!heap_)
throw
std::bad_alloc();
60
auto
*
memory
=
61
mi_heap_malloc_aligned(heap_.get(), count *
sizeof
(T),
alignof
(T));
62
if
(!
memory
)
throw
std::bad_alloc();
63
return
static_cast<
T*
>
(
memory
);
64
}
65
66
void
deallocate(T*, std::size_t)
noexcept
{ }
67
68
std::size_t max_size()
const
noexcept
69
{
70
return
std::numeric_limits<std::size_t>::max() /
sizeof
(T);
71
}
72
73
template
<
typename
U>
74
bool
operator==(
const
HeapAllocator<U>
& other)
const
noexcept
75
{
76
return
heap_.get() == other.heap_.get();
77
}
78
79
template
<
typename
U>
80
bool
operator!=(
const
HeapAllocator<U>
& other)
const
noexcept
81
{
82
return
!(*
this
== other);
83
}
84
85
private
:
86
explicit
HeapAllocator
(std::shared_ptr<mi_heap_t> heap) noexcept
87
: heap_(std::move(heap))
88
{
89
}
90
91
std::shared_ptr<mi_heap_t> heap_;
92
template
<
typename
U>
93
friend
class
HeapAllocator
;
94
friend
class
ScopedHeap
;
95
};
96
#else
97
template
<
typename
T>
98
using
HeapAllocator
= std::allocator<T>;
99
#endif
100
101
template
<
typename
T>
102
using
HeapVector
= std::vector<T, HeapAllocator<T>>;
103
104
// Raw allocations belong to the heap. Allocators and containers retain shared
105
// ownership, so their storage can outlive this handle. Bulk destruction frees
106
// buffer storage; it does not replace C++ element or nested-object destruction.
107
class
ScopedHeap
{
108
public
:
109
#ifdef USE_MIMALLOC
110
ScopedHeap
() : heap_(detail::makeHeap()) { }
111
#else
112
ScopedHeap
() =
default
;
113
#endif
114
ScopedHeap
(
const
ScopedHeap
&) =
delete
;
115
ScopedHeap
&
operator=
(
const
ScopedHeap
&) =
delete
;
116
ScopedHeap
(
ScopedHeap
&&) noexcept = default;
117
ScopedHeap
& operator=(
ScopedHeap
&&) noexcept = default;
118
119
template <typename T>
120
HeapAllocator
<T>
allocator
()
const
121
{
122
#ifdef USE_MIMALLOC
123
if
(!heap_)
throw
std::bad_alloc();
124
return
HeapAllocator<T>
(heap_);
125
#else
126
return
HeapAllocator<T>
();
127
#endif
128
}
129
130
template
<
typename
T>
131
HeapVector<T>
make_vector
()
const
132
{
133
return
HeapVector<T>
(
allocator<T>
());
134
}
135
136
#ifdef USE_MIMALLOC
137
mi_heap_t* get() const noexcept
138
{
139
return
heap_.get();
140
}
141
142
void
* allocate(std::size_t size)
143
{
144
if
(!heap_)
throw
std::bad_alloc();
145
return
mi_heap_malloc(heap_.get(), size);
146
}
147
148
void
* allocate_aligned(std::size_t size, std::size_t alignment)
149
{
150
if
(!heap_)
throw
std::bad_alloc();
151
return
mi_heap_malloc_aligned(heap_.get(), size, alignment);
152
}
153
154
private
:
155
std::shared_ptr<mi_heap_t> heap_;
156
#endif
157
};
158
159
}
// namespace gpr::memory
160
161
#endif
// GPR_MEMORY_SCOPEDHEAP_H_
gpr::memory::ScopedHeap
Definition
ScopedHeap.h:107
gpr::memory::ScopedHeap::ScopedHeap
ScopedHeap(const ScopedHeap &)=delete
gpr::memory::ScopedHeap::ScopedHeap
ScopedHeap()=default
gpr::memory::ScopedHeap::operator=
ScopedHeap & operator=(const ScopedHeap &)=delete
gpr::memory::ScopedHeap::allocator
HeapAllocator< T > allocator() const
Definition
ScopedHeap.h:120
gpr::memory::ScopedHeap::make_vector
HeapVector< T > make_vector() const
Definition
ScopedHeap.h:131
gpr::memory::ScopedHeap::ScopedHeap
ScopedHeap(ScopedHeap &&) noexcept=default
gpr::memory
Definition
ScopedHeap.h:20
gpr::memory::HeapVector
std::vector< T, HeapAllocator< T > > HeapVector
Definition
ScopedHeap.h:102
gpr::memory::HeapAllocator
std::allocator< T > HeapAllocator
Definition
ScopedHeap.h:98
gpr
memory
ScopedHeap.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf