-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheap.c
79 lines (67 loc) · 1.34 KB
/
heap.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#define SIZE_GiB (1ULL << 30)
int set_malloc;
int set_sbrk;
void usage(){
printf("Usage: ./heap [OPTION]\n");
printf("Allocates requested amount of memory\n\n");
printf("-m, --malloc uses malloc to allocate memory\n");
printf("-s, --sbrk uses sbrk to allocate memory\n");
printf("-h, --help prints out help message\n");
exit(EXIT_FAILURE);
}
void parse_options (int argc, char *argv[]){
int long_index = 0;
int option;
struct option long_options [] =
{
{"malloc", no_argument, 0, 'm'},
{"sbrk", no_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{0,0,0,0}
};
while ((option = getopt_long(argc,argv,"smh",long_options, &long_index)) != -1){
switch (option){
case 'm' :
set_malloc = 1;
break;
case 's' :
set_sbrk = 1;
break;
case 'h':
usage();
default:
usage();
}
}
}
int main(int argc, char *argv [])
{
int i=0;
void *ptr;
parse_options (argc,argv);
if (set_malloc == 1 && set_sbrk == 1)
usage();
else if (set_malloc == 1){
for(;;){
ptr = malloc(1 * SIZE_GiB);
if (ptr == NULL)
break;
i++;
}
free (ptr);
}
else if (set_sbrk == 1){
for(;;){
ptr = sbrk(1 * SIZE_GiB);
if (ptr == (void *)-1)
break;
i++;
}
}
printf("%d GiB allocated.\n",i);
return 0;
}