forked from hxim/paq8px
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgramChecker.hpp
49 lines (41 loc) · 1.17 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
#pragma once
#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
*/
ProgramChecker& operator=(ProgramChecker const & /*unused*/) { return *this; }
static ProgramChecker *instance;
public:
static ProgramChecker* getInstance();
void alloc(uint64_t n);
void free(uint64_t n);
double getRuntime() const;
/**
* Print elapsed time and used memory
*/
void print() const;
~ProgramChecker();
};