-
Notifications
You must be signed in to change notification settings - Fork 0
/
myMem.c
188 lines (174 loc) · 4.89 KB
/
myMem.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdio.h>
// Define alignment for memory blocks
typedef char ALIGN[16];
// Global variables to track memory statistics
size_t total_allocated = 0;
size_t total_freed = 0;
size_t current_usage = 0;
size_t allocation_count = 0;
// Union to represent a memory block header
union header {
struct {
size_t size; // Size of the memory block
unsigned is_free; // Flag to indicate if the block is free
union header *next; // Pointer to the next block in the list
} s;
ALIGN stub; // Aligning the header to 16 bytes
};
typedef union header header_t;
// Pointers to the start and end of the memory block list
header_t *head = NULL, *tail = NULL;
// Mutex for thread-safe operations
pthread_mutex_t global_malloc_lock;
// Function to find a free memory block of at least 'size' bytes
header_t *get_free_block(size_t size) {
header_t *curr = head;
size_t tempSize = __INT_MAX__;
while (curr) {
if (curr->s.is_free && curr->s.size >= size)
{
if(tempSize > curr->s.size)
tempSize = curr->s.size;
}
curr = curr->s.next;
}
header_t *curr1 = head;
while(curr1){
if(curr1->s.is_free && curr1->s.size == tempSize)
return curr1;
curr1 = curr1->s.next;
}
return NULL;
}
// Custom malloc function
void *malloc(size_t size) {
size_t total_size;
void *block;
header_t *header;
if (!size)
return NULL;
pthread_mutex_lock(&global_malloc_lock);
header = get_free_block(size);
if (header) {
header->s.is_free = 0;
pthread_mutex_unlock(&global_malloc_lock);
current_usage += size;
allocation_count++;
return (void *)(header + 1);
}
total_size = sizeof(header_t) + size;
block = sbrk(total_size);
if (block == (void *)-1) {
pthread_mutex_unlock(&global_malloc_lock);
return NULL;
}
header = block;
header->s.size = size;
header->s.is_free = 0;
header->s.next = NULL;
if (!head)
head = header;
if (tail)
tail->s.next = header;
tail = header;
pthread_mutex_unlock(&global_malloc_lock);
total_allocated += size;
current_usage += size;
allocation_count++;
return (void *)(header + 1);
}
// Function to coalesce adjacent free memory blocks
void coalesce_free_blocks() {
header_t *curr = head;
while (curr && curr->s.next) {
if (curr->s.is_free && curr->s.next->s.is_free) {
curr->s.size += sizeof(header_t) + curr->s.next->s.size;
curr->s.next = curr->s.next->s.next;
if (curr->s.next == NULL)
tail = curr;
} else {
curr = curr->s.next;
}
}
}
// Custom free function
void free(void *block) {
header_t *header, *tmp;
void *programbreak;
if (!block)
return;
pthread_mutex_lock(&global_malloc_lock);
header = (header_t *)block - 1;
programbreak = sbrk(0);
if ((char *)block + header->s.size == programbreak) {
if (head == tail)
head = tail = NULL;
else {
tmp = head;
while (tmp) {
if (tmp->s.next == tail) {
tmp->s.next = NULL;
tail = tmp;
}
tmp = tmp->s.next;
}
}
sbrk(0 - sizeof(header_t) - header->s.size);
pthread_mutex_unlock(&global_malloc_lock);
total_freed += header->s.size;
current_usage -= header->s.size;
return;
}
header->s.is_free = 1;
coalesce_free_blocks();
total_freed += header->s.size;
current_usage -= header->s.size;
pthread_mutex_unlock(&global_malloc_lock);
}
// Custom calloc function
void *calloc(size_t num, size_t nsize) {
if (!num || !nsize)
return NULL;
size_t size;
void *block;
size = num * nsize;
if (nsize != size / num)
return NULL;
block = malloc(size);
if (!block)
return NULL;
memset(block, 0, size);
return block;
}
// Custom realloc function
void *realloc(void *block, size_t size) {
void *ret;
header_t *header;
if (!block)
return malloc(size);
if (!size) {
free(block);
return NULL;
}
header = (header_t *)block - 1;
if (header->s.size >= size)
return block;
ret = malloc(size);
if (ret) {
memcpy(ret, block, header->s.size);
free(block);
}
return ret;
}
// Function to print memory allocation statistics
void print_memory_statistics() {
pthread_mutex_lock(&global_malloc_lock);
printf("Memory Allocation Statistics:\n");
printf("Total Allocated: %zu bytes\n", total_allocated);
printf("Total Freed: %zu bytes\n", total_freed);
printf("Current Usage: %zu bytes\n", current_usage);
pthread_mutex_unlock(&global_malloc_lock);
}