mitteLib
Loading...
Searching...
No Matches
tracker_allocator.hpp
Go to the documentation of this file.
1//
2// Created by spak on 9/20/22.
3//
4
5#ifndef MITTELIB_TRACKER_ALLOCATOR_HPP
6#define MITTELIB_TRACKER_ALLOCATOR_HPP
7
8#include <array>
9#include <atomic>
10#include <memory>
11
12namespace mlab {
13
15 std::atomic<std::size_t> _current;
16 std::atomic<std::size_t> _peak;
17
18 public:
19 [[nodiscard]] std::size_t current() const;
20 [[nodiscard]] std::size_t peak() const;
21
23
24 void update_peak(std::size_t new_current);
25
27
29
30 mem_counter &operator+=(std::size_t bytes);
31
32 mem_counter &operator-=(std::size_t bytes);
33
34 [[nodiscard]] static std::string format_mem(std::size_t bytes);
35 };
36
37 class mem_stats {
38 std::array<mem_counter, 20> _blocks;
40
41 [[nodiscard]] mem_counter const &by_size(std::size_t bytes) const;
42 [[nodiscard]] mem_counter &by_size(std::size_t bytes);
43
44 public:
45 [[nodiscard]] mem_counter const &total() const;
46 [[nodiscard]] mem_counter const &block_by_size(std::size_t bytes) const;
47
48 void print_stats() const;
49
50 [[nodiscard]] static mem_stats &instance();
51
52 void allocate(std::size_t bytes);
53
54 void deallocate(std::size_t bytes);
55 };
56
57 template <class T>
59 using value_type = T;
60
61 tracker_allocator() noexcept = default;
62 tracker_allocator(tracker_allocator &&) noexcept = default;
63 tracker_allocator(tracker_allocator const &) noexcept = default;
64
65 template <class U>
66 tracker_allocator(tracker_allocator<U> const &) noexcept {}
67
68 value_type *allocate(std::size_t n) {
70 return static_cast<value_type *>(::operator new(n * sizeof(value_type)));
71 }
72
73 void deallocate(value_type *p, std::size_t n) {
75 ::operator delete(p);
76 }
77 };
78
79
80 template <class T, class U>
81 bool operator==(tracker_allocator<T> const &, tracker_allocator<U> const &) noexcept {
82 return true;
83 }
84
85 template <class T, class U>
86 bool operator!=(tracker_allocator<T> const &x, tracker_allocator<U> const &y) noexcept {
87 return !(x == y);
88 }
89
90
91}// namespace mlab
92
93#endif//MITTELIB_TRACKER_ALLOCATOR_HPP
Definition tracker_allocator.hpp:14
mem_counter & operator-=(std::size_t bytes)
Definition tracker_allocator.cpp:46
mem_counter & operator--()
Definition tracker_allocator.cpp:32
std::atomic< std::size_t > _peak
Definition tracker_allocator.hpp:16
mem_counter()
Definition tracker_allocator.cpp:10
std::size_t peak() const
Definition tracker_allocator.cpp:16
std::atomic< std::size_t > _current
Definition tracker_allocator.hpp:15
std::size_t current() const
Definition tracker_allocator.cpp:12
mem_counter & operator+=(std::size_t bytes)
Definition tracker_allocator.cpp:41
static std::string format_mem(std::size_t bytes)
Definition tracker_allocator.cpp:56
mem_counter & operator++()
Definition tracker_allocator.cpp:27
void update_peak(std::size_t new_current)
Definition tracker_allocator.cpp:20
Definition tracker_allocator.hpp:37
static mem_stats & instance()
Definition tracker_allocator.cpp:109
mem_counter const & total() const
Definition tracker_allocator.cpp:123
void allocate(std::size_t bytes)
Definition tracker_allocator.cpp:114
void print_stats() const
Definition tracker_allocator.cpp:70
void deallocate(std::size_t bytes)
Definition tracker_allocator.cpp:119
std::array< mem_counter, 20 > _blocks
Definition tracker_allocator.hpp:38
mem_counter _total
Definition tracker_allocator.hpp:39
mem_counter const & block_by_size(std::size_t bytes) const
Definition tracker_allocator.cpp:126
mem_counter const & by_size(std::size_t bytes) const
Definition tracker_allocator.cpp:95
Definition log.cpp:8
bool operator!=(tracker_allocator< T > const &x, tracker_allocator< U > const &y) noexcept
Definition tracker_allocator.hpp:86
bool operator==(tracker_allocator< T > const &, tracker_allocator< U > const &) noexcept
Definition tracker_allocator.hpp:81
Definition tracker_allocator.hpp:58
void deallocate(value_type *p, std::size_t n)
Definition tracker_allocator.hpp:73
T value_type
Definition tracker_allocator.hpp:59
value_type * allocate(std::size_t n)
Definition tracker_allocator.hpp:68
tracker_allocator() noexcept=default