-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllocateDeallocate.c
378 lines (327 loc) · 11.8 KB
/
AllocateDeallocate.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//BT20CSE052-Siddheshkumar Jain
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct free_list{
struct free_list* next; //Pointer to next free block
struct free_list* prev; //Pointer to prev free block
int size; //Size of free block
int start; //Starting point of free block
};
struct allocated_list{
struct allocated_list* next; //Pointer to next allocated block
struct allocated_list* prev; //Pointer to previous allocated block
int size; //Size of process that is allocated
int start; //Starting point of allocated block
char processID[30]; //ID of allocated block
};
struct free_list* FreeListHead; //head of free list
struct allocated_list* allocatedHead; //head of allocated process list
int HEAPSIZE = 3000; //Heap storage available
void allocate_memory();
void deallocate_memory(char []);
void merge_free_list(struct free_list*);
void check_ID(char []);
void print_allocated_list();
void print_free_list();
int main()
{
int choice=1;
char processID[30];
FreeListHead = (struct free_list*)malloc(sizeof(struct free_list));
FreeListHead->next = FreeListHead->prev = NULL;
FreeListHead->size = HEAPSIZE;
FreeListHead->start = 0;
allocatedHead = NULL;
printf("------------------------------------------------------------------------------------------------\n");
printf("\t Memory allocation using FIRST FIT STRATEGY with memory of 3000 units available.\n");
printf("\t Please take care that memory asked for allocation is in integer format.\n");
printf("------------------------------------------------------------------------------------------------\n\n");
do
{
printf("Choose any one of the following options\n\n");
printf("1. Allocate Memory\n");
printf("2. Deallocate Memory\n");
printf("3. Allocated memory status\n");
printf("4. Free list status\n");
printf("0: EXIT\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
allocate_memory();
break;
case 2:
printf("Please enter the ID of the process to be deallocated from heap memory: ");
scanf("%s",processID);
deallocate_memory(processID);
break;
case 3:
print_allocated_list();
break;
case 4:
print_free_list();
break;
case 0:
break;
default:
printf("Wrong option entered\n");
break;
}
printf("-------------------------------------------------------------------------------------------\n");
printf("Live Status\n");
printf("\nAllocated List is :\n");
print_allocated_list();
printf("\nFree List is :\n");
print_free_list();
printf("--------------------------------------------------------------------------------------------\n");
}while(choice!=0);
return 0;
}
void allocate_memory()
{
int processMemory; //To keep track of size to be allocated
char processID[30]; //Labelling block
printf("Enter size you want to allocate : ");
scanf("%d", &processMemory);
printf("Enter the process ID: ");
scanf("%s", processID);
check_ID(processID);
struct free_list* freeTemp = FreeListHead;
/* Traverse till freeTemp->size< processMemory ,
as we are using First-fit alogrith */
while (freeTemp!=NULL && freeTemp->size<processMemory)
{
freeTemp = freeTemp->next;
}
/* If we reach end of list but can't find suitable free memory block */
if (freeTemp == NULL)
{
printf("\n!!Memory can't be allocated!!\n\n");
}
else
{
/* If size of free list block is greater than required process size,
It means free list block will be there with reduced size(freeTemp->size - processMemory) */
if (freeTemp->size - processMemory > 0)
{
struct free_list* temp = (struct free_list*)malloc(sizeof(struct free_list));
temp->start = freeTemp->start + processMemory;
temp->next = NULL;
temp->prev = NULL;
if (freeTemp == FreeListHead)
{
temp->next = FreeListHead->next;
FreeListHead = temp;
}
else
{
(freeTemp->prev)->next = temp;
temp->next = freeTemp->next;
temp->prev = freeTemp->prev;
if(freeTemp->next)
{
freeTemp->next->prev = temp;
}
}
temp->size = freeTemp->size - processMemory;
}
else
{ /*If size of free block equals required size,
It means we free list block should be deleted */
if (freeTemp->prev)
{
freeTemp->prev->next = freeTemp->next;
if(freeTemp->next)
{
freeTemp->next->prev = freeTemp->prev;
}
}
else
{
FreeListHead = freeTemp->next;
if(FreeListHead)
{
FreeListHead->prev = NULL;
}
}
}
/* Allocating memory and adding node in allocated list */
struct allocated_list* alloc_temp = (struct allocated_list*)malloc(sizeof(struct allocated_list));
alloc_temp->next = NULL;
alloc_temp->prev = NULL;
alloc_temp->size = processMemory;
strcpy(alloc_temp->processID , processID);
alloc_temp->start = freeTemp->start;
free(freeTemp);
/* If allocated node is first one */
if (allocatedHead == NULL)
{
allocatedHead = alloc_temp;
}
else
{
struct allocated_list* temp = allocatedHead;
/* Finding the position where to allocate */
while (temp->next && temp->start < alloc_temp->start)
{
temp = temp->next;
}
if (temp == allocatedHead && temp->start > alloc_temp->start) // If node is to be allocated at first position itself
{
alloc_temp->next = temp;
temp->prev = alloc_temp;
allocatedHead = alloc_temp;
}
else if (temp->next == NULL && temp->start < alloc_temp->start) // If node is to be allocated at last position itself
{
temp->next = alloc_temp;
alloc_temp->prev = temp;
alloc_temp->next = NULL;
}
else // If node is to be allocated in between
{
(temp->prev)->next = alloc_temp;
alloc_temp->prev = temp->prev;
alloc_temp->next = temp;
temp->prev = alloc_temp;
}
}
printf("Allocated from index: %d to %d \n" ,alloc_temp->start ,alloc_temp->start + alloc_temp->size-1);
}
return;
}
void deallocate_memory(char processID[])
{
struct allocated_list* allocateTemp = allocatedHead;
while (allocateTemp!=NULL && strcmp(allocateTemp->processID ,processID)) // Finding node with given processID
{
allocateTemp = allocateTemp->next;
}
if (allocateTemp == NULL)
{
printf("Not found\n");
}
else
{
/* the node that is to be inserted in freelist after being deallocated */
struct free_list* fptr = (struct free_list*)malloc(sizeof(struct free_list));
fptr->next = NULL;
fptr->prev = NULL;
fptr->size = allocateTemp->size;
fptr->start = allocateTemp->start;
if (allocateTemp == allocatedHead) //If memory to be deallocated is first node itself
{
allocatedHead = allocateTemp->next;
}
else
{
allocateTemp->prev->next = allocateTemp->next;
if (allocateTemp->next)
{
allocateTemp->next->prev = allocateTemp->prev;
}
}
struct free_list* temp = FreeListHead;
if (FreeListHead == NULL)
{
FreeListHead = fptr;
}
else
{
while (temp && temp->next && temp->start < fptr->start)
{
temp = temp->next;
}
if (temp && temp->next == NULL && temp->start < fptr->start) //If freelist node is to be inserted at end
{
temp->next = fptr;
fptr->prev = temp;
}
else //If freelist node is to be inserted at start
{
if (temp && temp == FreeListHead)
{
fptr->next = temp;
temp->prev = fptr;
FreeListHead = fptr;
}
else if(temp) //If freelist node is to be inserted in between
{
fptr->prev = temp->prev;
temp->prev->next = fptr;
temp->prev = fptr;
fptr->next = temp;
}
}
}
merge_free_list(fptr); //Merging continguous free blocks
}
return;
}
void merge_free_list(struct free_list* fptr)
{
if(fptr->prev) //if previous node exists and is adjacent
{
if (fptr->prev->start + fptr->prev->size == fptr->start)
{
fptr->prev->size = fptr->prev->size + fptr->size;
fptr->prev->next = fptr->next;
if(fptr->next)
{
fptr->next->prev = fptr->prev;
}
fptr = fptr->prev;
}
}
if(fptr->next) //If next node exists and is adjacent
{
if (fptr->start + fptr->size == fptr->next->start)
{
fptr->size += fptr->next->size;
fptr->next = fptr->next->next;
if(fptr->next)
{
fptr->next->prev = fptr;
}
}
}
return;
}
void check_ID(char s[])
{
int flag = 1;
struct allocated_list* allocateTemp = allocatedHead;
while (allocateTemp && flag)
{
if (strcmp(allocateTemp->processID , s)==0)
{
printf("\n!!Process with same ID found!!\nHence deallocating previous one\n");
deallocate_memory(s);
flag = 0;
}
allocateTemp = allocateTemp->next;
}
return;
}
void print_allocated_list()
{
struct allocated_list* allocateTemp = allocatedHead;
while(allocateTemp)
{
printf("%4d <---> %4d for process with processID ---> %s \n",allocateTemp->start,allocateTemp->start + allocateTemp->size-1,allocateTemp->processID);
allocateTemp = allocateTemp->next;
}
return;
}
void print_free_list()
{
struct free_list* freeTemp = FreeListHead;
while (freeTemp)
{
printf("%4d< ---> %4d \n",freeTemp->start,freeTemp->start + freeTemp->size-1);
freeTemp = freeTemp->next;
}
return;
}