-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemControllerAgent.cpp
116 lines (102 loc) · 2.88 KB
/
MemControllerAgent.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "MemControllerAgent.h"
#include "MemLeakControllerService.h"
#include "MemoryEntry.h"
#include "malloc_allocator.hpp"
#include "stack_helper.h"
#include <stdint.h>
#include <iostream>
#include <atomic>
#include <mutex>
#include <unordered_set>
#include <algorithm>
#include <string>
#include <sstream>
static const std::thread::id main_thdr_id = std::this_thread::get_id();
class MemControllerAgentImpl final
: public MemLeakControllerService::Client
{
public:
MemControllerAgentImpl(const std::thread::id& tid_)
: tid(tid_)
{
malloc_create(&alloc_entry_table);
malloc_create(&dealloc_entry_table);
malloc_create(&should_lock, false);
malloc_create(&guard);
std::cout << "Hello! from thread: " << tid << "\n";
MemLeakControllerService::GetInstance()->RegisterNewAgent(this);
}
~MemControllerAgentImpl()
{
std::cout << "Bye! from thread: " << tid << "\n";
MemLeakControllerService::GetInstance()->UnRegisterNewAgent(this);
free_new(alloc_entry_table);
free_new(dealloc_entry_table);
free_new(guard);
free_new(should_lock);
}
void OnNewAllocation(void* ptr, size_t n)
{
std::unique_lock<std::mutex> lock(*guard);
auto res = alloc_entry_table->insert(entry_t{ptr, n, tid, ""});
if (!res.second) {
std::cerr << "this cannot ben possible!" << std::endl;
}
}
void OnDelAllocation(void* ptr)
{
std::unique_lock<std::mutex> lock(*guard);
auto res = dealloc_entry_table->insert(entry_t{ptr, tid });
if (!res.second) {
std::cerr << "this cannot ben possible!" << std::endl;
}
}
size_t NumberOfAllocations() const override
{
std::unique_lock<std::mutex> lock(*guard);
return alloc_entry_table->size();
}
const std::thread::id& GetWorkingThread() const override
{
return tid;
}
void DumpCurrentAllocations(entry_set_t* g_alloc_entry_table,
entry_set_t* g_dealloc_entry_table) override
{
std::unique_lock<std::mutex> lock(*guard);
g_alloc_entry_table->insert(alloc_entry_table->begin(),
alloc_entry_table->end());
g_dealloc_entry_table->insert(dealloc_entry_table->begin(),
dealloc_entry_table->end());
}
private:
entry_set_t *alloc_entry_table;
entry_set_t *dealloc_entry_table;
std::atomic_bool* should_lock;
std::mutex* guard;
std::thread::id tid;
};
MemControllerAgent::MemControllerAgent()
{
auto thid = std::this_thread::get_id();
malloc_create(&p_impl, thid);
}
MemControllerAgent::~MemControllerAgent() {
// do not release main thread memory controller agent due to seg faults on static dtors.
if (main_thdr_id != p_impl->GetWorkingThread())
{
free_new(p_impl);
}
//else
//{
// std::cerr << "Upps I leaked [" << sizeof(MemControllerAgentImpl) << "] more bytes but it's okay!\n";
//}
}
void MemControllerAgent::allocated(void* ptr, size_t n) noexcept
{
p_impl->OnNewAllocation(ptr, n);
}
void MemControllerAgent::deallocated(void* ptr) noexcept
{
p_impl->OnDelAllocation(ptr);
}