-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmymalloc.c
More file actions
98 lines (93 loc) · 2.44 KB
/
mymalloc.c
File metadata and controls
98 lines (93 loc) · 2.44 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
#include "mymalloc.h"
#include <stdio.h>
void* mymalloc(int size, int line, char* file) {
int tsize = size;
void* ptr = &myblock[1];
int i = 0;
myblock[0] = '\x1F';
for (i = 1; i < sizeof(myblock); i++) {
if (tsize == 0) { // if enough space has been found
int m = 0;
int t = 0;
for (m = i; m < sizeof(myblock); m++) {
if (myblock[m] == '\x1E') {
break;
}
if (myblock[m] == '\x1F') {
tsize = size;
t = i;
i = m;
break;
}
}
if (tsize == size) {
continue;
}
if (myblock[i] == '\x1F') {
tsize = size;
continue;
}
myblock[i] = '\x1F';
break;
}
if (myblock[i] == '\x1E') { //basically checks if the block next to a deallocated ptr (B) is occupied by another deallocated ptr (B) or an active ptr (A)
int usable = 1;
int j = 0;
for (j = i; j < sizeof(myblock); j++) {
if (myblock[j] == '\x1F') {
usable = 0;
break;
}
if (myblock[j] == '\x1E')
break;
}
if (usable == 0) {
tsize = size;
continue;
}
if (usable == 1) { // if the adjacent block is also a deallocated ptr, remove this 'C' ex. 000B00B -> 000000B
myblock[i] = '\0';
tsize--;
continue;
}
}
if (myblock[i] == '\x1F') { // if this space already contains a ptr block, keep searching...
tsize = size;
continue;
}
if (myblock[i] != '\x1F') {
if (tsize == size) { // if you just started searching, assign this index as the beginning
ptr = &myblock[i];
}
tsize--;
continue;
}
}
if (tsize != 0) { //not enough space for allocation
printf("Overflow error: Not enough space in memory for allocation; On line %d in file \"%s\"\n", line, file);
return NULL;
}
return ptr;
}
void myfree(void* ptr, int line, char* file) {
int i = (char*)ptr - &myblock[0];
if (i < sizeof(myblock) && i >= 0) {
if (myblock[i - 1] != '\x1F' && myblock[i - 1] != '\x1E') {
printf("Invalid Pointer: Not a pointer; On line %d in file \"%s\"\n", line, file); // for scenarios such as free(p[2])
return;
}
for (i = (char*)ptr - &myblock[0]; i < sizeof(myblock); i++) {
if (myblock[i] == '\x1E') {
printf("Invalid Pointer: Pointer already freed; On line %d in file \"%s\"\n", line, file); // if ptr was already freed
break;
}
if (myblock[i] == '\x1F') {
myblock[i] = '\x1E';
break;
}
}
}
else {
printf("Invalid Pointer: Not an allocated pointer; On line %d in file \"%s\"\n", line, file); // if ptr is out of array bounds
}
}