IDA C++ SDK 9.2
Loading...
Searching...
No Matches
qallocator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "qalloc_shim.hpp"
4
5// Adapts an AllocPolicy to the C++ Allocator requirements (C++17).
6template <class T, class AllocPolicy>
8{
9 using value_type = T;
10 using pointer = T *;
11 using const_pointer = const T *;
12 using void_pointer = void *;
13 using const_void_pointer = const void *;
14 using size_type = std::size_t;
15 using difference_type = std::ptrdiff_t;
16
17 // C++20 deprecates rebind, but allocator_traits still recognizes it in C++17.
18 template <class U> struct rebind
19 {
21 };
22
23 // Enforce emptiness/statelessness for ABI reasons:
24 static_assert(std::is_empty<AllocPolicy>::value,
25 "Allocator policy must be empty/stateless for ABI stability");
26
27 // Propagation and equality: all instances are interchangeable (stateless).
30 using propagate_on_container_swap = std::true_type;
31 using is_always_equal = std::true_type;
32
33 qallocator() noexcept = default;
34
35 template <class U>
36 qallocator(const qallocator<U, AllocPolicy> &) noexcept {}
37
38 // Allocate n objects of T with proper alignment. Throw on failure.
39 [[nodiscard]] pointer allocate(size_type n)
40 {
41 if ( n > max_size() )
42 throw std::bad_alloc();
43 const std::size_t bytes = n * sizeof(T);
44 void *p = AlignedAllocPolicy::aligned_allocate(bytes, alignof(T));
45 if ( !p )
46 throw std::bad_alloc();
47 return static_cast<pointer>(p);
48 }
49
50 void deallocate(pointer p, size_type /*n*/) noexcept
51 {
53 }
54
55 size_type max_size() const noexcept
56 {
57 return size_type(-1) / sizeof(T);
58 }
59
60 // All stateless allocators compare equal.
61 bool operator==(const qallocator &) const noexcept { return true; }
62 bool operator!=(const qallocator &) const noexcept { return false; }
63
64private:
65 // AllocPolicy is assumed to provide unaligned allocate/deallocate functions
66 // that are noexcept. We use this to build an aligned allocator.
68};
idaman size_t n
Definition pro.h:1000
Definition qalloc_shim.hpp:37
static void * aligned_allocate(std::size_t n, std::size_t al) noexcept
Definition qalloc_shim.hpp:43
static void aligned_deallocate(void *p, std::size_t al) noexcept
Definition qalloc_shim.hpp:59
Definition qallocator.hpp:19
qallocator< U, AllocPolicy > other
Definition qallocator.hpp:20
size_type max_size() const noexcept
Definition qallocator.hpp:55
const void * const_void_pointer
Definition qallocator.hpp:13
std::true_type is_always_equal
Definition qallocator.hpp:31
qallocator() noexcept=default
bool operator!=(const qallocator &) const noexcept
Definition qallocator.hpp:62
std::true_type propagate_on_container_move_assignment
Definition qallocator.hpp:29
void deallocate(pointer p, size_type) noexcept
Definition qallocator.hpp:50
const T * const_pointer
Definition qallocator.hpp:11
pointer allocate(size_type n)
Definition qallocator.hpp:39
std::true_type propagate_on_container_copy_assignment
Definition qallocator.hpp:28
T * pointer
Definition qallocator.hpp:10
bool operator==(const qallocator &) const noexcept
Definition qallocator.hpp:61
std::ptrdiff_t difference_type
Definition qallocator.hpp:15
std::size_t size_type
Definition qallocator.hpp:14
std::true_type propagate_on_container_swap
Definition qallocator.hpp:30
T value_type
Definition qallocator.hpp:9
void * void_pointer
Definition qallocator.hpp:12