-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
165 lines (117 loc) · 3.25 KB
/
test.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
#include "my_malloc.h"
#include <stdio.h>
/***********REMOVE WHEN DONE*********/
extern void* heap;
extern void* heapEnd;
extern metadata_t* freelist[8];
/***********************************/
void printFreeList()
{
for(int i = 0; i < 8; i++)
{
metadata_t* curr = freelist[i];
int count = 0;
while(curr)
{
count++;
curr = curr->next;
}
printf("freeList[%d] elments = %d\n",i,count);
}
printf("\n");
}
void printVisualFreeList()
{
for(int i = 0; i < 8; i++)
{
metadata_t* curr = freelist[i];
int count = 0;
printf("|*********************|\n|* *|\n|* Bucket: %d *| ===>",i);
while(curr)
{
count++;
printf("Node %d: Block Size is %d ===>",count,curr->size);
curr = curr->next;
}
printf("\n");
printf("|* *|\n|*********************|\n");
//printf("freeList[%d] elments = %d\n",i,count);
}
printf("\n");
}
int main() {
/*
* Test code goes here
*/
/*int *a = my_malloc(sizeof(int));
if(a == 0)
{
printf("malloc failed");
return 0;
}
*a = 5;
printf("ptr: %p\n dereferenced pointer: %d\n",(void*)a,*a);
//printVisualFreeList();
my_free(a);
printVisualFreeList();
short *b = my_malloc(sizeof(short));
if(b == 0)
{
printf("malloc failed");
return 0;
}
*b = 20;
printf("ptr: %p\n dereferenced pointer: %d\n",(void*)b,*b);
b = my_realloc(b,sizeof(int));
*b = 8;
printf("ptr: %p\n dereferenced pointer: %d\n",(void*)b,*b);
b = my_realloc(b,100);
*b = 80;
printf("ptr: %p\n dereferenced pointer: %d\n",(void*)b,*b);
b = my_realloc(b,1000);
*b = 60;
printf("ptr: %p\n dereferenced pointer: %d\n",(void*)b,*b);
//printVisualFreeList();
printf("size of metadata_t header: %d\n", sizeof(metadata_t));
b = my_realloc(b,1001);
*b = 67;
printf("ptr: %p\n dereferenced pointer: %d\n",(void*)b,*b);
//printVisualFreeList();
b = my_realloc(b,2000);
*b = 600;
printf("ptr: %p\n dereferenced pointer: %d\n",(void*)b,*b);*/
void* a = my_malloc(470);
void* b = my_malloc(37);
void* c = my_malloc(2);
printVisualFreeList();
printf("\n");
void* d = my_malloc(500);
void* e = my_malloc(3);
void* f = my_malloc(200);
printVisualFreeList();
printf("\n");
my_free(d);
my_free(e);
my_free(f);
printVisualFreeList();
printf("\n");
a = my_realloc(a,1000);
b = my_realloc(b,200);
c = my_realloc(c,2000);
printVisualFreeList();
printf("\n");
my_free(c);
my_free(b);
my_free(a);
printVisualFreeList();
printf("\n");
void* g = my_malloc(-1);
if(!g)
printf("e is NULL\n");
g = my_malloc(1012);
void* h = my_malloc(500);
printVisualFreeList();
my_free(h);
my_free(g);
return 0;
}