-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
best_fit_memory.cpp
166 lines (156 loc) · 5.29 KB
/
best_fit_memory.cpp
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
/*
BEST FIT MEMORY ALLOCATION
Available memory blocks are accepted as input from the user
This strategy will continue searching for a block whose
size is closest to the requested block size. Pointer to
the block is returned after retaining residual space
*/
#include <bits/stdc++.h>
using namespace std;
//structure to represent memoryblock with id and size
typedef struct MemoryBlock
{
int id;
int size;
} MemoryBlock;
//to print the id of allocated memory block
void print_block(MemoryBlock block)
{
if (block.id != -1 && block.size != -1 && block.size != 0)
printf("Block #%d \n", block.id);
else
printf("Invalid Memory Block\n");
}
//linked list to store memory blocks
typedef struct MemoryManager
{
MemoryBlock b;
struct MemoryManager *llink, *rlink;
} MemoryManager;
//best fit memory allocation
MemoryBlock best_fit(MemoryManager *m, int memory)
{
MemoryBlock bl;
//if the list is empty
if (m == NULL)
{
bl.id = -1;
bl.size = -1;
}
else
{
MemoryManager *ptr = m, *ptr1, *ptr2, *p;
MemoryBlock best;
best.id = -1;
best.size = -1;
int count = 0;
//to find the most suitable memory block
while (ptr->rlink != NULL)
{
if (count == 0)
{
if (ptr->b.size >= memory)
{
best.id = ptr->b.id;
best.size = ptr->b.size;
count = 1;
}
}
else
{
if (ptr->b.size >= memory && ptr->b.size < best.size)
{
best.id = ptr->b.id;
best.size = ptr->b.size;
}
}
ptr = ptr->rlink;
}
best.size = memory;
bl = best;
ptr = m;
//to remove the memory allocated from the block
while (ptr->rlink != NULL)
{
if (ptr->b.id == best.id)
{
ptr->b.size -= memory;
break;
}
ptr = ptr->rlink;
}
}
return bl;
}
//to add available memory blocks to linked list
void deallocate(MemoryManager *m, MemoryBlock memory)
{
MemoryManager *ptr = m;
while (ptr->rlink != NULL)
{
ptr = ptr->rlink;
}
MemoryManager *n =new MemoryManager;
n->b = memory;
ptr->rlink = n;
n->llink = ptr;
n->rlink = NULL;
}
//driver code
int main()
{
MemoryManager *m = (MemoryManager *)malloc(sizeof(MemoryManager));
int n;
//accept the number of memory blocks and sizes as user input
printf("Enter the number of memory blocks: ");
scanf("%d", &n);
printf("Enter the size of the blocks: ");
for (int i = 0; i < n; i++)
{
int memory;
scanf("%d", &memory);
MemoryBlock *block = (MemoryBlock *)malloc(sizeof(MemoryBlock));
block->id = i + 1;
block->size = memory;
deallocate(m, *block);
}
//display available memory blocks with id and size
printf("AVAILABLE MEMORY BLOCKS\nBlock id\tSize\n");
MemoryManager *ptr = m->rlink;
while (ptr != NULL)
{
printf(" %d\t\t%d\n", ptr->b.id, ptr->b.size);
ptr = ptr->rlink;
}
MemoryBlock block;
int requested_memory;
//accept required memory size as user input
printf("Required Size: ");
scanf("%d", &requested_memory);
block = best_fit(m, requested_memory);
//if suitable memory block is not found
if (block.id == -1 || block.size == -1)
printf("Memory Insufficient !\n");
else
{
printf("Allocating ");
print_block(block);
}
return 0;
}
/*
SAMPLE I/O:
Enter the number of memory blocks: 5
Enter the size of the blocks: 100 250 400 375 500
AVAILABLE MEMORY BLOCKS
Block id Size
1 100
2 250
3 400
4 375
5 500
Required Size: 350
Allocating Block #4
Time Complexity: O(n)
Space Complexity: O(n)
*/