-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocator.hh
60 lines (46 loc) · 1.51 KB
/
allocator.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include <memory>
#include <iostream>
namespace util::allocator {
void add_memory (uintmax_t amount);
void free_memory (uintmax_t amount);
uintmax_t peak (void);
uintmax_t total (void);
uintmax_t freed (void);
uintmax_t now (void);
template <typename T>
class allocator {
public:
using base = std::allocator<T>;
private:
base alloc_;
public:
using value_type = typename base::value_type;
using size_type = typename base::size_type;
using difference_type = typename base::difference_type;
using propagate_on_container_move_assignment = typename base::propagate_on_container_move_assignment;
using is_always_equal = typename base::is_always_equal;
template <typename... ARGS>
allocator (ARGS&&... args) : alloc_{ std::forward<ARGS>(args)... } {}
template <typename... ARGS>
[[nodiscard]] T* allocate (std::size_t n, ARGS&&... args) {
// Log allocation
add_memory(n * sizeof(T));
// Calls base allocator
return this->alloc_.allocate(n, std::forward<ARGS>(args)...);
}
template <typename... ARGS>
void deallocate (T* p, std::size_t n, ARGS&&... args) {
// Log deallocation
free_memory(n * sizeof(T));
// Calls base deallocator
this->alloc_.deallocate(p, n, std::forward<ARGS>(args)...);
}
bool operator == (allocator const& ot) const {
return this->alloc_ == ot.alloc_;
}
bool operator != (allocator const& ot) const {
return this->alloc_ != ot.alloc_;
}
};
};