MCPcopy Create free account
hub / github.com/Tessil/ordered-map / custom_allocator

Class custom_allocator

tests/custom_allocator_tests.cpp:39–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37
38template <typename T>
39class custom_allocator {
40 public:
41 using value_type = T;
42 using pointer = T*;
43 using const_pointer = const T*;
44 using reference = T&;
45 using const_reference = const T&;
46 using size_type = std::size_t;
47 using difference_type = std::ptrdiff_t;
48 using propagate_on_container_move_assignment = std::true_type;
49
50 template <typename U>
51 struct rebind {
52 using other = custom_allocator<U>;
53 };
54
55 custom_allocator() = default;
56 custom_allocator(const custom_allocator&) = default;
57
58 template <typename U>
59 custom_allocator(const custom_allocator<U>&) {}
60
61 pointer address(reference x) const noexcept { return &x; }
62
63 const_pointer address(const_reference x) const noexcept { return &x; }
64
65 pointer allocate(size_type n, const void* /*hint*/ = 0) {
66 nb_custom_allocs++;
67
68 pointer ptr = static_cast<pointer>(std::malloc(n * sizeof(T)));
69 if (ptr == nullptr) {
70#ifdef TSL_OH_NO_EXCEPTIONS
71 std::abort();
72#else
73 throw std::bad_alloc();
74#endif
75 }
76
77 return ptr;
78 }
79
80 void deallocate(T* p, size_type /*n*/) { std::free(p); }
81
82 size_type max_size() const noexcept {
83 return std::numeric_limits<size_type>::max() / sizeof(value_type);
84 }
85
86 template <typename U, typename... Args>
87 void construct(U* p, Args&&... args) {
88 ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
89 }
90
91 template <typename U>
92 void destroy(U* p) {
93 p->~U();
94 }
95};
96

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected