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
20namespace gpr::memory {
21
22#ifdef USE_MIMALLOC
23namespace detail {
24
25inline 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
34class 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.
39template <typename T>
40class HeapAllocator {
41public:
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
85private:
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
97template <typename T>
98using HeapAllocator = std::allocator<T>;
99#endif
100
101template <typename T>
102using 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.
108public:
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>
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>
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
154private:
155 std::shared_ptr<mi_heap_t> heap_;
156#endif
157};
158
159} // namespace gpr::memory
160
161#endif // GPR_MEMORY_SCOPEDHEAP_H_
ScopedHeap(const ScopedHeap &)=delete
ScopedHeap & operator=(const ScopedHeap &)=delete
HeapAllocator< T > allocator() const
Definition ScopedHeap.h:120
HeapVector< T > make_vector() const
Definition ScopedHeap.h:131
ScopedHeap(ScopedHeap &&) noexcept=default
std::vector< T, HeapAllocator< T > > HeapVector
Definition ScopedHeap.h:102
std::allocator< T > HeapAllocator
Definition ScopedHeap.h:98