forked from HaoLi1996/p2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject2.c
192 lines (171 loc) · 4.04 KB
/
project2.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
// project2.c
// Author: Ata Fatahi Baarzi @mailto [email protected]
//Description: Parser and Handler for Project 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <stdint.h>
extern void setup( int malloc_type, int mem_size, void* start_of_memory );
extern void *my_malloc( int size );
extern void my_free( void *ptr );
FILE *fd;
struct handle{
char _handle;
int ** addresses;
struct handle * next;
int num_allocs;
};
typedef struct handle handle_t;
struct ops{
int numops ;
char handle;
char type;
int size ;
};
typedef struct ops ops_t;
int open_file(char *filename)
{
char mode = 'r';
fd = fopen(filename, &mode);
if (fd == NULL)
{
printf("Invalid input file specified: %s\n", filename);
return -1;
}
else
{
return 0;
}
}
void close_file()
{
fclose(fd);
}
int read_next_ops(ops_t * op){
char line[1024];
if(!fgets(line, 1024, fd)) {
return 0;
}
char *token;
char* rest=line;
if(token=strtok_r(rest, " ", &rest)) {
op->handle = *token;
} else {
return 0;
}
if(token=strtok_r(rest, " ", &rest)) {
op->numops = atof(token);
} else {
return 0;
}
if(token=strtok_r(rest, " ", &rest)) {
op->type = *token;
} else {
return 0;
}
if(token=strtok_r(rest, " ", &rest)) {
op->size = atof(token);
} else {
return 0;
}
return 1;
}
int main(int argc, char *argv[]){
if (argc < 3)
{
printf ("Not enough parameters specified. Usage: a.out <allocation_type> <input_file>\n");
printf (" Allocation type: 0 - Buddy System\n");
printf (" Allocation type: 1 - Slab Allocation\n");
return -1;
}
if (open_file(argv[2]) < 0){
return -1;
}
int size;
int RAM_SIZE=1<<20;//1024*1024
void* RAM=malloc(RAM_SIZE);//1024*1024
setup(atoi(argv[1]),RAM_SIZE,RAM);
handle_t * handles = NULL;
ops_t * op = (ops_t*) malloc(sizeof(ops_t));
while(read_next_ops(op)){
if(op->type == 'M'){//do my_alloc
if (handles == NULL){
handles = (handle_t *) malloc(sizeof(handle_t));
handles->_handle = op->handle;
handles->num_allocs = 0;
handles->addresses = (int**)malloc(sizeof(int*)*(op->numops+1));
//printf("handles %c",handles->_handle);
handles->next = NULL;
for (int i=1; i<= op->numops; i++){
void * x = my_malloc(op->size);
if(!(intptr_t)x || (intptr_t)x == -1){
if( handles != NULL && handles->num_allocs == 0)
{ free(handles);
handles = NULL;
}
printf("Allocation Error %c\n",op->handle );
break;
}
else
{
*(handles->addresses+i) = (int*)x;
handles->num_allocs+=1;
printf("Start of first Chunk %c is: %d\n",op->handle, (int)((void*)(*(handles->addresses+i)) - RAM));
}
}
}
else{
handle_t * hp = handles;
// printf("handles %c",hp->_handle);
while (hp->next != NULL){
//printf("handles %c",hp->_handle);
hp = hp->next;
}
hp->next = (handle_t *) malloc(sizeof(handle_t));
hp->next->_handle = op->handle;
hp->next->num_allocs = 0;
hp->next->addresses = (int**)malloc(sizeof(int*)*(op->numops+1));
hp->next->next= NULL;
for (int i=1; i<= op->numops; i++){
void * x = my_malloc(op->size);
if(!(intptr_t)x || (intptr_t)x == -1){
if(hp->next != NULL && hp->next->num_allocs == 0)
{
free(hp->next);
hp->next = NULL;
}
printf("Allocation Error %c\n",op->handle);
break;
}
else
{
*(hp->next->addresses+i) = (int*)x;
hp->next->num_allocs +=1;
printf("Start of Chunk %c is: %d\n",op->handle, (int)((void*)(*(hp->next->addresses+i)) - RAM));
}
}
}
}
else if(op->type == 'F'){//do free
handle_t * hp1 = NULL;
hp1= handles;
while(hp1 !=NULL){
if (hp1->_handle == op->handle){
int index = op->numops;
hp1->num_allocs-=1;
my_free((void *)(*(hp1->addresses+index)));
printf("freed object %c at %d\n", op->handle,(int)((void*)(*(hp1->addresses+index)) - RAM));
break;
}
hp1 = hp1->next;
}
}
else{
printf("Incorrect input file content\n");
return 0;
}
}
return 0;
}