forked from hxim/paq8px
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramChecker.hpp
51 lines (43 loc) · 1.34 KB
/
ProgramChecker.hpp
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
#ifndef PAQ8PX_PROGRAMCHECKER_HPP
#define PAQ8PX_PROGRAMCHECKER_HPP
#include <cassert>
#include <chrono>
#include <cinttypes>
#include <cstdint>
#include <cstdio>
/**
* Track time and memory used.
* @remark: only @ref Array<T> reports its memory usage, we don't know about other types
*/
class ProgramChecker {
private:
uint64_t memUsed {}; /**< Bytes currently in use (all allocated minus all freed) */
uint64_t maxMem {}; /**< Most bytes allocated ever */
std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
/**
* Constructor is private so that it cannot be called
*/
ProgramChecker() {
startTime = std::chrono::high_resolution_clock::now();
}
/**
* Copy constructor is private so that it cannot be called
*/
ProgramChecker(ProgramChecker const & /*unused*/) {}
/**
* Assignment operator is private so that it cannot be called
*/
auto operator=(ProgramChecker const & /*unused*/) -> ProgramChecker & { return *this; }
static ProgramChecker *instance;
public:
static auto getInstance() -> ProgramChecker *;
void alloc(uint64_t n);
void free(uint64_t n);
[[nodiscard]] auto getRuntime() const -> double;
/**
* Print elapsed time and used memory
*/
void print() const;
~ProgramChecker();
};
#endif //PAQ8PX_PROGRAMCHECKER_HPP