-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc.c
More file actions
218 lines (190 loc) · 7.4 KB
/
malloc.c
File metadata and controls
218 lines (190 loc) · 7.4 KB
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
#include<stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define SIZE 25000
#define SMALL_BLOCK_SIZE 762
#define LARGE_BLOCK_SIZE 3830
#define SMALL_BLOCK_COUNT 22
#define LARGE_BLOCK_COUNT 2
#define FREE_LIST(i) (Block *)(Heap + sizeof(Block)*i)
#define ALLOCATED_LIST(i) (Allocated *)(Heap + sizeof(Block)*(SMALL_BLOCK_COUNT + LARGE_BLOCK_COUNT) + sizeof(Block *)*i)
#define ASSIGN_BLOCK(BLOCK_COUNT) \
{ \
CHECK_MEM_SIZE\
\
Block *adr = NULL;\
Allocated *all = NULL;\
for(int i = 0; i < BLOCK_COUNT; i++) { \
if(i >= SMALL_BLOCK_COUNT){\
adr = (Block *)FREE_LIST(i);\
Block *head = (Block *)(void *)(Heap_pool + (i)*SMALL_BLOCK_SIZE + (i-SMALL_BLOCK_COUNT)*LARGE_BLOCK_SIZE);\
head->size = LARGE_BLOCK_SIZE-sizeof(Block);\
head->next = NULL;\
adr->next = head;\
adr->size = head->size;\
}else{\
adr = FREE_LIST(i);\
Block *head = (Block *)(void *)(Heap_pool + (i)*SMALL_BLOCK_SIZE);\
head->size = SMALL_BLOCK_SIZE-sizeof(Block);\
head->next = NULL;\
adr->next = head;\
adr->size = head->size;\
}\
all = ALLOCATED_LIST(i);\
all->next = NULL;\
}\
}
#define CHECK_MEM_SIZE \
{\
if((SMALL_BLOCK_COUNT + LARGE_BLOCK_COUNT)*(sizeof(Block) + sizeof(Allocated *)) + SMALL_BLOCK_COUNT*SMALL_BLOCK_SIZE + LARGE_BLOCK_SIZE*LARGE_BLOCK_COUNT > SIZE){\
printf("\nTotal size of small block and large block is exceed the allocated array size!!!\n");\
exit(0);\
}\
}
struct Block{
size_t size;
struct Block *next;
};
struct Allocated{
struct Block *next;
};
typedef struct Block Block;
typedef struct Allocated Allocated;
char Heap[SIZE];
char *Heap_pool = Heap + sizeof(Block)*(SMALL_BLOCK_COUNT + LARGE_BLOCK_COUNT) + sizeof(Block *)*(SMALL_BLOCK_COUNT + LARGE_BLOCK_COUNT);
int assign_block_check = 1;
void *MyMalloc(size_t size){
int i = 0;
Block *temp = NULL;
Block *pretmp = NULL;
Block *start = NULL;
void *location = NULL;
if(assign_block_check){
ASSIGN_BLOCK(SMALL_BLOCK_COUNT + LARGE_BLOCK_COUNT);
assign_block_check = 0;
}
if(size > SMALL_BLOCK_SIZE-sizeof(Block)) i = SMALL_BLOCK_COUNT;
do{
if(temp != NULL && temp->size >= size){
start = temp;
break;
}else if(i == SMALL_BLOCK_COUNT + LARGE_BLOCK_COUNT) temp = NULL;
else temp = FREE_LIST(i++);
}while(temp != NULL);
if(temp != NULL){
int temp_size = temp->size;
Block *temp_block = temp;
do{
if(temp->next != NULL){
if(temp->next->size >= size && temp->next->size <= temp_size){
temp_size = temp->next->size;
temp_block = temp->next;
pretmp = temp;
}
}
temp = temp->next;
}while(temp != NULL);
pretmp->next = temp_block->next;
temp_block->next = NULL;
temp = temp_block;
if(size == temp->size) location = (void *)((char *)temp + sizeof(Block));
else{
if((temp->size - size) > sizeof(Block)){
Block *c = (Block *)(void *)((char *)temp + sizeof(Block) + size);
c->size = temp->size - size - sizeof(Block);
c->next = pretmp->next;
pretmp->next = c;
location = (void *)((char *)temp + sizeof(Block));
}else{
size = temp->size;
location = (void *)((char *)temp + sizeof(Block));
}
}
start->size = 0;
temp = start->next;
while(temp != NULL){
if(start->size < temp->size) start->size = temp->size;
temp = temp->next;
}
Block *f = (Block *)(location - sizeof(Block));
Allocated *all = ALLOCATED_LIST(--i);
if(all->next == NULL) all->next = f;
else{
temp = all->next;
while(temp != NULL){
pretmp = temp;
temp = temp->next;
}
pretmp->next = f;
}
f->size = size;
f->next = NULL;
printf("\nAllocated\n");
return location;
}
printf("\nNot enough memory\n");
return NULL;
}
void MyFree(void *addr){
Block *temp = NULL;
Block *start = NULL;
Block *pretmp = NULL;
Block *pre_all = NULL;
Allocated *all = NULL;
Block *b_addr = (Block *)((char *)addr - sizeof(Block));
for(int i=0;i<(SMALL_BLOCK_COUNT + LARGE_BLOCK_COUNT);i++){
if(i < SMALL_BLOCK_COUNT && (char*)addr >= Heap_pool + (SMALL_BLOCK_SIZE*(i)) && (char*)addr < Heap_pool + (SMALL_BLOCK_SIZE*(i+1))){
temp = FREE_LIST(i);
all = ALLOCATED_LIST(i);
break;
}else if((char*)addr >= Heap_pool + (SMALL_BLOCK_SIZE*SMALL_BLOCK_COUNT + LARGE_BLOCK_SIZE*(i - SMALL_BLOCK_COUNT)) && (char*)addr < Heap_pool + SMALL_BLOCK_SIZE*SMALL_BLOCK_COUNT + (LARGE_BLOCK_SIZE*(i + 1 - SMALL_BLOCK_COUNT))){
temp = FREE_LIST(i);
all = ALLOCATED_LIST(i);
break;
}
}
start = temp;
if(temp != NULL){
pretmp = (Block *)all->next;
temp = NULL;
while(pretmp != NULL){
if((void *)b_addr == (void *)pretmp){
temp = start;
break;
}
pre_all = pretmp;
pretmp = pretmp->next;
}
if(temp != NULL){
if(pre_all == NULL) all->next = NULL;
else pre_all->next = b_addr->next;
}
while(temp != NULL){
if((char *)temp + sizeof(Block) + temp->size < (char *)b_addr){
if((char *)temp->next > (char *)b_addr + sizeof(Block) + b_addr->size || temp->next == NULL){
b_addr->next = temp->next;
temp->next = b_addr;
}else if((char *)temp->next == (char *)b_addr + sizeof(Block) + b_addr->size){
b_addr->size = b_addr->size + sizeof(Block) + temp->next->size;
b_addr->next = temp->next->next;
temp->next = b_addr;
}
}else if((char *)b_addr + sizeof(Block) + b_addr->size == (char *)temp){
b_addr->next = temp->next;
pretmp->next = b_addr;
b_addr->size = b_addr->size + sizeof(Block) + temp->size;
}
pretmp = temp;
temp = temp->next;
}
start->size = 0;
temp = start->next;
while(temp != NULL){
if(start->size < temp->size) start->size = temp->size;
temp = temp->next;
}
printf("\nMemory deallocated succesfully!!!\n");
return;
}
printf("\nWrong address!!! Cannot deallocate memory!!!\n");
}