-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.cpp
executable file
·178 lines (147 loc) · 3.65 KB
/
wrapper.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* A wrappper for allocator.cpp.
*
* Do not modify this file.
*/
#include "wrapper.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <pthread.h>
#include "allocator.cpp"
#include <unistd.h>
static pthread_mutex_t my_malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
static bool is_my_malloc_initialized = false;
#ifdef VALIDATE
static int seq = 0;
#ifdef USE_ONE_LOG
static std::ofstream *_log = 0;
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
#else
__thread static std::ofstream *_log = 0;
#endif
std::ofstream& getLog() {
#ifdef USE_ONE_LOG
pthread_mutex_lock(&log_mutex);
if (_log == 0) {
_log = new std::ofstream("tmp/1.out");
}
pthread_mutex_unlock(&log_mutex);
#else
// log is thread-local, so we don't need to lock.
if (_log == 0) {
char filename[20];
sprintf(filename, "tmp/%u.out", (unsigned int)pthread_self());
_log = new std::ofstream(filename);
}
#endif
return *_log;
}
#endif
void my_malloc_init()
{
pthread_mutex_lock(&my_malloc_mutex);
if (!is_my_malloc_initialized) {
#ifdef MYMALLOC
std::cout << "Using custom allocator.\n";
#endif
#ifdef VALIDATE
std::cout << "Running vaildate version.\n";
#endif
// Initialize memlib. (Students will not call this in their initialization.)
mem_init();
my::allocator::init();
is_my_malloc_initialized = true;
}
pthread_mutex_unlock(&my_malloc_mutex);
}
void my_free(void *ptr)
{
#ifdef VALIDATE
int i = __sync_fetch_and_add(&seq, 1);
std::ofstream& log = getLog();
#ifdef USE_ONE_LOG
pthread_mutex_lock(&log_mutex);
#endif /* USE_ONE_LOG */
log << i << " free " << ptr << "\n";
#ifdef USE_ONE_LOG
pthread_mutex_unlock(&log_mutex);
#endif /* USE_ONE_LOG */
#endif /* VALIDATE */
my::allocator::free(ptr);
}
void *my_malloc(size_t size)
{
if (!is_my_malloc_initialized) {
my_malloc_init();
}
void* ret = my::allocator::malloc(size);
#ifdef VALIDATE
int i = __sync_fetch_and_add(&seq, 1);
std::ofstream& log = getLog();
#ifdef USE_ONE_LOG
pthread_mutex_lock(&log_mutex);
#endif /* USE_ONE_LOG */
log << i << " malloc " << size << " " << ret << "\n";
#ifdef USE_ONE_LOG
pthread_mutex_unlock(&log_mutex);
#endif /* USE_ONE_LOG */
#endif /* VALIDATE */
return ret;
}
void *my_realloc(void* ptr, size_t size)
{
#ifdef VALIDATE
int i = __sync_fetch_and_add(&seq, 1);
std::ofstream& log = getLog();
#ifdef USE_ONE_LOG
pthread_mutex_lock(&log_mutex);
#endif /* USE_ONE_LOG */
log << i << " realloc-begin " << ptr << " " << size << "\n";
#ifdef USE_ONE_LOG
pthread_mutex_unlock(&log_mutex);
#endif /* USE_ONE_LOG */
#endif /* VALIDATE */
void* ret = my::allocator::realloc(ptr, size);
#ifdef VALIDATE
i = __sync_fetch_and_add(&seq, 1);
#ifdef USE_ONE_LOG
pthread_mutex_lock(&log_mutex);
#endif /* USE_ONE_LOG */
log << i << " realloc-end " << ptr << " " << size << " " << ret << "\n";
#ifdef USE_ONE_LOG
pthread_mutex_unlock(&log_mutex);
#endif /* USE_ONE_LOG */
#endif /* VALIDATE */
return ret;
}
//
// Hooks for benchmark programs
//
void end_thread() {
#ifdef VALIDATE
#ifndef USE_ONE_LOG
getLog().close();
pthread_mutex_lock(&my_malloc_mutex);
std::cerr << "Log file: " << (unsigned int) pthread_self() << "\n";
pthread_mutex_unlock(&my_malloc_mutex);
#endif /* USE_ONE_LOG */
#endif /* VALIDATE */
}
void end_program() {
#ifdef MYMALLOC
std::cerr << "Heap size: " << mem_heapsize() << "\n";
#ifdef VALIDATE
#ifdef USE_ONE_LOG
getLog().close();
pthread_mutex_lock(&my_malloc_mutex);
std::cerr << "Log file: 1\n";
pthread_mutex_unlock(&my_malloc_mutex);
#else
// End main thread
end_thread();
#endif /* USE_ONE_LOG */
#endif /* VALIDATE */
#endif /* MYMALLOC */
}