-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprg8.c
113 lines (109 loc) · 2.31 KB
/
prg8.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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int size,h=0,i=0,a[20],b[20],original_size,max_size;
void allocate()
{
int req,t,u,z=0;
int actual=original_size;
if(size<=0)
{
printf("No space available\n");
return;
}
printf("Enter the required allocation size\n");
scanf("%d",&req);
if(req<=max_size)
{
//int small;
//int valid=0;
z=0;
while((t=actual/pow(2,z))>=req)
{
u=t;
z+=1;
}
//printf("%d is the size required\n",u);
size-=u;
if(size<0)
{
printf("memory cannot be allocated\n");
size+=u;
return;
}
a[i++]=u;
b[h++]=req;
//printf("Remaining memory=%d\n",size);
while((size/pow(2,z))>=1)
z=z+1;
max_size=pow(2,z-1);
}
else
{
printf("Cannot allocate memory\n");
return;//exit(0);
}
}
void deallocate(int n)
{
int flag=0,z=0;
if(i==0)
{
printf("Memory not yet allocated \n");
return;
}
for (int j=0;j<=i;j++)
{
if(a[j]==n)
{
flag=1;
for(int k=j;k<i;k++)
{
a[k]=a[k+1];
b[k]=b[k+1];
}
size+=n;
while((size/pow(2,z))>=1)
z=z+1;
max_size=pow(2,z-1);
break;
}
}
//printf("Remaining memory = %d\n",size);
if(flag==0){
printf("Not found in memory\n");
return;
}
}
int main()
{
printf("Enter memory size\n");
scanf("%d",&size);
original_size=size;
max_size=size;
int mem,ch,k;
while(1)
{
printf("Enter 1.Allocate 2.Deallocate\n");
scanf("%d",&ch);
switch(ch)
{
case 1:allocate();
break;
case 2: if(max_size==size)
{printf("not allocated\n");
break;}
printf("Enter mem size to deallocate\n");
scanf("%d",&mem);
deallocate(mem);
break;
}
printf("Required \t\tAllocation\n");
for(int k=0;k<=i;k++)
{
if(a[k]!=0 && b[k]!=0)
printf("%d\t\t%d\n",b[k],a[k]);
}
}
return 0;
}