-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_malloc.c
174 lines (90 loc) · 2.58 KB
/
old_malloc.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
#include<stdio.h>
#define MEMSIZE 1000
struct test{
int a;
int tf()
{
return a;
}
};
char mem[MEMSIZE]; // 1000 bytes that currently only store character data types.
struct m_data{
struct m_data* next;
struct m_data* prev;
int isFree;
int size;
};
struct m_data* root = (struct m_data*)mem; // it's more useful to make root global
// it could well be that you reach the end of the linked list without ever finding a suitable fit
// finds first occurrence of suitable slot
// what is the termination condition if there is no suitable space available? If no suitable space is available then when will ultimately have reached the end of the queue.
struct m_data* find(int m_size)
{
struct m_data* p = root;
while(p->next !=NULL && (p->size < (m_size + sizeof(struct m_data)) || !p->isFree))
{
p = p->next;
}
return p;
}
// still confused about typecasting pointers into void pointer; what is allowable to return in case of function with void pointer.
void* mymalloc(int m_size)
//void* mymalloc()
{
//initialisation
static int init = 0;
// static struct m_data* root;
struct m_data* q;
if(!init)
{
init = 1;
root = (struct m_data*)mem;
root -> isFree = 1;
root -> next = NULL;
root -> prev = NULL;
root -> size = MEMSIZE - sizeof(struct m_data);
}
q = find(m_size); // first suitable block
// set up new m_data block to point at new free space
struct m_data* newB = (struct m_data*)((char*)q + sizeof(struct m_data) + m_size); // need to type cast to char in order to use pointer arithmetic
newB -> next = q->next;;//?????
newB -> prev = q;
newB -> isFree = 1;
newB -> size = (q->size - m_size - sizeof(struct m_data));
printf("newB = %p\n", newB);
// printf("%p\n", find(m_size));
q->isFree = 0;
q->size = m_size;
q->next = newB;
return (void*)(q+1);
}
void myfree(void* ptr)
{
struct m_data* t = (struct m_data*)ptr;
t->isFree = 1;
struct m_data* curr = root;
while(curr->next != NULL)
{
if(!(curr -> isFree)|| !(curr -> next -> isFree))
{
curr = curr -> next;
}
else
{
//merge
curr-> size += curr->size + sizeof(struct m_data) + curr->next->size;
curr -> next -> next -> prev = curr;
curr -> next = curr->next-> next;
}
}
}
int main()
{
printf("mymalloc() = %p\n ", mymalloc(128));
printf("mem[24] %p\n", &mem[24]);
printf("mem[152] = %p\n", &mem[152]);
// printf("mymalloc(32) again = %p\n", mymalloc(32));
int* p = mymalloc(32);
myfree(p);;
return 0;
}