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

Class custom_allocator

tests/custom_allocator_tests.cpp:40–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected