-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymalloc.h
169 lines (130 loc) · 3.64 KB
/
mymalloc.h
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
// up to 100 are used for mamaging the programe. therefore only 24900 is avaliable as memory.
// #define currentBlock memory
// #define preBlock (memory+8)
// #define nextblock (memory+16)
// #define blockSize *(int *)(memory+24)
// #define flagsearch *(int *)(memory+28)
// #define size *(int *)(memory+32)
// #define temp (memory+36)
// #define freeflag (memory+44)
// #define allocatedflag (memory+45)
// #define address (memory+46)
// char *currentBlock=memory;
// *freeflag = 'f';
// *allocatedflag = 'a';
char memory[25000] = {'\0'};
#define base (memory+100)
char *currentBlock=base;
char freeflag = 'f';
char allocatedflag = 'a';
char* findBlock(size_t size)
{
char *preBlock;
preBlock=currentBlock;
int blockSize = 0;
int flagsearch=0;
while( (currentBlock+5 <= preBlock) || (flagsearch!=1))
{
blockSize = *(int *)(currentBlock+1);
if(*currentBlock == freeflag && blockSize >= size + 5)
{
return (currentBlock);
}
else if(!currentBlock)
{
return NULL;
}
else
{
if( currentBlock + blockSize + size >= base + 24899 && flagsearch==1)
{
printf("Memory Overflow\n");
currentBlock=base;
return NULL;
}
if( currentBlock + blockSize + size >= base + 24899)
{
currentBlock=base;
flagsearch=1;
}
currentBlock = currentBlock + blockSize+1;
}
}
}
void splitBlock(char* block, size_t size)
{
char* temp;
int blockSize = *(int *)(block + 1);
blockSize = blockSize - size - 5;
temp = block + size + 5;
*temp = freeflag;
*(int *)(temp + 1) = blockSize;
*(int *)(block + 1) = size+5;
*block = allocatedflag;
currentBlock=temp;
}
char *MyMalloc(size_t size)
{
if(size <= 0)
{
return NULL;
}
if(!*base)
{
*base = freeflag;
*(int *)(base+1) = 24999 - 5;
}
char* block = findBlock(size);
if(block)
{
if(*(int *)(block+1) >= size)
{
splitBlock(block,size);
}
}
else
{
return NULL;
}
return (block);
}
void MyFree(char* address)
{
char *freemover, *preblock, *nextblock;
freemover = base;
int blockSize = *(int *)(freemover + 1);
while(true)
{
blockSize = *(int *)(freemover + 1);
if(freemover == address)
{
preblock = freemover;
nextblock = freemover ;
break;
}
else if(!*freemover)
{
break;
}
else
{
preblock = freemover;
freemover = freemover + blockSize;
nextblock = freemover + *(int *)(freemover + 1);
}
}
if(*nextblock == freeflag)
{
*freemover =freeflag;
*(int *)(freemover + 1) = *(int *)(freemover + 1) + *(int *)(nextblock + 1);
}
if(*preblock == freeflag)
{
*freemover = freeflag;
*(int *)(preblock + 1) = *(int *)(freemover + 1) + *(int *)(preblock + 1);
}
else
{
*address = freeflag;
}
}