-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_mem.c
238 lines (189 loc) · 7 KB
/
my_mem.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include<stdio.h>
#include<stdlib.h>
#include <errno.h>
#include "my_mem.h"
unsigned char *original_memory;
unsigned int original_size;
int freeNum;
int usedNum;
struct storage {
int blockSize;
unsigned char *location;
int freeZeroUsedOne;
};
struct storage *arr = NULL;
int globalMem;
void mem_init(unsigned char *my_memory, unsigned int my_mem_size){
original_memory = my_memory;
original_size = my_mem_size;
if (arr != NULL) {
free(arr);
}
// all structs are zeroed by calloc
arr = (struct storage *)calloc(my_mem_size, sizeof(struct storage));
globalMem = my_mem_size;
// array is initialized to one free block of globalMem size
arr[0].blockSize = globalMem;
arr[0].freeZeroUsedOne = 0;
arr[0].location = my_memory;
freeNum = 1;
usedNum = 0;
}
void mem_reset() {
//reset array to how it is after init
mem_init(original_memory, original_size);
}
void *my_malloc(unsigned size){
int i;
int j;
int localSize = size;
// if size of allocation is 0
if (localSize == 0) {
printf("cant allocate size 0 of memory\n");
return NULL;
}
if (localSize > globalMem) {
printf("cant allocate larger than originally allocated memory\n");
return NULL;
}
for (i = 0; i < globalMem; i++){
// save pointer of arr[i]
struct storage *block = &arr[i];
// if current block size is larger than what we want to allocate and it is free
if (block->blockSize >= localSize && block->freeZeroUsedOne == 0){
// if it is a LOT larger
if (block->blockSize / 2 > localSize){
// divide in half, one part to new block, other to give to user
block->blockSize /= 2;
block->freeZeroUsedOne = 1;
// need to shift everything by one to insert other half of block in next slot of array
for (j = (globalMem - 2); j > i; j--) {
arr[j+1] = arr[j];
}
// create the new block at the space we just created after shifting
struct storage *new_block = &arr[i + 1];
new_block->location = block->location + block->blockSize;
new_block->blockSize = block->blockSize;
new_block->freeZeroUsedOne = 0;
// used increases by 1, free doesnt decrease bc of the split we are adding new block that is free instead
usedNum = usedNum + 1;
return block->location;
}
// else if the block is not much bigger just allocate the whole block
else {
block->freeZeroUsedOne = 1;
freeNum = freeNum - 1;
usedNum = usedNum + 1;
return block->location;
}
}
}
// IF IVE GONE THROUGH FULL FOR LOOP AND HAVENT FOUND ANY MEMORY BIG ENOUGH
printf("No available memory for this size\n");
return NULL;
}
// merges the block at this index with the block after it
void merge_blocks(int index) {
int j;
struct storage *block = &arr[index];
struct storage *next = &arr[index+1];
// blockSize becomes the size of blocks merging
block->blockSize += next->blockSize;
// merge means one less free block (2 free blocks now 1)
freeNum = freeNum - 1;
for (j = (index + 1); j < (globalMem - 1); j++) {
// shift to the left
arr[j] = arr[j+1];
}
}
void my_free(void *mem_pointer){
int i;
for (i = 0; i < globalMem; i ++) {
struct storage *block = &arr[i];
// if we found the block with the pointer we want to free
if (block->location == mem_pointer) {
if (block->freeZeroUsedOne == 0) {
printf("this memory is already free\n");
return;
}
block->freeZeroUsedOne = 0;
freeNum = freeNum + 1;
usedNum = usedNum - 1;
// merge if one after is free and if not end of array
if (i != (globalMem -1) && arr[i + 1].freeZeroUsedOne == 0) {
merge_blocks(i);
}
// merge if one before is free and if not beginning of array
if (i != 0 && arr[i-1].freeZeroUsedOne == 0) {
merge_blocks(i-1);
}
return;
}
}
// reached end of for loop
printf("sorry this memory pointer cant be found\n");
}
void mem_get_stats(mem_stats_ptr mem_stats_ptr){
mem_stats_ptr->num_blocks_free = freeNum;
mem_stats_ptr->num_blocks_used = usedNum;
// if there are used blocks
if (usedNum > 0){
int i;
// if they are all allocated
if (freeNum == 0) {
mem_stats_ptr->smallest_block_free = 0;
mem_stats_ptr->largest_block_free = 0;
mem_stats_ptr->smallest_block_used = globalMem;
mem_stats_ptr->largest_block_used = 0;
}
else {
mem_stats_ptr->smallest_block_free = globalMem;
mem_stats_ptr->largest_block_free = 0;
mem_stats_ptr->smallest_block_used = globalMem;
mem_stats_ptr->largest_block_used = 0;
}
for (i = 0; i < usedNum + freeNum; i ++){
// stats about free blocks
if (arr[i].freeZeroUsedOne == 0){
// if current block is less than minFree set new min
if (arr[i].blockSize < mem_stats_ptr->smallest_block_free) {
mem_stats_ptr->smallest_block_free = arr[i].blockSize;
}
// if current block is more than maxFree set new max
if (arr[i].blockSize > mem_stats_ptr->largest_block_free) {
mem_stats_ptr->largest_block_free = arr[i].blockSize;
}
}
// stats about allocated blocks
else {
// if current block is less than minUsed set new min
if (arr[i].blockSize < mem_stats_ptr->smallest_block_used) {
mem_stats_ptr->smallest_block_used = arr[i].blockSize;
}
// if current block is more than maxUsed set new max
if (arr[i].blockSize > mem_stats_ptr->largest_block_used) {
mem_stats_ptr->largest_block_used = arr[i].blockSize;
}
}
}
}
else {
// one free block (all freed or right after mem_init)
mem_stats_ptr->smallest_block_free = globalMem;
mem_stats_ptr->largest_block_free = globalMem;
mem_stats_ptr->smallest_block_used = 0;
mem_stats_ptr->largest_block_used = 0;
}
}
void print_stats(char *prefix) {
mem_stats_struct mem_stats;
mem_get_stats(&mem_stats);
printf("mem stats: %s: %d free blocks, %d used blocks, free blocks: smallest=%d largest=%d, used blocks: smallest=%d largest=%d\n",
prefix,
mem_stats.num_blocks_free,
mem_stats.num_blocks_used,
mem_stats.smallest_block_free,
mem_stats.largest_block_free,
mem_stats.smallest_block_used,
mem_stats.largest_block_used);
}