-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymalloc.c
208 lines (176 loc) · 5.28 KB
/
mymalloc.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
#include <stdio.h>
#include <stdlib.h>
#include "mymalloc.h"
#define SIZE 25000
char RAM[SIZE]={'\0'};
struct node
{
int size;
int address;
struct node* next;
};
int usableLimit=SIZE; //upper limit of the usable area to user
struct node *FirstNodeUsed=NULL,*tempUsed; //data of the used list
struct node *firstNodeFree=NULL,*tempFree; //data of the free list
void addNodeUsed(int address,int size) //to the used list
{
if(FirstNodeUsed==NULL)
{
usableLimit=usableLimit-16; //allocating space for this node (16 bytes)
FirstNodeUsed=(struct node*)(RAM+usableLimit); //allocating memory from back end of the array
FirstNodeUsed->next=NULL;
FirstNodeUsed->size=size;
FirstNodeUsed->address=address;
tempUsed=FirstNodeUsed;
}
else{
struct node *newNode;
usableLimit=usableLimit-16; //allocating space for this node (16 bytes)
newNode=(struct node*)(RAM+usableLimit); //allocating memory from back end of the array
newNode->next=NULL;
newNode->size=size;
newNode->address=address;
tempUsed->next=newNode;
tempUsed=tempUsed->next;
}
}
void addNodeFree(int address,int size) //to the free list
{
if(firstNodeFree==NULL)
{
usableLimit=usableLimit-16; //allocating space for this node (16 bytes)
firstNodeFree=(struct node*)(RAM+usableLimit); //allocating memory from back end of the array
firstNodeFree->next=NULL;
firstNodeFree->size=size;
firstNodeFree->address=address;
tempFree=firstNodeFree;
}
else{
struct node *newNodeFree;
usableLimit=usableLimit-16; //allocating space for this node (16 bytes)
newNodeFree=(struct node*)(RAM+usableLimit); //allocating memory from back end of the array
newNodeFree->next=NULL;
newNodeFree->size=size;
newNodeFree->address=address;
tempFree->next=newNodeFree;
tempFree=tempFree->next;
}
}
void MyFree(void *pointer)
{
int arrayIndex=(char*)pointer-RAM; //to store the index value of the RAM array
struct node *temp2=FirstNodeUsed; //used list starting node to temp2
struct node *tempPrev2=FirstNodeUsed;
//REMOVE the corresponding data FROM THE USED LIST and ADDING corresponding data to free list
while(temp2!=NULL)
{
if(temp2->address==arrayIndex)
{
if(temp2==FirstNodeUsed) //if its the first node of the list
{
FirstNodeUsed=FirstNodeUsed->next;
addNodeFree(temp2->address,temp2->size); //ADDING THE NEWLY FREED AREA TO FREE LIST
break;
}
else if(temp2->next==NULL) //last node of the list
{
tempPrev2->next=NULL;
addNodeFree(temp2->address,temp2->size); //ADDING THE NEWLY FREED AREA TO FREE LIST
break;
}
else //middle node of the list
{
tempPrev2->next=temp2->next;
addNodeFree(temp2->address,temp2->size); //ADDING THE NEWLY FREED AREA TO FREE LIST
break;
}
}
tempPrev2=temp2;
temp2=temp2->next;
}
}
void *MyMalloc(size_t n)
{
//CHECK THE FREE LIST
void *result;
if(FirstNodeUsed==NULL) //to handle, when every slot is freed and wants to allocate memory again
{
usableLimit=SIZE;
firstNodeFree=NULL;
tempUsed=NULL;
tempFree=NULL;
}
struct node *temp1=firstNodeFree;
struct node *tempPrev1=firstNodeFree;
int isAllocationSuccessful=0,top=0;
if(n>SIZE)
{
printf("Memory Allocation Failed, Error - Not Enough Space.\n");
return (NULL);
}
if(temp1==NULL) //when whole memory is free (initial state)
{
result=RAM;
addNodeUsed(0,n);
addNodeFree(n,(usableLimit-n)-16);
}
else
{
while(temp1!=NULL)
{
if(temp1->size>=n) //first fit method
{
isAllocationSuccessful=1; //to detect successful allocations
if(temp1->size==n || (temp1->size-n)<16) //best fit || Internal fragmentation
{
result=RAM+temp1->address;
addNodeUsed(temp1->address,n); //adding to the used list
//removing that data from free list
if(temp1==firstNodeFree) //if its the first node of the list
{
temp1->size=0; //never remove the first one because we need that node to keep the algorithm funtional
break;
}
else if(temp1->next==NULL) //last node of the list
{
tempPrev1->next=NULL;
break;
}
else if(temp1->next==NULL) //middle node of the list
{
tempPrev1->next=temp1->next;
break;
}
}
else
{
result=RAM+temp1->address; //storing the starting address of the block we reserved to a void pointer "result"
//update the used list
addNodeUsed(temp1->address,n);
//update the free list
if(temp1==firstNodeFree) //first Node contains the upper usable limit
{
temp1->address=temp1->address+n;
temp1->size=usableLimit-temp1->address;
break;
}
else //for every other node
{
top=temp1->address+temp1->size;
temp1->address=temp1->address+n;
temp1->size=top-temp1->address;
break;
}
}
}
tempPrev1=temp1;
temp1=temp1->next;
}
if(!isAllocationSuccessful) //to detect unsuccessful allocations (It is 1 when memory is allocated)
{
printf("Memory Allocation Failed, Error - Not Enough Contiguous Space. \n");
return (NULL);
}
}
return (result);
}