-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
118 lines (103 loc) · 3.12 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include "lib/alloc.h"
#include "lib/batch.h"
#include "lib/random.h"
#include "lib/file.h"
#include "lib/leaky.h"
#define MAX_ALLOC 50 //Max number of allocations per round
#define MAX_SIZE 512 //Max bytes to allocate
#define MIN_SIZE 32 //Min bytes to allocate
#define ROUNDS 5 //Number of rounds
extern FILE* fp;
static void (*allocation_method)(ALLOCATION*, int, size_t, size_t, int) = NULL;
//Helpers
void display_help(char* str);
bool set_method(char* str);
bool set_file(char* str);
int main(int argc, char** argv) {
if (argc > 3)
display_help(argv[0]);
if (argc == 3) {
if (!(set_method(argv[1]) && set_file(argv[2])))
display_help(argv[0]);
} else if (argc == 2) {
if (argv[1][0] == '-') { //Argument is option
if (!set_method(argv[1]))
display_help(argv[0]);
fp = stdout;
} else {
if (!set_file(argv[1])) //Argument is filename
display_help(argv[0]);
allocation_method = batch_allocate;
}
} else { //Defaults
fp = stdout;
allocation_method = batch_allocate;
}
srand((unsigned int) time(0));
ALLOCATION tmp_ptr;
//simple zeromem-type loop
ALLOCATION buf[MAX_ALLOC];
for (int i = 0; i < MAX_ALLOC; ++i) {
buf[i].ptr = NULL;
buf[i].size = 0;
buf[i].valid = false;
}
//First malloc is always in different virtual page for some reason.
my_silent_malloc(&tmp_ptr, MIN_SIZE, MAX_SIZE);
allocation_method(buf, MAX_ALLOC, MIN_SIZE, MAX_SIZE, ROUNDS);
if (fp != stdout) {
fclose(fp);
}
//Freeing that temporary pointer before exiting.
my_silent_free(&tmp_ptr);
return 0;
}
void display_help(char* str) {
printf("usage: %s [option] [filename]\n\n", str);
puts("File is set to stdout by default");
puts(" -b => Batch allocation (default)");
puts(" -r => Random allocation");
puts(" -l => Leaky allocation");
puts(" -bm, -rm, -lm => Above methods with minified output");
exit(EXIT_FAILURE);
}
bool set_method(char* str) {
size_t len = strlen(str);
if (!(len == 2 || len == 3)) {
puts("ERR: Invalid flag specified.");
return false;
}
if (len == 3 && str[2] != 'm') {
puts("ERR: Invalid modifier (did you mean to enable minified mode?)");
return false;
} else if (len == 3) {
minified = 1;
}
char ch = str[1];
switch (ch) {
case 'b':
allocation_method = batch_allocate;
return true;
case 'r':
allocation_method = random_allocate;
return true;
case 'l':
allocation_method = leaky_allocate;
return true;
default:
puts("ERR: Flag not recognized.");
return false;
}
}
bool set_file(char* str) {
if ((fp = fopen(str, "w")) == NULL) {
puts("ERR: Could not open file! (Check permissions?)");
return false;
}
return true;
}