-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper.cpp
204 lines (174 loc) · 4.47 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* A wrappper for allocator.cpp.
*
* Do not modify this file.
*/
#include "wrapper.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stdio.h>
#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 *file_log = 0;
static std::stringstream *string_log = 0;
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
#else
__thread static std::ofstream *file_log = 0;
__thread static std::stringstream *string_log = 0;
#endif
std::ofstream& getFileLog() {
#ifdef USE_ONE_LOG
pthread_mutex_lock(&log_mutex);
if (file_log == 0) {
file_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 (file_log == 0) {
char filename[20];
sprintf(filename, "tmp/%u.out", (unsigned int)pthread_self());
file_log = new std::ofstream(filename);
}
#endif
return *file_log;
}
std::stringstream& getStringLog() {
#ifdef USE_ONE_LOG
pthread_mutex_lock(&log_mutex);
if (string_log == 0) {
string_log = new std::stringstream("");
}
pthread_mutex_unlock(&log_mutex);
#else
// log is thread-local, so we don't need to lock.
if (string_log == 0) {
//char filename[20];
//sprintf(filename, "tmp/%u.out", (unsigned int)pthread_self());
string_log = new std::stringstream("");
}
#endif
return *string_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 validate 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::stringstream& log = getStringLog();
#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::stringstream& log = getStringLog();
#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::stringstream& log = getStringLog();
#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();
std::string logString = getStringLog().str();
getFileLog() << logString;
getFileLog().close();
pthread_mutex_lock(&my_malloc_mutex);
std::cerr << "Log file: " << (unsigned int) pthread_self() << "\n";
pthread_mutex_unlock(&my_malloc_mutex);
#else
getFileLog() << getStringLog().str();
getFileLog().close();
#endif /* USE_ONE_LOG */
#endif /* VALIDATE */
}
void end_program() {
#ifdef MYMALLOC
std::cerr << "Heap size: " << mem_heapsize() << "\n";
#ifdef VALIDATE
#ifdef USE_ONE_LOG
getFileLog() << getStringLog().str();
getFileLog().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 */
}