-
Notifications
You must be signed in to change notification settings - Fork 0
/
ymalloc.c
159 lines (138 loc) · 3.68 KB
/
ymalloc.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
/*ymalloc_yfree
*
*Allocating big chunck of memory with yalloc once using malloc
*
*Maintaining separate lists of unallocated and allocated blocks, where unallocated list keeps the output of yalloc
*ymalloc requesting for memory block from unallocated list
*
* yfree freeing out the allocated memory block, ordering the unallocated list according to addresses
* and then merging the blocks with contiguous addresses
*/
#include<stdio.h>
#include<stdlib.h>
#define SIZE 1024*1024*8
#include<stdbool.h>
typedef struct memory{
size_t size; //keeping the block size required by the user, irrespective of meta data
struct memory *next; // pointing the next memory block in allocated/unallocated list
}node;
node *ublock=NULL,*ablock=NULL;
//merge sort for ordering the random unallocated block for merging them into continuous block
node *umergesort(node *head);
node *merge(node *head1,node *head2);
node* joinublock(node* ublock);
//merging the block into a continous memory according to there addresses
//static bool init=false;
//initializing yalloc once in a program
void* yalloc(){
ublock=(void*)malloc(SIZE+sizeof(node));
ublock->size=SIZE;
ublock->next=NULL;
return ublock;
}
void* ymalloc(long int x){
//node *ublock,*ablock,
node *temp,*buff;
buff=ublock;
buff=NULL;
static bool init=false;
if(!init){
ublock=yalloc();
init=true;
}
temp=ublock;
buff=temp;
do{
if((temp->size)==x){
ablock = temp;
ablock->next = NULL;
buff->next = temp->next;
temp = buff;
return ablock+sizeof(node);
}
else if((temp->size)>x){
if( ( (sizeof(node)+temp->size)-(x+sizeof(node)) ) > sizeof(node) ){
//worst case checking to keep atleast one block of 1 byte + sizeof(node)
if(ablock==NULL){
ablock = temp;
ablock->size = x;
}
else{
ablock->next = temp;
ablock = ablock->next;
}
ablock->next=NULL;
temp = temp+sizeof(node)+x;
temp->size = (temp->size) - x - sizeof(node);
// meta block of current ublock(temp) is ignored as it is not considered in size and directly allocated to ablock
buff->next = temp;
temp = buff;
ablock->next = NULL;
return ablock+sizeof(node);
}
}
buff=temp; //storing current node in buffer
}while(temp=temp->next);
return NULL; //if x is greater than unallocated block size
}
void yfree(void* t){
node* atemp,*buff,*utmp;
atemp = ablock;
utmp = ublock;
t = t-sizeof(node);
do{
if(t==atemp){
buff->next = atemp->next;
while((utmp = utmp->next)!= NULL);
//freeing allocated block and adding it to unallocated list
utmp->next=atemp;
utmp=utmp->next;
utmp->next=NULL;
}
buff=atemp;
}while(atemp=atemp->next);
ublock = umergesort(ublock);
ublock = joinublock(ublock);
}
node* umergesort(node* head) {
node* head1;
node* head2;
if((head == NULL) || (head->next == NULL))
return head;
head1 = head;
head2 = head->next;
while((head2 != NULL) && (head2->next != NULL)) {
head = head->next;
head2 = head->next->next;
}
head2 = head->next;
head->next = NULL;
return merge(umergesort(head1), umergesort(head2));
}
node* merge(node* head1, node* head2) {
node* head3;
if(head1 == NULL)
return head2;
if(head2 == NULL)
return head1;
if(head1 < head2) {
head3 = head1;
head3->next = merge(head1->next, head2);
} else {
head3 = head2;
head3->next = merge(head1, head2->next);
}
return head3;
}
node* joinublock(node* ublock){
node* temp;
temp=ublock;
do{
if((temp + sizeof(node) + temp->size)==temp->next){ //checking the presence of successive address block
temp->size = temp->size + ((temp->next)->size) + sizeof(node);
temp->next = (temp->next)->next;
}
}
while(temp=temp->next);
return ublock;
}