-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_management.c
More file actions
168 lines (146 loc) · 4.22 KB
/
Copy pathheap_management.c
File metadata and controls
168 lines (146 loc) · 4.22 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
/*
Author: Tarun Jain
Roll Number: MT2015120
File: RTOS Assignment 1 - Question 1 -Main File/ Test Case File
Problem Statement: Implement the malloc, free, calloc, realloc Function using your own Heap(Static array of 10240 Bytes)
*/
// Header Files
#include "heap_management.h"
// Global Variable
void *base = NULL; // For Base Address of Heap
// Initialization Function
void init()
{
limit = memory;
end_limit = (memory + MEM_SIZE);
}
// Demonstrate Memory Allocation/Deallocation/Reallocation/Free
void demo()
{
int *p1 = NULL; // To Store Integer Address
float *c1 = NULL; // To Store Float Address
double *f11 = NULL; // To Store Double Address
float *z1 = NULL; // To Store Float Address
int *p2 = NULL; // For Calloc
int i; // Loop Variable
void *zero =NULL;
printf("\nRequesting Integer Space ...\n");
p1 = (int *)my_malloc(sizeof(int));
// If no memory available
if( !p1)
{
printf("Integer Malloc Failed.\nTerminating ...\n");
exit(0);
}
printf("Integer Memory Allocation Successful: %p\n", p1);
printf("\nRequesting Float Space ...\n");
c1 = (float *)my_malloc(sizeof(float));
// If no memory available
if( !c1)
{
printf("Float Malloc Failed.\nTerminating ...\n");
exit(0);
}
printf("Float Memory Allocation Successful: %p\n", c1);
printf("\nRequesting Double Space ...\n");
f11 = (double *)my_malloc(sizeof(double));
// If no memory available
if( !f11)
{
printf("Double Malloc Failed.\nTerminating ...\n");
exit(0);
}
printf("Double Memory Allocation Successful: %p\n", f11);
printf("\nRequesting Zero Bytes Space ...\n");
zero = my_malloc(0);
// If no memory available
if( !zero)
{
printf("Can't Allocate Zero Byte.\n");
}
printf("Double Memory Allocation Successful: %p\n", f11);
printf("\nRe-alloc with NULL as Address - Behaves as Malloc ...\n");
zero = my_realloc(NULL,10);
// If no memory available
if( !zero)
{
printf("Re-alloc Failed.\nTerminating ...\n");
exit(0);
}
printf("Double Memory Allocation Successful: %p\n", f11);
printf("\nRemaining Size in Heap:%lu\n\n", free_space());
printf("\nReallocating Float Space ...\n");
z1 = (float *)my_realloc(c1, 5 * sizeof(float));
// If no memory available
if( !z1)
{
printf("Re-alloc Failed.\nTerminating ...\n");
exit(0);
}
printf("Float Re-Allocation Successful: %p\n", z1);
// Just for Address Computation
char *uv = (char *) z1;
uv = uv - BLOCK_SIZE;
struct block *uv1 = (struct block *)uv;
printf("Size of realloced space: %lu and address: %p", uv1 -> size, z1 );
printf("\n\nRemaining Size in Heap:%lu\n", free_space());
printf("\nRequesting Calloc Memory Allocation...\n");
p2 = (int *) my_calloc( (size_t)10, sizeof(int));
for( i = 1; i <= 10 ; i++)
p2[i] = i;
for( i = 1; i <= 10 ; i++)
printf("%d\t", p2[i]);
printf("\n\nStatus of Heap After Allocation of Memory:\n");
printf("Remaining Size in Heap:%lu\n\n", free_space());
struct block * tally = (struct block*)base;
while(tally)
{
printf("Meta Data Begins:%p\t", tally);
printf("Size of Block: %ld\t", tally->size);
printf("Free : %d\t", tally->free);
printf("Next: %p\t", tally -> next);
printf("Previous: %p\t", tally -> prev);
tally = tally -> next;
printf("\n");
}
printf("\nFree Up Double Space ...\n");
free(f11);
printf("After Free up of Double Space ...\n");
printf("\n\nStatus of Heap after Free Up of Memory:\n");
printf("Remaining Size in Heap:%lu\n\n", free_space());
tally = (struct block*)base;
while(tally)
{
printf("Meta Data Begins:%p\t", tally);
printf("Size of Block: %ld\t", tally->size);
printf("Free : %d\t", tally->free);
printf("Next: %p\t", tally -> next);
printf("Previous: %p\t", tally -> prev);
tally = tally -> next;
printf("\n");
}
printf("\nDefragmentation Started ...\n\n");
defragment_my_heap();
tally = (struct block*)base;
printf("\nAfter Defragmenting the Heap: \n");
while(tally)
{
printf("Meta Data Begins:%p\t", tally);
printf("Size of Block: %ld\t", tally->size);
printf("Free : %d\t", tally->free);
printf("Next: %p\t", tally -> next);
printf("Previous: %p\t", tally -> prev);
tally = tally -> next;
printf("\n");
}
printf("\n");
}
// Main Function
int main()
{
// Initialization
init();
// Demonstration
demo();
return 0;
}