Skip to content

Commit b8e1f42

Browse files
authored
Add files via upload
1 parent 198ff7f commit b8e1f42

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

old_malloc.c

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#include<stdio.h>
2+
3+
#define MEMSIZE 1000
4+
5+
struct test{
6+
7+
int a;
8+
9+
10+
int tf()
11+
{
12+
13+
return a;
14+
15+
}
16+
17+
18+
};
19+
20+
21+
22+
23+
24+
25+
26+
char mem[MEMSIZE]; // 1000 bytes that currently only store character data types.
27+
28+
struct m_data{
29+
30+
struct m_data* next;
31+
struct m_data* prev;
32+
int isFree;
33+
int size;
34+
};
35+
36+
37+
struct m_data* root = (struct m_data*)mem; // it's more useful to make root global
38+
39+
40+
// it could well be that you reach the end of the linked list without ever finding a suitable fit
41+
42+
43+
// finds first occurrence of suitable slot
44+
// 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.
45+
46+
struct m_data* find(int m_size)
47+
{
48+
struct m_data* p = root;
49+
50+
while(p->next !=NULL && (p->size < (m_size + sizeof(struct m_data)) || !p->isFree))
51+
{
52+
p = p->next;
53+
54+
}
55+
56+
return p;
57+
58+
}
59+
60+
61+
// still confused about typecasting pointers into void pointer; what is allowable to return in case of function with void pointer.
62+
63+
void* mymalloc(int m_size)
64+
//void* mymalloc()
65+
{
66+
67+
//initialisation
68+
static int init = 0;
69+
// static struct m_data* root;
70+
struct m_data* q;
71+
72+
73+
if(!init)
74+
{
75+
init = 1;
76+
root = (struct m_data*)mem;
77+
root -> isFree = 1;
78+
root -> next = NULL;
79+
root -> prev = NULL;
80+
root -> size = MEMSIZE - sizeof(struct m_data);
81+
}
82+
83+
q = find(m_size); // first suitable block
84+
85+
// set up new m_data block to point at new free space
86+
87+
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
88+
newB -> next = q->next;;//?????
89+
newB -> prev = q;
90+
newB -> isFree = 1;
91+
newB -> size = (q->size - m_size - sizeof(struct m_data));
92+
printf("newB = %p\n", newB);
93+
94+
95+
96+
// printf("%p\n", find(m_size));
97+
98+
q->isFree = 0;
99+
q->size = m_size;
100+
q->next = newB;
101+
102+
103+
return (void*)(q+1);
104+
105+
}
106+
107+
108+
109+
110+
void myfree(void* ptr)
111+
{
112+
113+
114+
struct m_data* t = (struct m_data*)ptr;
115+
t->isFree = 1;
116+
117+
118+
119+
struct m_data* curr = root;
120+
121+
while(curr->next != NULL)
122+
{
123+
124+
125+
if(!(curr -> isFree)|| !(curr -> next -> isFree))
126+
{
127+
curr = curr -> next;
128+
}
129+
else
130+
{
131+
//merge
132+
133+
curr-> size += curr->size + sizeof(struct m_data) + curr->next->size;
134+
curr -> next -> next -> prev = curr;
135+
curr -> next = curr->next-> next;
136+
137+
}
138+
139+
140+
141+
}
142+
143+
144+
145+
146+
}
147+
148+
149+
150+
151+
152+
153+
154+
int main()
155+
{
156+
157+
158+
159+
160+
printf("mymalloc() = %p\n ", mymalloc(128));
161+
printf("mem[24] %p\n", &mem[24]);
162+
printf("mem[152] = %p\n", &mem[152]);
163+
// printf("mymalloc(32) again = %p\n", mymalloc(32));
164+
int* p = mymalloc(32);
165+
166+
167+
myfree(p);;
168+
169+
170+
171+
172+
return 0;
173+
174+
}

0 commit comments

Comments
 (0)