-
Notifications
You must be signed in to change notification settings - Fork 0
/
memgrind.c
277 lines (217 loc) · 7.08 KB
/
memgrind.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "mymalloc.h"
double testCaseA() {
int x, y;
double time_total = 0;
struct timeval start;
struct timeval end;
char *pointer;
// take the average of 100 trials
for (x = 0; x < 100; x++) {
// start the timer
gettimeofday(&start, 0);
// malloc/free 150 times consecutively
for (y = 0; y < 150; y++) {
// allocate 1 byte and immediately free it
pointer = malloc(1);
free(pointer);
}
//end the timer
gettimeofday(&end, 0);
// calculate the duration of a single malloc/free call and add to total
time_total +=((end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec));
}
// return the average time elapsed
return time_total/100;
}
double testCaseB() {
int x, y, z, j, s=0, e=50;
double time_total = 0;
struct timeval start;
struct timeval end;
char *pointers[50]; //
// take the average of 100 trials
for(x=0; x<100; x++){
s = 0;
e = 50;
//start the timer
gettimeofday(&start, 0);
//fill out the array in three chunks
for(j=0; j<3; j++){
//save 50 pointers in the array
for(y=s; y<e; y++){
pointers[y] = (char*)malloc(1);
//Null check in case pointer fails
if (pointers[y] == NULL) {
return -1;
}
}
//free 50 pointers one by one
for(z=s; z<e; z++){
free(pointers[z]);
}
}
//end the timer
gettimeofday(&end, 0);
// calculate the duration of a single malloc/free call and add to total
time_total +=((end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec));
}
return time_total/100;
}
double testCaseC(){
int x, r, y, h, l;
struct timeval start;
struct timeval end;
char* pointers[50];
double time_total = 0;
for(x=0; x<100; x++){
y=0;
l=0;
gettimeofday(&start, 0);
while(1){
r = rand() % 2;
if(r==0){
pointers[y] = (char*)malloc(1);
if(pointers[y] == NULL){
return -1;
}
y++;
l++;
if(l == 50){break;}
}
else{
if(y != 0){
free(pointers[y-1]);
y--;
}
}
}
for(h=0; h<y; h++){
free(pointers[h]);
}
//end the timer
gettimeofday(&end, 0);
// calculate the duration of a single malloc/free call and add to total
time_total +=((end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec));
}
// return the average time elapsed
return time_total/100;
}
double testCaseD(){
int x, r, y, h, capacity, l;
struct timeval start;
struct timeval end;
char* pointers[50];
double time_total = 0;
// take the average of 100 trials
for(x=0; x<100; x++){
y=0;
l=0;
//start the timer
gettimeofday(&start, 0);
while(1){
r = rand() % 2; //randomly choose between malloc and free
if(r==0){
capacity = rand() % 63 + 1; //choose random number between 1 and 64
pointers[y] = (char*)malloc(capacity);
if(pointers[y] == NULL){ //Null check
return -1;
}
y++;
l++;
if(l == 50){break;}
}
else{
if(y != 0){
free(pointers[y-1]);
y--;
}
}
}
for(h=0; h<y; h++){
free(pointers[h]);
}
//end the timer
gettimeofday(&end, 0);
// calculate the duration of a single malloc/free call and add to total
time_total +=((end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec));
}
// return the average time elapsed
return time_total/100;
}
double testCaseE(){
int x, y, w, k, z;
struct timeval start;
struct timeval end;
char* pointers[49];
double time_total = 0;
for(x=0; x<100; x++){
//start the timer
gettimeofday(&start, 0);
for(y=0; y<49; y++){
pointers[y] = (char*)malloc(60);
}
//freeing each other pointer
for(w=0; w<49; w=w+2){
free(pointers[w]);
}
//mallocing the freed pointer with space thats different from the original one
for(k=0; k<49; k=k+2){
pointers[k] = (char*)malloc(50);
}
//freeinng all pointers
for(z=0; z<49; z++){
free(pointers[z]);
}
//end the timer
gettimeofday(&end, 0);
// calculate the duration of a single malloc/free call and add to total
time_total +=((end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec));
}
// return the average time elapsed
return time_total/100;
}
double testCaseF(){
int x, y, w, k, z;
struct timeval start;
struct timeval end;
char* pointers[49];
double time_total = 0;
for(x=0; x<100; x++){
//start the timer
gettimeofday(&start, 0);
for(y=0; y<49; y++){
pointers[y] = (char*)malloc(60);
}
//freeing each other pointer
for(w=0; w<49; w=w+2){
free(pointers[w]);
}
//mallocing the freed pointer with space thats different from the original one
for(k=0; k<49; k=k+2){
pointers[k] = (char*)malloc(30);
}
//freeinng all pointers
for(z=0; z<49; z++){
free(pointers[z]);
}
//end the timer
gettimeofday(&end, 0);
// calculate the duration of a single malloc/free call and add to total
time_total +=((end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec));
}
// return the average time elapsed
return time_total/100;
}
int main(int argc, char* argv[]) {
printf("Testcase A took on average %.2f microseconds to complete.\n", testCaseA());
printf("Testcase B took on average %.2f microseconds to complete.\n", testCaseB());
printf("Testcase C took on average %.2f microseconds to complete.\n", testCaseC());
printf("Testcase D took on average %.2f microseconds to complete.\n", testCaseD());
printf("Testcase E took on average %.2f microseconds to complete.\n", testCaseE());
printf("Testcase F took on average %.2f microseconds to complete.\n", testCaseF());
return 0;
}