vecmem 1.18.0
Loading...
Searching...
No Matches
unique_alloc_deleter.hpp
1/*
2 * VecMem project, part of the ACTS project (R&D line)
3 *
4 * (c) 2021-2025 CERN for the benefit of the ACTS project
5 *
6 * Mozilla Public License Version 2.0
7 */
8
9#pragma once
10
11#include <cassert>
12#include <type_traits>
13
14#include "vecmem/memory/memory_resource.hpp"
15#include "vecmem/vecmem_core_export.hpp"
16
17namespace vecmem::details {
32template <typename T>
34 /*
35 * Similarly, we cannot destroy objects, so we need to ensure that
36 * deallocation of objects is semantically equivalent to their destruction.
37 */
38 static_assert(std::is_trivially_destructible_v<std::remove_extent_t<T>>,
39 "Allocation pointer type must be trivially destructible.");
40
41public:
48 unique_alloc_deleter(void) = default;
49
61 unique_alloc_deleter(memory_resource& mr, std::size_t s, std::size_t a = 0)
62 : m_mr(&mr), m_size(s), m_align(a) {}
63
70
77
86
95 default;
96
102 void operator()(void* p) const {
103 assert(m_mr != nullptr || m_size == 0u);
104
105 /*
106 * Non-null pointers with a zero size can happen.
107 */
108 if (m_size == 0u) {
109 return;
110 }
111
112 /*
113 * Deallocate the memory that we were using.
114 */
115 if (m_align > 0) {
116 m_mr->deallocate(p, m_size, m_align);
117 } else {
118 m_mr->deallocate(p, m_size);
119 }
120 }
121
122private:
123 memory_resource* m_mr;
124 std::size_t m_size;
125 std::size_t m_align;
126};
127} // namespace vecmem::details
An allocator class that wraps a memory resource.
Definition allocator.hpp:37
Namespace for types that should not be used directly by clients.
Definition array.hpp:23
A deleter class for trivial allocations.
Definition unique_alloc_deleter.hpp:33
unique_alloc_deleter(void)=default
Default-construct a new unique allocation deleter.
unique_alloc_deleter & operator=(const unique_alloc_deleter &i)=default
Copy-assign a unique allocation deleter.
unique_alloc_deleter(unique_alloc_deleter &&i) noexcept=default
Move a unique allocation deleter.
unique_alloc_deleter(memory_resource &mr, std::size_t s, std::size_t a=0)
Construct a new unique allocation deleter.
Definition unique_alloc_deleter.hpp:61
unique_alloc_deleter & operator=(unique_alloc_deleter &&i) noexcept=default
Move-assign a unique allocation deleter.
void operator()(void *p) const
Activate the deletion mechanism of the deleter.
Definition unique_alloc_deleter.hpp:102
unique_alloc_deleter(const unique_alloc_deleter &i)=default
Copy a unique allocation deleter.