-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_bigmaac.c
103 lines (96 loc) · 2.93 KB
/
test_bigmaac.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
#include <stdio.h>
#include <stdlib.h>
#include "bigmaac.h"
#define N 20
int seed=0xbeef;
int checksum(int * p, size_t n ) {
int c = 0;
for (int i=0; i<n; i++) {
c+=p[i]*i;
}
return c;
}
int main() {
int * chunks[N];
int checksums[N];
int sizes[N];
int n_ints=DEFAULT_MIN_BIGMAAC_SIZE/(16*sizeof(int));
n_ints++; //make sure its big enough to trigger
//lets test some mallocs
fprintf(stderr,"Malloc\n");
for (int i=0; i<N; i++) {
int more=seed%100;
sizes[i]=more+n_ints;
chunks[i]=(int*)malloc(sizeof(int)*sizes[i]);
if (chunks[i]==NULL) {
fprintf(stderr,"Failed to malloc chunk %d\n",i);
exit(1);
}
for (int j=0; j<sizes[i]; j++) {
seed+=((seed%9)*(seed%7)+1)%(seed-1-seed%2);
chunks[i][j]=seed;
}
checksums[i]=checksum(chunks[i],sizes[i]);
fprintf(stdout,"%d %d\n",i,checksums[i]);
}
fprintf(stderr,"Malloc/Free\n");
for (int i=0; i<N; i++) {
seed+=((seed%9)*(seed%7)+1)%(seed-1-seed%2);
if (seed%(i+1)>i/2) {
free(chunks[i]);
chunks[i]=NULL;
if (seed%(i+1)<i/4) {
sizes[i]+=(seed%100);
chunks[i]=(int*)malloc(sizes[i]);
if (chunks[i]==NULL) {
fprintf(stderr,"Failed to malloc chunk 2 %d\n",i);
exit(1);
}
for (int j=0; j<sizes[i]; j++) {
seed+=((seed%9)*(seed%7)+1)%(seed-1-seed%2);
chunks[i][j]=seed;
}
checksums[i]=checksum(chunks[i],sizes[i]);
fprintf(stdout,"%d %d\n",i,checksums[i]);
}
}
}
fprintf(stderr,"Malloc/Free post\n");
for (int i=0; i<N; i++) {
if (chunks[i]!=NULL) {
checksums[i]=checksum(chunks[i],sizes[i]);
fprintf(stdout,"%d %d\n",i,checksums[i]);
}
}
fprintf(stderr,"Realloc\n");
for (int i=0; i<N; i++) {
if (chunks[i]==NULL) {
continue;
}
seed+=((seed%9)*(seed%7)+1)%(seed-1-seed%2);
if (seed%(i+1)>i/2) {
free(chunks[i]);
chunks[i]=NULL;
} else {
int old_size=sizes[i];
sizes[i]+=(seed%97);
if (seed%2==0) {
seed+=((seed%9)*(seed%7)+1)%(seed-1-seed%2);
sizes[i]-=(seed%101);
}
chunks[i]=(int*)realloc(chunks[i],sizeof(int)*sizes[i]);
if (sizes[i]>old_size) {
for (int j=old_size; j<sizes[i]; j++) {
seed+=((seed%9)*(seed%7)+1)%(seed-1-seed%2);
chunks[i][j]=seed;
}
}
}
}
for (int i=0; i<N; i++) {
if (chunks[i]!=NULL) {
checksums[i]=checksum(chunks[i],sizes[i]);
fprintf(stdout,"%d %d\n",i,checksums[i]);
}
}
}